Skip to content

Commit 8efb7d6

Browse files
authored
add merging by label (#114)
Co-authored-by: itachi sasuke <[email protected]>
1 parent 09cf08b commit 8efb7d6

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
name: On Pull Request Merged to Master
2+
3+
on:
4+
pull_request:
5+
# Only trigger on pull requests targeting main/master
6+
branches:
7+
- master
8+
- main
9+
types:
10+
- closed
11+
12+
jobs:
13+
run-on-pr-merged:
14+
runs-on: ubuntu-latest
15+
16+
# Only run if the PR was actually merged
17+
if: ${{ github.event.pull_request.merged == true }}
18+
19+
steps:
20+
- name: Add a comment to the merged PR (only if labeler is in the org)
21+
uses: actions/github-script@v7
22+
with:
23+
github-token: ${{ secrets.ORG_GH_TOKEN }}
24+
script: |
25+
// 1. The label format: e.g., "release-1", "release-1.0"
26+
const labelRegex = /^release-[0-9]+(\.[0-9]+)?$/;
27+
28+
// 2. Get PR info
29+
const pullRequestNumber = context.payload.pull_request.number;
30+
const labelsOnPR = context.payload.pull_request.labels || [];
31+
console.log("Labels on the Pull Request:", labelsOnPR.map(label => label.name));
32+
// 3. Get all timeline events to see who labeled the PR
33+
const { data: prEvents } = await github.rest.issues.listEvents({
34+
owner: context.repo.owner,
35+
repo: context.repo.repo,
36+
issue_number: pullRequestNumber
37+
});
38+
39+
// 4. Filter down to "labeled" events
40+
const labeledEvents = prEvents.filter(ev => ev.event === 'labeled');
41+
console.log("Labeled Events:",labeledEvents.map(event => ({
42+
label: event.label?.name,
43+
user: event.actor?.login,
44+
timestamp: event.created_at
45+
})));
46+
// 5. Build a map: labelName -> last user who added it
47+
// (We reverse to get the *most recent* labeler, if a label was added multiple times)
48+
const labelToLastLabeler = {};
49+
for (const event of labeledEvents.reverse()) {
50+
const labelName = event.label?.name;
51+
const userName = event.actor?.login;
52+
if (labelName && userName && !labelToLastLabeler[labelName]) {
53+
labelToLastLabeler[labelName] = userName;
54+
}
55+
}
56+
57+
// 6. For each label on the PR, check if it matches "release-.."
58+
// If yes, we see who labeled it last and check their membership
59+
for (const label of labelsOnPR) {
60+
if (labelRegex.test(label.name)) {
61+
const userWhoAddedLabel = labelToLastLabeler[label.name];
62+
63+
// If there's no recorded user (edge case), skip
64+
if (!userWhoAddedLabel) {
65+
console.log(`User not found for label: ${label.name}`);
66+
continue;
67+
}
68+
69+
// 7. Check if the user is in the org
70+
let isMember = false;
71+
try {
72+
await github.rest.orgs.checkMembershipForUser({
73+
org: 'TykTechnologies',
74+
username: userWhoAddedLabel
75+
});
76+
// If this call succeeds, they're a member
77+
isMember = true;
78+
console.log(`User '${userWhoAddedLabel}' is a member of the organization '${'TykTechnologies'}'.`);
79+
} catch (error) {
80+
// If 404, user is not a member. Anything else is an unexpected error.
81+
if (error.status === 404) {
82+
console.log(`User '${userWhoAddedLabel}' is NOT a member of the organization '${'TykTechnologies'}'.`);
83+
}else {
84+
console.error(`An error occurred while checking membership for user '${userWhoAddedLabel}':`, error);
85+
throw error;
86+
}
87+
}
88+
89+
// 8. Comment only if user is in the org
90+
if (isMember) {
91+
console.log(`Creating comment for label '${label.name}' on PR #${pullRequestNumber} by user '${userWhoAddedLabel}'.`);
92+
await github.rest.issues.createComment({
93+
owner: context.repo.owner,
94+
repo: context.repo.repo,
95+
issue_number: pullRequestNumber,
96+
body: `/release to ${label.name}`
97+
});
98+
}else{
99+
console.log(`No comment created for label '${label.name}' on PR #${pullRequestNumber} because the user '${userWhoAddedLabel}' is not a member of the organization 'TykTechnologies'.`);
100+
}
101+
}else{
102+
console.log(`Label '${label.name}' does not match the expected format.`);
103+
}
104+
}

0 commit comments

Comments
 (0)