|
| 1 | +name: Label external contributions |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + - cron: "7,22,37,52 * * * *" |
| 6 | + workflow_dispatch: |
| 7 | + |
| 8 | +permissions: {} |
| 9 | + |
| 10 | +concurrency: |
| 11 | + group: label-external-contributions |
| 12 | + cancel-in-progress: false |
| 13 | + |
| 14 | +jobs: |
| 15 | + label: |
| 16 | + if: github.ref_name == github.event.repository.default_branch |
| 17 | + runs-on: ubuntu-latest |
| 18 | + timeout-minutes: 10 |
| 19 | + permissions: |
| 20 | + pull-requests: write |
| 21 | + |
| 22 | + steps: |
| 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