Skip to content

Commit 7b27ac2

Browse files
authored
Merge pull request #189 from JayaShakthi97/changeset-checker
ci: Add change set checker workflow
2 parents 087e153 + d8e5c47 commit 7b27ac2

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.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+

.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)