|
| 1 | +// @ts-check |
| 2 | +/// <reference types="@actions/github-script" /> |
| 3 | + |
| 4 | +const { getErrorMessage } = require("./error_helpers.cjs"); |
| 5 | +const { withRetry, isTransientError, sleep } = require("./error_recovery.cjs"); |
| 6 | +const { fetchAndLogRateLimit } = require("./github_rate_limit_logger.cjs"); |
| 7 | +const { buildWorkflowRunUrl } = require("./workflow_metadata_helpers.cjs"); |
| 8 | + |
| 9 | +const LIST_PULL_REQUESTS_PER_PAGE = 100; |
| 10 | +const UPDATE_DELAY_MS = 1000; |
| 11 | + |
| 12 | +/** |
| 13 | + * @param {string} owner |
| 14 | + * @param {string} repo |
| 15 | + * @returns {Promise<number[]>} |
| 16 | + */ |
| 17 | +async function listOpenPullRequests(owner, repo) { |
| 18 | + const pulls = await github.paginate(github.rest.pulls.list, { |
| 19 | + owner, |
| 20 | + repo, |
| 21 | + state: "open", |
| 22 | + per_page: LIST_PULL_REQUESTS_PER_PAGE, |
| 23 | + }); |
| 24 | + |
| 25 | + return pulls.map(pr => pr.number).filter(number => Number.isInteger(number)); |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * @param {string} owner |
| 30 | + * @param {string} repo |
| 31 | + * @param {number[]} pullNumbers |
| 32 | + * @returns {Promise<number[]>} |
| 33 | + */ |
| 34 | +async function filterMergeablePullRequests(owner, repo, pullNumbers) { |
| 35 | + const mergeable = []; |
| 36 | + const baseRepository = `${owner}/${repo}`.toLowerCase(); |
| 37 | + |
| 38 | + for (const pullNumber of pullNumbers) { |
| 39 | + const { data: pull } = await withRetry( |
| 40 | + () => |
| 41 | + github.rest.pulls.get({ |
| 42 | + owner, |
| 43 | + repo, |
| 44 | + pull_number: pullNumber, |
| 45 | + }), |
| 46 | + { |
| 47 | + maxRetries: 2, |
| 48 | + initialDelayMs: 500, |
| 49 | + maxDelayMs: 2000, |
| 50 | + jitterMs: 0, |
| 51 | + shouldRetry: isTransientError, |
| 52 | + }, |
| 53 | + `fetch pull request #${pullNumber}` |
| 54 | + ); |
| 55 | + |
| 56 | + const headRepositoryRaw = pull?.head?.repo?.full_name; |
| 57 | + const headRepository = headRepositoryRaw?.toLowerCase() ?? ""; |
| 58 | + const isSameRepository = headRepository === baseRepository; |
| 59 | + const isMergeable = pull?.state === "open" && pull?.mergeable === true && pull?.draft !== true && isSameRepository; |
| 60 | + if (isMergeable) { |
| 61 | + mergeable.push(pullNumber); |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + let skipReason = "not_mergeable"; |
| 66 | + if (!isSameRepository) { |
| 67 | + skipReason = headRepository ? "head_repository_mismatch" : "head_repository_missing"; |
| 68 | + } |
| 69 | + core.info(`Skipping PR #${pullNumber}: reason=${skipReason}, mergeable=${String(pull?.mergeable)}, state=${pull?.state || "unknown"}, draft=${String(Boolean(pull?.draft))}, head_repo=${headRepository || "unknown"}`); |
| 70 | + } |
| 71 | + |
| 72 | + return mergeable; |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * @param {unknown} error |
| 77 | + * @returns {boolean} |
| 78 | + */ |
| 79 | +function isNonFatalUpdateBranchError(error) { |
| 80 | + if (typeof error === "object" && error !== null && "status" in error && error.status === 422) { |
| 81 | + return true; |
| 82 | + } |
| 83 | + |
| 84 | + const message = getErrorMessage(error).toLowerCase(); |
| 85 | + return message.includes("update branch failed") || message.includes("head branch is not behind"); |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * @param {string} owner |
| 90 | + * @param {string} repo |
| 91 | + * @param {number} pullNumber |
| 92 | + * @returns {Promise<void>} |
| 93 | + */ |
| 94 | +async function updatePullRequestBranch(owner, repo, pullNumber) { |
| 95 | + await withRetry( |
| 96 | + () => |
| 97 | + github.rest.pulls.updateBranch({ |
| 98 | + owner, |
| 99 | + repo, |
| 100 | + pull_number: pullNumber, |
| 101 | + }), |
| 102 | + { |
| 103 | + maxRetries: 2, |
| 104 | + initialDelayMs: 1000, |
| 105 | + maxDelayMs: 10000, |
| 106 | + shouldRetry: isTransientError, |
| 107 | + }, |
| 108 | + `update branch for pull request #${pullNumber}` |
| 109 | + ); |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * @param {string} owner |
| 114 | + * @param {string} repo |
| 115 | + * @param {number} pullNumber |
| 116 | + * @param {string} runUrl |
| 117 | + * @returns {Promise<void>} |
| 118 | + */ |
| 119 | +async function addMaintenanceUpdateComment(owner, repo, pullNumber, runUrl) { |
| 120 | + const body = `🛠️ Agentic Maintenance updated this pull request branch.\n\n[View workflow run](${runUrl})`; |
| 121 | + await github.rest.issues.createComment({ |
| 122 | + owner, |
| 123 | + repo, |
| 124 | + issue_number: pullNumber, |
| 125 | + body, |
| 126 | + }); |
| 127 | +} |
| 128 | + |
| 129 | +/** |
| 130 | + * Update all mergeable PR branches. |
| 131 | + * @returns {Promise<void>} |
| 132 | + */ |
| 133 | +async function main() { |
| 134 | + const owner = context.repo.owner; |
| 135 | + const repo = context.repo.repo; |
| 136 | + const runUrl = buildWorkflowRunUrl(context, context.repo); |
| 137 | + |
| 138 | + core.info(`Updating pull request branches in ${owner}/${repo}`); |
| 139 | + core.info(`Run URL: ${runUrl}`); |
| 140 | + await fetchAndLogRateLimit(github, "update_pull_request_branches_start"); |
| 141 | + |
| 142 | + const openPullRequests = await listOpenPullRequests(owner, repo); |
| 143 | + core.info(`Found ${openPullRequests.length} open pull request(s)`); |
| 144 | + if (openPullRequests.length === 0) return; |
| 145 | + |
| 146 | + const mergeablePullRequests = await filterMergeablePullRequests(owner, repo, openPullRequests); |
| 147 | + core.info(`Found ${mergeablePullRequests.length} mergeable pull request(s)`); |
| 148 | + if (mergeablePullRequests.length === 0) return; |
| 149 | + |
| 150 | + let updatedCount = 0; |
| 151 | + let skippedCount = 0; |
| 152 | + let failedCount = 0; |
| 153 | + |
| 154 | + for (let i = 0; i < mergeablePullRequests.length; i++) { |
| 155 | + const pullNumber = mergeablePullRequests[i]; |
| 156 | + try { |
| 157 | + core.info(`Updating branch for PR #${pullNumber}`); |
| 158 | + await updatePullRequestBranch(owner, repo, pullNumber); |
| 159 | + await addMaintenanceUpdateComment(owner, repo, pullNumber, runUrl); |
| 160 | + updatedCount++; |
| 161 | + } catch (error) { |
| 162 | + if (isNonFatalUpdateBranchError(error)) { |
| 163 | + skippedCount++; |
| 164 | + core.warning(`Skipping PR #${pullNumber}: ${getErrorMessage(error)}`); |
| 165 | + } else { |
| 166 | + failedCount++; |
| 167 | + core.error(`Failed to update branch for PR #${pullNumber}: ${getErrorMessage(error)}`); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + if (i < mergeablePullRequests.length - 1) { |
| 172 | + await sleep(UPDATE_DELAY_MS); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + await fetchAndLogRateLimit(github, "update_pull_request_branches_end"); |
| 177 | + core.notice(`update_pull_request_branches completed: updated=${updatedCount}, skipped=${skippedCount}, failed=${failedCount}`); |
| 178 | +} |
| 179 | + |
| 180 | +module.exports = { |
| 181 | + main, |
| 182 | + filterMergeablePullRequests, |
| 183 | + isNonFatalUpdateBranchError, |
| 184 | +}; |
0 commit comments