Skip to content

Commit 73cac81

Browse files
authored
chore: enforce planning label rules for issues (#421)
related: #327 This new GitHub Actions workflow listens for the 'opened' and 'labeled' events on GitHub issues and ensures any issue opened (or labelled) with a `kind/plan-xxx` label meets the following criteria: - only users listed in the `AUTHORIZED_USER` JSON array can label an issue with the `kind/plan-xxx` label - at most one of the `kind/plan-xxx` labels can exist on a given issue The set of supported/expected `kind/plan-xxx` labels are: - `kind/plan-epic` - `kind/plan-feature` - `kind/plan-task` If an issue (with a `kind/plan-xxx` label) is **opened** by a non-authorized user - the issue will be automatically closed when the GitHub action fires. If an issue is labelled with the `kind/plan-xxx` label by an unauthorized user - the `kind/plan-xxx` label is removed. In both cases, a GitHub comment is added to the issue explaining why the issue was updated by the bot. Please note, when opening a GH issue - **BOTH** the `opened` and `labeled` events will fire (if the issue has a label on it). As such, the GitHub action in this PR was deliberately structured to account for multiple instances running in parallel on the same underlying GitHub issue. Also, this work complements #361 - which defines GH issue templates that use the labels in a manner appropriate to satisfy the checks of this action. Signed-off-by: Andy Stoneberg <[email protected]>
1 parent 616d1a8 commit 73cac81

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
name: Validate Planning Issue
2+
3+
on:
4+
issues:
5+
types: [opened, labeled]
6+
7+
env:
8+
AUTHORIZED_USERS: '["thesuperzapper", "ederign", "andyatmiami", "paulovmr", "jenny_s51", "harshad16", "thaorell", "kimwnasptd"]'
9+
10+
permissions:
11+
issues: write
12+
13+
jobs:
14+
validate-issue:
15+
if: |
16+
(github.event.action == 'labeled' && (github.event.label.name == 'kind/plan-epic' || github.event.label.name == 'kind/plan-feature' || github.event.label.name == 'kind/plan-task')) ||
17+
(github.event.action == 'opened' && (contains(github.event.issue.labels.*.name, 'kind/plan-epic') || contains(github.event.issue.labels.*.name, 'kind/plan-feature') || contains(github.event.issue.labels.*.name, 'kind/plan-task')))
18+
runs-on: ubuntu-latest
19+
steps:
20+
- name: Log trigger
21+
uses: actions/github-script@v7
22+
with:
23+
script: |
24+
console.log(`Action triggered by: ${context.eventName} event with action: ${context.payload.action}`);
25+
if (context.payload.action === 'labeled') {
26+
console.log(`Label added: ${context.payload.label.name}`);
27+
} else if (context.payload.action === 'opened') {
28+
console.log(`Issue opened with labels: ${context.payload.issue.labels.map(l => l.name).join(', ')}`);
29+
}
30+
31+
- name: Handle labeled action
32+
if: github.event.action == 'labeled'
33+
uses: actions/github-script@v7
34+
with:
35+
script: |
36+
const AUTHORIZED_USERS = JSON.parse(process.env.AUTHORIZED_USERS);
37+
const actor = context.actor;
38+
const issueNumber = context.issue.number;
39+
const addedLabel = context.payload.label.name;
40+
41+
// First check user authorization
42+
if (!AUTHORIZED_USERS.includes(actor)) {
43+
// Remove the planning label
44+
await github.rest.issues.removeLabel({
45+
owner: context.repo.owner,
46+
repo: context.repo.repo,
47+
issue_number: issueNumber,
48+
name: addedLabel
49+
});
50+
51+
// Add a comment explaining why the label was removed
52+
await github.rest.issues.createComment({
53+
owner: context.repo.owner,
54+
repo: context.repo.repo,
55+
issue_number: issueNumber,
56+
body: `@${actor} You are not authorized to add planning labels. Only authorized users can add planning labels to issues.`
57+
});
58+
59+
core.setFailed(`User ${actor} is not authorized to add planning labels`);
60+
}
61+
62+
console.log(`User ${actor} is authorized to add planning labels`);
63+
64+
// Then check planning label requirements
65+
const { data: issue } = await github.rest.issues.get({
66+
owner: context.repo.owner,
67+
repo: context.repo.repo,
68+
issue_number: issueNumber
69+
});
70+
71+
const currentLabels = issue.labels.map(label => label.name);
72+
const planningLabels = ['kind/plan-epic', 'kind/plan-feature', 'kind/plan-task'];
73+
const presentPlanningLabels = currentLabels.filter(label => planningLabels.includes(label));
74+
75+
if (presentPlanningLabels.length !== 1) {
76+
// Remove the planning label
77+
await github.rest.issues.removeLabel({
78+
owner: context.repo.owner,
79+
repo: context.repo.repo,
80+
issue_number: issueNumber,
81+
name: addedLabel
82+
});
83+
84+
// Add a comment explaining why the label was removed
85+
await github.rest.issues.createComment({
86+
owner: context.repo.owner,
87+
repo: context.repo.repo,
88+
issue_number: issueNumber,
89+
body: `@${actor} Planning labels require exactly one planning label to be present: ${planningLabels.join(', ')}. Please ensure only one planning label is applied.`
90+
});
91+
92+
core.setFailed(`Planning labels require exactly one of: ${planningLabels.join(', ')}`);
93+
}
94+
95+
console.log(`Issue has valid planning label: ${presentPlanningLabels[0]}`);
96+
97+
- name: Handle opened action
98+
if: github.event.action == 'opened'
99+
uses: actions/github-script@v7
100+
with:
101+
script: |
102+
const AUTHORIZED_USERS = JSON.parse(process.env.AUTHORIZED_USERS);
103+
const actor = context.actor;
104+
const issueNumber = context.issue.number;
105+
106+
if (!AUTHORIZED_USERS.includes(actor)) {
107+
// Add a comment explaining why the issue will be closed
108+
await github.rest.issues.createComment({
109+
owner: context.repo.owner,
110+
repo: context.repo.repo,
111+
issue_number: issueNumber,
112+
body: `@${actor} You are not authorized to create planning issues. Only authorized users can create planning issues.`
113+
});
114+
115+
// Close the issue
116+
await github.rest.issues.update({
117+
owner: context.repo.owner,
118+
repo: context.repo.repo,
119+
issue_number: issueNumber,
120+
state: 'closed'
121+
});
122+
123+
core.setFailed(`User ${actor} is not authorized to create planning issues`);
124+
}
125+
126+
console.log(`User ${actor} is authorized to create planning issues`);

0 commit comments

Comments
 (0)