|
1 | 1 | import os |
2 | 2 | import json |
3 | 3 | import asyncio |
| 4 | +import logging |
| 5 | + |
4 | 6 | import aiohttp |
5 | 7 |
|
| 8 | +logger = logging.getLogger(__name__) |
| 9 | + |
6 | 10 | # Your personal access token |
7 | 11 | # TOKEN = "your_github_access_token" |
8 | 12 | TOKEN = os.environ["GH_ACCESS_TOKEN"] |
@@ -163,7 +167,11 @@ def sync_fork(upstream, fork, branch): |
163 | 167 |
|
164 | 168 | @snoop |
165 | 169 | def make_github_operations( |
166 | | - token, repo_urls, new_branch_name, transform_file_content |
| 170 | + token, |
| 171 | + repo_urls, |
| 172 | + new_branch_name, |
| 173 | + transform_file_content, |
| 174 | + fail_on_not_present: bool = True, |
167 | 175 | ): |
168 | 176 | g = Github(token) |
169 | 177 | user = g.get_user() |
@@ -198,27 +206,39 @@ def make_github_operations( |
198 | 206 | new_branch_ref.edit(upstream_default_branch_sha, force=True) |
199 | 207 |
|
200 | 208 | # Update the contents of testing.yml |
201 | | - file_path = ".github/workflows/testing.yml" |
| 209 | + file_path = ".github/workflows/pin_downstream.yml" |
| 210 | + old_content = None |
202 | 211 | try: |
203 | 212 | pygithub_fileobj = fork.get_contents( |
204 | 213 | file_path, ref=new_branch_name |
205 | 214 | ) |
| 215 | + old_content = pygithub_fileobj.decoded_content |
206 | 216 | except GithubException as error: |
207 | | - raise Exception( |
208 | | - f"{file_path} does not exist in {repo_url}, unable to update." |
209 | | - ) from error |
| 217 | + msg = f"{file_path} does not exist in {repo_url}" |
| 218 | + if fail_on_not_present: |
| 219 | + raise Exception(f"{msg}, unable to update.") from error |
| 220 | + else: |
| 221 | + logger.info(msg) |
210 | 222 |
|
211 | 223 | # Transform the content |
212 | | - content = transform_file_content(pygithub_fileobj.decoded_content) |
| 224 | + content = transform_file_content(old_content) |
213 | 225 |
|
214 | 226 | # Update file content |
215 | | - fork.update_file( |
216 | | - pygithub_fileobj.path, |
217 | | - "Update testing.yml", |
218 | | - content, |
219 | | - pygithub_fileobj.sha, |
220 | | - branch=new_branch_name, |
221 | | - ) |
| 227 | + if old_content: |
| 228 | + fork.update_file( |
| 229 | + pygithub_fileobj.path, |
| 230 | + "Update testing.yml", |
| 231 | + content, |
| 232 | + pygithub_fileobj.sha, |
| 233 | + branch=new_branch_name, |
| 234 | + ) |
| 235 | + else: |
| 236 | + fork.create_file( |
| 237 | + pygithub_fileobj.path, |
| 238 | + "Update testing.yml", |
| 239 | + content, |
| 240 | + branch=new_branch_name, |
| 241 | + ) |
222 | 242 |
|
223 | 243 | # Create Pull request |
224 | 244 | base_repo = repo |
@@ -247,15 +267,21 @@ def make_github_operations( |
247 | 267 | .joinpath( |
248 | 268 | ".github", |
249 | 269 | "workflows", |
250 | | - "testing.yml", |
| 270 | + "pin_downstream.yml", |
251 | 271 | ) |
252 | 272 | .read_text() |
253 | 273 | ) |
254 | 274 |
|
255 | 275 |
|
256 | 276 | async def main(repos, file_name): |
257 | 277 | make_github_operations( |
258 | | - token, repo_urls, new_branch_name, lambda content: content.upper() |
| 278 | + token, |
| 279 | + repo_urls, |
| 280 | + new_branch_name, |
| 281 | + lambda content: content.upper() |
| 282 | + if content is not None |
| 283 | + else file_content, |
| 284 | + fail_on_not_present=False, |
259 | 285 | ) |
260 | 286 | return |
261 | 287 | async with aiohttp.ClientSession(trust_env=True) as client: |
|
0 commit comments