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

Commit

Permalink
feature: git: github api: Take body from env
Browse files Browse the repository at this point in the history
Signed-off-by: John Andersen <[email protected]>
  • Loading branch information
pdxjohnny committed Dec 26, 2023
1 parent 9b7e7c2 commit b251770
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions feature/git/dffml_feature_git/github_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,20 +156,13 @@ async def fetch_files(client, owner, repo_name, pr_url, file_cursor):
import snoop


def sync_fork(upstream, fork, branch):
try:
print(f"Successfully synced {fork} branch {branch} with {upstream}")
except Exception as error:
raise Exception(
f"Unable to sync {fork} with {upstream} due to {error}"
) from error


@snoop
def make_github_operations(
token,
repo_urls,
new_branch_name,
make_commit_message,
make_pull_request_body,
transform_file_content,
fail_on_not_present: bool = True,
):
Expand All @@ -183,6 +176,9 @@ def make_github_operations(
repo = g.get_repo(f"{org}/{repo_name}")
fork = user.create_fork(repo)

commit_message = make_commit_message(repo, fork)
pull_request_body = make_pull_request_body(repo, fork)

# Create a new branch from the default branch
fork_default_branch = fork.get_branch(fork.default_branch)
try:
Expand Down Expand Up @@ -216,7 +212,7 @@ def make_github_operations(
except GithubException as error:
msg = f"{file_path} does not exist in {repo_url}"
if fail_on_not_present:
raise Exception(f"{msg}, unable to update.") from error
raise Exception(f"{msg}, unable to update") from error
else:
logger.info(msg)

Expand All @@ -227,15 +223,15 @@ def make_github_operations(
if old_content:
fork.update_file(
pygithub_fileobj.path,
"Update testing.yml",
commit_message,
content,
pygithub_fileobj.sha,
branch=new_branch_name,
)
else:
fork.create_file(
pygithub_fileobj.path,
"Update testing.yml",
commit_message,
content,
branch=new_branch_name,
)
Expand All @@ -246,13 +242,16 @@ def make_github_operations(
base = base_repo.default_branch
head = f"{user.login}:{fork.name}:{new_branch_name}"
base_repo.create_pull(
title="Update testing.yml", body="", head=head, base=base
title=commit_message,
body=pull_request_body,
head=head,
base=base,
)

except GithubException as e:
print(
f"Unable to complete operations for {repo_url} due to {str(e)}"
)
except GithubException as error:
raise Exception(
f"Unable to complete operations for {repo_url} due to {e}"
) from error


import pathlib
Expand All @@ -274,10 +273,16 @@ def make_github_operations(


async def main(repos, file_name):
commit_message = os.environ["COMMIT_MESSAGE"]
pull_request_body = os.environ["PULL_REQUEST_BODY"]
make_github_operations(
token,
repo_urls,
new_branch_name,
lambda _upstream, fork: commit_message,
lambda _upstream, fork: pull_request_body.replace(
"REPO_ORG/REPO_NAME", fork.full_name
),
lambda content: content.upper()
if content is not None
else file_content,
Expand Down

0 comments on commit b251770

Please sign in to comment.