Skip to content
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

Merged
merged 8 commits into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ jobs:
exit $diff

- name: Lint json files
run: yarn eslint --ext=json .
run: |
yarn eslint --ext=json .
echo 'Use yarn fix:json to fix issues'

outputs:
RUN_SCRIPTS: ${{ steps.setup.outputs.RUN_SCRIPTS }}
Expand Down
24 changes: 24 additions & 0 deletions .github/workflows/renovate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Renovate

on:
schedule:
- cron: '0 14 * * 5'

jobs:
renovate:
name: Renovate
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
millotp marked this conversation as resolved.
Show resolved Hide resolved

- name: Setup
id: setup
uses: ./.github/actions/setup
with:
type: minimal

- run: yarn workspace scripts renovateWeeklyPR
env:
GITHUB_TOKEN: ${{ secrets.TOKEN_RELEASE_BOT }}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"mustache": "4.2.0",
"prettier": "2.6.2",
"prettier-plugin-java": "1.6.1",
"renovate-config-algolia": "2.1.10",
"typescript": "4.6.3"
},
"engines": {
Expand Down
23 changes: 23 additions & 0 deletions renovate.json
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
Comment on lines +21 to +22
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

our CI will 🔥

Copy link
Collaborator Author

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 😬

}
15 changes: 13 additions & 2 deletions scripts/ci/codegen/__tests__/codegen.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Octokit } from '@octokit/rest';

import * as common from '../../../common';
import { cleanGeneratedBranch } from '../cleanGeneratedBranch';
import { pushGeneratedCode } from '../pushGeneratedCode';
Expand Down Expand Up @@ -28,6 +30,17 @@ describe('codegen', () => {
});

describe('upsertGenerationComment', () => {
beforeEach(() => {
// can't seem to mock `ensureGitHubToken` directly
jest
.spyOn(common, 'getOctokit')
.mockImplementation(() => new Octokit({ auth: `token mocked` }));
});

afterEach(() => {
jest.spyOn(common, 'getOctokit').mockRestore();
});

it('throws without parameter', async () => {
await expect(
// @ts-expect-error a parameter is required
Expand All @@ -38,8 +51,6 @@ describe('codegen', () => {
});

it('throws without PR_NUMBER environment variable', async () => {
process.env.GITHUB_TOKEN = 'foo';

await expect(upsertGenerationComment('codegen')).rejects.toThrow(
'`upsertGenerationComment` requires a `PR_NUMBER` environment variable.'
);
Expand Down
6 changes: 2 additions & 4 deletions scripts/ci/codegen/pushGeneratedCode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-console */
import { MAIN_BRANCH, run } from '../../common';
import { ensureGitHubToken, MAIN_BRANCH, run } from '../../common';
import { configureGitHubAuthor } from '../../release/common';
import { getNbGitDiff } from '../utils';

Expand All @@ -18,9 +18,7 @@ async function isUpToDate(baseBranch: string): Promise<boolean> {
* Push generated code for the current `JOB` and `CLIENT` on a `generated/` branch.
*/
export async function pushGeneratedCode(): Promise<void> {
if (!process.env.GITHUB_TOKEN) {
throw new Error('Environment variable `GITHUB_TOKEN` does not exist.');
}
ensureGitHubToken();

await configureGitHubAuthor();

Expand Down
8 changes: 4 additions & 4 deletions scripts/ci/codegen/spreadGeneration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
run,
toAbsolutePath,
REPO_URL,
GITHUB_TOKEN,
ensureGitHubToken,
} from '../../common';
import { getLanguageFolder } from '../../config';
import { cloneRepository, configureGitHubAuthor } from '../../release/common';
Expand Down Expand Up @@ -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 = (
Expand All @@ -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!,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
githubToken: GITHUB_TOKEN!,
githubToken: GITHUB_TOKEN,

tempDir: process.env.RUNNER_TEMP!,
});

Expand Down
5 changes: 2 additions & 3 deletions scripts/ci/codegen/upsertGenerationComment.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
/* eslint-disable no-console */
import { run, OWNER, REPO } from '../../common';
import { getOctokit } from '../../release/common';
import { run, OWNER, REPO, getOctokit } from '../../common';

import commentText from './text';

const BOT_NAME = 'algolia-bot';
const PR_NUMBER = parseInt(process.env.PR_NUMBER || '0', 10);
const octokit = getOctokit(process.env.GITHUB_TOKEN!);

const args = process.argv.slice(2);
const allowedTriggers = [
Expand Down Expand Up @@ -47,6 +45,7 @@ ${commentText[trigger].body(
* Adds or updates a comment on a pull request.
*/
export async function upsertGenerationComment(trigger: Trigger): Promise<void> {
const octokit = getOctokit();
if (!trigger || allowedTriggers.includes(trigger) === false) {
throw new Error(
`'upsertGenerationComment' requires a 'trigger' parameter (${allowedTriggers.join(
Expand Down
162 changes: 162 additions & 0 deletions scripts/ci/githubActions/renovateWeeklyPR.ts
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 #523
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will update this issue number once the renovate dashboard is open

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I won't open things you can try to guess the next number :D

`,
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);
}
})();
23 changes: 23 additions & 0 deletions scripts/common.ts
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';
Expand All @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need it we can enforce calling ensureGitHubToken everywhere no?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
export const GITHUB_TOKEN = ensureGitHubToken();

or maybe?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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(), '..');
Expand Down Expand Up @@ -258,3 +260,24 @@ export async function runComposerUpdate(verbose: boolean): Promise<void> {
);
}
}

export function ensureGitHubToken(): void {
if (!GITHUB_TOKEN)
throw new Error('Environment variable `GITHUB_TOKEN` does not exist.');
millotp marked this conversation as resolved.
Show resolved Hide resolved
}

export function getOctokit(): Octokit {
ensureGitHubToken();
return new Octokit({
auth: `token ${GITHUB_TOKEN}`,
});
}

export function wait(waitTime: number): Promise<void> {
if (waitTime <= 0) {
return Promise.resolve();
}
return new Promise((resolve) => {
setTimeout(resolve, waitTime);
});
}
1 change: 1 addition & 0 deletions scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"pre-commit": "./ci/husky/pre-commit.js",
"processRelease": "ts-node release/process-release.ts",
"pushGeneratedCode": "ts-node ci/codegen/pushGeneratedCode.ts",
"renovateWeeklyPR": "ts-node ci/githubActions/renovateWeeklyPR.ts",
"setRunVariables": "ts-node ci/githubActions/setRunVariables.ts",
"spreadGeneration": "ts-node ci/codegen/spreadGeneration.ts",
"test": "jest",
Expand Down
Loading