Skip to content

Commit 777d9dc

Browse files
committed
fix(scripts): Use gh api to fetch ci logs more reliably
1 parent 17bb1a1 commit 777d9dc

File tree

1 file changed

+101
-26
lines changed

1 file changed

+101
-26
lines changed

scripts/ci-report/fetch_stats_from_ci.sh

Lines changed: 101 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ set -euo pipefail
1212
dir="$(dirname "$0")"/ci-stats
1313
mkdir -p "${dir}"
1414

15+
# Disable gh run view logs, it's unreliable.
16+
USE_GH_RUN_VIEW_LOGS=0
17+
1518
pushd "${dir}" >/dev/null
1619

1720
# Stats step name, used for filtering log.
@@ -75,32 +78,104 @@ while read -r run; do
7578
job_log=run-"${database_id}"-job-"${job_database_id}"-"${job_name}".log
7679
if [[ ! -f "${job_log}" ]]; then
7780
echo "Fetching log for: ${job_name} (${job_database_id}, ${job_url})"
78-
# Example log (partial).
79-
# test-go (ubuntu-latest) Print test stats 2023-04-11T03:02:18.4063489Z ##[group]Run # Artifacts are not available after rerunning a job,
80-
# test-go (ubuntu-latest) Print test stats 2023-04-11T03:02:18.4063872Z # Artifacts are not available after rerunning a job,
81-
# test-go (ubuntu-latest) Print test stats 2023-04-11T03:02:18.4064188Z # so we need to print the test stats to the log.
82-
# test-go (ubuntu-latest) Print test stats 2023-04-11T03:02:18.4064642Z go run ./scripts/ci-report/main.go gotests.json | tee gotests_stats.json
83-
# test-go (ubuntu-latest) Print test stats 2023-04-11T03:02:18.4110112Z shell: /usr/bin/bash -e {0}
84-
# test-go (ubuntu-latest) Print test stats 2023-04-11T03:02:18.4110364Z ##[endgroup]
85-
# test-go (ubuntu-latest) Print test stats 2023-04-11T03:02:19.3440469Z {
86-
# test-go (ubuntu-latest) Print test stats 2023-04-11T03:02:19.3441078Z "packages": [
87-
# test-go (ubuntu-latest) Print test stats 2023-04-11T03:02:19.3441448Z {
88-
# test-go (ubuntu-latest) Print test stats 2023-04-11T03:02:19.3442927Z "name": "agent",
89-
# test-go (ubuntu-latest) Print test stats 2023-04-11T03:02:19.3443311Z "time": 17.538
90-
# test-go (ubuntu-latest) Print test stats 2023-04-11T03:02:19.3444048Z },
91-
# ...
92-
gh run view --job "${job_database_id}" --log >"${job_log}" || {
93-
# Sometimes gh fails to extract ZIP, etc. :'(
94-
rm -f "${job_log}"
95-
echo "Failed to fetch log for: ${job_name} (${job_database_id}, ${job_url}), skipping..."
96-
continue
97-
}
98-
log_lines="$(wc -l "${job_log}" | awk '{print $1}')"
99-
if [[ ${log_lines} -lt 2 ]]; then
100-
# Sometimes gh returns nothing and gives no error :'(
101-
rm -f "${job_log}"
102-
echo "Log is empty for: ${job_name} (${job_database_id}, ${job_url}), skipping..."
103-
continue
81+
82+
if [[ ${USE_GH_RUN_VIEW_LOGS} -eq 0 ]]; then
83+
# Since gh run view is unreliable, we will fetch the logs via API
84+
# instead, however, unfortunately the API does not provide the job
85+
# name in the log output.
86+
#
87+
# TODO(mafredri): This would be more reliably fetched from the following URL:
88+
# https://github.com/coder/coder/commit/${head_sha}/checks/${job_database_id}/logs/${job_step_number}
89+
# but it requires browser-level authentication(?).
90+
#
91+
# Example output:
92+
#
93+
# 2023-04-14T05:43:34.4763012Z ##[group]Run # Artifacts are not available after rerunning a job,
94+
# 2023-04-14T05:43:34.4763385Z # Artifacts are not available after rerunning a job,
95+
# 2023-04-14T05:43:34.4763815Z # so we need to print the test stats to the log.
96+
# 2023-04-14T05:43:34.4764149Z go run ./scripts/ci-report/main.go gotests.json | tee gotests_stats.json
97+
# 2023-04-14T05:43:34.4809056Z shell: /usr/bin/bash -e {0}
98+
# 2023-04-14T05:43:34.4809308Z ##[endgroup]
99+
# 2023-04-14T05:43:35.5934784Z {
100+
# 2023-04-14T05:43:35.5935419Z "packages": [
101+
# 2023-04-14T05:43:35.5936020Z {
102+
# 2023-04-14T05:43:35.5936585Z "name": "agent",
103+
# 2023-04-14T05:43:35.5937105Z "time": 17.044
104+
# 2023-04-14T05:43:35.5937631Z },
105+
gh api "/repos/coder/coder/actions/jobs/${job_database_id}/logs" >"${job_log}" || {
106+
# Sometimes gh fails to extract ZIP, etc. :'(
107+
rm -f "${job_log}"
108+
echo "Failed to fetch log for: ${job_name} (${job_database_id}, ${job_url}), skipping..."
109+
continue
110+
}
111+
112+
# Elaborate loop for finding the starting point for $job_step_name.
113+
# We check for the first occurrence of "##[group]" which contains
114+
# the go run command and then continue until we find the next
115+
# "##[group]". We then print everything in between.
116+
log_buffer=()
117+
found_step=0
118+
while read -r line; do
119+
if [[ ${found_step} -eq 1 ]] && [[ ${#log_buffer[@]} -eq 0 ]]; then
120+
if [[ ${line} == *"##[group]"* ]]; then
121+
break
122+
fi
123+
# Mimic output from gh run view.
124+
echo "${job_name}"$'\t'"${job_step_name}"$'\t'"${line}"
125+
fi
126+
if [[ ${found_step} -eq 0 ]] && [[ ${#log_buffer[@]} -eq 0 ]] && [[ ${line} != *"##[group]"* ]]; then
127+
continue
128+
fi
129+
if [[ ${line} == *"##[group]"* ]]; then
130+
log_buffer=("${line}")
131+
fi
132+
if [[ ${line} == *"##[endgroup]"* ]]; then
133+
if [[ ${found_step} -eq 1 ]]; then
134+
for bufline in "${log_buffer[@]}"; do
135+
# Mimic output from gh run view.
136+
echo "${job_name}"$'\t'"${job_step_name}"$'\t'"${bufline}"
137+
done
138+
fi
139+
log_buffer=()
140+
continue
141+
fi
142+
if [[ ${#log_buffer[@]} -gt 0 ]]; then
143+
log_buffer+=("${line}")
144+
fi
145+
# If line contains go run ./scripts/ci-report/main.go gotests.json
146+
if [[ ${line} == *"go run ./scripts/ci-report/main.go"* ]]; then
147+
found_step=1
148+
fi
149+
done <"${job_log}" >"${job_log}.parsed"
150+
mv "${job_log}.parsed" "${job_log}"
151+
else
152+
# Example log (partial).
153+
# test-go (ubuntu-latest) Print test stats 2023-04-11T03:02:18.4063489Z ##[group]Run # Artifacts are not available after rerunning a job,
154+
# test-go (ubuntu-latest) Print test stats 2023-04-11T03:02:18.4063872Z # Artifacts are not available after rerunning a job,
155+
# test-go (ubuntu-latest) Print test stats 2023-04-11T03:02:18.4064188Z # so we need to print the test stats to the log.
156+
# test-go (ubuntu-latest) Print test stats 2023-04-11T03:02:18.4064642Z go run ./scripts/ci-report/main.go gotests.json | tee gotests_stats.json
157+
# test-go (ubuntu-latest) Print test stats 2023-04-11T03:02:18.4110112Z shell: /usr/bin/bash -e {0}
158+
# test-go (ubuntu-latest) Print test stats 2023-04-11T03:02:18.4110364Z ##[endgroup]
159+
# test-go (ubuntu-latest) Print test stats 2023-04-11T03:02:19.3440469Z {
160+
# test-go (ubuntu-latest) Print test stats 2023-04-11T03:02:19.3441078Z "packages": [
161+
# test-go (ubuntu-latest) Print test stats 2023-04-11T03:02:19.3441448Z {
162+
# test-go (ubuntu-latest) Print test stats 2023-04-11T03:02:19.3442927Z "name": "agent",
163+
# test-go (ubuntu-latest) Print test stats 2023-04-11T03:02:19.3443311Z "time": 17.538
164+
# test-go (ubuntu-latest) Print test stats 2023-04-11T03:02:19.3444048Z },
165+
# ...
166+
gh run view --job "${job_database_id}" --log >"${job_log}" || {
167+
# Sometimes gh fails to extract ZIP, etc. :'(
168+
rm -f "${job_log}"
169+
echo "Failed to fetch log for: ${job_name} (${job_database_id}, ${job_url}), skipping..."
170+
continue
171+
}
172+
log_lines="$(wc -l "${job_log}" | awk '{print $1}')"
173+
if [[ ${log_lines} -lt 2 ]]; then
174+
# Sometimes gh returns nothing and gives no error :'(
175+
rm -f "${job_log}"
176+
echo "Log is empty for: ${job_name} (${job_database_id}, ${job_url}), skipping..."
177+
continue
178+
fi
104179
fi
105180
fi
106181

0 commit comments

Comments
 (0)