From 877f8cc1e1c00dee4bb48a00df64d800f131c1ed Mon Sep 17 00:00:00 2001 From: Pierre Millot Date: Thu, 19 May 2022 18:33:14 +0200 Subject: [PATCH 1/8] chore: add renovate --- package.json | 1 + renovate.json | 23 +++ scripts/ci/codegen/pushGeneratedCode.ts | 6 +- scripts/ci/codegen/spreadGeneration.ts | 8 +- scripts/ci/codegen/upsertGenerationComment.ts | 5 +- scripts/ci/githubActions/renovateWeeklyPR.ts | 160 ++++++++++++++++++ scripts/common.ts | 23 +++ scripts/release/common.ts | 8 - scripts/release/process-release.ts | 14 +- yarn.lock | 8 + 10 files changed, 230 insertions(+), 26 deletions(-) create mode 100644 renovate.json create mode 100644 scripts/ci/githubActions/renovateWeeklyPR.ts diff --git a/package.json b/package.json index 0bc1caa057..ccf8efb422 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/renovate.json b/renovate.json new file mode 100644 index 0000000000..566d11c302 --- /dev/null +++ b/renovate.json @@ -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 +} diff --git a/scripts/ci/codegen/pushGeneratedCode.ts b/scripts/ci/codegen/pushGeneratedCode.ts index 466d43233b..6f0cae3777 100644 --- a/scripts/ci/codegen/pushGeneratedCode.ts +++ b/scripts/ci/codegen/pushGeneratedCode.ts @@ -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'; @@ -18,9 +18,7 @@ async function isUpToDate(baseBranch: string): Promise { * Push generated code for the current `JOB` and `CLIENT` on a `generated/` branch. */ export async function pushGeneratedCode(): Promise { - if (!process.env.GITHUB_TOKEN) { - throw new Error('Environment variable `GITHUB_TOKEN` does not exist.'); - } + ensureGitHubToken(); await configureGitHubAuthor(); diff --git a/scripts/ci/codegen/spreadGeneration.ts b/scripts/ci/codegen/spreadGeneration.ts index 1bfc537588..bc930dcf06 100644 --- a/scripts/ci/codegen/spreadGeneration.ts +++ b/scripts/ci/codegen/spreadGeneration.ts @@ -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 { - if (!process.env.GITHUB_TOKEN) { - throw new Error('Environment variable `GITHUB_TOKEN` does not exist.'); - } + ensureGitHubToken(); const lastCommitMessage = await run('git log -1 --format="%s"'); const author = ( @@ -81,7 +81,7 @@ async function spreadGeneration(): Promise { for (const lang of langs) { const { tempGitDir } = await cloneRepository({ lang, - githubToken: process.env.GITHUB_TOKEN, + githubToken: GITHUB_TOKEN!, tempDir: process.env.RUNNER_TEMP!, }); diff --git a/scripts/ci/codegen/upsertGenerationComment.ts b/scripts/ci/codegen/upsertGenerationComment.ts index b9457566b7..2946fa2499 100644 --- a/scripts/ci/codegen/upsertGenerationComment.ts +++ b/scripts/ci/codegen/upsertGenerationComment.ts @@ -1,12 +1,11 @@ /* 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 octokit = getOctokit(); const args = process.argv.slice(2); const allowedTriggers = [ diff --git a/scripts/ci/githubActions/renovateWeeklyPR.ts b/scripts/ci/githubActions/renovateWeeklyPR.ts new file mode 100644 index 0000000000..ed54da45cd --- /dev/null +++ b/scripts/ci/githubActions/renovateWeeklyPR.ts @@ -0,0 +1,160 @@ +/* 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 { + 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 { + const create = await octokit.git.createRef({ + owner: OWNER, + repo: REPO, + ref: `refs/heads/${BRANCH}`, + sha, + }); + return create; +} + +async function deleteRef(octokit: Octokit): Promise { + 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 { + 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 { + 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 { + 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 { + // Next monday + const date = new Date(); + date.setDate(date.getDate() + 3); + + const title = `fix: dependencies ${date.toISOString().split('T')[0]}`; + const { data } = await octokit.pulls.create({ + repo: REPO, + owner: OWNER, + title, + body: `Weekly dependencies update. +Contributes to #523 + `, + head: BRANCH, + base: BRANCH_BASE, + }); + return data; +} + +async function resetBranch( + octokit: Octokit, + refBase: string, + exists: boolean +): Promise { + 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 => { + 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); + } +})(); diff --git a/scripts/common.ts b/scripts/common.ts index bf0bf875fd..5de52ce225 100644 --- a/scripts/common.ts +++ b/scripts/common.ts @@ -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; // 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,24 @@ export async function runComposerUpdate(verbose: boolean): Promise { ); } } + +export function ensureGitHubToken(): void { + if (!GITHUB_TOKEN) + throw new Error('Environment variable `GITHUB_TOKEN` does not exist.'); +} + +export function getOctokit(): Octokit { + ensureGitHubToken(); + return new Octokit({ + auth: `token ${GITHUB_TOKEN}`, + }); +} + +export function wait(waitTime: number): Promise { + if (waitTime <= 0) { + return Promise.resolve(); + } + return new Promise((resolve) => { + setTimeout(resolve, waitTime); + }); +} diff --git a/scripts/release/common.ts b/scripts/release/common.ts index c0c21ee309..1d0c0d6d9b 100644 --- a/scripts/release/common.ts +++ b/scripts/release/common.ts @@ -1,7 +1,5 @@ import path from 'path'; -import { Octokit } from '@octokit/rest'; - import config from '../../config/release.config.json'; import { run } from '../common'; import { getGitHubUrl } from '../config'; @@ -10,12 +8,6 @@ import type { Language } from '../types'; export const RELEASED_TAG = config.releasedTag; export const TEAM_SLUG = config.teamSlug; -export function getOctokit(githubToken: string): Octokit { - return new Octokit({ - auth: `token ${githubToken}`, - }); -} - export function getTargetBranch(language: string): string { return config.targetBranch[language] || config.defaultTargetBranch; } diff --git a/scripts/release/process-release.ts b/scripts/release/process-release.ts index 8645e3b205..c17560e059 100755 --- a/scripts/release/process-release.ts +++ b/scripts/release/process-release.ts @@ -19,6 +19,9 @@ import { emptyDirExceptForDotGit, GENERATORS, LANGUAGES, + getOctokit, + ensureGitHubToken, + GITHUB_TOKEN, } from '../common'; import { getClientsConfigField, @@ -34,7 +37,6 @@ import { getMarkdownSection, configureGitHubAuthor, cloneRepository, - getOctokit, } from './common'; import TEXT from './text'; import type { @@ -61,7 +63,7 @@ const BEFORE_CLIENT_COMMIT: { [lang: string]: BeforeClientCommitCommand } = { }; async function getIssueBody(): Promise { - const octokit = getOctokit(process.env.GITHUB_TOKEN!); + const octokit = getOctokit(); const { data: { body }, } = await octokit.rest.issues.get({ @@ -230,7 +232,7 @@ function formatGitTag({ } async function isAuthorizedRelease(): Promise { - const octokit = getOctokit(process.env.GITHUB_TOKEN!); + const octokit = getOctokit(); const { data: members } = await octokit.rest.teams.listMembersInOrg({ org: OWNER, team_slug: TEAM_SLUG, @@ -250,9 +252,7 @@ async function isAuthorizedRelease(): Promise { } async function processRelease(): Promise { - if (!process.env.GITHUB_TOKEN) { - throw new Error('Environment variable `GITHUB_TOKEN` does not exist.'); - } + ensureGitHubToken(); if (!process.env.EVENT_NUMBER) { throw new Error('Environment variable `EVENT_NUMBER` does not exist.'); @@ -308,7 +308,7 @@ async function processRelease(): Promise { )) { const { tempGitDir } = await cloneRepository({ lang: lang as Language, - githubToken: process.env.GITHUB_TOKEN, + githubToken: GITHUB_TOKEN!, tempDir: process.env.RUNNER_TEMP!, }); diff --git a/yarn.lock b/yarn.lock index e534c2e325..914b103ea9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -32,6 +32,7 @@ __metadata: mustache: 4.2.0 prettier: 2.6.2 prettier-plugin-java: 1.6.1 + renovate-config-algolia: 2.1.10 typescript: 4.6.3 languageName: unknown linkType: soft @@ -18794,6 +18795,13 @@ __metadata: languageName: node linkType: hard +"renovate-config-algolia@npm:2.1.10": + version: 2.1.10 + resolution: "renovate-config-algolia@npm:2.1.10" + checksum: 2fa9e541ce8672c2dd96b0719ae0ca054e9e4fcbc24c9a4b7aee9047cd22f0f7d48f45142b65b41a7bb7da7738deee40a02920ce7893c6eabd24d40c5828ed3e + languageName: node + linkType: hard + "repeat-string@npm:^1.5.4": version: 1.6.1 resolution: "repeat-string@npm:1.6.1" From 6fe4c36f9009482cf19b60d34f09ab320e5998b1 Mon Sep 17 00:00:00 2001 From: Pierre Millot Date: Thu, 19 May 2022 18:42:59 +0200 Subject: [PATCH 2/8] action --- .github/workflows/renovate.yml | 24 ++++++++++++++++++++++++ scripts/package.json | 3 ++- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/renovate.yml diff --git a/.github/workflows/renovate.yml b/.github/workflows/renovate.yml new file mode 100644 index 0000000000..201462b52f --- /dev/null +++ b/.github/workflows/renovate.yml @@ -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 + + - name: Setup + id: setup + uses: ./.github/actions/setup + with: + type: minimal + + - run: yarn workspace scripts renovateWeeklyPR + env: + GITHUB_TOKEN: ${{ secrets.TOKEN_RELEASE_BOT }} diff --git a/scripts/package.json b/scripts/package.json index a88f0e8909..b8beb1d85e 100644 --- a/scripts/package.json +++ b/scripts/package.json @@ -11,7 +11,8 @@ "setRunVariables": "ts-node ci/githubActions/setRunVariables.ts", "spreadGeneration": "ts-node ci/codegen/spreadGeneration.ts", "test": "jest", - "upsertGenerationComment": "ts-node ci/codegen/upsertGenerationComment.ts" + "upsertGenerationComment": "ts-node ci/codegen/upsertGenerationComment.ts", + "renovateWeeklyPR": "ts-node ci/githubActuons/renovateWeeklyPR.ts" }, "devDependencies": { "@apidevtools/swagger-parser": "10.0.3", From faec510351850b5b17523ec0b002ee5dd46bef8c Mon Sep 17 00:00:00 2001 From: Pierre Millot Date: Fri, 20 May 2022 10:50:27 +0200 Subject: [PATCH 3/8] fix tests --- scripts/ci/codegen/__tests__/codegen.test.ts | 15 +++++++++++++-- scripts/ci/codegen/upsertGenerationComment.ts | 2 +- scripts/release/create-release-issue.ts | 10 +++++----- scripts/release/process-release.ts | 3 --- 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/scripts/ci/codegen/__tests__/codegen.test.ts b/scripts/ci/codegen/__tests__/codegen.test.ts index 6d482f4e7b..c3f9747fd8 100644 --- a/scripts/ci/codegen/__tests__/codegen.test.ts +++ b/scripts/ci/codegen/__tests__/codegen.test.ts @@ -1,3 +1,5 @@ +import { Octokit } from '@octokit/rest'; + import * as common from '../../../common'; import { cleanGeneratedBranch } from '../cleanGeneratedBranch'; import { pushGeneratedCode } from '../pushGeneratedCode'; @@ -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 @@ -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.' ); diff --git a/scripts/ci/codegen/upsertGenerationComment.ts b/scripts/ci/codegen/upsertGenerationComment.ts index 2946fa2499..bc339ac9d4 100644 --- a/scripts/ci/codegen/upsertGenerationComment.ts +++ b/scripts/ci/codegen/upsertGenerationComment.ts @@ -5,7 +5,6 @@ import commentText from './text'; const BOT_NAME = 'algolia-bot'; const PR_NUMBER = parseInt(process.env.PR_NUMBER || '0', 10); -const octokit = getOctokit(); const args = process.argv.slice(2); const allowedTriggers = [ @@ -46,6 +45,7 @@ ${commentText[trigger].body( * Adds or updates a comment on a pull request. */ export async function upsertGenerationComment(trigger: Trigger): Promise { + const octokit = getOctokit(); if (!trigger || allowedTriggers.includes(trigger) === false) { throw new Error( `'upsertGenerationComment' requires a 'trigger' parameter (${allowedTriggers.join( diff --git a/scripts/release/create-release-issue.ts b/scripts/release/create-release-issue.ts index 66e6fc4cdf..0060182f94 100755 --- a/scripts/release/create-release-issue.ts +++ b/scripts/release/create-release-issue.ts @@ -10,11 +10,13 @@ import { MAIN_BRANCH, OWNER, REPO, + getOctokit, + ensureGitHubToken, } from '../common'; import { getPackageVersionDefault } from '../config'; import type { Language } from '../types'; -import { RELEASED_TAG, getOctokit } from './common'; +import { RELEASED_TAG } from './common'; import TEXT from './text'; import type { Versions, @@ -181,9 +183,7 @@ export function decideReleaseStrategy({ /* eslint-enable no-param-reassign */ async function createReleaseIssue(): Promise { - if (!process.env.GITHUB_TOKEN) { - throw new Error('Environment variable `GITHUB_TOKEN` does not exist.'); - } + ensureGitHubToken(); if ((await run('git rev-parse --abbrev-ref HEAD')) !== MAIN_BRANCH) { throw new Error( @@ -289,7 +289,7 @@ async function createReleaseIssue(): Promise { TEXT.approval, ].join('\n\n'); - const octokit = getOctokit(process.env.GITHUB_TOKEN); + const octokit = getOctokit(); octokit.rest.issues .create({ diff --git a/scripts/release/process-release.ts b/scripts/release/process-release.ts index c17560e059..0a917a4ced 100755 --- a/scripts/release/process-release.ts +++ b/scripts/release/process-release.ts @@ -20,7 +20,6 @@ import { GENERATORS, LANGUAGES, getOctokit, - ensureGitHubToken, GITHUB_TOKEN, } from '../common'; import { @@ -252,8 +251,6 @@ async function isAuthorizedRelease(): Promise { } async function processRelease(): Promise { - ensureGitHubToken(); - if (!process.env.EVENT_NUMBER) { throw new Error('Environment variable `EVENT_NUMBER` does not exist.'); } From f3d26be012bf22876058e70fa5e5f31a301114d5 Mon Sep 17 00:00:00 2001 From: Pierre Millot Date: Fri, 20 May 2022 11:28:45 +0200 Subject: [PATCH 4/8] fix json --- .github/workflows/check.yml | 4 +++- scripts/package.json | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 3ad09d5afe..6a5cf23967 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -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 }} diff --git a/scripts/package.json b/scripts/package.json index b8beb1d85e..292de02bcf 100644 --- a/scripts/package.json +++ b/scripts/package.json @@ -8,11 +8,11 @@ "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/githubActuons/renovateWeeklyPR.ts", "setRunVariables": "ts-node ci/githubActions/setRunVariables.ts", "spreadGeneration": "ts-node ci/codegen/spreadGeneration.ts", "test": "jest", - "upsertGenerationComment": "ts-node ci/codegen/upsertGenerationComment.ts", - "renovateWeeklyPR": "ts-node ci/githubActuons/renovateWeeklyPR.ts" + "upsertGenerationComment": "ts-node ci/codegen/upsertGenerationComment.ts" }, "devDependencies": { "@apidevtools/swagger-parser": "10.0.3", From 9ec95b83ee81e9c29a86d600c68f9814ab82abbc Mon Sep 17 00:00:00 2001 From: Pierre Millot Date: Fri, 20 May 2022 12:32:28 +0200 Subject: [PATCH 5/8] fix pr title --- scripts/ci/githubActions/renovateWeeklyPR.ts | 4 +++- scripts/package.json | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/scripts/ci/githubActions/renovateWeeklyPR.ts b/scripts/ci/githubActions/renovateWeeklyPR.ts index ed54da45cd..695bc7dab3 100644 --- a/scripts/ci/githubActions/renovateWeeklyPR.ts +++ b/scripts/ci/githubActions/renovateWeeklyPR.ts @@ -91,7 +91,9 @@ async function createPR(octokit: Octokit): Promise { const date = new Date(); date.setDate(date.getDate() + 3); - const title = `fix: dependencies ${date.toISOString().split('T')[0]}`; + const title = `chore(scripts): dependencies ${ + date.toISOString().split('T')[0] + }`; const { data } = await octokit.pulls.create({ repo: REPO, owner: OWNER, diff --git a/scripts/package.json b/scripts/package.json index 292de02bcf..c76b869bb0 100644 --- a/scripts/package.json +++ b/scripts/package.json @@ -8,7 +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/githubActuons/renovateWeeklyPR.ts", + "renovateWeeklyPR": "ts-node ci/githubActions/renovateWeeklyPR.ts", "setRunVariables": "ts-node ci/githubActions/setRunVariables.ts", "spreadGeneration": "ts-node ci/codegen/spreadGeneration.ts", "test": "jest", From 0ce6e38eecb7a105e9d80979627d5524d8ef1d4a Mon Sep 17 00:00:00 2001 From: algolia-bot Date: Fri, 20 May 2022 14:25:01 +0200 Subject: [PATCH 6/8] review --- .github/workflows/renovate.yml | 2 -- scripts/ci/codegen/__tests__/codegen.test.ts | 14 +++----------- scripts/common.ts | 11 +++++++---- 3 files changed, 10 insertions(+), 17 deletions(-) diff --git a/.github/workflows/renovate.yml b/.github/workflows/renovate.yml index 201462b52f..e96311383b 100644 --- a/.github/workflows/renovate.yml +++ b/.github/workflows/renovate.yml @@ -10,8 +10,6 @@ jobs: runs-on: ubuntu-20.04 steps: - uses: actions/checkout@v2 - with: - fetch-depth: 0 - name: Setup id: setup diff --git a/scripts/ci/codegen/__tests__/codegen.test.ts b/scripts/ci/codegen/__tests__/codegen.test.ts index c3f9747fd8..a00e57f750 100644 --- a/scripts/ci/codegen/__tests__/codegen.test.ts +++ b/scripts/ci/codegen/__tests__/codegen.test.ts @@ -1,5 +1,3 @@ -import { Octokit } from '@octokit/rest'; - import * as common from '../../../common'; import { cleanGeneratedBranch } from '../cleanGeneratedBranch'; import { pushGeneratedCode } from '../pushGeneratedCode'; @@ -23,6 +21,7 @@ describe('codegen', () => { describe('pushGeneratedCode', () => { it('throws without GITHUB_TOKEN environment variable', async () => { + process.env.GITHUB_TOKEN = ''; await expect(pushGeneratedCode()).rejects.toThrow( 'Environment variable `GITHUB_TOKEN` does not exist.' ); @@ -30,15 +29,8 @@ 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(); + beforeAll(() => { + process.env.GITHUB_TOKEN = 'mocked'; }); it('throws without parameter', async () => { diff --git a/scripts/common.ts b/scripts/common.ts index 5de52ce225..9d29a682c5 100644 --- a/scripts/common.ts +++ b/scripts/common.ts @@ -261,15 +261,18 @@ export async function runComposerUpdate(verbose: boolean): Promise { } } -export function ensureGitHubToken(): void { - if (!GITHUB_TOKEN) +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 { - ensureGitHubToken(); + const token = ensureGitHubToken(); return new Octokit({ - auth: `token ${GITHUB_TOKEN}`, + auth: `token ${token}`, }); } From 476da34d49ace5c8d0281a50974b61611e28b67c Mon Sep 17 00:00:00 2001 From: algolia-bot Date: Fri, 20 May 2022 14:25:31 +0200 Subject: [PATCH 7/8] bump issue --- scripts/ci/githubActions/renovateWeeklyPR.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/ci/githubActions/renovateWeeklyPR.ts b/scripts/ci/githubActions/renovateWeeklyPR.ts index 695bc7dab3..546632f4c4 100644 --- a/scripts/ci/githubActions/renovateWeeklyPR.ts +++ b/scripts/ci/githubActions/renovateWeeklyPR.ts @@ -99,7 +99,7 @@ async function createPR(octokit: Octokit): Promise { owner: OWNER, title, body: `Weekly dependencies update. -Contributes to #523 +Contributes to #528 `, head: BRANCH, base: BRANCH_BASE, From 41b2ff8f96d2a5bc0a2c5b2063fd616b11672056 Mon Sep 17 00:00:00 2001 From: Pierre Millot Date: Fri, 20 May 2022 15:48:04 +0200 Subject: [PATCH 8/8] review --- scripts/ci/codegen/spreadGeneration.ts | 5 ++--- scripts/common.ts | 1 - scripts/release/process-release.ts | 6 ++++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/scripts/ci/codegen/spreadGeneration.ts b/scripts/ci/codegen/spreadGeneration.ts index bc930dcf06..d3d56be914 100644 --- a/scripts/ci/codegen/spreadGeneration.ts +++ b/scripts/ci/codegen/spreadGeneration.ts @@ -8,7 +8,6 @@ import { run, toAbsolutePath, REPO_URL, - GITHUB_TOKEN, ensureGitHubToken, } from '../../common'; import { getLanguageFolder } from '../../config'; @@ -62,7 +61,7 @@ export function cleanUpCommitMessage(commitMessage: string): string { } async function spreadGeneration(): Promise { - ensureGitHubToken(); + const githubToken = ensureGitHubToken(); const lastCommitMessage = await run('git log -1 --format="%s"'); const author = ( @@ -81,7 +80,7 @@ async function spreadGeneration(): Promise { for (const lang of langs) { const { tempGitDir } = await cloneRepository({ lang, - githubToken: GITHUB_TOKEN!, + githubToken, tempDir: process.env.RUNNER_TEMP!, }); diff --git a/scripts/common.ts b/scripts/common.ts index 9d29a682c5..949ea8577a 100644 --- a/scripts/common.ts +++ b/scripts/common.ts @@ -27,7 +27,6 @@ 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; // This script is run by `yarn workspace ...`, which means the current working directory is `./script` export const ROOT_DIR = path.resolve(process.cwd(), '..'); diff --git a/scripts/release/process-release.ts b/scripts/release/process-release.ts index 0a917a4ced..347674d1e1 100755 --- a/scripts/release/process-release.ts +++ b/scripts/release/process-release.ts @@ -20,7 +20,7 @@ import { GENERATORS, LANGUAGES, getOctokit, - GITHUB_TOKEN, + ensureGitHubToken, } from '../common'; import { getClientsConfigField, @@ -251,6 +251,8 @@ async function isAuthorizedRelease(): Promise { } async function processRelease(): Promise { + const githubToken = ensureGitHubToken(); + if (!process.env.EVENT_NUMBER) { throw new Error('Environment variable `EVENT_NUMBER` does not exist.'); } @@ -305,7 +307,7 @@ async function processRelease(): Promise { )) { const { tempGitDir } = await cloneRepository({ lang: lang as Language, - githubToken: GITHUB_TOKEN!, + githubToken, tempDir: process.env.RUNNER_TEMP!, });