|
| 1 | +import * as debug from 'debug'; |
| 2 | + |
| 3 | +import { MAIN_BRANCH, REPOS } from '../constants'; |
| 4 | +import { getOctokit } from './octokit'; |
| 5 | +import { PullsListResponseItem } from '../types'; |
| 6 | +import { Octokit } from '@octokit/rest'; |
| 7 | +import { getInfraPRText } from './pr-text'; |
| 8 | +import { getFileContent } from './arc-image'; |
| 9 | + |
| 10 | +export async function rollInfra( |
| 11 | + rollKey: string, |
| 12 | + bumpSubject: string, |
| 13 | + newShortVersion: string, |
| 14 | + filePath: string, |
| 15 | + newContent: string, |
| 16 | +): Promise<any> { |
| 17 | + const d = debug(`roller/infra/${rollKey}:rollInfra()`); |
| 18 | + const octokit = await getOctokit(); |
| 19 | + |
| 20 | + const branchName = `roller/infra/${rollKey}`; |
| 21 | + const shortRef = `heads/${branchName}`; |
| 22 | + const ref = `refs/${shortRef}`; |
| 23 | + |
| 24 | + const { owner, repo } = REPOS.electronInfra; |
| 25 | + |
| 26 | + // Look for a pre-existing PR that targets this branch to see if we can update that. |
| 27 | + let existingPrsForBranch: PullsListResponseItem[] = []; |
| 28 | + try { |
| 29 | + existingPrsForBranch = (await octokit.paginate('GET /repos/:owner/:repo/pulls', { |
| 30 | + head: branchName, |
| 31 | + owner, |
| 32 | + repo, |
| 33 | + state: 'open', |
| 34 | + })) as PullsListResponseItem[]; |
| 35 | + } catch {} |
| 36 | + |
| 37 | + const prs = existingPrsForBranch.filter((pr) => |
| 38 | + pr.title.startsWith(`build: bump ${bumpSubject}`), |
| 39 | + ); |
| 40 | + |
| 41 | + const defaultBranchHeadSha = ( |
| 42 | + await octokit.repos.getBranch({ |
| 43 | + owner, |
| 44 | + repo, |
| 45 | + branch: MAIN_BRANCH, |
| 46 | + }) |
| 47 | + ).data.commit.sha; |
| 48 | + |
| 49 | + const { raw: currentContent, sha: currentSha } = await getFileContent(octokit, filePath); |
| 50 | + |
| 51 | + if (prs.length) { |
| 52 | + // Update existing PR(s) |
| 53 | + for (const pr of prs) { |
| 54 | + d(`Found existing PR: #${pr.number} opened by ${pr.user.login}`); |
| 55 | + |
| 56 | + // Check to see if automatic infra roll has been temporarily disabled |
| 57 | + const hasPauseLabel = pr.labels.some((label) => label.name === 'roller/pause'); |
| 58 | + if (hasPauseLabel) { |
| 59 | + d(`Automatic updates have been paused for #${pr.number}, skipping infra roll.`); |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + d(`Attempting infra update for #${pr.number}`); |
| 64 | + if (currentContent.trim() !== newContent.trim()) { |
| 65 | + await updateFile( |
| 66 | + octokit, |
| 67 | + bumpSubject, |
| 68 | + newShortVersion, |
| 69 | + filePath, |
| 70 | + newContent, |
| 71 | + branchName, |
| 72 | + currentSha, |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + await octokit.pulls.update({ |
| 77 | + owner, |
| 78 | + repo, |
| 79 | + pull_number: pr.number, |
| 80 | + ...getInfraPRText(bumpSubject, newShortVersion), |
| 81 | + }); |
| 82 | + } |
| 83 | + } else { |
| 84 | + try { |
| 85 | + d(`roll triggered for ${bumpSubject}=${newShortVersion}`); |
| 86 | + |
| 87 | + try { |
| 88 | + await octokit.git.getRef({ owner, repo, ref: shortRef }); |
| 89 | + d(`Ref ${ref} already exists`); |
| 90 | + } catch { |
| 91 | + d(`Creating ref=${ref} at sha=${defaultBranchHeadSha}`); |
| 92 | + await octokit.git.createRef({ owner, repo, ref, sha: defaultBranchHeadSha }); |
| 93 | + } |
| 94 | + |
| 95 | + await updateFile( |
| 96 | + octokit, |
| 97 | + bumpSubject, |
| 98 | + newShortVersion, |
| 99 | + filePath, |
| 100 | + newContent, |
| 101 | + branchName, |
| 102 | + currentSha, |
| 103 | + ); |
| 104 | + |
| 105 | + d(`Raising a PR for ${branchName} to ${repo}`); |
| 106 | + await octokit.pulls.create({ |
| 107 | + owner, |
| 108 | + repo, |
| 109 | + base: MAIN_BRANCH, |
| 110 | + head: `${owner}:${branchName}`, |
| 111 | + ...getInfraPRText(bumpSubject, newShortVersion), |
| 112 | + }); |
| 113 | + } catch (e) { |
| 114 | + d(`Error rolling ${owner}/${repo} to ${newShortVersion}`, e); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +async function updateFile( |
| 120 | + octokit: Octokit, |
| 121 | + bumpSubject: string, |
| 122 | + newShortVersion: string, |
| 123 | + filePath: string, |
| 124 | + newContent: string, |
| 125 | + branchName: string, |
| 126 | + currentSha: string, |
| 127 | +) { |
| 128 | + await octokit.repos.createOrUpdateFileContents({ |
| 129 | + ...REPOS.electronInfra, |
| 130 | + path: filePath, |
| 131 | + message: `chore: bump ${bumpSubject} in ${filePath} to ${newShortVersion}`, |
| 132 | + content: Buffer.from(newContent).toString('base64'), |
| 133 | + branch: branchName, |
| 134 | + sha: currentSha, |
| 135 | + }); |
| 136 | +} |
0 commit comments