aboutsummaryrefslogtreecommitdiff
path: root/.github/workflows/label-next-release-issues.yml
blob: 5e98b1f5cb4a74b2aed2d517dbcfc491568a3280 (plain)
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
name: Label next-release issues

on:
  push:
    branches: [master]

permissions:
  contents: read
  issues: write

env:
  NEXT_RELEASE_LABEL: included-in-next-release

jobs:
  label:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Label issues referenced by pushed commits
        shell: bash
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          set -euo pipefail

          BEFORE="${{ github.event.before }}"
          AFTER="${{ github.sha }}"
          ZERO_SHA="0000000000000000000000000000000000000000"

          if [ "$BEFORE" = "$ZERO_SHA" ]; then
            RANGE_ARGS=(-1 "$AFTER")
            echo "Scanning latest commit $AFTER for refs #<issue> lines."
          else
            RANGE_ARGS=("$BEFORE..$AFTER")
            echo "Scanning pushed commits in $BEFORE..$AFTER for refs #<issue> lines."
          fi

          mapfile -t ISSUES < <(
            git log --format=%b "${RANGE_ARGS[@]}" \
              | perl -ne 'print "$1\n" if /^\s*refs\s+#([0-9]+)\s*$/i' \
              | sort -nu
          )

          if [ "${#ISSUES[@]}" -eq 0 ]; then
            echo "No issue refs found."
            exit 0
          fi

          if ! gh api "repos/${GITHUB_REPOSITORY}/labels/${NEXT_RELEASE_LABEL}" >/dev/null 2>&1; then
            gh api -X POST "repos/${GITHUB_REPOSITORY}/labels" \
              -f name="$NEXT_RELEASE_LABEL" \
              -f color="5319E7" \
              -f description="Landed on master and planned for the next release." \
              >/dev/null \
              || gh api "repos/${GITHUB_REPOSITORY}/labels/${NEXT_RELEASE_LABEL}" >/dev/null
          fi

          for issue in "${ISSUES[@]}"; do
            echo "Checking #$issue"
            if ! data="$(gh api "repos/${GITHUB_REPOSITORY}/issues/${issue}")"; then
              echo "::warning::Could not read issue #$issue; skipping."
              continue
            fi

            if jq -e 'has("pull_request")' <<<"$data" >/dev/null; then
              echo "Skipping #$issue because it is a pull request."
              continue
            fi

            if [ "$(jq -r '.state' <<<"$data")" != "open" ]; then
              echo "Skipping #$issue because it is not open."
              continue
            fi

            if ! gh issue edit "$issue" --repo "$GITHUB_REPOSITORY" --add-label "$NEXT_RELEASE_LABEL"; then
              echo "::warning::Could not label issue #$issue."
              continue
            fi
          done