-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathbuild-smoketest
More file actions
executable file
·307 lines (259 loc) · 9.82 KB
/
build-smoketest
File metadata and controls
executable file
·307 lines (259 loc) · 9.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/bin/bash
# smoketest - Timed testcase builder based on build-testcase
# Records how long each create-commit* script takes to appear on its target
# branch and prints a summary table at the end.
. ./.gh-api-examples.conf
normal=$(tput sgr0)
highlight=$(tput setaf 2)
bold=$(tput bold)
declare -a r_script
declare -a r_branch
declare -a r_sha
declare -a r_file
declare -a r_duration
declare -a r_status
declare -a r_type
count=0
get_epoch() {
python3 -c "import time; print(f'{time.time():.3f}')"
}
# Run a setup step (non-commit), time it, display and record the result
run_timed_step() {
local script="$1"
local label="$2"
local jq_filter="$3"
local t_start
t_start=$(get_epoch)
local output
output=$(./"${script}" 2>/dev/null)
local rc=$?
local t_end
t_end=$(get_epoch)
local duration
duration=$(python3 -c "print(f'{${t_end} - ${t_start}:.3f}')")
local display_val=""
if [ -n "$jq_filter" ] && [ $rc -eq 0 ]; then
display_val=$(echo "$output" | jq -r "${jq_filter}" 2>/dev/null)
display_val="${display_val:0:15}"
fi
local verified="FAIL"
if [ $rc -eq 0 ]; then
verified="OK"
fi
r_script[$count]="$script"
r_branch[$count]="-"
r_sha[$count]="-"
r_file[$count]="-"
r_duration[$count]="$duration"
r_status[$count]="$verified"
r_type[$count]="setup"
count=$((count + 1))
if [ "$verified" = "OK" ]; then
printf " ${highlight}✅ %-53s${normal} %-15s %8ss\n" "$label" "$display_val" "$duration"
else
printf " ❌ %-53s %-15s %8ss\n" "$label" "$display_val" "$duration"
fi
}
# Run a create-commit script, time it, verify the commit on the given branch
run_timed_commit() {
local script="$1"
local verify_branch="$2"
local t_start
t_start=$(get_epoch)
local output
output=$(./"${script}" 2>/dev/null)
local rc=$?
local sha="N/A"
local file_path="N/A"
if [ $rc -eq 0 ]; then
sha=$(echo "$output" | jq -r '.commit.sha // .object.sha // "N/A"' 2>/dev/null)
file_path=$(echo "$output" | jq -r '.content.path // "N/A"' 2>/dev/null)
fi
# Poll the branch commit history until the SHA appears (max 15 attempts)
local verified="FAIL"
if [ "$sha" != "N/A" ] && [ "$sha" != "null" ] && [ -n "$sha" ]; then
for attempt in $(seq 1 15); do
if curl --silent \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
-H "Accept: application/vnd.github.v3+json" \
"${GITHUB_API_BASE_URL}/repos/${org}/${repo}/commits?sha=${verify_branch}&per_page=5" \
| jq -e '.[].sha' 2>/dev/null \
| grep -q "${sha}"; then
verified="OK"
break
fi
sleep 1
done
fi
local t_end
t_end=$(get_epoch)
local duration
duration=$(python3 -c "print(f'{${t_end} - ${t_start}:.3f}')")
r_script[$count]="$script"
r_branch[$count]="$verify_branch"
r_sha[$count]="${sha:0:7}"
r_file[$count]="$file_path"
r_duration[$count]="$duration"
r_status[$count]="$verified"
r_type[$count]="commit"
count=$((count + 1))
if [ "$verified" = "OK" ]; then
printf " ${highlight}✅ %-53s${normal} %-15s %8ss\n" "$script" "${sha:0:7}" "$duration"
else
printf " ❌ %-53s %-15s %8ss\n" "$script" "${sha:0:7}" "$duration"
fi
}
print_table() {
local sep55
sep55=$(printf -- '-%.0s' $(seq 1 55))
local sep15
sep15=$(printf -- '-%.0s' $(seq 1 15))
local sep10
sep10=$(printf -- '-%.0s' $(seq 1 10))
local sep6
sep6=$(printf -- '-%.0s' $(seq 1 6))
echo ""
printf "${bold}${highlight}=== Commit Timing Summary ===${normal}\n"
echo ""
printf "${bold}%-55s %-15s %-10s %10s %s${normal}\n" \
"Script" "Branch" "SHA" "Duration" "Status"
printf "%-55s %-15s %-10s %10s %s\n" \
"$sep55" "$sep15" "$sep10" "$sep10" "$sep6"
local total_dur=0
for ((i = 0; i < count; i++)); do
local icon
if [ "${r_status[$i]}" = "OK" ]; then
icon="✅"
else
icon="❌"
fi
printf "%-55s %-15s %-10s %9ss %s\n" \
"${r_script[$i]}" "${r_branch[$i]}" "${r_sha[$i]}" \
"${r_duration[$i]}" "$icon"
total_dur=$(python3 -c "print(f'{${total_dur} + ${r_duration[$i]}:.3f}')")
done
printf "%-55s %-15s %-10s %9ss\n" "" "" "Total:" "$total_dur"
echo ""
}
# Verify that all new_branch commit files appear in the PR files list
verify_pr_files() {
local pr_number="$1"
printf "\n${bold}${highlight}=== Pull Request #${pr_number} File Verification ===${normal}\n\n"
# Fetch PR files
local pr_files
pr_files=$(curl --silent \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
-H "Accept: application/vnd.github.v3+json" \
"${GITHUB_API_BASE_URL}/repos/${org}/${repo}/pulls/${pr_number}/files")
local pr_file_list
pr_file_list=$(echo "$pr_files" | jq -r '.[].filename' 2>/dev/null)
if [ -z "$pr_file_list" ]; then
echo " ❌ Could not retrieve PR files"
return 1
fi
local sep55
sep55=$(printf -- '-%.0s' $(seq 1 55))
local sep35
sep35=$(printf -- '-%.0s' $(seq 1 35))
local sep6
sep6=$(printf -- '-%.0s' $(seq 1 6))
printf "${bold}%-55s %-35s %s${normal}\n" "Script" "File" "In PR"
printf "%-55s %-35s %s\n" "$sep55" "$sep35" "$sep6"
local all_ok=true
for ((i = 0; i < count; i++)); do
# Only check commits that targeted the PR head branch
if [ "${r_branch[$i]}" != "${branch_name}" ]; then
continue
fi
local file="${r_file[$i]}"
local icon="❌"
if [ "$file" != "N/A" ] && echo "$pr_file_list" | grep -qx "$file"; then
icon="✅"
else
all_ok=false
fi
printf "%-55s %-35s %s\n" "${r_script[$i]}" "$file" "$icon"
done
echo ""
if [ "$all_ok" = true ]; then
printf "${highlight}All new_branch commit files are present in PR #${pr_number} ✅${normal}\n"
else
printf "Some commit files are missing from PR #${pr_number} ❌\n"
fi
echo ""
}
# ========== Main ==========
printf "${highlight}"
cat << 'EOF'
____ _
| _ \ _____ _____ _ __ ___ _ __ ___ ___ | | _____
| |_) / _ \ \ /\ / / _ \ '__/ __| '_ ` _ \ / _ \| |/ / _ \
| __/ (_) \ V V / __/ | \__ \ | | | | | (_) | < __/
|_| \___/ \_/\_/ \___|_| |___/_| |_| |_|\___/|_|\_\___|
EOF
printf "${normal}"
printf "\n${highlight}--- Setup ---${normal}\n"
repo_check=$(curl -s -o /dev/null -w "%{http_code}" \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
"${GITHUB_API_BASE_URL}/repos/${org}/${repo}")
if [ "$repo_check" = "200" ]; then
./delete-repo.sh
sleep 5
fi
run_timed_step "create-repo-testrepo.sh" "Creating repo" ".name"
run_timed_step "enable-secret-scanning.sh" "Enabling secret scanning" ""
run_timed_step "enable-dependabot-alerts.sh" "Enabling dependabot alerts" ""
run_timed_step "add-team-to-repo.sh" "Adding team to repo" ""
run_timed_step "create-webhook.sh" "Creating webhook" ".id"
printf "\n${highlight}--- Commits on ${base_branch} (default branch) ---${normal}\n"
run_timed_commit "create-commit-readme.sh" "${base_branch}"
run_timed_commit "create-commit-workflow-pwr.sh" "${base_branch}"
run_timed_commit "create-commit-codeowners.sh" "${base_branch}"
run_timed_commit "create-commit-python-pip.sh" "${base_branch}"
run_timed_commit "create-commit-security-shai-hulud-package-json.sh" "${base_branch}"
run_timed_commit "create-commit-github-token-compromised-secret.sh" "${base_branch}"
run_timed_commit "trigger-generic-secret-type.sh" "${base_branch}"
run_timed_commit "create-commit-bad-practice.sh" "${base_branch}"
run_timed_step "enable-code-scanning-default-setup.sh" "Enabling code scanning" ""
sleep 2
printf "\n${highlight}--- Branch ---${normal}\n"
run_timed_step "create-branch-newbranch.sh" "Creating new branch (${branch_name})" ".ref"
printf "\n${highlight}--- Commits on ${branch_name} ---${normal}\n"
run_timed_commit "create-commit-on-new-branch.sh" "${branch_name}"
run_timed_commit "create-commit-update-readme.sh" "${branch_name}"
run_timed_commit "create-commit-gitattributes.sh" "${branch_name}"
printf "\n${highlight}--- Post-commit setup ---${normal}\n"
run_timed_step "create-repo-issue.sh" "Creating an issue" ".html_url"
run_timed_step "create-issue-comment.sh" "Creating an issue comment" ".html_url"
t_pr_start=$(get_epoch)
pr_output=$(./create-pull-request.sh 2>/dev/null)
t_pr_end=$(get_epoch)
pr_number=$(echo "$pr_output" | jq -r '.number')
pr_url=$(echo "$pr_output" | jq -r '.html_url')
pr_duration=$(python3 -c "print(f'{${t_pr_end} - ${t_pr_start}:.3f}')")
pr_status="OK"
if [ -z "$pr_number" ] || [ "$pr_number" = "null" ]; then
pr_status="FAIL"
fi
r_script[$count]="create-pull-request.sh"
r_branch[$count]="-"
r_sha[$count]="-"
r_file[$count]="-"
r_duration[$count]="$pr_duration"
r_status[$count]="$pr_status"
r_type[$count]="setup"
count=$((count + 1))
if [ "$pr_status" = "OK" ]; then
printf " ${highlight}✅ %-53s${normal} %-15s %8ss\n" "Creating a pull request" "${pr_url:0:15}" "$pr_duration"
else
printf " ❌ %-53s %-15s %8ss\n" "Creating a pull request" "${pr_url:0:15}" "$pr_duration"
fi
run_timed_step "create-pull-request-review-comment.sh" "Creating a PR review comment" ".html_url"
run_timed_step "set-branch-protection.sh" "Setting branch protection" ".url"
run_timed_step "create-approving-review-for-a-pull-request.sh" "Approving pull request review" ".state"
run_timed_step "tiny-merge-a-pull-request.sh" "Merging pull request" ".sha"
run_timed_step "create-release.sh" "Creating a release" ".html_url"
run_timed_step "upload-a-release-asset.sh" "Uploading a release asset" ".browser_download_url"
print_table
verify_pr_files "$pr_number"