Skip to content
This repository was archived by the owner on Aug 25, 2024. It is now read-only.

Commit 9b7e7c2

Browse files
committed
feature: git: github api: Update testing.yml or create
Signed-off-by: John Andersen <johnandersenpdx@gmail.com>
1 parent baed89d commit 9b7e7c2

1 file changed

Lines changed: 41 additions & 15 deletions

File tree

feature/git/dffml_feature_git/github_api.py

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import os
22
import json
33
import asyncio
4+
import logging
5+
46
import aiohttp
57

8+
logger = logging.getLogger(__name__)
9+
610
# Your personal access token
711
# TOKEN = "your_github_access_token"
812
TOKEN = os.environ["GH_ACCESS_TOKEN"]
@@ -163,7 +167,11 @@ def sync_fork(upstream, fork, branch):
163167

164168
@snoop
165169
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,
167175
):
168176
g = Github(token)
169177
user = g.get_user()
@@ -198,27 +206,39 @@ def make_github_operations(
198206
new_branch_ref.edit(upstream_default_branch_sha, force=True)
199207

200208
# 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
202211
try:
203212
pygithub_fileobj = fork.get_contents(
204213
file_path, ref=new_branch_name
205214
)
215+
old_content = pygithub_fileobj.decoded_content
206216
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)
210222

211223
# Transform the content
212-
content = transform_file_content(pygithub_fileobj.decoded_content)
224+
content = transform_file_content(old_content)
213225

214226
# 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+
)
222242

223243
# Create Pull request
224244
base_repo = repo
@@ -247,15 +267,21 @@ def make_github_operations(
247267
.joinpath(
248268
".github",
249269
"workflows",
250-
"testing.yml",
270+
"pin_downstream.yml",
251271
)
252272
.read_text()
253273
)
254274

255275

256276
async def main(repos, file_name):
257277
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,
259285
)
260286
return
261287
async with aiohttp.ClientSession(trust_env=True) as client:

0 commit comments

Comments
 (0)