From d59d99665b119141e506a492d03e381937a0ca40 Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Thu, 14 May 2026 23:47:59 -0500 Subject: [PATCH] ci: Automate PR cleanup (#27667) --- .github/workflows/close-prs.yml | 50 ++++ .github/workflows/close-stale-prs.yml | 235 ----------------- script/github/close-prs.ts | 347 ++++++++++++++++++++++++++ 3 files changed, 397 insertions(+), 235 deletions(-) create mode 100644 .github/workflows/close-prs.yml delete mode 100644 .github/workflows/close-stale-prs.yml create mode 100644 script/github/close-prs.ts diff --git a/.github/workflows/close-prs.yml b/.github/workflows/close-prs.yml new file mode 100644 index 0000000000..a1e603a881 --- /dev/null +++ b/.github/workflows/close-prs.yml @@ -0,0 +1,50 @@ +name: close-prs + +on: + schedule: + - cron: "0 22 * * *" # Daily at 10:00 PM UTC + workflow_dispatch: + inputs: + dry-run: + description: "Log matching PRs without closing them" + type: boolean + default: true + max-close: + description: "Maximum matching PRs to close" + type: string + required: false + default: "50" + +jobs: + close: + runs-on: ubuntu-latest + timeout-minutes: 240 + permissions: + contents: read + issues: write + pull-requests: write + steps: + - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1 + + - uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2.2.0 + with: + bun-version: latest + + - name: Close old PRs without enough positive reactions + env: + GITHUB_TOKEN: ${{ github.token }} + run: | + max_close="${{ inputs['max-close'] }}" + if [ -z "$max_close" ]; then + max_close="50" + fi + + args=("--threshold" "2" "--age-months" "1" "--sleep-ms" "20000" "--max-close" "$max_close") + + if [ "${{ github.event_name }}" = "schedule" ]; then + args+=("--execute") + elif [ "${{ inputs['dry-run'] }}" = "false" ]; then + args+=("--execute") + fi + + bun script/github/close-prs.ts "${args[@]}" diff --git a/.github/workflows/close-stale-prs.yml b/.github/workflows/close-stale-prs.yml deleted file mode 100644 index 3a0fa4b5c7..0000000000 --- a/.github/workflows/close-stale-prs.yml +++ /dev/null @@ -1,235 +0,0 @@ -name: close-stale-prs - -on: - workflow_dispatch: - inputs: - dryRun: - description: "Log actions without closing PRs" - type: boolean - default: false - schedule: - - cron: "0 6 * * *" - -permissions: - contents: read - issues: write - pull-requests: write - -jobs: - close-stale-prs: - runs-on: ubuntu-latest - timeout-minutes: 15 - steps: - - name: Close inactive PRs - uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 - with: - github-token: ${{ secrets.GITHUB_TOKEN }} - script: | - const DAYS_INACTIVE = 60 - const MAX_RETRIES = 3 - - // Adaptive delay: fast for small batches, slower for large to respect - // GitHub's 80 content-generating requests/minute limit - const SMALL_BATCH_THRESHOLD = 10 - const SMALL_BATCH_DELAY_MS = 1000 // 1s for daily operations (≤10 PRs) - const LARGE_BATCH_DELAY_MS = 2000 // 2s for backlog (>10 PRs) = ~30 ops/min, well under 80 limit - - const startTime = Date.now() - const cutoff = new Date(Date.now() - DAYS_INACTIVE * 24 * 60 * 60 * 1000) - const { owner, repo } = context.repo - const dryRun = context.payload.inputs?.dryRun === "true" - - core.info(`Dry run mode: ${dryRun}`) - core.info(`Cutoff date: ${cutoff.toISOString()}`) - - function sleep(ms) { - return new Promise(resolve => setTimeout(resolve, ms)) - } - - async function withRetry(fn, description = 'API call') { - let lastError - for (let attempt = 0; attempt < MAX_RETRIES; attempt++) { - try { - const result = await fn() - return result - } catch (error) { - lastError = error - const isRateLimited = error.status === 403 && - (error.message?.includes('rate limit') || error.message?.includes('secondary')) - - if (!isRateLimited) { - throw error - } - - // Parse retry-after header, default to 60 seconds - const retryAfter = error.response?.headers?.['retry-after'] - ? parseInt(error.response.headers['retry-after']) - : 60 - - // Exponential backoff: retryAfter * 2^attempt - const backoffMs = retryAfter * 1000 * Math.pow(2, attempt) - - core.warning(`${description}: Rate limited (attempt ${attempt + 1}/${MAX_RETRIES}). Waiting ${backoffMs / 1000}s before retry...`) - - await sleep(backoffMs) - } - } - core.error(`${description}: Max retries (${MAX_RETRIES}) exceeded`) - throw lastError - } - - const query = ` - query($owner: String!, $repo: String!, $cursor: String) { - repository(owner: $owner, name: $repo) { - pullRequests(first: 100, states: OPEN, after: $cursor) { - pageInfo { - hasNextPage - endCursor - } - nodes { - number - title - author { - login - } - createdAt - commits(last: 1) { - nodes { - commit { - committedDate - } - } - } - comments(last: 1) { - nodes { - createdAt - } - } - reviews(last: 1) { - nodes { - createdAt - } - } - } - } - } - } - ` - - const allPrs = [] - let cursor = null - let hasNextPage = true - let pageCount = 0 - - while (hasNextPage) { - pageCount++ - core.info(`Fetching page ${pageCount} of open PRs...`) - - const result = await withRetry( - () => github.graphql(query, { owner, repo, cursor }), - `GraphQL page ${pageCount}` - ) - - allPrs.push(...result.repository.pullRequests.nodes) - hasNextPage = result.repository.pullRequests.pageInfo.hasNextPage - cursor = result.repository.pullRequests.pageInfo.endCursor - - core.info(`Page ${pageCount}: fetched ${result.repository.pullRequests.nodes.length} PRs (total: ${allPrs.length})`) - - // Delay between pagination requests (use small batch delay for reads) - if (hasNextPage) { - await sleep(SMALL_BATCH_DELAY_MS) - } - } - - core.info(`Found ${allPrs.length} open pull requests`) - - const stalePrs = allPrs.filter((pr) => { - const dates = [ - new Date(pr.createdAt), - pr.commits.nodes[0] ? new Date(pr.commits.nodes[0].commit.committedDate) : null, - pr.comments.nodes[0] ? new Date(pr.comments.nodes[0].createdAt) : null, - pr.reviews.nodes[0] ? new Date(pr.reviews.nodes[0].createdAt) : null, - ].filter((d) => d !== null) - - const lastActivity = dates.sort((a, b) => b.getTime() - a.getTime())[0] - - if (!lastActivity || lastActivity > cutoff) { - core.info(`PR #${pr.number} is fresh (last activity: ${lastActivity?.toISOString() || "unknown"})`) - return false - } - - core.info(`PR #${pr.number} is STALE (last activity: ${lastActivity.toISOString()})`) - return true - }) - - if (!stalePrs.length) { - core.info("No stale pull requests found.") - return - } - - core.info(`Found ${stalePrs.length} stale pull requests`) - - // ============================================ - // Close stale PRs - // ============================================ - const requestDelayMs = stalePrs.length > SMALL_BATCH_THRESHOLD - ? LARGE_BATCH_DELAY_MS - : SMALL_BATCH_DELAY_MS - - core.info(`Using ${requestDelayMs}ms delay between operations (${stalePrs.length > SMALL_BATCH_THRESHOLD ? 'large' : 'small'} batch mode)`) - - let closedCount = 0 - let skippedCount = 0 - - for (const pr of stalePrs) { - const issue_number = pr.number - const closeComment = `Closing this pull request because it has had no updates for more than ${DAYS_INACTIVE} days. If you plan to continue working on it, feel free to reopen or open a new PR.` - - if (dryRun) { - core.info(`[dry-run] Would close PR #${issue_number} from ${pr.author?.login || 'unknown'}: ${pr.title}`) - continue - } - - try { - // Add comment - await withRetry( - () => github.rest.issues.createComment({ - owner, - repo, - issue_number, - body: closeComment, - }), - `Comment on PR #${issue_number}` - ) - - // Close PR - await withRetry( - () => github.rest.pulls.update({ - owner, - repo, - pull_number: issue_number, - state: "closed", - }), - `Close PR #${issue_number}` - ) - - closedCount++ - core.info(`Closed PR #${issue_number} from ${pr.author?.login || 'unknown'}: ${pr.title}`) - - // Delay before processing next PR - await sleep(requestDelayMs) - } catch (error) { - skippedCount++ - core.error(`Failed to close PR #${issue_number}: ${error.message}`) - } - } - - const elapsed = Math.round((Date.now() - startTime) / 1000) - core.info(`\n========== Summary ==========`) - core.info(`Total open PRs found: ${allPrs.length}`) - core.info(`Stale PRs identified: ${stalePrs.length}`) - core.info(`PRs closed: ${closedCount}`) - core.info(`PRs skipped (errors): ${skippedCount}`) - core.info(`Elapsed time: ${elapsed}s`) - core.info(`=============================`) diff --git a/script/github/close-prs.ts b/script/github/close-prs.ts new file mode 100644 index 0000000000..9acfdaed8d --- /dev/null +++ b/script/github/close-prs.ts @@ -0,0 +1,347 @@ +#!/usr/bin/env bun + +import { parseArgs } from "util" + +const defaultRepo = "anomalyco/opencode" +const defaultAgeMonths = 1 +const defaultThreshold = 2 +const defaultSleepMs = 20_000 +const defaultPrintLimit = 50 +const positiveReactions = new Set(["THUMBS_UP", "HEART", "HOORAY", "ROCKET"]) + +const { values } = parseArgs({ + args: Bun.argv.slice(2), + options: { + execute: { type: "boolean", default: false }, + "dry-run": { type: "boolean", default: false }, + repo: { type: "string", default: defaultRepo }, + threshold: { type: "string", default: String(defaultThreshold) }, + "age-months": { type: "string", default: String(defaultAgeMonths) }, + "max-close": { type: "string" }, + "sleep-ms": { type: "string", default: String(defaultSleepMs) }, + "print-limit": { type: "string", default: String(defaultPrintLimit) }, + help: { type: "boolean", short: "h", default: false }, + }, +}) + +if (values.help) { + console.log(` +Usage: bun script/github/close-prs.ts [options] + +Dry-run is the default. The script only comments and closes PRs when --execute is passed. + +Criteria: + - PRs created within the last month are untouched + - PRs older than one month are closed when they have fewer than 2 positive reactions + - Positive reactions are THUMBS_UP, HEART, HOORAY, and ROCKET reactions on the PR + +Options: + --execute Comment and close matching PRs + --dry-run Explicitly run without changing anything + --repo Repository to clean up (default: ${defaultRepo}) + --threshold Positive reaction threshold (default: ${defaultThreshold}) + --age-months Age cutoff in months (default: ${defaultAgeMonths}) + --max-close Maximum matching PRs to process + --sleep-ms Delay between closing PRs (default: ${defaultSleepMs}) + --print-limit Number of matching PRs to print in dry-run (default: ${defaultPrintLimit}) + -h, --help Show this help message + +Examples: + bun script/github/close-prs.ts + bun script/github/close-prs.ts --threshold 2 --print-limit 100 + bun script/github/close-prs.ts --execute --threshold 2 --max-close 25 +`) + process.exit(0) +} + +if (values.execute && values["dry-run"]) { + console.error("Use either --execute or --dry-run, not both") + process.exit(1) +} + +const token = await requireToken() +const repo = requireRepo(values.repo) +const threshold = requirePositiveInteger("threshold", values.threshold) +const ageMonths = requirePositiveInteger("age-months", values["age-months"]) +const maxClose = + values["max-close"] === undefined ? undefined : requirePositiveInteger("max-close", values["max-close"]) +const sleepMs = requireNonNegativeInteger("sleep-ms", values["sleep-ms"]) +const printLimit = requireNonNegativeInteger("print-limit", values["print-limit"]) +const cutoff = subtractMonths(new Date(), ageMonths) + +const headers = { + Authorization: `Bearer ${token}`, + "Content-Type": "application/json", + Accept: "application/vnd.github+json", + "X-GitHub-Api-Version": "2022-11-28", +} + +type PullRequest = { + number: number + title: string + url: string + createdAt: string + reactionGroups: Array<{ + content: string + users: { + totalCount: number + } + }> +} + +type GraphqlResponse = { + data?: { + rateLimit: { + cost: number + remaining: number + resetAt: string + } + repository: { + pullRequests: { + pageInfo: { + hasNextPage: boolean + endCursor: string | null + } + nodes: PullRequest[] + } + } + } + errors?: Array<{ + message: string + }> +} + +type CleanupCandidate = PullRequest & { + positiveReactions: number +} + +const message = `Automated PR Cleanup + +Thank you for contributing to opencode. + +Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions. + +This PR was closed because it matched the following cleanup criteria: + +- The PR was created more than ${ageMonths === 1 ? "1 month" : `${ageMonths} months`} ago +- The PR had fewer than ${threshold} positive reactions +- Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR + +PRs created within the last ${ageMonths === 1 ? "month are" : `${ageMonths} months are`} not affected by this cleanup. + +If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate. + +Thanks again for taking the time to contribute.` + +async function main() { + console.log(`${values.execute ? "EXECUTE" : "DRY RUN"}: PR cleanup for ${repo.owner}/${repo.name}`) + console.log(`Cutoff: ${cutoff.toISOString()}`) + console.log(`Threshold: fewer than ${threshold} positive reactions`) + + const prs = await fetchOpenPullRequests() + const recentCount = prs.filter((pr) => new Date(pr.createdAt) >= cutoff).length + const candidates = prs + .map((pr) => ({ ...pr, positiveReactions: positiveReactionCount(pr) })) + .filter((pr) => new Date(pr.createdAt) < cutoff && pr.positiveReactions < threshold) + const selected = maxClose === undefined ? candidates : candidates.slice(0, maxClose) + + console.log(`Fetched ${prs.length} open PRs`) + console.log(`Matching cleanup criteria: ${candidates.length}`) + console.log(`Recent PRs untouched: ${recentCount}`) + console.log( + `Older PRs with at least ${threshold} positive reactions untouched: ${prs.length - candidates.length - recentCount}`, + ) + + if (selected.length === 0) return + + if (!values.execute) { + console.log(`\nDry-run only. Re-run with --execute to comment and close matching PRs.`) + console.log(`Showing ${Math.min(printLimit, selected.length)} of ${selected.length} matching PRs:\n`) + for (const pr of selected.slice(0, printLimit)) { + console.log(`#${pr.number} ${pr.createdAt} positive=${pr.positiveReactions} ${pr.url}`) + } + if (selected.length > printLimit) console.log(`... ${selected.length - printLimit} more not shown`) + return + } + + console.log(`\nCommenting and closing ${selected.length} PRs...`) + for (const pr of selected) { + await closePullRequest(pr) + if (sleepMs > 0) await sleep(sleepMs) + } + console.log(`Closed ${selected.length} PRs`) +} + +async function fetchOpenPullRequests() { + const prs: PullRequest[] = [] + let endCursor: string | null = null + + while (true) { + const page = await graphql({ + query: `query($owner: String!, $name: String!, $endCursor: String) { + rateLimit { + cost + remaining + resetAt + } + repository(owner: $owner, name: $name) { + pullRequests(first: 100, states: OPEN, orderBy: { field: CREATED_AT, direction: ASC }, after: $endCursor) { + pageInfo { + hasNextPage + endCursor + } + nodes { + number + title + url + createdAt + reactionGroups { + content + users { + totalCount + } + } + } + } + } + }`, + variables: { + owner: repo.owner, + name: repo.name, + endCursor, + }, + }) + + prs.push(...page.repository.pullRequests.nodes) + console.log( + `Fetched ${prs.length} PRs, GraphQL rate limit remaining ${page.rateLimit.remaining} (cost ${page.rateLimit.cost})`, + ) + + if (page.rateLimit.remaining < 100) { + const delay = Math.max(0, new Date(page.rateLimit.resetAt).getTime() - Date.now()) + 1_000 + console.warn(`GraphQL rate limit low; sleeping ${Math.ceil(delay / 1000)}s until reset`) + await sleep(delay) + } + + if (!page.repository.pullRequests.pageInfo.hasNextPage) return prs + endCursor = page.repository.pullRequests.pageInfo.endCursor + } +} + +async function graphql(input: { query: string; variables: Record }) { + const response = await githubRequest("/graphql", { + method: "POST", + body: JSON.stringify(input), + }) + const body = (await response.json()) as GraphqlResponse + if (body.errors?.length) + throw new Error(`GitHub GraphQL error: ${body.errors.map((error) => error.message).join(", ")}`) + if (!body.data) throw new Error("GitHub GraphQL response did not include data") + return body.data +} + +async function closePullRequest(pr: CleanupCandidate) { + await githubRequest(`/repos/${repo.owner}/${repo.name}/issues/${pr.number}/comments`, { + method: "POST", + body: JSON.stringify({ body: message }), + }) + await githubRequest(`/repos/${repo.owner}/${repo.name}/pulls/${pr.number}`, { + method: "PATCH", + body: JSON.stringify({ state: "closed" }), + }) + console.log(`Closed #${pr.number} positive=${pr.positiveReactions} ${pr.url}`) +} + +async function githubRequest(path: string, init: RequestInit, attempt = 0): Promise { + const response = await fetch(path.startsWith("https://") ? path : `https://api.github.com${path}`, { + ...init, + headers: { + ...headers, + ...init.headers, + }, + }) + + if (response.ok) return response + + const body = await response.text() + const retryAfter = response.headers.get("retry-after") + const reset = response.headers.get("x-ratelimit-reset") + const retryMs = retryAfter + ? Number(retryAfter) * 1000 + : response.headers.get("x-ratelimit-remaining") === "0" && reset + ? Math.max(0, Number(reset) * 1000 - Date.now()) + 1_000 + : body.toLowerCase().includes("secondary rate limit") + ? 300_000 + : 0 + + if ((response.status === 403 || response.status === 429) && retryMs > 0 && attempt < 10) { + console.warn(`GitHub rate limit hit; sleeping ${Math.ceil(retryMs / 1000)}s before retry ${attempt + 1}`) + await sleep(retryMs) + return githubRequest(path, init, attempt + 1) + } + + throw new Error(`GitHub request failed: ${response.status} ${response.statusText}\n${body}`) +} + +function positiveReactionCount(pr: PullRequest) { + return pr.reactionGroups + .filter((group) => positiveReactions.has(group.content)) + .reduce((total, group) => total + group.users.totalCount, 0) +} + +function requireRepo(value: string | undefined) { + if (!value) throw new Error("repo is required") + const [owner, name] = value.split("/") + if (!owner || !name) throw new Error(`Invalid repo ${value}; expected owner/name`) + return { owner, name } +} + +async function requireToken() { + const envToken = process.env.GITHUB_TOKEN ?? process.env.GH_TOKEN + if (envToken) return envToken + + const proc = Bun.spawn(["gh", "auth", "token"], { + stdout: "pipe", + stderr: "pipe", + }) + const stdout = await new Response(proc.stdout).text() + const stderr = await new Response(proc.stderr).text() + const exitCode = await proc.exited + if (exitCode === 0 && stdout.trim()) return stdout.trim() + + throw new Error( + `GitHub authentication is required. Set GITHUB_TOKEN/GH_TOKEN or run gh auth login.\n${stderr.trim()}`, + ) +} + +function requirePositiveInteger(name: string, value: string | undefined) { + const parsed = Number(value) + if (!Number.isInteger(parsed) || parsed <= 0) throw new Error(`${name} must be a positive integer`) + return parsed +} + +function requireNonNegativeInteger(name: string, value: string | undefined) { + const parsed = Number(value) + if (!Number.isInteger(parsed) || parsed < 0) throw new Error(`${name} must be a non-negative integer`) + return parsed +} + +function subtractMonths(date: Date, months: number) { + const result = new Date(date) + const day = result.getUTCDate() + result.setUTCDate(1) + result.setUTCMonth(result.getUTCMonth() - months) + result.setUTCDate( + Math.min(day, new Date(Date.UTC(result.getUTCFullYear(), result.getUTCMonth() + 1, 0)).getUTCDate()), + ) + return result +} + +function sleep(ms: number) { + return new Promise((resolve) => setTimeout(resolve, ms)) +} + +void main().catch((error) => { + console.error("Error:", error) + process.exit(1) +})