Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using pr_comment_id to simplify handling of issue comments #250

Merged
merged 6 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 17 additions & 33 deletions tasks/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ def check_build_status(slurm_out, eessi_tarballs):
return False


def update_pr_comment(tarball, repo_name, pr_number, state, msg):
def update_pr_comment(tarball, repo_name, pr_number, pr_comment_id, state, msg):
"""
Update pull request comment which contains specific tarball name.
Update pull request comment for the given comment id or tarball name

Args:
tarball (string): name of tarball that is looked for in a PR comment
Expand All @@ -202,36 +202,18 @@ def update_pr_comment(tarball, repo_name, pr_number, state, msg):
Returns:
None (implicitly)
"""
funcname = sys._getframe().f_code.co_name

gh = github.get_instance()
repo = gh.get_repo(repo_name)
pull_request = repo.get_pull(pr_number)

# TODO does this always return all comments?
comments = pull_request.get_issue_comments()
for comment in comments:
# NOTE
# adjust search string if format changed by event handler
# (separate process running eessi_bot_event_handler.py)
re_tarball = f".*{tarball}.*"
comment_match = re.search(re_tarball, comment.body)

if comment_match:
log(f"{funcname}(): found comment with id {comment.id}")

issue_comment = pull_request.get_issue_comment(int(comment.id))

dt = datetime.now(timezone.utc)
comment_update = (f"\n|{dt.strftime('%b %d %X %Z %Y')}|{state}|"
f"transfer of `{tarball}` to S3 bucket {msg}|")

# append update to existing comment
issue_comment.edit(issue_comment.body + comment_update)
issue_comment = pr_comments.determine_issue_comment(pull_request, pr_comment_id, tarball)
if issue_comment:
dt = datetime.now(timezone.utc)
comment_update = (f"\n|{dt.strftime('%b %d %X %Z %Y')}|{state}|"
f"transfer of `{tarball}` to S3 bucket {msg}|")

# leave 'for' loop (only update one comment, because tarball
# should only be referenced in one comment)
break
# append update to existing comment
issue_comment.edit(issue_comment.body + comment_update)


def append_tarball_to_upload_log(tarball, job_dir):
Expand All @@ -253,7 +235,7 @@ def append_tarball_to_upload_log(tarball, job_dir):
upload_log.write(f"{job_plus_tarball}\n")


def upload_tarball(job_dir, build_target, timestamp, repo_name, pr_number):
def upload_tarball(job_dir, build_target, timestamp, repo_name, pr_number, pr_comment_id):
"""
Upload built tarball to an S3 bucket.

Expand All @@ -263,6 +245,7 @@ def upload_tarball(job_dir, build_target, timestamp, repo_name, pr_number):
timestamp (int): timestamp of the tarball
repo_name (string): repository of the pull request
pr_number (int): number of the pull request
pr_comment_id (int): id of the pull request comment

Returns:
None (implicitly)
Expand Down Expand Up @@ -295,13 +278,13 @@ def upload_tarball(job_dir, build_target, timestamp, repo_name, pr_number):
# bucket spec may be a mapping of target repo id to bucket name
bucket_name = bucket_spec.get(target_repo_id)
if bucket_name is None:
update_pr_comment(tarball, repo_name, pr_number, "not uploaded",
update_pr_comment(tarball, repo_name, pr_number, pr_comment_id, "not uploaded",
f"failed (no bucket specified for {target_repo_id})")
return
else:
log(f"Using bucket for {target_repo_id}: {bucket_name}")
else:
update_pr_comment(tarball, repo_name, pr_number, "not uploaded",
update_pr_comment(tarball, repo_name, pr_number, pr_comment_id, "not uploaded",
f"failed (incorrect bucket spec: {bucket_spec})")
return

Expand All @@ -328,11 +311,11 @@ def upload_tarball(job_dir, build_target, timestamp, repo_name, pr_number):
# add file to 'job_dir/../uploaded.txt'
append_tarball_to_upload_log(tarball, job_dir)
# update pull request comment
update_pr_comment(tarball, repo_name, pr_number, "uploaded",
update_pr_comment(tarball, repo_name, pr_number, pr_comment_id, "uploaded",
"succeeded")
else:
# update pull request comment
update_pr_comment(tarball, repo_name, pr_number, "not uploaded",
update_pr_comment(tarball, repo_name, pr_number, pr_comment_id, "not uploaded",
"failed")


Expand Down Expand Up @@ -543,4 +526,5 @@ def deploy_built_artefacts(pr, event_info):
for target, job in to_be_deployed.items():
job_dir = job['job_dir']
timestamp = job['timestamp']
upload_tarball(job_dir, target, timestamp, repo_name, pr.number)
pr_comment_id = job['pr_comment_id']
upload_tarball(job_dir, target, timestamp, repo_name, pr.number, pr_comment_id)
21 changes: 21 additions & 0 deletions tools/pr_comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,27 @@ def create_comment(repo_name, pr_number, comment):
return pull_request.create_issue_comment(comment)


def determine_issue_comment(pull_request, pr_comment_id, search_pattern=None):
"""
Determine issue comment for a given id or using a search pattern.

Args:
pull_request (github.PullRequest.PullRequest): instance representing the pull request
pr_comment_id (int): number of the comment to the pull request to be returned
search_pattern (string): pattern used to determine the comment to the pull request to be returned

Returns:
github.IssueComment.IssueComment instance or None (note, github refers to
PyGithub, not the github from the internal connections module)
"""

if pr_comment_id != -1:
return pull_request.get_issue_comment(pr_comment_id)
else:
# use search pattern to determine issue comment
return get_comment(pull_request, search_pattern)


@retry(Exception, tries=5, delay=1, backoff=2, max_delay=30)
def get_comment(pr, search_pattern):
"""
Expand Down
Loading