Skip to content

fix(ci): make generation comment clearer #258

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

Merged
merged 4 commits into from
Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 12 additions & 0 deletions .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ name: Setup

description: Setup CI environment.

inputs:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Because this file is quite different with minimal, is it worth it to create another file instead ?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure how nested composite would behave but splitting the setup file would definitely make sense. Might be worth investigating that

type:
description: Type of setup, `minimal` will only run the JavaScript installation.
required: false

runs:
using: composite
steps:
Expand All @@ -10,13 +15,15 @@ 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
java-version: 11.0.4
cache: gradle

- name: Validate gradle wrapper
if: inputs.type != 'minimal'
uses: gradle/wrapper-validation-action@v1

- name: Install Node
Expand All @@ -29,6 +36,7 @@ runs:
uses: ./.github/actions/cache

- name: Setting diff outputs variables
if: inputs.type != 'minimal'
id: diff
shell: bash
run: |
Expand Down Expand Up @@ -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: |
Expand All @@ -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: |
Expand Down Expand Up @@ -117,6 +127,7 @@ runs:
fi

- name: Compute the Java client build matrix
if: inputs.type != 'minimal'
id: java-matrix
shell: bash
run: |
Expand All @@ -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: |
Expand Down
21 changes: 0 additions & 21 deletions .github/workflows/codegen-cleanup.yml

This file was deleted.

51 changes: 51 additions & 0 deletions .github/workflows/codegen.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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 }}

- name: Add cleanup comment
run: yarn workspace scripts upsertGenerationComment cleanup
env:
GITHUB_TOKEN: ${{ secrets.TOKEN_GENERATE_BOT }}
PR_NUMBER: ${{ github.event.number }}
89 changes: 89 additions & 0 deletions scripts/ci/codegen/__tests__/codegen.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { cleanGeneratedBranch } from '../cleanGeneratedBranch';
Copy link
Collaborator

Choose a reason for hiding this comment

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

Once again you should not import files with global code, you can wrap the global code in an if like that

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah yes I just did in the last commit actually to follow what Eunjae did on the other tests

Copy link
Collaborator

Choose a reason for hiding this comment

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

cool ! That way you can also call directly the function instead of using await run('yarn workspace scripts...

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
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can pass undefined to remove the @ts-expect-error

Copy link
Member Author

Choose a reason for hiding this comment

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

It wouldn't be accurate as we actually don't accept undefined, it's only here to make sure it throws

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?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)."
`);
});

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)."
`);
});
});
});
});
16 changes: 9 additions & 7 deletions scripts/ci/codegen/cleanGeneratedBranch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import { run } from '../../common';
/**
* Deletes a branch for its `generated/${headRef}` name on origin.
*/
async function cleanGeneratedBranch(headRef: string): Promise<void> {
export async function cleanGeneratedBranch(headRef: string): Promise<void> {
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}`))) {
Expand All @@ -22,10 +28,6 @@ async function cleanGeneratedBranch(headRef: string): Promise<void> {

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.'
);
if (require.main === module) {
cleanGeneratedBranch(args[0]);
}

cleanGeneratedBranch(args[0]);
Loading