|
| 1 | +name: Remove Labels and Assignees on Issue Close |
| 2 | + |
| 3 | +on: |
| 4 | + issues: |
| 5 | + types: [closed] |
| 6 | + pull_request: |
| 7 | + types: [closed] |
| 8 | + pull_request_target: |
| 9 | + types: [closed] |
| 10 | + |
| 11 | +jobs: |
| 12 | + remove-labels-and-assignees: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + steps: |
| 15 | + - name: Remove labels and assignees |
| 16 | + uses: actions/github-script@v7 |
| 17 | + with: |
| 18 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 19 | + script: | |
| 20 | + const { owner, repo } = context.repo; |
| 21 | + |
| 22 | + async function processIssue(issueNumber) { |
| 23 | + try { |
| 24 | + const { data: currentLabels } = await github.rest.issues.listLabelsOnIssue({ |
| 25 | + owner, |
| 26 | + repo, |
| 27 | + issue_number: issueNumber |
| 28 | + }); |
| 29 | +
|
| 30 | + const labelsToKeep = currentLabels |
| 31 | + .filter(label => label.name === '⏱︎ Stale') |
| 32 | + .map(label => label.name); |
| 33 | +
|
| 34 | + await github.rest.issues.setLabels({ |
| 35 | + owner, |
| 36 | + repo, |
| 37 | + issue_number: issueNumber, |
| 38 | + labels: labelsToKeep |
| 39 | + }); |
| 40 | +
|
| 41 | + const { data: issue } = await github.rest.issues.get({ |
| 42 | + owner, |
| 43 | + repo, |
| 44 | + issue_number: issueNumber |
| 45 | + }); |
| 46 | +
|
| 47 | + if (issue.assignees && issue.assignees.length > 0) { |
| 48 | + await github.rest.issues.removeAssignees({ |
| 49 | + owner, |
| 50 | + repo, |
| 51 | + issue_number: issueNumber, |
| 52 | + assignees: issue.assignees.map(assignee => assignee.login) |
| 53 | + }); |
| 54 | + } |
| 55 | + } catch (error) { |
| 56 | + if (error.status !== 404) { |
| 57 | + console.error(`Error processing issue ${issueNumber}:`, error); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | +
|
| 62 | + if (context.eventName === 'issues' || context.eventName === 'pull_request' || context.eventName === 'pull_request_target') { |
| 63 | + const issue = context.payload.issue || context.payload.pull_request; |
| 64 | + await processIssue(issue.number); |
| 65 | + } |
| 66 | +
|
| 67 | + if (context.eventName === 'pull_request' || context.eventName === 'pull_request_target') { |
| 68 | + const { data: closedIssues } = await github.rest.search.issuesAndPullRequests({ |
| 69 | + q: `repo:${owner}/${repo} is:issue is:closed linked:${context.payload.pull_request.number}`, |
| 70 | + per_page: 100 |
| 71 | + }); |
| 72 | + for (const issue of closedIssues.items) { |
| 73 | + await processIssue(issue.number); |
| 74 | + } |
| 75 | + } |
0 commit comments