Skip to content

Commit f8b7985

Browse files
committed
Add change set checker workflow
1 parent 087e153 commit f8b7985

File tree

2 files changed

+213
-0
lines changed

2 files changed

+213
-0
lines changed
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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.RELEASE_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/[email protected]
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+
// Fetch all comments on the pull request.
64+
const comments = await github.issues.listComments({
65+
owner: REPO_OWNER,
66+
repo: REPO_NAME,
67+
issue_number: PR_NUMBER,
68+
});
69+
70+
console.log("COMMENTS_URL: https://api.github.com/repos/" + REPO_NAME + "/issues/" + PR_NUMBER + "/comments");
71+
72+
for (const comment of comments.data) {
73+
console.log("COMMENT_OWNER: " + comment.user.login);
74+
75+
// Identify the changeset comment by its heading.
76+
if (comment.body.includes("🦋 Changeset detected") || comment.body.includes("⚠️ No Changeset found") || comment.body.includes("❌ Invalid Changeset detected")) {
77+
console.log("COMMENT_ID_TO_DELETE: " + comment.id);
78+
79+
// Remove the changeset comment using the comment ID.
80+
await github.issues.deleteComment({
81+
owner: REPO_OWNER,
82+
repo: REPO_NAME,
83+
comment_id: comment.id,
84+
});
85+
}
86+
}
87+
88+
- name: 💬 Add Changeset Comment
89+
uses: actions/[email protected]
90+
with:
91+
github-token: ${{ env.GH_TOKEN }}
92+
script: |
93+
const fs = require('fs');
94+
const PR_NUMBER = Number(fs.readFileSync('./PR_NUMBER', 'utf8').trim());
95+
const REPO_OWNER = context.repo.owner;
96+
const REPO_NAME = context.repo.repo;
97+
const IS_RELEASE_MODE = process.env.IS_RELEASE_MODE === 'true';
98+
99+
const files = await github.pulls.listFiles({
100+
owner: REPO_OWNER,
101+
repo: REPO_NAME,
102+
pull_number: PR_NUMBER,
103+
});
104+
105+
const CHANGED_FILES = files.data.map(file => file.filename);
106+
console.log("CHANGED_FILES_URL: https://api.github.com/repos/" + REPO_NAME + "/pulls/" + PR_NUMBER + "/files");
107+
console.log("CHANGED_FILES:", CHANGED_FILES);
108+
109+
const CHANGESET_FILES = CHANGED_FILES.filter(filename => /^\.changeset\/.*\.md$/.test(filename));
110+
const CHANGES_COUNT = CHANGESET_FILES.length;
111+
console.log("CHANGES_COUNT:", CHANGES_COUNT);
112+
console.log("IS_RELEASE_MODE:", IS_RELEASE_MODE);
113+
114+
let invalidChangesets = [];
115+
116+
// Check for major/minor changesets in release mode (master branch)
117+
if (IS_RELEASE_MODE && CHANGES_COUNT > 0) {
118+
for (const changesetFile of CHANGESET_FILES) {
119+
try {
120+
const fileResponse = await github.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+
// Parse changeset frontmatter to check for major/minor releases
131+
const frontmatterMatch = content.match(/^---\n([\s\S]*?)\n---/);
132+
if (frontmatterMatch) {
133+
const frontmatter = frontmatterMatch[1];
134+
if (frontmatter.includes(': major') || frontmatter.includes(': minor')) {
135+
invalidChangesets.push({
136+
file: changesetFile,
137+
type: frontmatter.includes(': major') ? 'major' : 'minor'
138+
});
139+
}
140+
}
141+
} catch (error) {
142+
console.log("Error reading changeset file:", changesetFile, error.message);
143+
}
144+
}
145+
}
146+
147+
let COMMENT;
148+
if (CHANGES_COUNT > 0) {
149+
if (IS_RELEASE_MODE && invalidChangesets.length > 0) {
150+
console.log("Invalid changesets detected in release mode:", invalidChangesets);
151+
const invalidFiles = invalidChangesets.map(cs => `- \`${cs.file}\` (${cs.type})`).join('\n');
152+
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>`;
153+
} else {
154+
console.log("Changeset detected");
155+
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>`;
156+
}
157+
} else {
158+
console.log("No changeset detected");
159+
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.`;
160+
}
161+
162+
await github.issues.createComment({
163+
owner: REPO_OWNER,
164+
repo: REPO_NAME,
165+
issue_number: PR_NUMBER,
166+
body: COMMENT,
167+
});

.github/workflows/receive-pr.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 receive a PR and save the PR number for later use.
22+
23+
name: 📩 Receive PR
24+
25+
on:
26+
pull_request:
27+
branches: [main]
28+
29+
jobs:
30+
save-pr-information:
31+
runs-on: ubuntu-latest
32+
steps:
33+
- name: ⬇️ Checkout
34+
uses: actions/checkout@v4
35+
36+
- name: ℹ️ Display PR Information
37+
run: echo "PR Number \#${{github.event.number}}"
38+
39+
- name: 💾 Save PR Number for Later Use
40+
run: echo "${{github.event.number}}" > PR_NUMBER
41+
42+
- name: 📦 Upload PR Number as Artifact
43+
uses: actions/upload-artifact@v4
44+
with:
45+
name: pr-number
46+
path: PR_NUMBER

0 commit comments

Comments
 (0)