forked from stack-auth/stack-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
104 lines (90 loc) · 3.92 KB
/
all-good.yaml
File metadata and controls
104 lines (90 loc) · 3.92 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
name: "all-good: Did all the other checks pass?"
on:
push:
branches:
- main
- dev
pull_request:
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.ref != 'refs/heads/main' && github.ref != 'refs/heads/dev' }}
jobs:
all-good:
runs-on: ubuntu-latest
env:
REPO: ${{ github.repository }}
COMMIT: ${{ github.sha }}
PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }}
steps:
- name: Wait for 60 seconds
run: sleep 60
- name: Poll Checks API until complete
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
echo "Checking check runs for commit ${COMMIT} in repo ${REPO}..."
function get_check_runs() {
local endpoint=$1
local response
response=$(curl -s -f -H "Authorization: Bearer ${GITHUB_TOKEN}" "$endpoint")
if [ $? -ne 0 ]; then
echo "Error fetching from $endpoint" >&2
echo "{}"
return 1
fi
echo "$response"
}
function count_pending_checks() {
local response=$1
echo "$response" | jq '([.check_runs[]? | select(.status != "completed" and .name != "all-good")] | length) // 0'
}
function count_failed_checks() {
local response=$1
echo "$response" | jq '([.check_runs[]? | select(.conclusion != "success" and .conclusion != "skipped" and .conclusion != "neutral" and .name != "all-good")] | length) // 0'
}
while true; do
# Always check the current commit's checks
commit_response=$(get_check_runs "https://api.github.com/repos/${REPO}/commits/${COMMIT}/check-runs")
commit_total=$(echo "$commit_response" | jq -r '.total_count // 0')
# If this is a PR, check the PR's head commit checks
pr_total=0
if [ -n "$PR_HEAD_SHA" ] && [ "$PR_HEAD_SHA" != "$COMMIT" ]; then
pr_response=$(get_check_runs "https://api.github.com/repos/${REPO}/commits/${PR_HEAD_SHA}/check-runs")
pr_total=$(echo "$pr_response" | jq -r '.total_count // 0')
echo "Found ${commit_total} current commit checks and ${pr_total} PR head commit checks"
else
echo "Found ${commit_total} commit checks"
fi
# If no checks found at all, wait and retry
if [ "$commit_total" -eq 0 ] && { [ -z "$PR_HEAD_SHA" ] || [ "$pr_total" -eq 0 ]; }; then
echo "No check runs found. Waiting..."
sleep 10
continue
fi
# Check for pending runs in both current and PR head commit checks
commit_pending=$(count_pending_checks "$commit_response")
pr_pending=0
if [ -n "$PR_HEAD_SHA" ] && [ "$PR_HEAD_SHA" != "$COMMIT" ]; then
pr_pending=$(count_pending_checks "$pr_response")
fi
total_pending=$((commit_pending + pr_pending))
if [ "$total_pending" -gt 0 ]; then
echo "$total_pending check run(s) still in progress. Waiting..."
sleep 10
continue
fi
# Check for failures in both current and PR head commit checks
commit_failed=$(count_failed_checks "$commit_response")
pr_failed=0
if [ -n "$PR_HEAD_SHA" ] && [ "$PR_HEAD_SHA" != "$COMMIT" ]; then
pr_failed=$(count_failed_checks "$pr_response")
fi
total_failed=$((commit_failed + pr_failed))
if [ "$total_failed" -eq 0 ]; then
echo "All check runs passed!"
exit 0
else
echo "Some check runs failed. Exiting with error."
exit 1
fi
done