From da2a9d8b6d677b2485c0cbb665df512f20d30084 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Vannicatte?= Date: Wed, 16 Mar 2022 10:59:17 +0100 Subject: [PATCH 1/4] fix(ci): make generation comment clearer --- .github/actions/setup/action.yml | 12 +++ .github/workflows/codegen-cleanup.yml | 21 ---- .github/workflows/codegen.yml | 45 ++++++++ scripts/ci/codegen/__tests__/codegen.test.ts | 81 ++++++++++++++ scripts/ci/codegen/cleanGeneratedBranch.ts | 14 +-- scripts/ci/codegen/pushGeneratedCode.ts | 100 +++--------------- scripts/ci/codegen/text.ts | 26 +++++ scripts/ci/codegen/upsertGenerationComment.ts | 97 +++++++++++++++++ scripts/package.json | 1 + 9 files changed, 284 insertions(+), 113 deletions(-) delete mode 100644 .github/workflows/codegen-cleanup.yml create mode 100644 .github/workflows/codegen.yml create mode 100644 scripts/ci/codegen/__tests__/codegen.test.ts create mode 100644 scripts/ci/codegen/text.ts create mode 100644 scripts/ci/codegen/upsertGenerationComment.ts diff --git a/.github/actions/setup/action.yml b/.github/actions/setup/action.yml index 4e89564a6f..2dabeace15 100644 --- a/.github/actions/setup/action.yml +++ b/.github/actions/setup/action.yml @@ -2,6 +2,11 @@ name: Setup description: Setup CI environment. +inputs: + type: + description: Type of setup, `minimal` will only run the JavaScript installation. + required: false + runs: using: composite steps: @@ -10,6 +15,7 @@ runs: run: echo "CACHE_VERSION=$(< .github/.cache_version)" >> $GITHUB_ENV - name: Install Java + if: inputs.type != 'minimal' uses: actions/setup-java@v2 with: distribution: zulu @@ -17,6 +23,7 @@ runs: cache: gradle - name: Validate gradle wrapper + if: inputs.type != 'minimal' uses: gradle/wrapper-validation-action@v1 - name: Install Node @@ -29,6 +36,7 @@ runs: uses: ./.github/actions/cache - name: Setting diff outputs variables + if: inputs.type != 'minimal' id: diff shell: bash run: | @@ -61,6 +69,7 @@ runs: echo "::set-output name=ORIGIN_BRANCH::$origin" - name: Compute specs matrix + if: inputs.type != 'minimal' id: spec-matrix shell: bash run: | @@ -81,6 +90,7 @@ runs: echo "::set-output name=RUN_SPECS::$run" - name: Compute the JS client build matrix + if: inputs.type != 'minimal' id: js-matrix shell: bash run: | @@ -117,6 +127,7 @@ runs: fi - name: Compute the Java client build matrix + if: inputs.type != 'minimal' id: java-matrix shell: bash run: | @@ -137,6 +148,7 @@ runs: echo "::set-output name=RUN_CLIENT::$run" - name: Compute the PHP client build matrix + if: inputs.type != 'minimal' id: php-matrix shell: bash run: | diff --git a/.github/workflows/codegen-cleanup.yml b/.github/workflows/codegen-cleanup.yml deleted file mode 100644 index bd5f198ffb..0000000000 --- a/.github/workflows/codegen-cleanup.yml +++ /dev/null @@ -1,21 +0,0 @@ -name: Codegen cleanup - -on: - pull_request: - types: [closed] - -jobs: - codegen: - runs-on: ubuntu-20.04 - timeout-minutes: 10 - steps: - - uses: actions/checkout@v2 - with: - fetch-depth: 0 - ref: main - - - name: Setup - uses: ./.github/actions/setup - - - name: Clean previously generated branch - run: yarn workspace scripts cleanGeneratedBranch ${{ github.head_ref }} diff --git a/.github/workflows/codegen.yml b/.github/workflows/codegen.yml new file mode 100644 index 0000000000..44b5951f41 --- /dev/null +++ b/.github/workflows/codegen.yml @@ -0,0 +1,45 @@ +name: Codegen + +on: + pull_request: + types: [opened, synchronize, closed] + +jobs: + notification: + runs-on: ubuntu-20.04 + timeout-minutes: 10 + if: (github.event.action == 'opened' || github.event.action == 'synchronize') && github.event.number + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 0 + ref: ${{ github.event.pull_request.head.ref }} + + - name: Setup + uses: ./.github/actions/setup + with: + type: minimal + + - name: Add notification comment + run: yarn workspace scripts upsertGenerationComment notification + env: + GITHUB_TOKEN: ${{ secrets.TOKEN_GENERATE_BOT }} + PR_NUMBER: ${{ github.event.number }} + + cleanup: + runs-on: ubuntu-20.04 + timeout-minutes: 10 + if: github.event.action == 'closed' + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 0 + ref: main + + - name: Setup + uses: ./.github/actions/setup + with: + type: minimal + + - name: Clean previously generated branch + run: yarn workspace scripts cleanGeneratedBranch ${{ github.head_ref }} diff --git a/scripts/ci/codegen/__tests__/codegen.test.ts b/scripts/ci/codegen/__tests__/codegen.test.ts new file mode 100644 index 0000000000..6f173988d9 --- /dev/null +++ b/scripts/ci/codegen/__tests__/codegen.test.ts @@ -0,0 +1,81 @@ +import { cleanGeneratedBranch } from '../cleanGeneratedBranch'; +import { pushGeneratedCode } from '../pushGeneratedCode'; +import commentText from '../text'; +import { + getCommentBody, + upsertGenerationComment, +} from '../upsertGenerationComment'; + +describe('codegen', () => { + describe('cleanGeneratedBranch', () => { + it('throws without parameters', async () => { + await expect( + // @ts-expect-error a parameter is required + cleanGeneratedBranch() + ).rejects.toThrow( + 'The base branch should be passed as a cli parameter of the `cleanGeneratedBranch` script.' + ); + }); + }); + + describe('pushGeneratedCode', () => { + it('throws without GITHUB_TOKEN environment variable', async () => { + await expect(pushGeneratedCode()).rejects.toThrow( + 'Environment variable `GITHUB_TOKEN` does not exist.' + ); + }); + }); + + describe('upsertGenerationComment', () => { + it('throws without parameter', async () => { + await expect( + // @ts-expect-error a parameter is required + upsertGenerationComment() + ).rejects.toThrow( + '`upsertGenerationComment` requires a `trigger` parameter (`codegen` | `notification`).' + ); + }); + + 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.' + ); + }); + }); + + describe('getCommentBody', () => { + it('returns the right comment for a `notification` trigger', async () => { + expect(await getCommentBody('notification')).toMatchInlineSnapshot(` + "🔨 The codegen job will run at the end of the CI. + + _Make sure your last commit does not contains generated code, it will be automatically pushed by our CI._" + `); + }); + + it('returns the right comment for a `noGen` trigger', async () => { + expect(await getCommentBody('noGen')).toMatchInlineSnapshot(` + "✗ No code generated. + + _If you believe this is an issue on our side, please [open an issue](https://github.com/algolia/api-clients-automation/issues/new)_" + `); + }); + + describe('text', () => { + it('creates a comment body for the parameters', () => { + expect( + commentText.codegen.body( + 'myBranch', + 'myCommit', + 42, + 'theGeneratedCommit' + ) + ).toMatchInlineSnapshot(` + "🔨 Triggered by commit [myCommit](https://github.com/algolia/api-clients-automation/pull/42/commits/myCommit). + 🔍 Browse the generated code on branch [myBranch](https://github.com/algolia/api-clients-automation/tree/myBranch): [theGeneratedCommit](https://github.com/algolia/api-clients-automation/commit/theGeneratedCommit)." + `); + }); + }); + }); +}); diff --git a/scripts/ci/codegen/cleanGeneratedBranch.ts b/scripts/ci/codegen/cleanGeneratedBranch.ts index e686243d7b..9b7a14301d 100644 --- a/scripts/ci/codegen/cleanGeneratedBranch.ts +++ b/scripts/ci/codegen/cleanGeneratedBranch.ts @@ -4,7 +4,13 @@ import { run } from '../../common'; /** * Deletes a branch for its `generated/${headRef}` name on origin. */ -async function cleanGeneratedBranch(headRef: string): Promise { +export async function cleanGeneratedBranch(headRef: string): Promise { + if (!headRef) { + throw new Error( + 'The base branch should be passed as a cli parameter of the `cleanGeneratedBranch` script.' + ); + } + const generatedCodeBranch = `generated/${headRef}`; if (!(await run(`git ls-remote --heads origin ${generatedCodeBranch}`))) { @@ -22,10 +28,4 @@ async function cleanGeneratedBranch(headRef: string): Promise { const args = process.argv.slice(2); -if (!args || args.length === 0) { - throw new Error( - 'The base branch should be passed as a cli parameter of the `cleanGeneratedBranch` script.' - ); -} - cleanGeneratedBranch(args[0]); diff --git a/scripts/ci/codegen/pushGeneratedCode.ts b/scripts/ci/codegen/pushGeneratedCode.ts index 8fbdf7cd5e..e49ee096d5 100644 --- a/scripts/ci/codegen/pushGeneratedCode.ts +++ b/scripts/ci/codegen/pushGeneratedCode.ts @@ -1,87 +1,18 @@ /* eslint-disable no-console */ -import { Octokit } from '@octokit/rest'; - import { run } from '../../common'; -import { configureGitHubAuthor, OWNER, REPO } from '../../release/common'; - -if (!process.env.GITHUB_TOKEN) { - throw new Error('Environment variable `GITHUB_TOKEN` does not exist.'); -} +import { configureGitHubAuthor } from '../../release/common'; const PR_NUMBER = parseInt(process.env.PR_NUMBER || '0', 10); -const FOLDERS_TO_CHECK = ['clients', 'specs/bundled']; -// this should be changed to the bot name once we have the logs -const BOT_NAME = 'shortcuts'; - -const octokit = new Octokit({ - auth: `token ${process.env.GITHUB_TOKEN}`, -}); - -async function getCommentBody(commit: string, branch: string): Promise { - const repoUrl = `https://github.com/${OWNER}/${REPO}`; - const generatedCommit = await run('git show -s --format=%H'); - const header = `✔️ codegen triggered on commit [${commit}](${repoUrl}/pull/${PR_NUMBER}/commits/${commit}).`; - const body = `🔍 Browse the generated code on branch [${branch}](${repoUrl}/tree/${branch}): [${generatedCommit}](${repoUrl}/commit/${generatedCommit}).`; - - return `${header} - -${body}`; -} +const FOLDERS_TO_CHECK = 'clients specs/bundled'; /** - * Add or update comment to the current `PR_NUMBER`. + * Push generated code for the current `JOB` and `CLIENT` on a `generated/` branch. */ -async function upsertCommentToPullRequest( - baseCommit: string, - generatedCodeBranch: string -): Promise { - const baseOctokitConfig = { - owner: OWNER, - repo: REPO, - issue_number: PR_NUMBER, - }; - - try { - // Search for a previous comment from our bot. - const previousComment = await octokit.rest.issues - .listComments(baseOctokitConfig) - .then( - (res) => - res.data.filter( - (comment) => - comment.user?.login === BOT_NAME && - // this shouldn't be needed once we have a proper bot running - comment.body?.startsWith('✔️ codegen triggered on commit') - )[0] - ); - const commentBody = await getCommentBody(baseCommit, generatedCodeBranch); - - if (previousComment?.id) { - console.log(`Previous bot comment found ${previousComment.id}.`); - - await octokit.rest.issues.updateComment({ - ...baseOctokitConfig, - body: commentBody, - comment_id: previousComment.id, - }); - - return; - } - - console.log('Creating new comment.'); - await octokit.rest.issues.createComment({ - ...baseOctokitConfig, - body: commentBody, - }); - } catch (e) { - throw new Error(`Error with GitHub API: ${e}`); +export async function pushGeneratedCode(): Promise { + if (!process.env.GITHUB_TOKEN) { + throw new Error('Environment variable `GITHUB_TOKEN` does not exist.'); } -} -/** - * Push generated code for the current `JOB` and `CLIENT` on a `generated/` branch. - */ -async function pushGeneratedCode(): Promise { await configureGitHubAuthor(); const baseBranch = await run('git branch --show-current'); @@ -89,25 +20,23 @@ async function pushGeneratedCode(): Promise { // determine generated branch name based on current branch const generatedCodeBranch = `generated/${baseBranch}`; - const generatedFolders = FOLDERS_TO_CHECK.join(' '); if ( (await run( - `git status --porcelain ${generatedFolders} | wc -l | tr -d ' '` + `git status --porcelain ${FOLDERS_TO_CHECK} | wc -l | tr -d ' '` )) === '0' ) { console.log(`No generated code changes found for '${baseBranch}'.`); + if (PR_NUMBER) { + await run(`yarn workspace scripts upsertGenerationComment noGen`); + } + return; } await run(`yarn workspace scripts cleanGeneratedBranch ${baseBranch}`); - const baseCommit = await run(`git show ${baseBranch} -s --format=%H`); - console.log( - `Codegen triggered on branch '${baseBranch}' for commit ${baseCommit}.` - ); - console.log(`Creating branch for generated code: '${generatedCodeBranch}'`); await run(`git branch ${generatedCodeBranch}`); const commitMessage = @@ -116,15 +45,16 @@ async function pushGeneratedCode(): Promise { Co-authored-by: %an <%ae>"`); console.log( - `Pushing code for folders '${generatedFolders}' to generated branch: ${generatedCodeBranch}` + `Pushing code for folders '${FOLDERS_TO_CHECK}' to generated branch: ${generatedCodeBranch}` ); await run(`git checkout ${generatedCodeBranch}`); - await run(`git add ${generatedFolders}`); + await run(`git add ${FOLDERS_TO_CHECK}`); await run(`git commit -m "${commitMessage}"`); await run(`git push origin ${generatedCodeBranch}`); + await run(`git checkout ${baseBranch}`); if (PR_NUMBER) { - await upsertCommentToPullRequest(baseCommit, generatedCodeBranch); + await run(`yarn workspace scripts upsertGenerationComment codegen`); } } diff --git a/scripts/ci/codegen/text.ts b/scripts/ci/codegen/text.ts new file mode 100644 index 0000000000..d701ca0ef4 --- /dev/null +++ b/scripts/ci/codegen/text.ts @@ -0,0 +1,26 @@ +import { OWNER, REPO } from '../../release/common'; + +const REPO_URL = `https://github.com/${OWNER}/${REPO}`; + +export default { + notification: { + header: '🔨 The codegen job will run at the end of the CI.', + body: '_Make sure your last commit does not contains generated code, it will be automatically pushed by our CI._', + }, + noGen: { + header: '✗ No code generated.', + body: `_If you believe this is an issue on our side, please [open an issue](${REPO_URL}/issues/new)_`, + }, + codegen: { + header: '✔️ Code generated!', + body( + branch: string, + commit: string, + eventNumber: number, + generatedCommit: string + ): string { + return `🔨 Triggered by commit [${commit}](${REPO_URL}/pull/${eventNumber}/commits/${commit}). +🔍 Browse the generated code on branch [${branch}](${REPO_URL}/tree/${branch}): [${generatedCommit}](${REPO_URL}/commit/${generatedCommit}).`; + }, + }, +}; diff --git a/scripts/ci/codegen/upsertGenerationComment.ts b/scripts/ci/codegen/upsertGenerationComment.ts new file mode 100644 index 0000000000..772beb5e1d --- /dev/null +++ b/scripts/ci/codegen/upsertGenerationComment.ts @@ -0,0 +1,97 @@ +/* eslint-disable no-console */ +import { Octokit } from '@octokit/rest'; + +import { run } from '../../common'; +import { OWNER, REPO } from '../../release/common'; + +import commentText from './text'; + +// this should be changed to the bot name once we have the logs +const BOT_NAME = 'shortcuts'; +const PR_NUMBER = parseInt(process.env.PR_NUMBER || '0', 10); +const octokit = new Octokit({ + auth: `token ${process.env.GITHUB_TOKEN}`, +}); + +const args = process.argv.slice(2); +const allowedTriggers = ['notification', 'codegen', 'noGen']; + +type Trigger = 'codegen' | 'noGen' | 'notification'; + +export async function getCommentBody(trigger: Trigger): Promise { + if (trigger === 'notification' || trigger === 'noGen') { + return `${commentText[trigger].header} + +${commentText[trigger].body}`; + } + + const baseBranch = await run('git branch --show-current'); + const commit = await run(`git show ${baseBranch} -s --format=%H`); + const generatedCommit = await run('git show -s --format=%H'); + const branch = `generated/${baseBranch}`; + + return `${commentText.codegen.header} + +${commentText.codegen.body(branch, commit, PR_NUMBER, generatedCommit)}`; +} + +/** + * Adds or updates a comment on a pull request. + */ +export async function upsertGenerationComment(trigger: Trigger): Promise { + if (!trigger || allowedTriggers.includes(trigger) === false) { + throw new Error( + '`upsertGenerationComment` requires a `trigger` parameter (`codegen` | `notification`).' + ); + } + + if (!PR_NUMBER) { + throw new Error( + '`upsertGenerationComment` requires a `PR_NUMBER` environment variable.' + ); + } + + console.log(`Upserting comment for trigger: ${trigger}`); + + try { + const baseOctokitConfig = { + owner: OWNER, + repo: REPO, + issue_number: PR_NUMBER, + body: await getCommentBody(trigger), + }; + + // Search for a previous comment from our bot. + const previousComment = await octokit.rest.issues + .listComments(baseOctokitConfig) + .then( + (res) => + res.data.filter( + (comment) => + comment.user?.login === BOT_NAME && + // this shouldn't be needed once we have a proper bot running + (comment.body?.startsWith(commentText.codegen.header) || + comment.body?.startsWith(commentText.noGen.header) || + comment.body?.startsWith(commentText.notification.header)) + )[0] + ); + + if (previousComment?.id) { + console.log(`Previous bot comment found ${previousComment.id}.`); + + await octokit.rest.issues.updateComment({ + ...baseOctokitConfig, + comment_id: previousComment.id, + }); + + return; + } + + console.log('Creating new comment.'); + await octokit.rest.issues.createComment(baseOctokitConfig); + } catch (e) { + throw new Error(`Error with GitHub API: ${e}`); + } +} + +upsertGenerationComment(args[0] as Trigger); diff --git a/scripts/package.json b/scripts/package.json index 1142182689..cbd9ab784a 100644 --- a/scripts/package.json +++ b/scripts/package.json @@ -6,6 +6,7 @@ "processRelease": "ts-node release/process-release.ts", "pushGeneratedCode": "ts-node ci/codegen/pushGeneratedCode.ts", "cleanGeneratedBranch": "ts-node ci/codegen/cleanGeneratedBranch.ts", + "upsertGenerationComment": "ts-node ci/codegen/upsertGenerationComment.ts", "test": "jest" }, "devDependencies": { From 00be87921e28bd566ba0e0b8d31c712013d4d222 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Vannicatte?= Date: Wed, 16 Mar 2022 14:04:05 +0100 Subject: [PATCH 2/4] fix scripts --- scripts/ci/codegen/cleanGeneratedBranch.ts | 4 +++- scripts/ci/codegen/pushGeneratedCode.ts | 4 +++- scripts/ci/codegen/upsertGenerationComment.ts | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/scripts/ci/codegen/cleanGeneratedBranch.ts b/scripts/ci/codegen/cleanGeneratedBranch.ts index 9b7a14301d..d90e83a187 100644 --- a/scripts/ci/codegen/cleanGeneratedBranch.ts +++ b/scripts/ci/codegen/cleanGeneratedBranch.ts @@ -28,4 +28,6 @@ export async function cleanGeneratedBranch(headRef: string): Promise { const args = process.argv.slice(2); -cleanGeneratedBranch(args[0]); +if (require.main === module) { + cleanGeneratedBranch(args[0]); +} diff --git a/scripts/ci/codegen/pushGeneratedCode.ts b/scripts/ci/codegen/pushGeneratedCode.ts index e49ee096d5..c8179ecc9a 100644 --- a/scripts/ci/codegen/pushGeneratedCode.ts +++ b/scripts/ci/codegen/pushGeneratedCode.ts @@ -58,4 +58,6 @@ Co-authored-by: %an <%ae>"`); } } -pushGeneratedCode(); +if (require.main === module) { + pushGeneratedCode(); +} diff --git a/scripts/ci/codegen/upsertGenerationComment.ts b/scripts/ci/codegen/upsertGenerationComment.ts index 772beb5e1d..26adc0e42b 100644 --- a/scripts/ci/codegen/upsertGenerationComment.ts +++ b/scripts/ci/codegen/upsertGenerationComment.ts @@ -94,4 +94,6 @@ export async function upsertGenerationComment(trigger: Trigger): Promise { } } -upsertGenerationComment(args[0] as Trigger); +if (require.main === module) { + upsertGenerationComment(args[0] as Trigger); +} From 88ccbea4a338361423799e98027ecbf29f1e59f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Vannicatte?= Date: Wed, 16 Mar 2022 14:25:03 +0100 Subject: [PATCH 3/4] apply changes from suggestion --- scripts/ci/codegen/__tests__/codegen.test.ts | 2 +- scripts/ci/codegen/pushGeneratedCode.ts | 2 +- scripts/ci/codegen/text.ts | 2 +- scripts/ci/codegen/upsertGenerationComment.ts | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/ci/codegen/__tests__/codegen.test.ts b/scripts/ci/codegen/__tests__/codegen.test.ts index 6f173988d9..5c748b6eb3 100644 --- a/scripts/ci/codegen/__tests__/codegen.test.ts +++ b/scripts/ci/codegen/__tests__/codegen.test.ts @@ -58,7 +58,7 @@ describe('codegen', () => { expect(await getCommentBody('noGen')).toMatchInlineSnapshot(` "✗ No code generated. - _If you believe this is an issue on our side, please [open an issue](https://github.com/algolia/api-clients-automation/issues/new)_" + _If you believe this is an issue on our side, please [open an issue](https://github.com/algolia/api-clients-automation/issues/new?template=Bug_report.md)_" `); }); diff --git a/scripts/ci/codegen/pushGeneratedCode.ts b/scripts/ci/codegen/pushGeneratedCode.ts index c8179ecc9a..d69a5ffc71 100644 --- a/scripts/ci/codegen/pushGeneratedCode.ts +++ b/scripts/ci/codegen/pushGeneratedCode.ts @@ -3,7 +3,7 @@ import { run } from '../../common'; import { configureGitHubAuthor } from '../../release/common'; const PR_NUMBER = parseInt(process.env.PR_NUMBER || '0', 10); -const FOLDERS_TO_CHECK = 'clients specs/bundled'; +const FOLDERS_TO_CHECK = 'openapitools.json clients specs/bundled'; /** * Push generated code for the current `JOB` and `CLIENT` on a `generated/` branch. diff --git a/scripts/ci/codegen/text.ts b/scripts/ci/codegen/text.ts index d701ca0ef4..a2fab869c8 100644 --- a/scripts/ci/codegen/text.ts +++ b/scripts/ci/codegen/text.ts @@ -9,7 +9,7 @@ export default { }, noGen: { header: '✗ No code generated.', - body: `_If you believe this is an issue on our side, please [open an issue](${REPO_URL}/issues/new)_`, + body: `_If you believe this is an issue on our side, please [open an issue](${REPO_URL}/issues/new?template=Bug_report.md)_`, }, codegen: { header: '✔️ Code generated!', diff --git a/scripts/ci/codegen/upsertGenerationComment.ts b/scripts/ci/codegen/upsertGenerationComment.ts index 26adc0e42b..2a010dd546 100644 --- a/scripts/ci/codegen/upsertGenerationComment.ts +++ b/scripts/ci/codegen/upsertGenerationComment.ts @@ -16,7 +16,7 @@ const octokit = new Octokit({ const args = process.argv.slice(2); const allowedTriggers = ['notification', 'codegen', 'noGen']; -type Trigger = 'codegen' | 'noGen' | 'notification'; +type Trigger = keyof typeof commentText; export async function getCommentBody(trigger: Trigger): Promise { if (trigger === 'notification' || trigger === 'noGen') { From d7abc0436eafd4eadfa0897f6406831021fa8754 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Vannicatte?= Date: Wed, 16 Mar 2022 15:07:53 +0100 Subject: [PATCH 4/4] add cleanup trigger --- .github/workflows/codegen.yml | 6 ++++++ scripts/ci/codegen/__tests__/codegen.test.ts | 10 +++++++++- scripts/ci/codegen/text.ts | 6 +++++- scripts/ci/codegen/upsertGenerationComment.ts | 8 ++++++-- 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/.github/workflows/codegen.yml b/.github/workflows/codegen.yml index 44b5951f41..33a337b484 100644 --- a/.github/workflows/codegen.yml +++ b/.github/workflows/codegen.yml @@ -43,3 +43,9 @@ jobs: - name: Clean previously generated branch run: yarn workspace scripts cleanGeneratedBranch ${{ github.head_ref }} + + - name: Add cleanup comment + run: yarn workspace scripts upsertGenerationComment cleanup + env: + GITHUB_TOKEN: ${{ secrets.TOKEN_GENERATE_BOT }} + PR_NUMBER: ${{ github.event.number }} diff --git a/scripts/ci/codegen/__tests__/codegen.test.ts b/scripts/ci/codegen/__tests__/codegen.test.ts index 5c748b6eb3..0471c03441 100644 --- a/scripts/ci/codegen/__tests__/codegen.test.ts +++ b/scripts/ci/codegen/__tests__/codegen.test.ts @@ -58,7 +58,15 @@ describe('codegen', () => { expect(await getCommentBody('noGen')).toMatchInlineSnapshot(` "✗ No code generated. - _If you believe this is an issue on our side, please [open an issue](https://github.com/algolia/api-clients-automation/issues/new?template=Bug_report.md)_" + _If you believe this is an issue on our side, please [open an issue](https://github.com/algolia/api-clients-automation/issues/new?template=Bug_report.md)._" + `); + }); + + it('returns the right comment for a `cleanup` trigger', async () => { + expect(await getCommentBody('cleanup')).toMatchInlineSnapshot(` + "✗ The generated branch has been deleted. + + If the PR has been merged, you can check the generated code on the [\`generated/main\` branch](https://github.com/algolia/api-clients-automation/tree/generated/main)." `); }); diff --git a/scripts/ci/codegen/text.ts b/scripts/ci/codegen/text.ts index a2fab869c8..50611be8fe 100644 --- a/scripts/ci/codegen/text.ts +++ b/scripts/ci/codegen/text.ts @@ -9,7 +9,11 @@ export default { }, noGen: { header: '✗ No code generated.', - body: `_If you believe this is an issue on our side, please [open an issue](${REPO_URL}/issues/new?template=Bug_report.md)_`, + body: `_If you believe this is an issue on our side, please [open an issue](${REPO_URL}/issues/new?template=Bug_report.md)._`, + }, + cleanup: { + header: '✗ The generated branch has been deleted.', + body: `If the PR has been merged, you can check the generated code on the [\`generated/main\` branch](${REPO_URL}/tree/generated/main).`, }, codegen: { header: '✔️ Code generated!', diff --git a/scripts/ci/codegen/upsertGenerationComment.ts b/scripts/ci/codegen/upsertGenerationComment.ts index 2a010dd546..4fedf40689 100644 --- a/scripts/ci/codegen/upsertGenerationComment.ts +++ b/scripts/ci/codegen/upsertGenerationComment.ts @@ -14,12 +14,16 @@ const octokit = new Octokit({ }); const args = process.argv.slice(2); -const allowedTriggers = ['notification', 'codegen', 'noGen']; +const allowedTriggers = ['notification', 'codegen', 'noGen', 'cleanup']; type Trigger = keyof typeof commentText; export async function getCommentBody(trigger: Trigger): Promise { - if (trigger === 'notification' || trigger === 'noGen') { + if ( + trigger === 'notification' || + trigger === 'noGen' || + trigger === 'cleanup' + ) { return `${commentText[trigger].header} ${commentText[trigger].body}`;