|
| 1 | +name: PR Release Comment Bot |
| 2 | +run-name: ${{ github.actor }} triggered release comment bot |
| 3 | + |
| 4 | +on: |
| 5 | + workflow_dispatch: |
| 6 | + inputs: |
| 7 | + test_version: |
| 8 | + description: 'Test version (e.g., v3.4.0)' |
| 9 | + required: false |
| 10 | + default: 'v3.4.0' |
| 11 | + test_pr_number: |
| 12 | + description: 'Test PR number' |
| 13 | + required: false |
| 14 | + default: '' |
| 15 | + workflow_run: |
| 16 | + workflows: ["Deploy CD"] |
| 17 | + types: [completed] |
| 18 | + branches: [next] |
| 19 | + |
| 20 | +jobs: |
| 21 | + comment-on-pr: |
| 22 | + name: Comment on Released PR |
| 23 | + runs-on: ubuntu-latest |
| 24 | + if: ${{ github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success' }} |
| 25 | + |
| 26 | + steps: |
| 27 | + - name: Checkout Repository |
| 28 | + uses: actions/checkout@v3 |
| 29 | + with: |
| 30 | + fetch-depth: 0 |
| 31 | + |
| 32 | + - name: Get Version and PR from Tag |
| 33 | + id: tag-info |
| 34 | + run: | |
| 35 | + if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then |
| 36 | + VERSION="${{ github.event.inputs.test_version }}" |
| 37 | + PR_NUMBER="${{ github.event.inputs.test_pr_number }}" |
| 38 | + echo "🧪 TEST MODE: version=${VERSION}, pr=${PR_NUMBER}" |
| 39 | + else |
| 40 | + # Fetch all tags from remote |
| 41 | + git fetch --tags |
| 42 | + |
| 43 | + # Get the latest tag |
| 44 | + VERSION=$(git describe --tags --abbrev=0 2>/dev/null || echo "") |
| 45 | + if [ -z "$VERSION" ]; then |
| 46 | + echo "❌ No tags found" |
| 47 | + exit 1 |
| 48 | + fi |
| 49 | + |
| 50 | + # Get PR number from tag message |
| 51 | + TAG_MESSAGE=$(git tag -l --format='%(contents:subject)' "$VERSION") |
| 52 | + PR_NUMBER=$(echo "$TAG_MESSAGE" | grep -oE '#[0-9]+' | head -1 | tr -d '#') |
| 53 | + |
| 54 | + echo "📦 Tag: ${VERSION}, PR: #${PR_NUMBER}" |
| 55 | + fi |
| 56 | + |
| 57 | + echo "version=${VERSION}" >> $GITHUB_OUTPUT |
| 58 | + echo "version_number=${VERSION#v}" >> $GITHUB_OUTPUT |
| 59 | + echo "pr_number=${PR_NUMBER}" >> $GITHUB_OUTPUT |
| 60 | + |
| 61 | + - name: Get Package from PR Files |
| 62 | + id: get-package |
| 63 | + uses: actions/github-script@v7 |
| 64 | + with: |
| 65 | + script: | |
| 66 | + const prNumber = '${{ steps.tag-info.outputs.pr_number }}'; |
| 67 | + if (!prNumber) { |
| 68 | + console.log('No PR number found'); |
| 69 | + core.setOutput('package', 'webex'); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + try { |
| 74 | + const files = await github.paginate(github.rest.pulls.listFiles, { |
| 75 | + owner: context.repo.owner, |
| 76 | + repo: context.repo.repo, |
| 77 | + pull_number: parseInt(prNumber, 10), |
| 78 | + per_page: 100 |
| 79 | + }); |
| 80 | + |
| 81 | + for (const file of files) { |
| 82 | + const parts = file.filename.split('/'); |
| 83 | + if (parts[0] === 'packages' && parts[1] === '@webex' && parts[2]) { |
| 84 | + core.setOutput('package', `@webex/${parts[2]}`); |
| 85 | + console.log(`✅ Found package: @webex/${parts[2]}`); |
| 86 | + return; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + console.log('No @webex package found, defaulting to webex'); |
| 91 | + } catch (e) { |
| 92 | + console.log('Error listing PR files:', e.message); |
| 93 | + } |
| 94 | + |
| 95 | + core.setOutput('package', 'webex'); |
| 96 | + |
| 97 | + - name: Post Release Comment |
| 98 | + uses: actions/github-script@v7 |
| 99 | + with: |
| 100 | + script: | |
| 101 | + const version = '${{ steps.tag-info.outputs.version }}'; |
| 102 | + const versionNumber = '${{ steps.tag-info.outputs.version_number }}'; |
| 103 | + const stableVersion = versionNumber.replace(/-next\..*$/, ''); |
| 104 | + const prNumber = '${{ steps.tag-info.outputs.pr_number }}'; |
| 105 | + const pkg = '${{ steps.get-package.outputs.package }}'; |
| 106 | + |
| 107 | + if (!prNumber) { |
| 108 | + console.log('❌ No PR number found, skipping comment'); |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + console.log(`📝 Posting comment to PR #${prNumber}`); |
| 113 | + |
| 114 | + // Build changelog URL with package auto-selection |
| 115 | + const changelogUrl = new URL('https://web-sdk.webex.com/changelog/'); |
| 116 | + changelogUrl.searchParams.set('stable_version', stableVersion); |
| 117 | + changelogUrl.searchParams.set('prNumber', prNumber); |
| 118 | + if (pkg) { |
| 119 | + changelogUrl.searchParams.set('package', pkg); |
| 120 | + } |
| 121 | + |
| 122 | + // Create comment body |
| 123 | + const body = [ |
| 124 | + '## 🎉 Your changes are now available!', |
| 125 | + '', |
| 126 | + `**Released in:** [\`${version}\`](https://github.com/webex/webex-js-sdk/releases/tag/${version})`, |
| 127 | + `**Package:** \`${pkg}\``, |
| 128 | + '', |
| 129 | + `📖 **[View changelog →](${changelogUrl})**`, |
| 130 | + '', |
| 131 | + 'Thank you for your contribution!', |
| 132 | + '', |
| 133 | + '---', |
| 134 | + '<sub>🤖 This is an automated message.</sub>' |
| 135 | + ].join('\n'); |
| 136 | + |
| 137 | + // Check if PR exists and is merged |
| 138 | + let pr; |
| 139 | + try { |
| 140 | + pr = await github.rest.pulls.get({ |
| 141 | + owner: context.repo.owner, |
| 142 | + repo: context.repo.repo, |
| 143 | + pull_number: parseInt(prNumber) |
| 144 | + }); |
| 145 | + } catch (error) { |
| 146 | + console.log(`❌ PR #${prNumber} not found: ${error.message}`); |
| 147 | + return; |
| 148 | + } |
| 149 | + |
| 150 | + if (!pr.data.merged_at) { |
| 151 | + console.log(`⚠️ PR #${prNumber} is not merged, skipping`); |
| 152 | + return; |
| 153 | + } |
| 154 | + |
| 155 | + // Check for existing comment to avoid duplicates |
| 156 | + const comments = await github.rest.issues.listComments({ |
| 157 | + owner: context.repo.owner, |
| 158 | + repo: context.repo.repo, |
| 159 | + issue_number: parseInt(prNumber) |
| 160 | + }); |
| 161 | + |
| 162 | + const alreadyCommented = comments.data.some(c => |
| 163 | + c.body.includes('Your changes are now available') && |
| 164 | + c.body.includes(version) |
| 165 | + ); |
| 166 | + |
| 167 | + if (alreadyCommented) { |
| 168 | + console.log(`⏭️ Already commented on PR #${prNumber} for ${version}`); |
| 169 | + return; |
| 170 | + } |
| 171 | + |
| 172 | + // Post the comment |
| 173 | + await github.rest.issues.createComment({ |
| 174 | + owner: context.repo.owner, |
| 175 | + repo: context.repo.repo, |
| 176 | + issue_number: parseInt(prNumber), |
| 177 | + body |
| 178 | + }); |
| 179 | + |
| 180 | + console.log(`✅ Successfully commented on PR #${prNumber}`); |
| 181 | + |
| 182 | + - name: Release Summary |
| 183 | + run: | |
| 184 | + echo "### 📊 Bot Summary" >> $GITHUB_STEP_SUMMARY |
| 185 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 186 | + echo "**Version:** ${{ steps.tag-info.outputs.version }}" >> $GITHUB_STEP_SUMMARY |
| 187 | + echo "**PR:** #${{ steps.tag-info.outputs.pr_number }}" >> $GITHUB_STEP_SUMMARY |
| 188 | + echo "**Package:** ${{ steps.get-package.outputs.package }}" >> $GITHUB_STEP_SUMMARY |
| 189 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 190 | + echo "✅ Comment posted on the merged PR" >> $GITHUB_STEP_SUMMARY |
0 commit comments