Skip to content

Commit 1c6450d

Browse files
committed
Feat: Make sure this action is also triggered on PR issue close
1 parent 7948a03 commit 1c6450d

File tree

1 file changed

+57
-28
lines changed

1 file changed

+57
-28
lines changed

.github/workflows/remove-labels-and-assignees-on-close.yml

Lines changed: 57 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ on:
55
types: [closed]
66
pull_request:
77
types: [closed]
8+
pull_request_target:
9+
types: [closed]
10+
workflow_run:
11+
workflows: ["*"]
12+
types: [completed]
813

914
jobs:
1015
remove-labels-and-assignees:
@@ -15,43 +20,67 @@ jobs:
1520
with:
1621
github-token: ${{ secrets.GITHUB_TOKEN }}
1722
script: |
18-
const issue = context.payload.issue || context.payload.pull_request;
1923
const { owner, repo } = context.repo;
2024
21-
try {
22-
const { data: currentLabels } = await github.rest.issues.listLabelsOnIssue({
23-
owner,
24-
repo,
25-
issue_number: issue.number
26-
});
25+
async function processIssue(issueNumber) {
26+
try {
27+
const { data: currentLabels } = await github.rest.issues.listLabelsOnIssue({
28+
owner,
29+
repo,
30+
issue_number: issueNumber
31+
});
2732
28-
const labelsToKeep = currentLabels
29-
.filter(label => label.name === '⏱︎ Stale')
30-
.map(label => label.name);
33+
const labelsToKeep = currentLabels
34+
.filter(label => label.name === '⏱︎ Stale')
35+
.map(label => label.name);
3136
32-
await github.rest.issues.setLabels({
33-
owner,
34-
repo,
35-
issue_number: issue.number,
36-
labels: labelsToKeep
37-
});
38-
} catch (error) {
39-
if (error.status !== 404) {
40-
throw error;
41-
}
42-
}
43-
44-
if (issue.assignees && issue.assignees.length > 0) {
45-
try {
46-
await github.rest.issues.removeAssignees({
37+
await github.rest.issues.setLabels({
4738
owner,
4839
repo,
49-
issue_number: issue.number,
50-
assignees: issue.assignees.map(assignee => assignee.login)
40+
issue_number: issueNumber,
41+
labels: labelsToKeep
5142
});
43+
44+
const { data: issue } = await github.rest.issues.get({
45+
owner,
46+
repo,
47+
issue_number: issueNumber
48+
});
49+
50+
if (issue.assignees && issue.assignees.length > 0) {
51+
await github.rest.issues.removeAssignees({
52+
owner,
53+
repo,
54+
issue_number: issueNumber,
55+
assignees: issue.assignees.map(assignee => assignee.login)
56+
});
57+
}
5258
} catch (error) {
5359
if (error.status !== 404) {
54-
throw error;
60+
console.error(`Error processing issue ${issueNumber}:`, error);
5561
}
5662
}
5763
}
64+
65+
if (context.eventName === 'issues' || context.eventName === 'pull_request' || context.eventName === 'pull_request_target') {
66+
const issue = context.payload.issue || context.payload.pull_request;
67+
await processIssue(issue.number);
68+
} else if (context.eventName === 'workflow_run') {
69+
const { data: closedIssues } = await github.rest.search.issuesAndPullRequests({
70+
q: `repo:${owner}/${repo} is:issue is:closed closed:${context.payload.workflow_run.updated_at}`,
71+
per_page: 100
72+
});
73+
for (const issue of closedIssues.items) {
74+
await processIssue(issue.number);
75+
}
76+
}
77+
78+
if (context.eventName === 'pull_request' || context.eventName === 'pull_request_target') {
79+
const { data: closedIssues } = await github.rest.search.issuesAndPullRequests({
80+
q: `repo:${owner}/${repo} is:issue is:closed linked:${context.payload.pull_request.number}`,
81+
per_page: 100
82+
});
83+
for (const issue of closedIssues.items) {
84+
await processIssue(issue.number);
85+
}
86+
}

0 commit comments

Comments
 (0)