-
Notifications
You must be signed in to change notification settings - Fork 3
127 lines (115 loc) · 4.81 KB
/
Copy pathOnLabelApproved.yml
File metadata and controls
127 lines (115 loc) · 4.81 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
name: Re-run all checks for PR
on:
pull_request_target:
types: [labeled]
workflow_dispatch:
inputs:
pr:
description: "PR number to re-run (manual)"
required: true
type: number
dry_run:
description: "Only list; do not approve/rerun"
required: false
default: "true"
type: choice
options: ["true", "false"]
permissions:
actions: write
contents: read
pull-requests: read
env:
ORG: 'zul8ha'
REPO: 'zul8ha/demo-repository' # апстрим-репо, где бегают ранки
GH_TOKEN: ${{ secrets.GH_TOKEN }}
GITHUB_TOKEN: ${{ secrets.READ_ORG_TOKEN }}
jobs:
run:
# для label-трига — только если лейбл тот, что нам нужен
if: github.event_name != 'pull_request_target' || github.event.label.name == 'approved'
runs-on: ubuntu-latest
steps:
- name: Resolve PR number and dry-run flag
id: in
run: |
set -euo pipefail
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
pr="${{ github.event.inputs.pr }}"
dry="${{ github.event.inputs.dry_run }}"
else
pr="${{ github.event.pull_request.number }}"
dry="false" # при label-триггере по умолчанию исполняем
fi
echo "pr=$pr" >> "$GITHUB_OUTPUT"
echo "dry=$dry" >> "$GITHUB_OUTPUT"
- name: (Optional) Check actor is org member (for label trigger)
if: github.event_name == 'pull_request_target'
run: |
set -euo pipefail
resp=$(curl -sS \
-H "Authorization: Bearer $GH_TOKEN" \
-H "Accept: application/vnd.github+json" \
https://api.github.com/orgs/${{ env.ORG }}/memberships/${{ github.actor }})
state=$(jq -r '.state // empty' <<< "$resp")
if [ "$state" != "active" ]; then
echo "Not an org member (or token lacks read:org)"; exit 1
fi
- name: Fetch PR head info
id: pr
run: |
set -euo pipefail
pr="${{ steps.in.outputs.pr }}"
pr_json=$(gh api -H "X-GitHub-Api-Version: 2022-11-28" "/repos/${{ env.REPO }}/pulls/$pr")
head_sha=$(jq -r '.head.sha' <<<"$pr_json")
echo "head_sha=$head_sha" >> "$GITHUB_OUTPUT"
echo "PR #$pr head_sha=$head_sha"
- name: List workflow runs for this PR@head_sha
id: runs
run: |
set -euo pipefail
pr="${{ steps.in.outputs.pr }}"
head_sha="${{ steps.pr.outputs.head_sha }}"
# Сольём все ранки события pull_request, пагинация включена
gh api -H "X-GitHub-Api-Version: 2022-11-28" \
"/repos/${{ env.REPO }}/actions/runs?event=pull_request&per_page=100" --paginate \
| jq -r --argjson pr "$pr" --arg sha "$head_sha" '
.workflow_runs[]
| select( any(.pull_requests[]?; .number == $pr) )
| select(.head_sha == $sha) # важный фильтр: только текущий коммит PR
| [.id, .name, .status, .conclusion, .html_url] | @tsv
' | tee runs.tsv
count=$(wc -l < runs.tsv | tr -d ' ')
echo "found=$count" >> "$GITHUB_OUTPUT"
echo "Найдено ранков: $count"
echo ""
echo "Список:"
cat runs.tsv || true
- name: Approve waiting (needs approval) runs from fork
if: steps.runs.outputs.found != '0' && steps.in.outputs.dry == 'false'
run: |
set -euo pipefail
while IFS=$'\t' read -r RUN_ID NAME STATUS CONCLUSION URL; do
[ -z "${RUN_ID:-}" ] && continue
echo "Approve try: $RUN_ID ($NAME) $URL"
gh api -X POST -H "X-GitHub-Api-Version: 2022-11-28" \
"/repos/${{ env.REPO }}/actions/runs/$RUN_ID/approve" \
|| echo " Not awaiting approval (ok)"
done < runs.tsv
- name: Re-run all COMPLETED runs
if: steps.runs.outputs.found != '0' && steps.in.outputs.dry == 'false'
run: |
set -euo pipefail
while IFS=$'\t' read -r RUN_ID NAME STATUS CONCLUSION URL; do
[ -z "${RUN_ID:-}" ] && continue
if [ "$STATUS" = "completed" ]; then
echo "Re-run: $RUN_ID ($NAME) [$CONCLUSION] $URL"
gh api -X POST -H "X-GitHub-Api-Version: 2022-11-28" \
"/repos/${{ env.REPO }}/actions/runs/$RUN_ID/rerun" \
|| echo " Cannot rerun (unexpected) — skip"
else
echo "Skip (status=$STATUS): $RUN_ID $URL"
fi
done < runs.tsv
- name: Summary
run: |
echo "Done. Dry-run=${{ steps.in.outputs.dry }}. Processed ${{ steps.runs.outputs.found }} runs."