|
| 1 | +# ------------------------------------------------------------------------------------- |
| 2 | +# |
| 3 | +# Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com). |
| 4 | +# |
| 5 | +# WSO2 LLC. licenses this file to you under the Apache License, |
| 6 | +# Version 2.0 (the "License"); you may not use this file except |
| 7 | +# in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, |
| 13 | +# software distributed under the License is distributed on an |
| 14 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +# KIND, either express or implied. See the License for the |
| 16 | +# specific language governing permissions and limitations |
| 17 | +# under the License. |
| 18 | +# |
| 19 | +# -------------------------------------------------------------------------------------- |
| 20 | + |
| 21 | +# This workflow will check if a submitted PR has changesets. |
| 22 | + |
| 23 | +name: 🦋 Check for Changeset |
| 24 | + |
| 25 | +on: |
| 26 | + workflow_run: |
| 27 | + workflows: ["📩 Receive PR"] |
| 28 | + types: |
| 29 | + - completed |
| 30 | + |
| 31 | +env: |
| 32 | + GH_TOKEN: ${{ secrets.ASGARDEO_GITHUB_BOT_TOKEN }} |
| 33 | + IS_RELEASE_MODE: false |
| 34 | + |
| 35 | +jobs: |
| 36 | + check-changeset: |
| 37 | + runs-on: ubuntu-latest |
| 38 | + if: > |
| 39 | + github.event.workflow_run.event == 'pull_request' && |
| 40 | + github.event.workflow_run.conclusion == 'success' |
| 41 | + steps: |
| 42 | + - name: 📥 Download PR Number Artifact |
| 43 | + uses: actions/download-artifact@v4 |
| 44 | + with: |
| 45 | + name: pr-number |
| 46 | + github-token: ${{ env.GH_TOKEN }} |
| 47 | + repository: ${{ github.repository }} |
| 48 | + run-id: ${{ github.event.workflow_run.id }} |
| 49 | + |
| 50 | + - name: 📝 Display PR Number |
| 51 | + run: cat ./PR_NUMBER |
| 52 | + |
| 53 | + - name: 💬 Remove Existing Changeset Comment |
| 54 | + uses: actions/github-script@v7 |
| 55 | + with: |
| 56 | + github-token: ${{ env.GH_TOKEN }} |
| 57 | + script: | |
| 58 | + const fs = require('fs'); |
| 59 | + const PR_NUMBER = Number(fs.readFileSync('./PR_NUMBER', 'utf8').trim()); |
| 60 | + const REPO_OWNER = context.repo.owner; |
| 61 | + const REPO_NAME = context.repo.repo; |
| 62 | +
|
| 63 | + const comments = await github.rest.issues.listComments({ |
| 64 | + owner: REPO_OWNER, |
| 65 | + repo: REPO_NAME, |
| 66 | + issue_number: PR_NUMBER, |
| 67 | + }); |
| 68 | +
|
| 69 | + console.log("COMMENTS_URL: https://api.github.com/repos/" + REPO_NAME + "/issues/" + PR_NUMBER + "/comments"); |
| 70 | +
|
| 71 | + for (const comment of comments.data) { |
| 72 | + console.log("COMMENT_OWNER: " + comment.user.login); |
| 73 | +
|
| 74 | + if ( |
| 75 | + comment.body.includes("🦋 Changeset detected") || |
| 76 | + comment.body.includes("⚠️ No Changeset found") || |
| 77 | + comment.body.includes("❌ Invalid Changeset detected") |
| 78 | + ) { |
| 79 | + console.log("COMMENT_ID_TO_DELETE: " + comment.id); |
| 80 | +
|
| 81 | + await github.rest.issues.deleteComment({ |
| 82 | + owner: REPO_OWNER, |
| 83 | + repo: REPO_NAME, |
| 84 | + comment_id: comment.id, |
| 85 | + }); |
| 86 | + } |
| 87 | + } |
| 88 | +
|
| 89 | + - name: 💬 Add Changeset Comment |
| 90 | + uses: actions/github-script@v7 |
| 91 | + with: |
| 92 | + github-token: ${{ env.GH_TOKEN }} |
| 93 | + script: | |
| 94 | + const fs = require('fs'); |
| 95 | + const PR_NUMBER = Number(fs.readFileSync('./PR_NUMBER', 'utf8').trim()); |
| 96 | + const REPO_OWNER = context.repo.owner; |
| 97 | + const REPO_NAME = context.repo.repo; |
| 98 | + const IS_RELEASE_MODE = process.env.IS_RELEASE_MODE === 'true'; |
| 99 | +
|
| 100 | + const files = await github.rest.pulls.listFiles({ |
| 101 | + owner: REPO_OWNER, |
| 102 | + repo: REPO_NAME, |
| 103 | + pull_number: PR_NUMBER, |
| 104 | + }); |
| 105 | +
|
| 106 | + const CHANGED_FILES = files.data.map(file => file.filename); |
| 107 | + console.log("CHANGED_FILES_URL: https://api.github.com/repos/" + REPO_NAME + "/pulls/" + PR_NUMBER + "/files"); |
| 108 | + console.log("CHANGED_FILES:", CHANGED_FILES); |
| 109 | +
|
| 110 | + const CHANGESET_FILES = CHANGED_FILES.filter(filename => /^\.changeset\/.*\.md$/.test(filename)); |
| 111 | + const CHANGES_COUNT = CHANGESET_FILES.length; |
| 112 | + console.log("CHANGES_COUNT:", CHANGES_COUNT); |
| 113 | + console.log("IS_RELEASE_MODE:", IS_RELEASE_MODE); |
| 114 | +
|
| 115 | + let invalidChangesets = []; |
| 116 | +
|
| 117 | + if (IS_RELEASE_MODE && CHANGES_COUNT > 0) { |
| 118 | + for (const changesetFile of CHANGESET_FILES) { |
| 119 | + try { |
| 120 | + const fileResponse = await github.rest.repos.getContent({ |
| 121 | + owner: REPO_OWNER, |
| 122 | + repo: REPO_NAME, |
| 123 | + path: changesetFile, |
| 124 | + ref: `refs/pull/${PR_NUMBER}/head` |
| 125 | + }); |
| 126 | +
|
| 127 | + const content = Buffer.from(fileResponse.data.content, 'base64').toString('utf8'); |
| 128 | + console.log("CHANGESET_CONTENT:", changesetFile, content); |
| 129 | +
|
| 130 | + const frontmatterMatch = content.match(/^---\n([\s\S]*?)\n---/); |
| 131 | + if (frontmatterMatch) { |
| 132 | + const frontmatter = frontmatterMatch[1]; |
| 133 | + if (frontmatter.includes(': major') || frontmatter.includes(': minor')) { |
| 134 | + invalidChangesets.push({ |
| 135 | + file: changesetFile, |
| 136 | + type: frontmatter.includes(': major') ? 'major' : 'minor' |
| 137 | + }); |
| 138 | + } |
| 139 | + } |
| 140 | + } catch (error) { |
| 141 | + console.log("Error reading changeset file:", changesetFile, error.message); |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | +
|
| 146 | + let COMMENT; |
| 147 | + if (CHANGES_COUNT > 0) { |
| 148 | + if (IS_RELEASE_MODE && invalidChangesets.length > 0) { |
| 149 | + console.log("Invalid changesets detected in release mode:", invalidChangesets); |
| 150 | + const invalidFiles = invalidChangesets.map(cs => `- \`${cs.file}\` (${cs.type})`).join('\n'); |
| 151 | + COMMENT = `<h3>❌ Invalid Changeset detected</h3><p><b>🚀 WSO2 Identity Server release mode is currently activated!</b></p><p>The following changesets contain major/minor versions, but the master branch only allows patch releases:</p>\n${invalidFiles}\n<p><b>📋 Action Required:</b></p><ul><li>🔧 Update these changesets to use patch versions only, OR</li><li>🎯 Target the <code>next</code> branch for minor/major releases</li></ul><p>📚 Refer <a href="https://github.com/wso2/identity-apps/blob/master/docs/release/README.md">Release Documentation</a> to learn about our release strategy.</p>`; |
| 152 | + } else { |
| 153 | + console.log("Changeset detected"); |
| 154 | + COMMENT = `<h3>🦋 Changeset detected</h3><p><b>The changes in this PR will be included in the next version bump.</b></p><p>Not sure what this means? <a href="https://github.com/changesets/changesets/blob/master/docs/adding-a-changeset.md">Click here to learn what changesets are</a>.</p>`; |
| 155 | + } |
| 156 | + } else { |
| 157 | + console.log("No changeset detected"); |
| 158 | + COMMENT = `<h3>⚠️ No Changeset found</h3>Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go.</p><p><b>If these changes should result in a version bump, you need to add a changeset.</b></p><p>Refer <a href="https://github.com/wso2/identity-apps/blob/master/docs/release/README.md">Release Documentation</a> to learn how to add a changeset.`; |
| 159 | + } |
| 160 | +
|
| 161 | + await github.rest.issues.createComment({ |
| 162 | + owner: REPO_OWNER, |
| 163 | + repo: REPO_NAME, |
| 164 | + issue_number: PR_NUMBER, |
| 165 | + body: COMMENT, |
| 166 | + }); |
| 167 | +
|
0 commit comments