|
1 | 1 | name: Label external contributions |
2 | 2 |
|
3 | 3 | on: |
4 | | - pull_request_target: |
5 | | - types: |
6 | | - - opened |
7 | | - - reopened |
8 | | - - ready_for_review |
| 4 | + schedule: |
| 5 | + - cron: "7,22,37,52 * * * *" |
| 6 | + workflow_dispatch: |
9 | 7 |
|
10 | 8 | permissions: {} |
11 | 9 |
|
| 10 | +concurrency: |
| 11 | + group: label-external-contributions |
| 12 | + cancel-in-progress: false |
| 13 | + |
12 | 14 | jobs: |
13 | 15 | label: |
14 | | - # Label non-draft PRs from personal accounts that are not owning (github) organization members or owners, |
15 | | - # including outside collaborators and prior contributors. GitHub organization members are excluded, and bot accounts too. |
16 | | - if: >- |
17 | | - github.event.pull_request.draft == false && |
18 | | - github.event.pull_request.user.type == 'User' && |
19 | | - github.event.pull_request.author_association != 'MEMBER' && |
20 | | - github.event.pull_request.author_association != 'OWNER' |
| 16 | + if: github.ref_name == github.event.repository.default_branch |
21 | 17 | runs-on: ubuntu-latest |
22 | | - timeout-minutes: 5 |
| 18 | + timeout-minutes: 10 |
23 | 19 | permissions: |
24 | | - issues: write |
25 | | - concurrency: |
26 | | - group: label-external-contribution-${{ github.event.pull_request.number }} |
27 | | - cancel-in-progress: false |
| 20 | + pull-requests: write |
28 | 21 |
|
29 | 22 | steps: |
30 | | - - name: Add external contribution label |
31 | | - uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 |
32 | | - with: |
33 | | - github-token: ${{ github.token }} |
34 | | - script: | |
35 | | - const label = 'external-contribution'; |
36 | | - const pullRequest = context.payload.pull_request; |
37 | | -
|
38 | | - if (pullRequest.labels.some(({ name }) => name === label)) { |
39 | | - return; |
40 | | - } |
41 | | -
|
42 | | - const events = await github.paginate( |
43 | | - github.rest.issues.listEvents, |
44 | | - { |
45 | | - ...context.repo, |
46 | | - issue_number: pullRequest.number, |
47 | | - per_page: 100, |
48 | | - }, |
49 | | - ); |
50 | | - if ( |
51 | | - events.some(({ event, label: eventLabel }) => |
52 | | - event === 'unlabeled' && eventLabel?.name === label |
53 | | - ) |
54 | | - ) { |
55 | | - return; |
56 | | - } |
57 | | -
|
58 | | - await github.rest.issues.addLabels({ |
59 | | - ...context.repo, |
60 | | - issue_number: pullRequest.number, |
61 | | - labels: [label], |
62 | | - }); |
| 23 | + - name: Label external contributions |
| 24 | + env: |
| 25 | + GH_TOKEN: ${{ github.token }} |
| 26 | + REPO: ${{ github.repository }} |
| 27 | + run: | |
| 28 | + set -euo pipefail |
| 29 | +
|
| 30 | + label="external-contribution" |
| 31 | + updated_cutoff=$(date -u -d "1 hour ago" "+%Y-%m-%dT%H:%M:%SZ") |
| 32 | +
|
| 33 | + while IFS= read -r pr_number; do |
| 34 | + if [[ ! "$pr_number" =~ ^[1-9][0-9]*$ ]]; then |
| 35 | + echo "Skipping malformed pull request number." |
| 36 | + continue |
| 37 | + fi |
| 38 | +
|
| 39 | + pr_json=$(gh api "repos/$REPO/pulls/$pr_number") |
| 40 | + if ! jq -e \ |
| 41 | + --arg repo "$REPO" \ |
| 42 | + --arg label "$label" \ |
| 43 | + '.state == "open" and |
| 44 | + .draft == false and |
| 45 | + .base.repo.full_name == $repo and |
| 46 | + (.head.repo.full_name | type == "string") and |
| 47 | + .head.repo.full_name != $repo and |
| 48 | + .user.type == "User" and |
| 49 | + .author_association != "MEMBER" and |
| 50 | + .author_association != "OWNER" and |
| 51 | + (any(.labels[]?; .name == $label) | not)' \ |
| 52 | + >/dev/null <<<"$pr_json"; then |
| 53 | + continue |
| 54 | + fi |
| 55 | +
|
| 56 | + events=$(gh api --paginate \ |
| 57 | + "repos/$REPO/issues/$pr_number/events?per_page=100" | |
| 58 | + jq -cs 'add') |
| 59 | +
|
| 60 | + if jq -e --arg label "$label" \ |
| 61 | + 'any(.[]; .event == "labeled" and .label.name == $label)' \ |
| 62 | + >/dev/null <<<"$events"; then |
| 63 | + continue |
| 64 | + fi |
| 65 | +
|
| 66 | + jq -n --arg label "$label" '{labels: [$label]}' | |
| 67 | + gh api --method POST \ |
| 68 | + "repos/$REPO/issues/$pr_number/labels" \ |
| 69 | + --input - \ |
| 70 | + >/dev/null |
| 71 | + echo "Labelled pull request #$pr_number." |
| 72 | + done < <( |
| 73 | + gh api --method GET --paginate "repos/$REPO/issues" \ |
| 74 | + -f state=open \ |
| 75 | + -f since="$updated_cutoff" \ |
| 76 | + -f sort=updated \ |
| 77 | + -f direction=desc \ |
| 78 | + -f per_page=100 | |
| 79 | + jq -r '.[] | select(.pull_request != null) | .number' |
| 80 | + ) |
0 commit comments