Skip to content

Commit c7b9e40

Browse files
authored
Merge pull request #14517 from jcogs33/jcogs33/update-framework-cov-diff-workflow
CI: Update framework coverage difference commenter
2 parents 990d716 + 7c053ed commit c7b9e40

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

.github/workflows/csv-coverage-pr-artifacts.yml

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,32 @@ jobs:
8989
- name: Save PR number
9090
run: |
9191
mkdir -p pr
92-
echo ${{ github.event.pull_request.number }} > pr/NR
92+
echo ${PR_NUMBER} > pr/NR
93+
env:
94+
PR_NUMBER: ${{ github.event.pull_request.number }}
9395
- name: Upload PR number
9496
uses: actions/upload-artifact@v3
9597
with:
9698
name: pr
9799
path: pr/
100+
- name: Save comment ID (if it exists)
101+
run: |
102+
# Find the latest comment starting with COMMENT_PREFIX
103+
COMMENT_PREFIX=":warning: The head of this PR and the base branch were compared for differences in the framework coverage reports."
104+
COMMENT_ID=$(gh api "repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments" --paginate | jq --arg prefix "${COMMENT_PREFIX}" 'map(select(.body|startswith($prefix)) | .id) | max // empty')
105+
if [[ -z ${COMMENT_ID} ]]
106+
then
107+
echo "Comment not found. Not uploading 'comment/ID' artifact."
108+
else
109+
mkdir -p comment
110+
echo ${COMMENT_ID} > comment/ID
111+
fi
112+
env:
113+
GITHUB_TOKEN: ${{ github.token }}
114+
PR_NUMBER: ${{ github.event.pull_request.number }}
115+
- name: Upload comment ID (if it exists)
116+
uses: actions/upload-artifact@v3
117+
with:
118+
name: comment
119+
path: comment/
120+
if-no-files-found: ignore

misc/scripts/library-coverage/comment-pr.py

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,20 @@ def comment_pr(repo, run_id):
5656
if os.path.isdir("pr"):
5757
shutil.rmtree("pr")
5858

59+
try:
60+
utils.download_artifact(repo, "comment", "comment", run_id)
61+
with open("comment/ID") as file:
62+
raw_comment_id = int(file.read().strip())
63+
except Exception as e:
64+
# If there is no existing comment, the `comment/ID` artifact
65+
# will not exist. This will cause `utils.download_artifact`
66+
# to fail, so we catch that and set `raw_comment_id` to `None`.
67+
print("Could not retrieve an existing comment ID. \n", e)
68+
raw_comment_id = None
69+
finally:
70+
if os.path.isdir("comment"):
71+
shutil.rmtree("comment")
72+
5973
# Try storing diff for previous run:
6074
prev_run_id = 0
6175
prev_diff_exists = False
@@ -95,14 +109,37 @@ def comment_pr(repo, run_id):
95109

96110
comment = comment_first_line + \
97111
"A recent commit removed the previously reported differences."
98-
post_comment(comment, repo, pr_number)
112+
113+
if raw_comment_id is None:
114+
post_initial_comment(comment, repo, pr_number)
115+
else:
116+
update_existing_comment(comment, repo, pr_number, raw_comment_id)
99117

100118

101-
def post_comment(comment, repo, pr_number):
119+
def post_initial_comment(comment, repo, pr_number):
102120
print(f"Posting comment to PR #{pr_number}")
103121
utils.subprocess_run(["gh", "pr", "comment", str(pr_number),
104122
"--repo", repo, "--body", comment])
105123

124+
def update_existing_comment(comment, repo, pr_number, raw_comment_id):
125+
# Fetch existing comment, and validate:
126+
# - comment belongs to the PR with number `pr_number`
127+
# - comment starts with the expected prefix `comment_first_line`
128+
# - comment author is github-actions[bot]
129+
comment_author = "github-actions[bot]"
130+
filter = f"select(.issue_url | endswith(\"{repo}/issues/{pr_number}\")) " + \
131+
f"| select(.body | startswith(\"{comment_first_line}\")) " + \
132+
f"| select(.user.login == \"{comment_author}\") " + \
133+
"| .id"
134+
comment_id = utils.subprocess_check_output(["gh", "api", f"repos/{repo}/issues/comments/{raw_comment_id}",
135+
"--jq", filter]).strip()
136+
137+
if comment_id:
138+
print(f"Updating comment {comment_id} on PR #{pr_number}")
139+
utils.subprocess_run(["gh", "api", f"repos/{repo}/issues/comments/{comment_id}",
140+
"-X", "PATCH", "-f", f"body={comment}"])
141+
else:
142+
print(f"Comment {raw_comment_id} did not pass validations: not editing.")
106143

107144
def get_previous_run_id(repo, run_id, pr_number):
108145
"""
@@ -118,7 +155,7 @@ def get_previous_run_id(repo, run_id, pr_number):
118155
pr_repo = this_run["head_repository"]
119156

120157
# Get all previous runs that match branch, repo and workflow name:
121-
output = utils.subprocess_check_output(["gh", "api", "-X", "GET", f"repos/{repo}/actions/runs", "-f", "event=pull_request", "-f", "status=success", "-f", f"branch='{pr_branch}'", "--paginate",
158+
output = utils.subprocess_check_output(["gh", "api", "-X", "GET", f"repos/{repo}/actions/runs", "-f", "event=pull_request", "-f", "status=success", "-f", f"branch={pr_branch}", "--paginate",
122159
"--jq", f'[.workflow_runs.[] | select(.head_repository.full_name=="{pr_repo}" and .name=="{artifacts_workflow_name}")] | sort_by(.id) | reverse | [.[].id]'])
123160

124161
ids = []

0 commit comments

Comments
 (0)