|
11 | 11 | from models import Release |
12 | 12 |
|
13 | 13 |
|
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" |
17 | 17 |
|
18 | 18 |
|
19 | 19 | @dataclasses.dataclass(eq=True) |
@@ -55,101 +55,105 @@ def __init__(self, work_dir: str): |
55 | 55 | self.work_dir = work_dir |
56 | 56 | self.example_metadata_path = path.join(self.work_dir, csvdb_folder) |
57 | 57 |
|
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") |
60 | 60 |
|
61 | 61 | def checkout(self): |
62 | 62 | # 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)) |
70 | 76 | subprocess.check_call(cmd, cwd=self.work_dir) |
71 | 77 |
|
72 | 78 | 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) |
75 | 81 | self.release_db = DatabaseInternal(csv_reader) |
76 | 82 |
|
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) |
79 | 85 | self.file_db = DatabaseInternal(csv_reader) |
80 | 86 |
|
81 | 87 | 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"]) |
85 | 91 | for row in self.release_db.rows: |
86 | 92 | csv_writer.writerow(row) |
87 | 93 |
|
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"]) |
91 | 97 | for row in self.file_db.rows: |
92 | 98 | csv_writer.writerow(row) |
93 | 99 |
|
94 | 100 | def commit(self, tag): |
95 | 101 | if not self.branch: |
96 | 102 | # 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)) |
101 | 107 | subprocess.check_call(cmd, cwd=self.example_metadata_path) |
102 | 108 |
|
103 | 109 | # 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)) |
106 | 112 | subprocess.check_call(cmd, cwd=self.example_metadata_path) |
107 | 113 |
|
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)) |
110 | 116 | subprocess.check_call(cmd, cwd=self.example_metadata_path) |
111 | 117 |
|
112 | 118 | # 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)) |
120 | 123 | subprocess.check_call(cmd, cwd=self.example_metadata_path) |
121 | 124 |
|
122 | 125 | def push(self, github_token: str): |
123 | 126 | if self.branch: |
124 | | - title = f'[Automation] Update metadata on {self.date_str}' |
| 127 | + title = f"[Automation] Update metadata on {self.date_str}" |
125 | 128 | # 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] |
128 | 131 | # do not print this as it contains token |
129 | 132 | # logging.info('Command line: ' + ' '.join(cmd)) |
130 | 133 | subprocess.check_call(cmd, cwd=self.example_metadata_path) |
131 | 134 |
|
132 | 135 | # create github pull request |
133 | 136 | owner = _repository_owner(example_repo) |
134 | 137 | name = _repository_name(example_repo) |
135 | | - head = f'{owner}:{self.branch}' |
| 138 | + head = f"{owner}:{self.branch}" |
136 | 139 | repo = GitHubRepository(owner, name, github_token) |
137 | 140 | 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}") |
140 | 143 |
|
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: |
143 | 147 | # add a new release and all the example files |
144 | 148 | # return false, if release already exists in DB |
145 | 149 |
|
146 | 150 | release_id = self._query_release(name, language) |
147 | 151 | if release_id: |
148 | | - logging.warning(f'Release already exists for {language}#{name}') |
| 152 | + logging.warning(f"Release already exists for {language}#{name}") |
149 | 153 | return False |
150 | 154 |
|
151 | 155 | 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") |
153 | 157 |
|
154 | 158 | release_id = self.release_db.append([name, language, tag, package, version, date_epoch, date_str]) |
155 | 159 |
|
@@ -178,8 +182,8 @@ def _query_release(self, name: str, language: str) -> Union[str, None]: |
178 | 182 |
|
179 | 183 |
|
180 | 184 | 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) |
182 | 186 |
|
183 | 187 |
|
184 | 188 | 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) |
0 commit comments