Feat: Make sure this action is also triggered on PR issue close

This commit is contained in:
peaklabs-dev
2024-09-16 10:23:05 +02:00
parent 7948a0309f
commit 1c6450da24

View File

@@ -5,6 +5,11 @@ on:
types: [closed] types: [closed]
pull_request: pull_request:
types: [closed] types: [closed]
pull_request_target:
types: [closed]
workflow_run:
workflows: ["*"]
types: [completed]
jobs: jobs:
remove-labels-and-assignees: remove-labels-and-assignees:
@@ -15,14 +20,14 @@ jobs:
with: with:
github-token: ${{ secrets.GITHUB_TOKEN }} github-token: ${{ secrets.GITHUB_TOKEN }}
script: | script: |
const issue = context.payload.issue || context.payload.pull_request;
const { owner, repo } = context.repo; const { owner, repo } = context.repo;
async function processIssue(issueNumber) {
try { try {
const { data: currentLabels } = await github.rest.issues.listLabelsOnIssue({ const { data: currentLabels } = await github.rest.issues.listLabelsOnIssue({
owner, owner,
repo, repo,
issue_number: issue.number issue_number: issueNumber
}); });
const labelsToKeep = currentLabels const labelsToKeep = currentLabels
@@ -32,26 +37,50 @@ jobs:
await github.rest.issues.setLabels({ await github.rest.issues.setLabels({
owner, owner,
repo, repo,
issue_number: issue.number, issue_number: issueNumber,
labels: labelsToKeep labels: labelsToKeep
}); });
} catch (error) {
if (error.status !== 404) { const { data: issue } = await github.rest.issues.get({
throw error; owner,
} repo,
} issue_number: issueNumber
});
if (issue.assignees && issue.assignees.length > 0) { if (issue.assignees && issue.assignees.length > 0) {
try {
await github.rest.issues.removeAssignees({ await github.rest.issues.removeAssignees({
owner, owner,
repo, repo,
issue_number: issue.number, issue_number: issueNumber,
assignees: issue.assignees.map(assignee => assignee.login) assignees: issue.assignees.map(assignee => assignee.login)
}); });
}
} catch (error) { } catch (error) {
if (error.status !== 404) { if (error.status !== 404) {
throw error; console.error(`Error processing issue ${issueNumber}:`, error);
} }
} }
} }
if (context.eventName === 'issues' || context.eventName === 'pull_request' || context.eventName === 'pull_request_target') {
const issue = context.payload.issue || context.payload.pull_request;
await processIssue(issue.number);
} else if (context.eventName === 'workflow_run') {
const { data: closedIssues } = await github.rest.search.issuesAndPullRequests({
q: `repo:${owner}/${repo} is:issue is:closed closed:${context.payload.workflow_run.updated_at}`,
per_page: 100
});
for (const issue of closedIssues.items) {
await processIssue(issue.number);
}
}
if (context.eventName === 'pull_request' || context.eventName === 'pull_request_target') {
const { data: closedIssues } = await github.rest.search.issuesAndPullRequests({
q: `repo:${owner}/${repo} is:issue is:closed linked:${context.payload.pull_request.number}`,
per_page: 100
});
for (const issue of closedIssues.items) {
await processIssue(issue.number);
}
}