From 2ba72c139eff0d264574a35dc0ee32a0de6a5cb8 Mon Sep 17 00:00:00 2001 From: Pierre Millot Date: Thu, 19 May 2022 11:40:59 +0200 Subject: [PATCH] chore(ci): close generation comment with last commit --- .github/workflows/check.yml | 1 - .github/workflows/codegen.yml | 6 ++-- scripts/ci/codegen/__tests__/codegen.test.ts | 12 ++++++-- scripts/ci/codegen/cleanGeneratedBranch.ts | 3 +- scripts/ci/codegen/text.ts | 23 ++++++++------- scripts/ci/codegen/upsertGenerationComment.ts | 29 +++++++------------ 6 files changed, 37 insertions(+), 37 deletions(-) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 1ba040ab5d..3ad09d5afe 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -316,7 +316,6 @@ jobs: - name: Push generated code id: pushGeneratedCode - if: ${{ needs.setup.outputs.RUN_CODEGEN == 'true' }} run: yarn workspace scripts pushGeneratedCode env: GITHUB_TOKEN: ${{ secrets.TOKEN_GENERATE_BOT }} diff --git a/.github/workflows/codegen.yml b/.github/workflows/codegen.yml index 33a337b484..0cf464513b 100644 --- a/.github/workflows/codegen.yml +++ b/.github/workflows/codegen.yml @@ -41,11 +41,11 @@ jobs: with: type: minimal - - 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 }} + + - 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 index b4aa42c3cc..3e726208e0 100644 --- a/scripts/ci/codegen/__tests__/codegen.test.ts +++ b/scripts/ci/codegen/__tests__/codegen.test.ts @@ -7,6 +7,11 @@ import { upsertGenerationComment, } from '../upsertGenerationComment'; +jest.mock('../../../common', () => ({ + ...(jest.requireActual('../../../common') as any), + run: jest.fn().mockResolvedValue('mocked'), +})); + describe('codegen', () => { describe('cleanGeneratedBranch', () => { it('throws without parameters', async () => { @@ -67,7 +72,8 @@ describe('codegen', () => { 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 [\`${MAIN_BRANCH}\` branch](https://github.com/algolia/api-clients-automation/tree/${MAIN_BRANCH})." + If the PR has been merged, you can check the generated code on the [\`${MAIN_BRANCH}\` branch](https://github.com/algolia/api-clients-automation/tree/${MAIN_BRANCH}). + You can still access the last generated [commit](https://github.com/algolia/api-clients-automation/commit/mocked)." `); }); @@ -75,10 +81,10 @@ describe('codegen', () => { it('creates a comment body for the parameters', () => { expect( commentText.codegen.body( + 'theGeneratedCommit', 'myBranch', 'myCommit', - 42, - 'theGeneratedCommit' + 42 ) ).toMatchInlineSnapshot(` " diff --git a/scripts/ci/codegen/cleanGeneratedBranch.ts b/scripts/ci/codegen/cleanGeneratedBranch.ts index c45a4a95f0..252ddbd0a1 100644 --- a/scripts/ci/codegen/cleanGeneratedBranch.ts +++ b/scripts/ci/codegen/cleanGeneratedBranch.ts @@ -26,8 +26,7 @@ export async function cleanGeneratedBranch(headRef: string): Promise { await run(`git push -d origin ${generatedCodeBranch}`); } -const args = process.argv.slice(2); - if (require.main === module) { + const args = process.argv.slice(2); cleanGeneratedBranch(args[0]); } diff --git a/scripts/ci/codegen/text.ts b/scripts/ci/codegen/text.ts index e90f8eb4a3..0859cee2d3 100644 --- a/scripts/ci/codegen/text.ts +++ b/scripts/ci/codegen/text.ts @@ -4,31 +4,34 @@ export default { commitStartMessage: 'chore: generated code for commit', 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._', + body: (): string => + '_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?template=Bug_report.md)._`, + body: (): string => + `_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 [\`${MAIN_BRANCH}\` branch](${REPO_URL}/tree/${MAIN_BRANCH}).`, + body: ( + generatedCommit: string + ): string => `If the PR has been merged, you can check the generated code on the [\`${MAIN_BRANCH}\` branch](${REPO_URL}/tree/${MAIN_BRANCH}). +You can still access the last generated [commit](${REPO_URL}/commit/${generatedCommit}).`, }, codegen: { header: '### ✔️ Code generated!', - body( + body: ( + generatedCommit: string, branch: string, commit: string, - eventNumber: number, - generatedCommit: string - ): string { - return ` + eventNumber: number + ): string => ` | Name | Link | |---------------------------------|------------------------| | 🔨 Triggered by | [\`${commit}\`](${REPO_URL}/pull/${eventNumber}/commits/${commit}) | | 🔍 Generated code | [\`${generatedCommit}\`](${REPO_URL}/commit/${generatedCommit}) | | 🌲 Generated branch | [\`${branch}\`](${REPO_URL}/tree/${branch}) | -`; - }, +`, }, }; diff --git a/scripts/ci/codegen/upsertGenerationComment.ts b/scripts/ci/codegen/upsertGenerationComment.ts index b5cc7c0009..f2d6c84a7a 100644 --- a/scripts/ci/codegen/upsertGenerationComment.ts +++ b/scripts/ci/codegen/upsertGenerationComment.ts @@ -9,23 +9,16 @@ 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 = ['notification', 'codegen', 'noGen', 'cleanup']; +const allowedTriggers = [ + 'notification', + 'codegen', + 'noGen', + 'cleanup', +] as const; -type Trigger = keyof typeof commentText; +type Trigger = typeof allowedTriggers[number]; export async function getCommentBody(trigger: Trigger): Promise { - // All of the case where we are not pushing generated code. - if ( - trigger === 'notification' || - trigger === 'noGen' || - trigger === 'cleanup' - ) { - return `${commentText[trigger].header} - -${commentText[trigger].body}`; - } - - // We are on a codegen step on a pull request here const generatedBranch = await run('git branch --show-current'); const baseBranch = generatedBranch.replace('generated/', ''); const baseCommit = await run(`git show ${baseBranch} -s --format=%H`); @@ -34,13 +27,13 @@ ${commentText[trigger].body}`; `git show ${generatedBranch} -s --format=%H` ); - return `${commentText.codegen.header} + return `${commentText[trigger].header} -${commentText.codegen.body( +${commentText[trigger].body( + generatedCommit, generatedBranch, baseCommit, - PR_NUMBER, - generatedCommit + PR_NUMBER )}`; }