-
Notifications
You must be signed in to change notification settings - Fork 66
feat(github-actions): add stale draft PR action #3602
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
thePunderWoman
wants to merge
1
commit into
angular:main
Choose a base branch
from
thePunderWoman:stale-drafts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| load("@devinfra_npm//:defs.bzl", "npm_link_all_packages") | ||
| load("//tools:defaults.bzl", "esbuild_checked_in", "ts_project") | ||
|
|
||
| package(default_visibility = ["//.github/local-actions/stale-cleanup:__subpackages__"]) | ||
|
|
||
| npm_link_all_packages() | ||
|
|
||
| ts_project( | ||
| name = "lib", | ||
| srcs = glob( | ||
| ["lib/*.ts"], | ||
| ), | ||
| tsconfig = "//.github/local-actions:tsconfig", | ||
| deps = [ | ||
| "//.github/local-actions/stale-cleanup:node_modules/@actions/core", | ||
| "//.github/local-actions/stale-cleanup:node_modules/@actions/github", | ||
| "//.github/local-actions/stale-cleanup:node_modules/@octokit/rest", | ||
| "//.github/local-actions/stale-cleanup:node_modules/@types/node", | ||
| "//github-actions:utils", | ||
| ], | ||
| ) | ||
|
|
||
| esbuild_checked_in( | ||
| name = "main", | ||
| srcs = [ | ||
| ":lib", | ||
| ], | ||
| entry_point = "lib/main.ts", | ||
| format = "esm", | ||
| platform = "node", | ||
| target = "node24", | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| name: 'Stale Draft PR Cleanup' | ||
| description: 'Automatically closes draft PRs that have been inactive for a specific number of days.' | ||
| author: 'Angular' | ||
| inputs: | ||
| angular-robot-key: | ||
| description: 'The private key for the Angular Robot Github app.' | ||
| required: true | ||
| repos: | ||
| description: | | ||
| The repositories in which to clean up stale draft PRs. The organization name is derived from | ||
| the context in where the action runs. | ||
| required: true | ||
| runs: | ||
| using: 'node24' | ||
| main: 'main.js' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import * as core from '@actions/core'; | ||
| import {context} from '@actions/github'; | ||
| import {Octokit} from '@octokit/rest'; | ||
| import { | ||
| ANGULAR_ROBOT, | ||
| getAuthTokenFor, | ||
| revokeActiveInstallationToken, | ||
| } from '../../../../github-actions/utils.js'; | ||
|
|
||
| const STALE_DAYS = 28; | ||
|
|
||
| export async function closeStaleDraftPrs(github: Octokit, repo: string): Promise<void> { | ||
| 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!`; | ||
|
|
||
| const threshold = new Date(); | ||
| threshold.setDate(threshold.getDate() - STALE_DAYS); | ||
| const thresholdIso = threshold.toISOString(); | ||
|
|
||
| const repositoryName = `${context.repo.owner}/${repo}`; | ||
| const query = `repo:${repositoryName} is:pr is:draft is:open updated:<${thresholdIso} sort:updated-asc`; | ||
| core.info('Stale Draft PR Query: ' + query); | ||
|
|
||
| let closeCount = 0; | ||
| // We look at 100 at a time to avoid handling too many PRs in one go. | ||
| // With each batch of 100 we'll eventually burn down the list of all stale draft PRs. | ||
| const prResponse = await github.search.issuesAndPullRequests({ | ||
| q: query, | ||
| per_page: 100, | ||
| }); | ||
|
|
||
| core.info(`Stale Draft PR Query found ${prResponse.data.total_count} items`); | ||
|
|
||
| if (!prResponse.data.items.length) { | ||
| core.info(`No draft PRs to close`); | ||
| return; | ||
| } | ||
|
|
||
| core.info(`Attempting to close up to ${prResponse.data.items.length} draft PR(s)`); | ||
| core.startGroup('Closing stale draft PRs'); | ||
|
|
||
| for (const item of prResponse.data.items) { | ||
| if (!item.pull_request) continue; | ||
|
|
||
| try { | ||
| await github.request('POST /graphql', { | ||
| query: ` | ||
| mutation CloseStalePR($id: ID!, $body: String!) { | ||
| addComment(input: {subjectId: $id, body: $body}) { | ||
| clientMutationId | ||
| } | ||
| closePullRequest(input: {pullRequestId: $id}) { | ||
| pullRequest { | ||
| state | ||
| } | ||
| } | ||
| } | ||
| `, | ||
| variables: { | ||
| id: item.node_id, | ||
| body: message, | ||
| }, | ||
| }); | ||
|
|
||
| ++closeCount; | ||
| } catch (error: unknown) { | ||
| const e = error as Error & {request?: unknown}; | ||
| core.warning(`Unable to close draft PR ${repositoryName}#${item.number}: ${e.message}`); | ||
| if (typeof e.request === 'object') { | ||
| core.error(JSON.stringify(e.request, null, 2)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| core.endGroup(); | ||
| core.info(`Closed ${closeCount} stale draft PR(s)`); | ||
| } | ||
|
|
||
| async function main() { | ||
| const github = new Octokit({auth: await getAuthTokenFor(ANGULAR_ROBOT)}); | ||
| try { | ||
| const repos = core.getMultilineInput('repos', {required: true, trimWhitespace: true}); | ||
| await core.group('Repos being cleaned:', async () => | ||
| repos.forEach((repo) => core.info(`- ${repo}`)), | ||
| ); | ||
| for (const repo of repos) { | ||
| await closeStaleDraftPrs(github, repo); | ||
| } | ||
| } catch (error: any) { | ||
| core.debug(error.message); | ||
| core.setFailed(error.message); | ||
| } finally { | ||
| await revokeActiveInstallationToken(github); | ||
| } | ||
| } | ||
|
|
||
| main().catch((err) => { | ||
| console.error(err); | ||
| core.setFailed('Failed with the above error'); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.