-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathlabel-external-contributions.yml
More file actions
80 lines (69 loc) · 2.44 KB
/
Copy pathlabel-external-contributions.yml
File metadata and controls
80 lines (69 loc) · 2.44 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
name: Label external contributions
on:
schedule:
- cron: "7,22,37,52 * * * *"
workflow_dispatch:
permissions: {}
concurrency:
group: label-external-contributions
cancel-in-progress: false
jobs:
label:
if: github.ref_name == github.event.repository.default_branch
runs-on: ubuntu-latest
timeout-minutes: 10
permissions:
pull-requests: write
steps:
- name: Label external contributions
env:
GH_TOKEN: ${{ github.token }}
REPO: ${{ github.repository }}
run: |
set -euo pipefail
label="external-contribution"
updated_cutoff=$(date -u -d "1 hour ago" "+%Y-%m-%dT%H:%M:%SZ")
while IFS= read -r pr_number; do
if [[ ! "$pr_number" =~ ^[1-9][0-9]*$ ]]; then
echo "Skipping malformed pull request number."
continue
fi
pr_json=$(gh api "repos/$REPO/pulls/$pr_number")
if ! jq -e \
--arg repo "$REPO" \
--arg label "$label" \
'.state == "open" and
.draft == false and
.base.repo.full_name == $repo and
(.head.repo.full_name | type == "string") and
.head.repo.full_name != $repo and
.user.type == "User" and
.author_association != "MEMBER" and
.author_association != "OWNER" and
(any(.labels[]?; .name == $label) | not)' \
>/dev/null <<<"$pr_json"; then
continue
fi
events=$(gh api --paginate \
"repos/$REPO/issues/$pr_number/events?per_page=100" |
jq -cs 'add')
if jq -e --arg label "$label" \
'any(.[]; .event == "labeled" and .label.name == $label)' \
>/dev/null <<<"$events"; then
continue
fi
jq -n --arg label "$label" '{labels: [$label]}' |
gh api --method POST \
"repos/$REPO/issues/$pr_number/labels" \
--input - \
>/dev/null
echo "Labelled pull request #$pr_number."
done < <(
gh api --method GET --paginate "repos/$REPO/issues" \
-f state=open \
-f since="$updated_cutoff" \
-f sort=updated \
-f direction=desc \
-f per_page=100 |
jq -r '.[] | select(.pull_request != null) | .number'
)