Skip to content

Commit 414852c

Browse files
examples automation examples-dir for tsp (#8854)
1 parent aa0fc47 commit 414852c

28 files changed

Lines changed: 1223 additions & 874 deletions

tools/azure-rest-api-specs-examples-automation/automation/csv_database.py

Lines changed: 53 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
from models import Release
1212

1313

14-
example_repo: str = 'https://github.com/Azure/azure-rest-api-specs-examples'
15-
csvdb_folder: str = 'csvdb'
16-
metadata_branch: str = 'metadata'
14+
example_repo: str = "https://github.com/Azure/azure-rest-api-specs-examples"
15+
csvdb_folder: str = "csvdb"
16+
metadata_branch: str = "metadata"
1717

1818

1919
@dataclasses.dataclass(eq=True)
@@ -55,101 +55,105 @@ def __init__(self, work_dir: str):
5555
self.work_dir = work_dir
5656
self.example_metadata_path = path.join(self.work_dir, csvdb_folder)
5757

58-
self.index_file_path = path.join(self.example_metadata_path, 'java-library-example-index.csv')
59-
self.list_file_path = path.join(self.example_metadata_path, 'java-library-example-list.csv')
58+
self.index_file_path = path.join(self.example_metadata_path, "java-library-example-index.csv")
59+
self.list_file_path = path.join(self.example_metadata_path, "java-library-example-list.csv")
6060

6161
def checkout(self):
6262
# checkout metadata branch from azure-rest-api-specs-examples repo
63-
cmd = ['git', 'clone',
64-
'--quiet',
65-
'--depth', '1',
66-
'--branch', metadata_branch,
67-
example_repo, self.example_metadata_path]
68-
logging.info(f'Checking out repository: {example_repo}, branch {metadata_branch}')
69-
logging.info('Command line: ' + ' '.join(cmd))
63+
cmd = [
64+
"git",
65+
"clone",
66+
"--quiet",
67+
"--depth",
68+
"1",
69+
"--branch",
70+
metadata_branch,
71+
example_repo,
72+
self.example_metadata_path,
73+
]
74+
logging.info(f"Checking out repository: {example_repo}, branch {metadata_branch}")
75+
logging.info("Command line: " + " ".join(cmd))
7076
subprocess.check_call(cmd, cwd=self.work_dir)
7177

7278
def load(self):
73-
with open(self.index_file_path, 'r', newline='') as csv_file:
74-
csv_reader = csv.reader(csv_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
79+
with open(self.index_file_path, "r", newline="") as csv_file:
80+
csv_reader = csv.reader(csv_file, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL)
7581
self.release_db = DatabaseInternal(csv_reader)
7682

77-
with open(self.list_file_path, 'r', newline='') as csv_file:
78-
csv_reader = csv.reader(csv_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
83+
with open(self.list_file_path, "r", newline="") as csv_file:
84+
csv_reader = csv.reader(csv_file, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL)
7985
self.file_db = DatabaseInternal(csv_reader)
8086

8187
def dump(self):
82-
with open(self.index_file_path, 'w', newline='') as csv_file:
83-
csv_writer = csv.writer(csv_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
84-
csv_writer.writerow(['id', 'name', 'language', 'tag', 'package', 'version', 'date_epoch', 'date'])
88+
with open(self.index_file_path, "w", newline="") as csv_file:
89+
csv_writer = csv.writer(csv_file, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL)
90+
csv_writer.writerow(["id", "name", "language", "tag", "package", "version", "date_epoch", "date"])
8591
for row in self.release_db.rows:
8692
csv_writer.writerow(row)
8793

88-
with open(self.list_file_path, 'w', newline='') as csv_file:
89-
csv_writer = csv.writer(csv_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
90-
csv_writer.writerow(['id', 'file', 'release_id'])
94+
with open(self.list_file_path, "w", newline="") as csv_file:
95+
csv_writer = csv.writer(csv_file, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL)
96+
csv_writer.writerow(["id", "file", "release_id"])
9197
for row in self.file_db.rows:
9298
csv_writer.writerow(row)
9399

94100
def commit(self, tag):
95101
if not self.branch:
96102
# git checkout new branch
97-
self.date_str = datetime.now().strftime('%Y-%m-%d')
98-
self.branch = f'automation-metadata-{self.date_str}'
99-
cmd = ['git', 'checkout', '-b', self.branch]
100-
logging.info('Command line: ' + ' '.join(cmd))
103+
self.date_str = datetime.now().strftime("%Y-%m-%d")
104+
self.branch = f"automation-metadata-{self.date_str}"
105+
cmd = ["git", "checkout", "-b", self.branch]
106+
logging.info("Command line: " + " ".join(cmd))
101107
subprocess.check_call(cmd, cwd=self.example_metadata_path)
102108

103109
# git add
104-
cmd = ['git', 'add', 'java-library-example-index.csv']
105-
logging.info('Command line: ' + ' '.join(cmd))
110+
cmd = ["git", "add", "java-library-example-index.csv"]
111+
logging.info("Command line: " + " ".join(cmd))
106112
subprocess.check_call(cmd, cwd=self.example_metadata_path)
107113

108-
cmd = ['git', 'add', 'java-library-example-list.csv']
109-
logging.info('Command line: ' + ' '.join(cmd))
114+
cmd = ["git", "add", "java-library-example-list.csv"]
115+
logging.info("Command line: " + " ".join(cmd))
110116
subprocess.check_call(cmd, cwd=self.example_metadata_path)
111117

112118
# git commit
113-
title = f'[Automation] Update metadata on {tag}'
114-
logging.info(f'git commit: {title}')
115-
cmd = ['git',
116-
'-c', 'user.name=azure-sdk',
117-
'-c', 'user.email=azuresdk@microsoft.com',
118-
'commit', '-m', title]
119-
logging.info('Command line: ' + ' '.join(cmd))
119+
title = f"[Automation] Update metadata on {tag}"
120+
logging.info(f"git commit: {title}")
121+
cmd = ["git", "-c", "user.name=azure-sdk", "-c", "user.email=azuresdk@microsoft.com", "commit", "-m", title]
122+
logging.info("Command line: " + " ".join(cmd))
120123
subprocess.check_call(cmd, cwd=self.example_metadata_path)
121124

122125
def push(self, github_token: str):
123126
if self.branch:
124-
title = f'[Automation] Update metadata on {self.date_str}'
127+
title = f"[Automation] Update metadata on {self.date_str}"
125128
# git push
126-
remote_uri = 'https://' + github_token + '@' + example_repo[len('https://'):]
127-
cmd = ['git', 'push', remote_uri, self.branch]
129+
remote_uri = "https://" + github_token + "@" + example_repo[len("https://") :]
130+
cmd = ["git", "push", remote_uri, self.branch]
128131
# do not print this as it contains token
129132
# logging.info('Command line: ' + ' '.join(cmd))
130133
subprocess.check_call(cmd, cwd=self.example_metadata_path)
131134

132135
# create github pull request
133136
owner = _repository_owner(example_repo)
134137
name = _repository_name(example_repo)
135-
head = f'{owner}:{self.branch}'
138+
head = f"{owner}:{self.branch}"
136139
repo = GitHubRepository(owner, name, github_token)
137140
pull_number = repo.create_pull_request(title, head, metadata_branch)
138-
repo.add_label(pull_number, ['auto-merge'])
139-
logging.info(f'succeeded, pull number {pull_number}')
141+
repo.add_label(pull_number, ["auto-merge"])
142+
logging.info(f"succeeded, pull number {pull_number}")
140143

141-
def new_release(self, name: str, language: str, tag: str, package: str, version: str, date: datetime,
142-
files: List[str]) -> bool:
144+
def new_release(
145+
self, name: str, language: str, tag: str, package: str, version: str, date: datetime, files: List[str]
146+
) -> bool:
143147
# add a new release and all the example files
144148
# return false, if release already exists in DB
145149

146150
release_id = self._query_release(name, language)
147151
if release_id:
148-
logging.warning(f'Release already exists for {language}#{name}')
152+
logging.warning(f"Release already exists for {language}#{name}")
149153
return False
150154

151155
date_epoch = int(date.timestamp())
152-
date_str = datetime.fromtimestamp(date_epoch).strftime('%m/%d/%Y')
156+
date_str = datetime.fromtimestamp(date_epoch).strftime("%m/%d/%Y")
153157

154158
release_id = self.release_db.append([name, language, tag, package, version, date_epoch, date_str])
155159

@@ -178,8 +182,8 @@ def _query_release(self, name: str, language: str) -> Union[str, None]:
178182

179183

180184
def _repository_owner(repository: str) -> str:
181-
return re.match(r'https://github.com/([^/:]+)/.*', repository).group(1)
185+
return re.match(r"https://github.com/([^/:]+)/.*", repository).group(1)
182186

183187

184188
def _repository_name(repository: str) -> str:
185-
return re.match(r'https://github.com/[^/:]+/(.*)', repository).group(1)
189+
return re.match(r"https://github.com/[^/:]+/(.*)", repository).group(1)

tools/azure-rest-api-specs-examples-automation/automation/github.py

Lines changed: 32 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
class GitHubRepository:
7-
api_host: str = 'https://api.github.com'
7+
api_host: str = "https://api.github.com"
88
owner: str
99
name: str
1010
token: str
@@ -15,84 +15,65 @@ def __init__(self, owner: str, name: str, token):
1515
self.token = token
1616

1717
def create_pull_request(self, title: str, head: str, base: str) -> int:
18-
logging.info(f'Create pull request: {head}')
18+
logging.info(f"Create pull request: {head}")
1919

20-
request_uri = f'{self.api_host}/repos/{self.owner}/{self.name}/pulls'
21-
request_body = {
22-
'title': title,
23-
'head': head,
24-
'base': base
25-
}
26-
pull_request_response = requests.post(request_uri,
27-
json=request_body,
28-
headers=self._headers())
20+
request_uri = f"{self.api_host}/repos/{self.owner}/{self.name}/pulls"
21+
request_body = {"title": title, "head": head, "base": base}
22+
pull_request_response = requests.post(request_uri, json=request_body, headers=self._headers())
2923
if pull_request_response.status_code == 201:
30-
logging.info('Pull request created')
31-
return pull_request_response.json()['number']
24+
logging.info("Pull request created")
25+
return pull_request_response.json()["number"]
3226
else:
33-
logging.error(f'Request failed: {pull_request_response.status_code}\n{pull_request_response.json()}')
27+
logging.error(f"Request failed: {pull_request_response.status_code}\n{pull_request_response.json()}")
3428
pull_request_response.raise_for_status()
3529

3630
def list_pull_requests(self) -> List[Dict[str, Any]]:
37-
logging.info(f'List pull requests')
31+
logging.info(f"List pull requests")
3832

39-
request_uri = f'{self.api_host}/repos/{self.owner}/{self.name}/pulls?per_page=100'
40-
pull_request_response = requests.get(request_uri,
41-
headers=self._headers())
33+
request_uri = f"{self.api_host}/repos/{self.owner}/{self.name}/pulls?per_page=100"
34+
pull_request_response = requests.get(request_uri, headers=self._headers())
4235
if pull_request_response.status_code == 200:
43-
logging.info('Pull request created')
36+
logging.info("Pull request created")
4437
return pull_request_response.json()
4538
else:
46-
logging.error(f'Request failed: {pull_request_response.status_code}\n{pull_request_response.json()}')
39+
logging.error(f"Request failed: {pull_request_response.status_code}\n{pull_request_response.json()}")
4740
return []
4841

4942
def merge_pull_request(self, pull_request: Dict):
50-
title = pull_request['title']
51-
logging.info(f'Merge pull request: {title}')
43+
title = pull_request["title"]
44+
logging.info(f"Merge pull request: {title}")
5245

53-
pull_number = int(pull_request['number'])
46+
pull_number = int(pull_request["number"])
5447

55-
request_uri = f'{self.api_host}/repos/{self.owner}/{self.name}/pulls/{pull_number}/merge'
56-
request_body = {
57-
'commit_title': title,
58-
'merge_method': 'squash'
59-
}
60-
merge_response = requests.put(request_uri,
61-
json=request_body,
62-
headers=self._headers())
48+
request_uri = f"{self.api_host}/repos/{self.owner}/{self.name}/pulls/{pull_number}/merge"
49+
request_body = {"commit_title": title, "merge_method": "squash"}
50+
merge_response = requests.put(request_uri, json=request_body, headers=self._headers())
6351
if merge_response.status_code == 200:
64-
logging.info('Pull request merged')
52+
logging.info("Pull request merged")
6553
else:
66-
logging.error(f'Request failed: {merge_response.status_code}\n{merge_response.json()}')
54+
logging.error(f"Request failed: {merge_response.status_code}\n{merge_response.json()}")
6755
merge_response.raise_for_status()
6856

6957
def list_releases(self, per_page: int, page: int = 1) -> List[Dict[str, Any]]:
70-
request_uri = f'{self.api_host}/repos/{self.owner}/{self.name}/releases'
71-
releases_response = requests.get(request_uri,
72-
params={'per_page': per_page, 'page': page},
73-
headers=self._headers())
58+
request_uri = f"{self.api_host}/repos/{self.owner}/{self.name}/releases"
59+
releases_response = requests.get(
60+
request_uri, params={"per_page": per_page, "page": page}, headers=self._headers()
61+
)
7462
if releases_response.status_code == 200:
7563
return releases_response.json()
7664
else:
77-
logging.error(f'Request failed: {releases_response.status_code}\n{releases_response.json()}')
65+
logging.error(f"Request failed: {releases_response.status_code}\n{releases_response.json()}")
7866
releases_response.raise_for_status()
7967

8068
def add_label(self, pull_number: int, labels: List[str]):
81-
request_uri = f'{self.api_host}/repos/{self.owner}/{self.name}/issues/{pull_number}/labels'
82-
request_body = {
83-
'labels': labels
84-
}
85-
add_label_response = requests.post(request_uri,
86-
json=request_body,
87-
headers=self._headers())
69+
request_uri = f"{self.api_host}/repos/{self.owner}/{self.name}/issues/{pull_number}/labels"
70+
request_body = {"labels": labels}
71+
add_label_response = requests.post(request_uri, json=request_body, headers=self._headers())
8872
if add_label_response.status_code == 200:
89-
logging.info('Label added')
73+
logging.info("Label added")
9074
else:
91-
logging.error(f'Request failed: {add_label_response.status_code}\n{add_label_response.json()}')
75+
logging.error(f"Request failed: {add_label_response.status_code}\n{add_label_response.json()}")
9276
add_label_response.raise_for_status()
9377

9478
def _headers(self) -> Dict[str, str]:
95-
return {
96-
'X-GitHub-Api-Version': '2022-11-28',
97-
'Authorization': f'token {self.token}'
98-
}
79+
return {"X-GitHub-Api-Version": "2022-11-28", "Authorization": f"token {self.token}"}

0 commit comments

Comments
 (0)