-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: add renovate APIC-490 #522
Changes from 7 commits
877f8cc
6fe4c36
faec510
f3d26be
9ec95b8
0ce6e38
476da34
41b2ff8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
name: Renovate | ||
|
||
on: | ||
schedule: | ||
- cron: '0 14 * * 5' | ||
|
||
jobs: | ||
renovate: | ||
name: Renovate | ||
runs-on: ubuntu-20.04 | ||
steps: | ||
- uses: actions/checkout@v2 | ||
|
||
- name: Setup | ||
id: setup | ||
uses: ./.github/actions/setup | ||
with: | ||
type: minimal | ||
|
||
- run: yarn workspace scripts renovateWeeklyPR | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.TOKEN_RELEASE_BOT }} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"extends": [ | ||
"config:js-app", | ||
"algolia" | ||
], | ||
"enabledManagers": [ | ||
"npm", | ||
"nvm" | ||
], | ||
"baseBranches": [ | ||
"chore/renovateBaseBranch" | ||
], | ||
"packageRules": [ | ||
{ | ||
"matchDepTypes": [ | ||
"required_provider" | ||
], | ||
"rangeStrategy": "bump" | ||
} | ||
], | ||
"prHourlyLimit": 10, | ||
"prConcurrentLimit": 50 | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -8,6 +8,8 @@ import { | |||||
run, | ||||||
toAbsolutePath, | ||||||
REPO_URL, | ||||||
GITHUB_TOKEN, | ||||||
ensureGitHubToken, | ||||||
} from '../../common'; | ||||||
import { getLanguageFolder } from '../../config'; | ||||||
import { cloneRepository, configureGitHubAuthor } from '../../release/common'; | ||||||
|
@@ -60,9 +62,7 @@ export function cleanUpCommitMessage(commitMessage: string): string { | |||||
} | ||||||
|
||||||
async function spreadGeneration(): Promise<void> { | ||||||
if (!process.env.GITHUB_TOKEN) { | ||||||
throw new Error('Environment variable `GITHUB_TOKEN` does not exist.'); | ||||||
} | ||||||
ensureGitHubToken(); | ||||||
millotp marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
const lastCommitMessage = await run('git log -1 --format="%s"'); | ||||||
const author = ( | ||||||
|
@@ -81,7 +81,7 @@ async function spreadGeneration(): Promise<void> { | |||||
for (const lang of langs) { | ||||||
const { tempGitDir } = await cloneRepository({ | ||||||
lang, | ||||||
githubToken: process.env.GITHUB_TOKEN, | ||||||
githubToken: GITHUB_TOKEN!, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
tempDir: process.env.RUNNER_TEMP!, | ||||||
}); | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
/* eslint-disable no-console */ | ||
// script coming from crawler <3 | ||
import type { Octokit } from '@octokit/rest'; | ||
|
||
import { getOctokit, OWNER, REPO, wait } from '../../common'; | ||
|
||
const BRANCH = 'chore/renovateBaseBranch'; | ||
const BRANCH_BASE = 'main'; | ||
const EMPTY_COMMIT_MSG = 'Automatic empty commit'; | ||
|
||
async function getRef( | ||
octokit: Octokit, | ||
branch: string | ||
): Promise<string | false> { | ||
try { | ||
const ref = await octokit.git.getRef({ | ||
owner: OWNER, | ||
repo: REPO, | ||
ref: `heads/${branch}`, | ||
}); | ||
return ref.data.object.sha; | ||
} catch (err) { | ||
if (!(err instanceof Error) || (err as any).status !== 404) { | ||
throw err; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
async function createBranch(octokit: Octokit, sha: string): Promise<any> { | ||
const create = await octokit.git.createRef({ | ||
owner: OWNER, | ||
repo: REPO, | ||
ref: `refs/heads/${BRANCH}`, | ||
sha, | ||
}); | ||
return create; | ||
} | ||
|
||
async function deleteRef(octokit: Octokit): Promise<any> { | ||
console.log(`Deleting ref for ${BRANCH}`); | ||
const ref = await octokit.git.deleteRef({ | ||
owner: OWNER, | ||
repo: REPO, | ||
ref: `heads/${BRANCH}`, | ||
}); | ||
return ref; | ||
} | ||
|
||
async function updateRef(octokit: Octokit, sha: string): Promise<any> { | ||
console.log(`Changing ref for ${BRANCH} to`, sha); | ||
const ref = await octokit.git.updateRef({ | ||
owner: OWNER, | ||
repo: REPO, | ||
ref: `heads/${BRANCH}`, | ||
sha, | ||
}); | ||
return ref; | ||
} | ||
|
||
async function getCommit(octokit: Octokit, sha: string): Promise<any> { | ||
const commit = await octokit.git.getCommit({ | ||
owner: OWNER, | ||
repo: REPO, | ||
commit_sha: sha, | ||
}); | ||
return commit.data; | ||
} | ||
|
||
function isCommitAnEmptyCommit(commit: any): boolean { | ||
return commit.message.search(EMPTY_COMMIT_MSG) >= 0; | ||
} | ||
|
||
async function createEmptyCommit( | ||
octokit: Octokit, | ||
refCommit: any | ||
): Promise<any> { | ||
console.log('Creating empty commit'); | ||
const commit = await octokit.git.createCommit({ | ||
owner: OWNER, | ||
repo: REPO, | ||
message: EMPTY_COMMIT_MSG, | ||
tree: refCommit.tree.sha, | ||
parents: [refCommit.sha], | ||
}); | ||
return commit.data; | ||
} | ||
|
||
async function createPR(octokit: Octokit): Promise<any> { | ||
// Next monday | ||
const date = new Date(); | ||
date.setDate(date.getDate() + 3); | ||
|
||
const title = `chore(scripts): dependencies ${ | ||
date.toISOString().split('T')[0] | ||
}`; | ||
const { data } = await octokit.pulls.create({ | ||
repo: REPO, | ||
owner: OWNER, | ||
title, | ||
body: `Weekly dependencies update. | ||
Contributes to #528 | ||
`, | ||
head: BRANCH, | ||
base: BRANCH_BASE, | ||
}); | ||
return data; | ||
} | ||
|
||
async function resetBranch( | ||
octokit: Octokit, | ||
refBase: string, | ||
exists: boolean | ||
): Promise<void> { | ||
if (exists) { | ||
console.log('Deleting branch'); | ||
await deleteRef(octokit); | ||
await wait(5000); | ||
} | ||
|
||
console.log('Creating branch'); | ||
|
||
await createBranch(octokit, refBase); | ||
|
||
const commit = await getCommit(octokit, refBase); | ||
|
||
const empty = await createEmptyCommit(octokit, commit); | ||
await updateRef(octokit, empty.sha); | ||
} | ||
|
||
(async (): Promise<void> => { | ||
try { | ||
const octokit = getOctokit(); | ||
|
||
const refBase = await getRef(octokit, BRANCH_BASE); | ||
const refTarget = await getRef(octokit, BRANCH); | ||
console.log(BRANCH_BASE, 'is at', refBase); | ||
console.log(BRANCH, 'is at', refTarget); | ||
|
||
if (!refBase) { | ||
console.error('no sha for base branch'); | ||
return; | ||
} | ||
|
||
if (refTarget) { | ||
console.log('Branch exists'); | ||
const commit = await getCommit(octokit, refTarget); | ||
|
||
if (isCommitAnEmptyCommit(commit)) { | ||
console.log('Empty commit exists'); | ||
return; | ||
} | ||
} | ||
|
||
await resetBranch(octokit, refBase, Boolean(refTarget)); | ||
|
||
console.log('Creating pull request'); | ||
await createPR(octokit); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
})(); |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,6 +1,7 @@ | ||||||
import fsp from 'fs/promises'; | ||||||
import path from 'path'; | ||||||
|
||||||
import { Octokit } from '@octokit/rest'; | ||||||
import execa from 'execa'; // https://github.com/sindresorhus/execa/tree/v5.1.1 | ||||||
import { hashElement } from 'folder-hash'; | ||||||
import { remove } from 'fs-extra'; | ||||||
|
@@ -26,6 +27,7 @@ export const REPO_URL = `https://github.com/${OWNER}/${REPO}`; | |||||
export const CI = Boolean(process.env.CI); | ||||||
export const DOCKER = Boolean(process.env.DOCKER); | ||||||
export const BUNDLE_WITH_DOC = process.env.BUNDLE_WITH_DOC === 'true'; | ||||||
export const GITHUB_TOKEN = process.env.GITHUB_TOKEN; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need it we can enforce calling ensureGitHubToken everywhere no? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
or maybe? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no because this file is in a lot of other scripts that don't need the token, we want it to throw only when we need it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ahhh I thought it was the release common make sense |
||||||
|
||||||
// This script is run by `yarn workspace ...`, which means the current working directory is `./script` | ||||||
export const ROOT_DIR = path.resolve(process.cwd(), '..'); | ||||||
|
@@ -258,3 +260,27 @@ export async function runComposerUpdate(verbose: boolean): Promise<void> { | |||||
); | ||||||
} | ||||||
} | ||||||
|
||||||
export function ensureGitHubToken(): string { | ||||||
// use process.env here to mock with jest | ||||||
if (!process.env.GITHUB_TOKEN) { | ||||||
throw new Error('Environment variable `GITHUB_TOKEN` does not exist.'); | ||||||
} | ||||||
return process.env.GITHUB_TOKEN; | ||||||
} | ||||||
|
||||||
export function getOctokit(): Octokit { | ||||||
const token = ensureGitHubToken(); | ||||||
return new Octokit({ | ||||||
auth: `token ${token}`, | ||||||
}); | ||||||
} | ||||||
|
||||||
export function wait(waitTime: number): Promise<void> { | ||||||
if (waitTime <= 0) { | ||||||
return Promise.resolve(); | ||||||
} | ||||||
return new Promise((resolve) => { | ||||||
setTimeout(resolve, waitTime); | ||||||
}); | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
our CI will 🔥
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sure it can handle it 😬