Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a5f52e8

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

File tree

1 file changed

+94
-21
lines changed

1 file changed

+94
-21
lines changed

scripts/ci-report/fetch_stats_from_ci.sh

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

0 commit comments

Comments
 (0)