|
| 1 | +/* eslint-disable no-console */ |
| 2 | +// script coming from crawler <3 |
| 3 | +import type { Octokit } from '@octokit/rest'; |
| 4 | + |
| 5 | +import { getOctokit, OWNER, REPO, wait } from '../../common'; |
| 6 | + |
| 7 | +const BRANCH = 'chore/renovateBaseBranch'; |
| 8 | +const BRANCH_BASE = 'main'; |
| 9 | +const EMPTY_COMMIT_MSG = 'Automatic empty commit'; |
| 10 | + |
| 11 | +async function getRef( |
| 12 | + octokit: Octokit, |
| 13 | + branch: string |
| 14 | +): Promise<string | false> { |
| 15 | + try { |
| 16 | + const ref = await octokit.git.getRef({ |
| 17 | + owner: OWNER, |
| 18 | + repo: REPO, |
| 19 | + ref: `heads/${branch}`, |
| 20 | + }); |
| 21 | + return ref.data.object.sha; |
| 22 | + } catch (err) { |
| 23 | + if (!(err instanceof Error) || (err as any).status !== 404) { |
| 24 | + throw err; |
| 25 | + } |
| 26 | + } |
| 27 | + return false; |
| 28 | +} |
| 29 | + |
| 30 | +async function createBranch(octokit: Octokit, sha: string): Promise<any> { |
| 31 | + const create = await octokit.git.createRef({ |
| 32 | + owner: OWNER, |
| 33 | + repo: REPO, |
| 34 | + ref: `refs/heads/${BRANCH}`, |
| 35 | + sha, |
| 36 | + }); |
| 37 | + return create; |
| 38 | +} |
| 39 | + |
| 40 | +async function deleteRef(octokit: Octokit): Promise<any> { |
| 41 | + console.log(`Deleting ref for ${BRANCH}`); |
| 42 | + const ref = await octokit.git.deleteRef({ |
| 43 | + owner: OWNER, |
| 44 | + repo: REPO, |
| 45 | + ref: `heads/${BRANCH}`, |
| 46 | + }); |
| 47 | + return ref; |
| 48 | +} |
| 49 | + |
| 50 | +async function updateRef(octokit: Octokit, sha: string): Promise<any> { |
| 51 | + console.log(`Changing ref for ${BRANCH} to`, sha); |
| 52 | + const ref = await octokit.git.updateRef({ |
| 53 | + owner: OWNER, |
| 54 | + repo: REPO, |
| 55 | + ref: `heads/${BRANCH}`, |
| 56 | + sha, |
| 57 | + }); |
| 58 | + return ref; |
| 59 | +} |
| 60 | + |
| 61 | +async function getCommit(octokit: Octokit, sha: string): Promise<any> { |
| 62 | + const commit = await octokit.git.getCommit({ |
| 63 | + owner: OWNER, |
| 64 | + repo: REPO, |
| 65 | + commit_sha: sha, |
| 66 | + }); |
| 67 | + return commit.data; |
| 68 | +} |
| 69 | + |
| 70 | +function isCommitAnEmptyCommit(commit: any): boolean { |
| 71 | + return commit.message.search(EMPTY_COMMIT_MSG) >= 0; |
| 72 | +} |
| 73 | + |
| 74 | +async function createEmptyCommit( |
| 75 | + octokit: Octokit, |
| 76 | + refCommit: any |
| 77 | +): Promise<any> { |
| 78 | + console.log('Creating empty commit'); |
| 79 | + const commit = await octokit.git.createCommit({ |
| 80 | + owner: OWNER, |
| 81 | + repo: REPO, |
| 82 | + message: EMPTY_COMMIT_MSG, |
| 83 | + tree: refCommit.tree.sha, |
| 84 | + parents: [refCommit.sha], |
| 85 | + }); |
| 86 | + return commit.data; |
| 87 | +} |
| 88 | + |
| 89 | +async function createPR(octokit: Octokit): Promise<any> { |
| 90 | + // Next monday |
| 91 | + const date = new Date(); |
| 92 | + date.setDate(date.getDate() + 3); |
| 93 | + |
| 94 | + const title = `chore(scripts): dependencies ${ |
| 95 | + date.toISOString().split('T')[0] |
| 96 | + }`; |
| 97 | + const { data } = await octokit.pulls.create({ |
| 98 | + repo: REPO, |
| 99 | + owner: OWNER, |
| 100 | + title, |
| 101 | + body: `Weekly dependencies update. |
| 102 | +Contributes to #528 |
| 103 | + `, |
| 104 | + head: BRANCH, |
| 105 | + base: BRANCH_BASE, |
| 106 | + }); |
| 107 | + return data; |
| 108 | +} |
| 109 | + |
| 110 | +async function resetBranch( |
| 111 | + octokit: Octokit, |
| 112 | + refBase: string, |
| 113 | + exists: boolean |
| 114 | +): Promise<void> { |
| 115 | + if (exists) { |
| 116 | + console.log('Deleting branch'); |
| 117 | + await deleteRef(octokit); |
| 118 | + await wait(5000); |
| 119 | + } |
| 120 | + |
| 121 | + console.log('Creating branch'); |
| 122 | + |
| 123 | + await createBranch(octokit, refBase); |
| 124 | + |
| 125 | + const commit = await getCommit(octokit, refBase); |
| 126 | + |
| 127 | + const empty = await createEmptyCommit(octokit, commit); |
| 128 | + await updateRef(octokit, empty.sha); |
| 129 | +} |
| 130 | + |
| 131 | +(async (): Promise<void> => { |
| 132 | + try { |
| 133 | + const octokit = getOctokit(); |
| 134 | + |
| 135 | + const refBase = await getRef(octokit, BRANCH_BASE); |
| 136 | + const refTarget = await getRef(octokit, BRANCH); |
| 137 | + console.log(BRANCH_BASE, 'is at', refBase); |
| 138 | + console.log(BRANCH, 'is at', refTarget); |
| 139 | + |
| 140 | + if (!refBase) { |
| 141 | + console.error('no sha for base branch'); |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + if (refTarget) { |
| 146 | + console.log('Branch exists'); |
| 147 | + const commit = await getCommit(octokit, refTarget); |
| 148 | + |
| 149 | + if (isCommitAnEmptyCommit(commit)) { |
| 150 | + console.log('Empty commit exists'); |
| 151 | + return; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + await resetBranch(octokit, refBase, Boolean(refTarget)); |
| 156 | + |
| 157 | + console.log('Creating pull request'); |
| 158 | + await createPR(octokit); |
| 159 | + } catch (err) { |
| 160 | + console.error(err); |
| 161 | + } |
| 162 | +})(); |
0 commit comments