|
| 1 | +import * as core from '@actions/core'; |
| 2 | +import {context} from '@actions/github'; |
| 3 | +import {Octokit} from '@octokit/rest'; |
| 4 | +import { |
| 5 | + ANGULAR_ROBOT, |
| 6 | + getAuthTokenFor, |
| 7 | + revokeActiveInstallationToken, |
| 8 | +} from '../../../../github-actions/utils.js'; |
| 9 | + |
| 10 | +const STALE_DAYS = 28; |
| 11 | + |
| 12 | +export async function closeStaleDraftPrs(github: Octokit, repo: string): Promise<void> { |
| 13 | + const message = `This draft PR is being closed because it has been stale for ${STALE_DAYS} days and has seen no activity from you. If you'd like to see this change land, you can re-open this PR. Thank you for being an Angular contributor!`; |
| 14 | + |
| 15 | + const threshold = new Date(); |
| 16 | + threshold.setDate(threshold.getDate() - STALE_DAYS); |
| 17 | + const thresholdIso = threshold.toISOString(); |
| 18 | + |
| 19 | + const repositoryName = `${context.repo.owner}/${repo}`; |
| 20 | + const query = `repo:${repositoryName} is:pr is:draft is:open updated:<${thresholdIso} sort:updated-asc`; |
| 21 | + core.info('Stale Draft PR Query: ' + query); |
| 22 | + |
| 23 | + let closeCount = 0; |
| 24 | + // We look at 100 at a time to avoid handling too many PRs in one go. |
| 25 | + // With each batch of 100 we'll eventually burn down the list of all stale draft PRs. |
| 26 | + const prResponse = await github.search.issuesAndPullRequests({ |
| 27 | + q: query, |
| 28 | + per_page: 100, |
| 29 | + }); |
| 30 | + |
| 31 | + core.info(`Stale Draft PR Query found ${prResponse.data.total_count} items`); |
| 32 | + |
| 33 | + if (!prResponse.data.items.length) { |
| 34 | + core.info(`No draft PRs to close`); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + core.info(`Attempting to close up to ${prResponse.data.items.length} draft PR(s)`); |
| 39 | + core.startGroup('Closing stale draft PRs'); |
| 40 | + |
| 41 | + for (const item of prResponse.data.items) { |
| 42 | + if (!item.pull_request) continue; |
| 43 | + |
| 44 | + try { |
| 45 | + await github.request('POST /graphql', { |
| 46 | + query: ` |
| 47 | + mutation CloseStalePR($id: ID!, $body: String!) { |
| 48 | + addComment(input: {subjectId: $id, body: $body}) { |
| 49 | + clientMutationId |
| 50 | + } |
| 51 | + closePullRequest(input: {pullRequestId: $id}) { |
| 52 | + pullRequest { |
| 53 | + state |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + `, |
| 58 | + variables: { |
| 59 | + id: item.node_id, |
| 60 | + body: message, |
| 61 | + }, |
| 62 | + }); |
| 63 | + |
| 64 | + ++closeCount; |
| 65 | + } catch (error: unknown) { |
| 66 | + const e = error as Error & {request?: unknown}; |
| 67 | + core.warning(`Unable to close draft PR ${repositoryName}#${item.number}: ${e.message}`); |
| 68 | + if (typeof e.request === 'object') { |
| 69 | + core.error(JSON.stringify(e.request, null, 2)); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + core.endGroup(); |
| 75 | + core.info(`Closed ${closeCount} stale draft PR(s)`); |
| 76 | +} |
| 77 | + |
| 78 | +async function main() { |
| 79 | + const github = new Octokit({auth: await getAuthTokenFor(ANGULAR_ROBOT)}); |
| 80 | + try { |
| 81 | + const repos = core.getMultilineInput('repos', {required: true, trimWhitespace: true}); |
| 82 | + await core.group('Repos being cleaned:', async () => |
| 83 | + repos.forEach((repo) => core.info(`- ${repo}`)), |
| 84 | + ); |
| 85 | + for (const repo of repos) { |
| 86 | + await closeStaleDraftPrs(github, repo); |
| 87 | + } |
| 88 | + } catch (error: any) { |
| 89 | + core.debug(error.message); |
| 90 | + core.setFailed(error.message); |
| 91 | + } finally { |
| 92 | + await revokeActiveInstallationToken(github); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +main().catch((err) => { |
| 97 | + console.error(err); |
| 98 | + core.setFailed('Failed with the above error'); |
| 99 | +}); |
0 commit comments