|
| 1 | +# Release Labeler Workflow |
| 2 | +# Automatically labels PRs and issues with the release version they shipped in |
| 3 | +# |
| 4 | +# Features: |
| 5 | +# - Creates `released:vX.Y.Z` label on release publish |
| 6 | +# - Labels all PRs merged between previous and current release |
| 7 | +# - Labels issues that were closed by those PRs |
| 8 | +# - Adds comment linking to the release |
| 9 | +# |
| 10 | +# Usage: |
| 11 | +# 1. Copy to .github/workflows/release-labeler.yml |
| 12 | +# 2. Ensure GITHUB_TOKEN has issues:write and pull-requests:write permissions |
| 13 | + |
| 14 | +name: Release Labeler |
| 15 | + |
| 16 | +on: |
| 17 | + release: |
| 18 | + types: [published] |
| 19 | + |
| 20 | +permissions: |
| 21 | + contents: read |
| 22 | + issues: write |
| 23 | + pull-requests: write |
| 24 | + |
| 25 | +jobs: |
| 26 | + label-release: |
| 27 | + name: Label PRs and Issues |
| 28 | + runs-on: ubuntu-latest |
| 29 | + steps: |
| 30 | + - name: Get release info |
| 31 | + id: release |
| 32 | + env: |
| 33 | + RELEASE_TAG: ${{ github.event.release.tag_name }} |
| 34 | + RELEASE_URL: ${{ github.event.release.html_url }} |
| 35 | + run: | |
| 36 | + echo "tag=${RELEASE_TAG}" >> $GITHUB_OUTPUT |
| 37 | + echo "url=${RELEASE_URL}" >> $GITHUB_OUTPUT |
| 38 | + # Label format: released:v1.2.3 |
| 39 | + echo "label=released:${RELEASE_TAG}" >> $GITHUB_OUTPUT |
| 40 | + |
| 41 | + - name: Get previous release tag |
| 42 | + id: prev_release |
| 43 | + env: |
| 44 | + GH_TOKEN: ${{ github.token }} |
| 45 | + CURRENT_TAG: ${{ steps.release.outputs.tag }} |
| 46 | + run: | |
| 47 | + # Get the previous release tag (skip current) |
| 48 | + PREV_TAG=$(gh api repos/${{ github.repository }}/releases \ |
| 49 | + --jq "[.[] | select(.tag_name != \"${CURRENT_TAG}\" and .prerelease == false)] | .[0].tag_name // \"\"") |
| 50 | + echo "tag=${PREV_TAG}" >> $GITHUB_OUTPUT |
| 51 | + echo "Previous release: ${PREV_TAG:-none}" |
| 52 | + |
| 53 | + - name: Create release label |
| 54 | + env: |
| 55 | + GH_TOKEN: ${{ github.token }} |
| 56 | + LABEL_NAME: ${{ steps.release.outputs.label }} |
| 57 | + RELEASE_TAG: ${{ steps.release.outputs.tag }} |
| 58 | + run: | |
| 59 | + # Create label if it doesn't exist (green color: 0e8a16) |
| 60 | + gh label create "${LABEL_NAME}" \ |
| 61 | + --repo ${{ github.repository }} \ |
| 62 | + --color 0e8a16 \ |
| 63 | + --description "Released in ${RELEASE_TAG}" \ |
| 64 | + 2>/dev/null || echo "Label already exists" |
| 65 | + |
| 66 | + - name: Find and label merged PRs |
| 67 | + env: |
| 68 | + GH_TOKEN: ${{ github.token }} |
| 69 | + LABEL_NAME: ${{ steps.release.outputs.label }} |
| 70 | + RELEASE_URL: ${{ steps.release.outputs.url }} |
| 71 | + RELEASE_TAG: ${{ steps.release.outputs.tag }} |
| 72 | + PREV_TAG: ${{ steps.prev_release.outputs.tag }} |
| 73 | + run: | |
| 74 | + # Get merge commits between tags |
| 75 | + if [[ -n "${PREV_TAG}" ]]; then |
| 76 | + echo "Finding PRs merged between ${PREV_TAG} and ${RELEASE_TAG}" |
| 77 | + # Get commits between tags |
| 78 | + COMMITS=$(gh api repos/${{ github.repository }}/compare/${PREV_TAG}...${RELEASE_TAG} \ |
| 79 | + --jq '.commits[].sha' 2>/dev/null || echo "") |
| 80 | + else |
| 81 | + echo "No previous release found, labeling PRs from last 30 days" |
| 82 | + # Fallback: get recent merged PRs |
| 83 | + COMMITS="" |
| 84 | + fi |
| 85 | + |
| 86 | + # Find merged PRs |
| 87 | + if [[ -n "${COMMITS}" ]]; then |
| 88 | + # Search for PRs by merge commit |
| 89 | + for sha in ${COMMITS}; do |
| 90 | + PR_NUM=$(gh api repos/${{ github.repository }}/commits/${sha}/pulls \ |
| 91 | + --jq '.[0].number // empty' 2>/dev/null || echo "") |
| 92 | + if [[ -n "${PR_NUM}" ]]; then |
| 93 | + echo "Found PR #${PR_NUM} for commit ${sha:0:7}" |
| 94 | + # Add label |
| 95 | + gh pr edit ${PR_NUM} --repo ${{ github.repository }} \ |
| 96 | + --add-label "${LABEL_NAME}" 2>/dev/null || true |
| 97 | + # Add comment (skip if already commented) |
| 98 | + EXISTING=$(gh pr view ${PR_NUM} --repo ${{ github.repository }} \ |
| 99 | + --json comments --jq ".comments[].body | select(contains(\"${RELEASE_TAG}\"))" 2>/dev/null || echo "") |
| 100 | + if [[ -z "${EXISTING}" ]]; then |
| 101 | + gh pr comment ${PR_NUM} --repo ${{ github.repository }} \ |
| 102 | + --body "Released in [${RELEASE_TAG}](${RELEASE_URL})" 2>/dev/null || true |
| 103 | + fi |
| 104 | + fi |
| 105 | + done |
| 106 | + else |
| 107 | + # Fallback: find PRs merged to default branch recently |
| 108 | + echo "Using fallback: searching recently merged PRs" |
| 109 | + gh pr list --repo ${{ github.repository }} --state merged --limit 50 \ |
| 110 | + --json number,mergedAt --jq '.[].number' | while read PR_NUM; do |
| 111 | + gh pr edit ${PR_NUM} --repo ${{ github.repository }} \ |
| 112 | + --add-label "${LABEL_NAME}" 2>/dev/null || true |
| 113 | + done |
| 114 | + fi |
| 115 | + |
| 116 | + - name: Find and label closed issues |
| 117 | + env: |
| 118 | + GH_TOKEN: ${{ github.token }} |
| 119 | + LABEL_NAME: ${{ steps.release.outputs.label }} |
| 120 | + RELEASE_URL: ${{ steps.release.outputs.url }} |
| 121 | + RELEASE_TAG: ${{ steps.release.outputs.tag }} |
| 122 | + run: | |
| 123 | + # Find issues that reference the release label PRs |
| 124 | + # Get PRs with the release label |
| 125 | + PR_NUMBERS=$(gh pr list --repo ${{ github.repository }} --state merged \ |
| 126 | + --label "${LABEL_NAME}" --json number --jq '.[].number') |
| 127 | + |
| 128 | + for PR_NUM in ${PR_NUMBERS}; do |
| 129 | + # Get issues linked/closed by this PR |
| 130 | + LINKED_ISSUES=$(gh pr view ${PR_NUM} --repo ${{ github.repository }} \ |
| 131 | + --json closingIssuesReferences --jq '.closingIssuesReferences[].number' 2>/dev/null || echo "") |
| 132 | + |
| 133 | + for ISSUE_NUM in ${LINKED_ISSUES}; do |
| 134 | + echo "Labeling issue #${ISSUE_NUM} (closed by PR #${PR_NUM})" |
| 135 | + gh issue edit ${ISSUE_NUM} --repo ${{ github.repository }} \ |
| 136 | + --add-label "${LABEL_NAME}" 2>/dev/null || true |
| 137 | + # Add comment |
| 138 | + EXISTING=$(gh issue view ${ISSUE_NUM} --repo ${{ github.repository }} \ |
| 139 | + --json comments --jq ".comments[].body | select(contains(\"${RELEASE_TAG}\"))" 2>/dev/null || echo "") |
| 140 | + if [[ -z "${EXISTING}" ]]; then |
| 141 | + gh issue comment ${ISSUE_NUM} --repo ${{ github.repository }} \ |
| 142 | + --body "Fixed in [${RELEASE_TAG}](${RELEASE_URL})" 2>/dev/null || true |
| 143 | + fi |
| 144 | + done |
| 145 | + done |
| 146 | + |
| 147 | + - name: Summary |
| 148 | + env: |
| 149 | + GH_TOKEN: ${{ github.token }} |
| 150 | + LABEL_NAME: ${{ steps.release.outputs.label }} |
| 151 | + run: | |
| 152 | + PR_COUNT=$(gh pr list --repo ${{ github.repository }} --state merged \ |
| 153 | + --label "${LABEL_NAME}" --json number --jq 'length') |
| 154 | + ISSUE_COUNT=$(gh issue list --repo ${{ github.repository }} --state closed \ |
| 155 | + --label "${LABEL_NAME}" --json number --jq 'length') |
| 156 | + echo "## Release Labeling Complete" >> $GITHUB_STEP_SUMMARY |
| 157 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 158 | + echo "- **Label:** \`${LABEL_NAME}\`" >> $GITHUB_STEP_SUMMARY |
| 159 | + echo "- **PRs labeled:** ${PR_COUNT}" >> $GITHUB_STEP_SUMMARY |
| 160 | + echo "- **Issues labeled:** ${ISSUE_COUNT}" >> $GITHUB_STEP_SUMMARY |
| 161 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 162 | + echo "[View all labeled items](https://github.com/${{ github.repository }}/issues?q=label%3A${LABEL_NAME})" >> $GITHUB_STEP_SUMMARY |
0 commit comments