|
| 1 | +name: Update Next Release PR |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [closed] |
| 6 | + branches: [develop] |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: read |
| 10 | + pull-requests: write |
| 11 | + |
| 12 | +jobs: |
| 13 | + update-next-release: |
| 14 | + if: github.event.pull_request.merged == true |
| 15 | + runs-on: ubuntu-latest |
| 16 | + steps: |
| 17 | + # The default workflow token cannot open PRs in this org; mint a |
| 18 | + # short-lived token from the configured GitHub App instead. |
| 19 | + - uses: actions/create-github-app-token@v3 |
| 20 | + id: app-token |
| 21 | + with: |
| 22 | + app-id: ${{ secrets.APP_CLIENT_ID }} |
| 23 | + private-key: ${{ secrets.APP_PRIVATE_KEY }} |
| 24 | + |
| 25 | + - name: Update Next Release PR |
| 26 | + env: |
| 27 | + GH_TOKEN: ${{ steps.app-token.outputs.token }} |
| 28 | + PR_NUMBER: ${{ github.event.pull_request.number }} |
| 29 | + PR_TITLE: ${{ github.event.pull_request.title }} |
| 30 | + PR_URL: ${{ github.event.pull_request.html_url }} |
| 31 | + PR_BODY: ${{ github.event.pull_request.body }} |
| 32 | + REPO: ${{ github.repository }} |
| 33 | + ALLOWED_REPOS: "rtk-ai/icm" |
| 34 | + run: | |
| 35 | + set -euo pipefail |
| 36 | +
|
| 37 | + URL_PATTERN="" |
| 38 | + for repo in $ALLOWED_REPOS; do |
| 39 | + URL_PATTERN="${URL_PATTERN}|https://github\\.com/${repo}/issues/[0-9]+" |
| 40 | + done |
| 41 | + URL_PATTERN="${URL_PATTERN#|}" |
| 42 | +
|
| 43 | + if printf '%s' "$PR_TITLE" | grep -qiE '^feat'; then |
| 44 | + SECTION="Feats" |
| 45 | + elif printf '%s' "$PR_TITLE" | grep -qiE '^fix'; then |
| 46 | + SECTION="Fix" |
| 47 | + else |
| 48 | + SECTION="Other" |
| 49 | + fi |
| 50 | +
|
| 51 | + ISSUE_REFS="" |
| 52 | + if [ -n "$PR_BODY" ]; then |
| 53 | + ISSUE_REFS=$(echo "$PR_BODY" \ |
| 54 | + | grep -oiE "(closes|fixes|resolves):?\s+#[0-9]+|${URL_PATTERN}" \ |
| 55 | + | grep -oE '#[0-9]+|issues/[0-9]+' \ |
| 56 | + | sed 's|issues/|#|' \ |
| 57 | + | sort -u \ |
| 58 | + || true) |
| 59 | + fi |
| 60 | +
|
| 61 | + ENTRY="- ${PR_TITLE} [#${PR_NUMBER}](${PR_URL})" |
| 62 | + if [ -n "$ISSUE_REFS" ]; then |
| 63 | + CLOSES_PARTS="" |
| 64 | + while IFS= read -r ref; do |
| 65 | + [ -z "$ref" ] && continue |
| 66 | + NUM="${ref#\#}" |
| 67 | + ISSUE_URL="https://github.com/${REPO}/issues/${NUM}" |
| 68 | + if [ -n "$CLOSES_PARTS" ]; then |
| 69 | + CLOSES_PARTS="${CLOSES_PARTS}, [${ref}](${ISSUE_URL}) (to verify)" |
| 70 | + else |
| 71 | + CLOSES_PARTS="Closes [${ref}](${ISSUE_URL}) (to verify)" |
| 72 | + fi |
| 73 | + done <<< "$ISSUE_REFS" |
| 74 | + ENTRY="${ENTRY} — ${CLOSES_PARTS}" |
| 75 | + fi |
| 76 | +
|
| 77 | + NEXT_PR=$(gh pr list \ |
| 78 | + --repo "$REPO" \ |
| 79 | + --label next-release \ |
| 80 | + --base main \ |
| 81 | + --head develop \ |
| 82 | + --state open \ |
| 83 | + --json number,body \ |
| 84 | + --jq '.[0] // empty') |
| 85 | +
|
| 86 | + NEXT_PR_NUMBER="" |
| 87 | + if [ -n "$NEXT_PR" ]; then |
| 88 | + NEXT_PR_NUMBER=$(echo "$NEXT_PR" | jq -r '.number') |
| 89 | + fi |
| 90 | +
|
| 91 | + if [ -z "$NEXT_PR_NUMBER" ]; then |
| 92 | + TEMPLATE="### Feats |
| 93 | +
|
| 94 | + ### Fix |
| 95 | +
|
| 96 | + ### Other" |
| 97 | +
|
| 98 | + PR_CREATE_URL=$(gh pr create \ |
| 99 | + --repo "$REPO" \ |
| 100 | + --base main \ |
| 101 | + --head develop \ |
| 102 | + --title "Next Release" \ |
| 103 | + --label next-release \ |
| 104 | + --body "$TEMPLATE") |
| 105 | +
|
| 106 | + NEXT_PR_NUMBER=$(echo "$PR_CREATE_URL" | grep -oE '/pull/[0-9]+' | grep -oE '[0-9]+') |
| 107 | + CURRENT_BODY="$TEMPLATE" |
| 108 | + else |
| 109 | + CURRENT_BODY=$(echo "$NEXT_PR" | jq -r '.body') |
| 110 | + fi |
| 111 | +
|
| 112 | + SECTION_HEADER="### ${SECTION}" |
| 113 | + export ENTRY |
| 114 | + if echo "$CURRENT_BODY" | grep -qF "$SECTION_HEADER"; then |
| 115 | + UPDATED_BODY=$(echo "$CURRENT_BODY" | awk -v section="$SECTION_HEADER" ' |
| 116 | + $0 == section { |
| 117 | + print |
| 118 | + print ENVIRON["ENTRY"] |
| 119 | + next |
| 120 | + } |
| 121 | + { print } |
| 122 | + ') |
| 123 | + else |
| 124 | + UPDATED_BODY="${CURRENT_BODY} |
| 125 | +
|
| 126 | + ${SECTION_HEADER} |
| 127 | + ${ENTRY}" |
| 128 | + fi |
| 129 | +
|
| 130 | + gh pr edit "$NEXT_PR_NUMBER" \ |
| 131 | + --repo "$REPO" \ |
| 132 | + --body "$UPDATED_BODY" |
| 133 | +
|
| 134 | + echo "Updated Next Release PR #${NEXT_PR_NUMBER} — added entry to ### ${SECTION}" |
0 commit comments