Skip to content

Commit a9ca8fc

Browse files
committed
#2465: Update comments script
1 parent 2fd2099 commit a9ca8fc

File tree

1 file changed

+31
-48
lines changed

1 file changed

+31
-48
lines changed

scripts/report_logs_in_comment.sh

Lines changed: 31 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -47,80 +47,65 @@ cmake_output_log="$2"
4747
build_number="$3"
4848
pull_request_number="$4"
4949
repository_name="$5"
50-
github_pat="$6"
50+
github_token="$6"
5151
build_id="$7"
52-
job_id="$8"
52+
job_name="$8"
5353
job_status="$9"
54-
task_id="28db5144-7e5d-5c90-2820-8676d630d9d2"
5554

5655
echo "job_status: $job_status"
57-
if test "$job_status" = "Succeeded" || test "$job_status" = "SucceededWithIssues"
58-
then
56+
if [[ "$job_status" == "success" || "$job_status" == "failure" ]]; then
5957
succeeded=1
6058
else
6159
succeeded=0
6260
fi
6361

64-
# Extract compilation's errors and warnings from log file
6562
warnings_errors=$(cat "$compilation_errors_warnings_out")
6663

67-
# Extract tests' report from log file
6864
delimiter="-=-=-=-"
6965
tests_failures=""
70-
if test -f "$cmake_output_log"
71-
then
66+
if [[ -f "$cmake_output_log" ]]; then
7267
tests_failures=$(< "$cmake_output_log" sed -n -e '/The following tests FAILED:/,$p')
7368
tests_failures=${tests_failures//$'\n'/$delimiter}
7469
tabulation=" "
7570
tests_failures=${tests_failures//$'\t'/$tabulation}
7671
fi
7772

78-
if test "$succeeded" -eq 1
79-
then
80-
if test -z "$warnings_errors"
81-
then
82-
warnings_errors='Compilation - successful'
83-
fi
84-
85-
if test -z "$tests_failures"
86-
then
87-
tests_failures='Testing - passed'
88-
fi
73+
if [[ "$succeeded" -eq 1 ]]; then
74+
[[ -z "$warnings_errors" ]] && warnings_errors='Compilation - successful'
75+
[[ -z "$tests_failures" ]] && tests_failures='Testing - passed'
8976
else
90-
if test -z "$warnings_errors" && test -z "$tests_failures"
91-
then
77+
if [[ -z "$warnings_errors" && -z "$tests_failures" ]]; then
9278
warnings_errors='Build failed for unknown reason. Check build logs'
9379
fi
9480
fi
9581

96-
# Concatenate both reports into one
9782
val="$warnings_errors""$delimiter""$delimiter""$tests_failures"
9883
max_comment_size=3000
99-
if test ${#val} -gt "$max_comment_size"
100-
then
84+
if [[ ${#val} -gt "$max_comment_size" ]]; then
10185
val="${val:0:max_comment_size}%0D%0A%0D%0A%0D%0A ==> And there is more. Read log. <=="
10286
fi
10387

104-
# Build comment
10588
commit_sha="$(git log --skip=1 -1 --pretty=format:%H)"
10689
commit_date="$(TZ=UTC0 git show -s --format=%cd --date=format-local:'%Y-%m-%d %H:%M:%S' "$commit_sha")"
107-
build_link='[Build log](https://dev.azure.com/DARMA-tasking/DARMA/_build/results?buildId='"$build_id"'&view=logs&j='"$job_id"'&t='"$task_id)"
108-
comment_body="Build for $commit_sha ($commit_date UTC)\n\n"'```'"\n$val\n"'```'"\n\n$build_link"
10990

110-
# Fix new lines
111-
new_line="\n"
112-
comment_body=${comment_body//$delimiter/$new_line}
113-
quotation_mark="\""
114-
new_quotation_mark="\\\""
115-
comment_body=${comment_body//$quotation_mark/$new_quotation_mark}
91+
# Fetch numeric job ID from GitHub API
92+
job_id=$(curl -s -H "Authorization: token $github_token" \
93+
"https://api.github.com/repos/${repository_name}/actions/runs/${build_id}/jobs" | \
94+
jq -r --arg name "$job_name" '.jobs[] | select(.name == $name) | .id')
11695

117-
# Ensure there's no temporary json file
118-
if test -f data.json
119-
then
120-
rm data.json
96+
# Fallback to run-level link if job ID is unavailable
97+
if [[ -n "$job_id" && "$job_id" != "null" ]]; then
98+
build_link="[Build log](https://github.com/${repository_name}/actions/runs/${build_id}/job/${job_id})"
99+
else
100+
build_link="[Build log](https://github.com/${repository_name}/actions/runs/${build_id})"
121101
fi
122102

123-
# Prepare data send with request to GitHub
103+
comment_body="Build for $commit_sha ($commit_date UTC)\n\n"'```'"\n$val\n"'```'"\n\n$build_link"
104+
comment_body=${comment_body//$delimiter/$'\n'}
105+
comment_body=${comment_body//\"/\\\"}
106+
107+
rm -f data.json
108+
124109
{
125110
echo "{"
126111
echo ' "event_type": "comment-pr",'
@@ -132,13 +117,11 @@ echo " }"
132117
echo "}"
133118
} >> data.json
134119

135-
# Send GitHub request to post a PR comment
136-
curl \
137-
--request POST \
138-
--url https://api.github.com/repos/"$repository_name"/dispatches \
139-
--header "Accept: application/vnd.github.everest-preview+json" \
140-
--header "Authorization: token $github_pat" \
141-
--data "@data.json"
120+
curl \
121+
--request POST \
122+
--url "https://api.github.com/repos/${repository_name}/dispatches" \
123+
--header "Accept: application/vnd.github.everest-preview+json" \
124+
--header "Authorization: token ${github_token}" \
125+
--data "@data.json"
142126

143-
# Clean up
144-
rm data.json
127+
rm -f data.json

0 commit comments

Comments
 (0)