diff --git a/.github/workflows/remove-labels-and-assignees-on-close.yml b/.github/workflows/remove-labels-and-assignees-on-close.yml index a2291f7f9..81f7d35f3 100644 --- a/.github/workflows/remove-labels-and-assignees-on-close.yml +++ b/.github/workflows/remove-labels-and-assignees-on-close.yml @@ -5,6 +5,11 @@ on: types: [closed] pull_request: types: [closed] + pull_request_target: + types: [closed] + workflow_run: + workflows: ["*"] + types: [completed] jobs: remove-labels-and-assignees: @@ -15,43 +20,67 @@ jobs: with: github-token: ${{ secrets.GITHUB_TOKEN }} script: | - const issue = context.payload.issue || context.payload.pull_request; const { owner, repo } = context.repo; - try { - const { data: currentLabels } = await github.rest.issues.listLabelsOnIssue({ - owner, - repo, - issue_number: issue.number - }); - - const labelsToKeep = currentLabels - .filter(label => label.name === '⏱︎ Stale') - .map(label => label.name); - - await github.rest.issues.setLabels({ - owner, - repo, - issue_number: issue.number, - labels: labelsToKeep - }); - } catch (error) { - if (error.status !== 404) { - throw error; - } - } - - if (issue.assignees && issue.assignees.length > 0) { + async function processIssue(issueNumber) { try { - await github.rest.issues.removeAssignees({ + const { data: currentLabels } = await github.rest.issues.listLabelsOnIssue({ owner, repo, - issue_number: issue.number, - assignees: issue.assignees.map(assignee => assignee.login) + issue_number: issueNumber }); + + const labelsToKeep = currentLabels + .filter(label => label.name === '⏱︎ Stale') + .map(label => label.name); + + await github.rest.issues.setLabels({ + owner, + repo, + issue_number: issueNumber, + labels: labelsToKeep + }); + + const { data: issue } = await github.rest.issues.get({ + owner, + repo, + issue_number: issueNumber + }); + + if (issue.assignees && issue.assignees.length > 0) { + await github.rest.issues.removeAssignees({ + owner, + repo, + issue_number: issueNumber, + assignees: issue.assignees.map(assignee => assignee.login) + }); + } } catch (error) { 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); + } + }