Repository navigation
118 lines (114 loc) · 5.06 KB
/
Copy pathpr_labels.yml
File metadata and controls
118 lines (114 loc) · 5.06 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
name: PR labels
# pull_request_target רץ מקוד ענף הבסיס עם הרשאות כתיבה, ולכן אין כאן checkout של קוד ה-PR.
on:
pull_request_target:
types: [labeled, synchronize]
permissions: {}
jobs:
# automerge מאשר קומיט מסוים: כל דחיפה מסירה אותו, ודחיפה בכוח מסירה גם את run-ci.
# הדחיפה של הבוט (ריבייס לפני מיזוג, automerge.yml) לא מסירה תוויות.
drop_on_push:
name: Drop labels on push
if: >-
github.event.action == 'synchronize' && github.event.sender.login != 'otzaria-bot[bot]' &&
(contains(github.event.pull_request.labels.*.name, 'automerge') ||
contains(github.event.pull_request.labels.*.name, 'run-ci'))
runs-on: ubuntu-latest
permissions:
contents: read
issues: write
pull-requests: write
steps:
- env:
GH_TOKEN: ${{ github.token }}
PR: ${{ github.event.pull_request.number }}
BEFORE: ${{ github.event.before }}
AFTER: ${{ github.event.after }}
LABELS: ${{ toJSON(github.event.pull_request.labels.*.name) }}
run: |
drop() {
jq -e --arg l "$1" 'index($l) != null' <<< "$LABELS" > /dev/null || return 0
echo "Removing $1"
gh api -X DELETE "repos/$GITHUB_REPOSITORY/issues/$PR/labels/$1" > /dev/null || echo "$1 already removed"
}
drop automerge
status=$(gh api "repos/$GITHUB_REPOSITORY/compare/$BEFORE...$AFTER" --jq .status) || status=unavailable
if [ "$status" = ahead ]; then
echo "Regular push; run-ci stays."
else
echo "Force push ($status)."
drop run-ci
fi
rerun_on_label:
name: Rerun gated workflows
if: github.event.action == 'labeled' && github.event.label.name == 'run-ci'
runs-on: ubuntu-latest
steps:
# הרצה חוזרת בטוקן של הבוט: זו של GITHUB_TOKEN לא מפעילה workflow_run בסיומה (automerge).
- id: bot
uses: actions/create-github-app-token@v2
with:
app-id: ${{ secrets.BOT_APP_ID }}
private-key: ${{ secrets.BOT_APP_PRIVATE_KEY }}
- env:
GH_TOKEN: ${{ steps.bot.outputs.token }}
PR: ${{ github.event.pull_request.number }}
SHA: ${{ github.event.pull_request.head.sha }}
shell: bash
run: |
R=$GITHUB_REPOSITORY
# A queued label event may outlive its commit or approval. Restarting
# that old run can cancel current-head CI through PR concurrency.
require_current_pr() {
local snapshot predicate_status
if ! snapshot=$(gh api "repos/$R/pulls/$PR"); then
echo "::error::Could not verify the current PR before restarting CI."
exit 1
fi
if jq -e --arg sha "$SHA" \
'.state == "open" and .head.sha == $sha and any(.labels[]?; .name == "run-ci")' \
<<< "$snapshot" > /dev/null; then
return 0
else
predicate_status=$?
fi
if [ "$predicate_status" != 1 ]; then
echo "::error::Could not parse the current PR before restarting CI."
exit "$predicate_status"
fi
echo "PR #$PR is no longer open, labeled run-ci, and at $SHA; stopping this label event."
exit 0
}
require_current_pr
# ה-workflows שאינם מאחורי השער כבר רצו (או רצים על אירוע התווית עצמו).
ungated='Lockfile Guard|Goldens Guard|UI Baseline Guard|CI Automation Tests'
runs=$(gh api --paginate "repos/$R/actions/runs?head_sha=$SHA&event=pull_request&per_page=100" \
--jq '.workflow_runs[] | [.workflow_id, .run_number, .id, .name] | @tsv' \
| sort -t$'\t' -k1,1n -k2,2nr | sort -s -t$'\t' -u -k1,1n)
[ -n "$runs" ] || { echo "No pull_request runs for $SHA"; exit 0; }
while IFS=$'\t' read -r _ _ id name; do
if grep -qxE "$ungated" <<< "$name"; then
echo "$name: not gated"
continue
fi
require_current_pr
status=$(gh api "repos/$R/actions/runs/$id" --jq .status)
if [ "$status" != completed ]; then
require_current_pr
echo "$name: run $id is $status; cancelling before rerun"
gh run cancel "$id" -R "$R" || true
for _ in $(seq 60); do
require_current_pr
status=$(gh api "repos/$R/actions/runs/$id" --jq .status)
[ "$status" = completed ] && break
sleep 5
done
fi
if [ "$status" != completed ]; then
echo "::warning::$name: run $id is $status; not rerun"
continue
fi
require_current_pr
echo "$name: rerunning run $id"
gh run rerun "$id" -R "$R" || echo "::warning::$name: rerun of $id failed"
done <<< "$runs"