|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +const { setTimeout } = require("node:timers/promises"); |
| 19 | + |
| 20 | +const IDLE_DAYS = 14; |
| 21 | +const QUERY = `query( |
| 22 | + $owner: String!, $repo: String!, $number: Int!, |
| 23 | + $checksCursor: String, $reviewsCursor: String, |
| 24 | + $loadChecks: Boolean!, $loadReviews: Boolean! |
| 25 | +) { |
| 26 | + repository(owner: $owner, name: $repo) { |
| 27 | + pullRequest(number: $number) { |
| 28 | + id number state isDraft updatedAt headRefOid mergeable |
| 29 | + mergeQueueEntry { id } |
| 30 | + latestOpinionatedReviews(first: 100, after: $reviewsCursor) @include(if: $loadReviews) { |
| 31 | + nodes { state commit { oid } } |
| 32 | + pageInfo { hasNextPage endCursor } |
| 33 | + } |
| 34 | + commits(last: 1) @include(if: $loadChecks) { |
| 35 | + nodes { commit { |
| 36 | + statusCheckRollup { |
| 37 | + contexts(first: 100, after: $checksCursor) { |
| 38 | + nodes { |
| 39 | + ... on CheckRun { status conclusion } |
| 40 | + ... on StatusContext { state } |
| 41 | + } |
| 42 | + pageInfo { hasNextPage endCursor } |
| 43 | + } |
| 44 | + } |
| 45 | + } } |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | +}`; |
| 50 | + |
| 51 | +const sameVersion = (a, b) => |
| 52 | + a.headRefOid === b.headRefOid && a.updatedAt === b.updatedAt; |
| 53 | + |
| 54 | +async function loadPullRequest(github, repo, number) { |
| 55 | + const variables = { ...repo, number, loadChecks: true, loadReviews: true }; |
| 56 | + let pr; |
| 57 | + const checks = []; |
| 58 | + const reviews = []; |
| 59 | + while (variables.loadChecks || variables.loadReviews) { |
| 60 | + const { repository } = await github.graphql(QUERY, variables); |
| 61 | + const current = repository.pullRequest; |
| 62 | + if (!current || (pr && !sameVersion(pr, current))) return null; |
| 63 | + pr = { ...pr, ...current }; |
| 64 | + if (variables.loadChecks) { |
| 65 | + const connection = |
| 66 | + current.commits.nodes[0]?.commit.statusCheckRollup?.contexts; |
| 67 | + checks.push(...(connection?.nodes ?? [])); |
| 68 | + variables.loadChecks = connection?.pageInfo.hasNextPage ?? false; |
| 69 | + variables.checksCursor = connection?.pageInfo.endCursor; |
| 70 | + } |
| 71 | + if (variables.loadReviews) { |
| 72 | + const connection = current.latestOpinionatedReviews; |
| 73 | + reviews.push(...connection.nodes); |
| 74 | + variables.loadReviews = connection.pageInfo.hasNextPage; |
| 75 | + variables.reviewsCursor = connection.pageInfo.endCursor; |
| 76 | + } |
| 77 | + } |
| 78 | + return { ...pr, checks, reviews }; |
| 79 | +} |
| 80 | + |
| 81 | +function reasonsToDraft(pr, cutoff) { |
| 82 | + if ( |
| 83 | + !pr || |
| 84 | + pr.state !== "OPEN" || |
| 85 | + pr.isDraft || |
| 86 | + pr.mergeQueueEntry || |
| 87 | + !(Date.parse(pr.updatedAt) <= cutoff) |
| 88 | + ) |
| 89 | + return []; |
| 90 | + |
| 91 | + const reasons = []; |
| 92 | + if (pr.mergeable === "CONFLICTING") reasons.push("merge conflicts"); |
| 93 | + // The rollup reports FAILURE for cancelled checks too. |
| 94 | + if ( |
| 95 | + pr.checks.some( |
| 96 | + (check) => |
| 97 | + (check.status === "COMPLETED" && |
| 98 | + ["FAILURE", "TIMED_OUT"].includes(check.conclusion)) || |
| 99 | + ["FAILURE", "ERROR"].includes(check.state) |
| 100 | + ) |
| 101 | + ) |
| 102 | + reasons.push("failing CI checks"); |
| 103 | + |
| 104 | + if ( |
| 105 | + pr.reviews.some( |
| 106 | + (review) => |
| 107 | + review.state === "CHANGES_REQUESTED" && |
| 108 | + review.commit?.oid === pr.headRefOid |
| 109 | + ) |
| 110 | + ) |
| 111 | + reasons.push("changes requested on the latest push"); |
| 112 | + return reasons; |
| 113 | +} |
| 114 | + |
| 115 | +async function run({ github, context, core, dryRun = true }) { |
| 116 | + const repo = { owner: context.repo.owner, repo: context.repo.repo }; |
| 117 | + const cutoff = Date.now() - IDLE_DAYS * 24 * 60 * 60 * 1000; |
| 118 | + // Collect the list before mutations change updated_at and its pagination. |
| 119 | + const prs = await github.paginate(github.rest.pulls.list, { |
| 120 | + ...repo, |
| 121 | + state: "open", |
| 122 | + sort: "updated", |
| 123 | + direction: "asc", |
| 124 | + per_page: 100, |
| 125 | + }); |
| 126 | + const candidates = prs.filter( |
| 127 | + (listed) => !listed.draft && Date.parse(listed.updated_at) <= cutoff |
| 128 | + ); |
| 129 | + if (!candidates.length) return []; |
| 130 | + |
| 131 | + // GitHub computes mergeability lazily; warm every candidate before deciding. |
| 132 | + for (const listed of candidates) { |
| 133 | + await loadPullRequest(github, repo, listed.number); |
| 134 | + } |
| 135 | + await setTimeout(30_000); |
| 136 | + |
| 137 | + const results = []; |
| 138 | + for (const listed of candidates) { |
| 139 | + let pr = await loadPullRequest(github, repo, listed.number); |
| 140 | + if (pr?.mergeable === "UNKNOWN") { |
| 141 | + core.warning(`Mergeability is still unknown for #${listed.number}.`); |
| 142 | + } |
| 143 | + let reasons = reasonsToDraft(pr, cutoff); |
| 144 | + if (!reasons.length) continue; |
| 145 | + if (!dryRun) { |
| 146 | + const fresh = await loadPullRequest(github, repo, listed.number); |
| 147 | + if (!fresh || !sameVersion(pr, fresh)) continue; |
| 148 | + pr = fresh; |
| 149 | + reasons = reasonsToDraft(pr, cutoff); |
| 150 | + if (!reasons.length) continue; |
| 151 | + await github.graphql( |
| 152 | + `mutation($id: ID!) { |
| 153 | + convertPullRequestToDraft(input: { pullRequestId: $id }) { |
| 154 | + pullRequest { id } |
| 155 | + } |
| 156 | + }`, |
| 157 | + { id: pr.id } |
| 158 | + ); |
| 159 | + // Respect GitHub's secondary rate limits between writes. |
| 160 | + await setTimeout(1000); |
| 161 | + await github.rest.issues.createComment({ |
| 162 | + ...repo, |
| 163 | + issue_number: pr.number, |
| 164 | + body: |
| 165 | + `Marked as draft after ${IDLE_DAYS} days without activity: ${reasons.join( |
| 166 | + "; " |
| 167 | + )}.\n\n` + |
| 168 | + "Please address these items and mark **Ready for review** when ready.", |
| 169 | + }); |
| 170 | + await setTimeout(1000); |
| 171 | + } |
| 172 | + core.info( |
| 173 | + `${dryRun ? "Would draft" : "Drafted"} #${pr.number}: ${reasons.join( |
| 174 | + "; " |
| 175 | + )}` |
| 176 | + ); |
| 177 | + results.push({ number: pr.number, reasons }); |
| 178 | + } |
| 179 | + return results; |
| 180 | +} |
| 181 | + |
| 182 | +module.exports = { run }; |
0 commit comments