Skip to content

Commit 3faaf4f

Browse files
authored
fix(ci): make generation comment clearer (#258)
1 parent 8bc743e commit 3faaf4f

File tree

9 files changed

+313
-114
lines changed

9 files changed

+313
-114
lines changed

.github/actions/setup/action.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ name: Setup
22

33
description: Setup CI environment.
44

5+
inputs:
6+
type:
7+
description: Type of setup, `minimal` will only run the JavaScript installation.
8+
required: false
9+
510
runs:
611
using: composite
712
steps:
@@ -10,13 +15,15 @@ runs:
1015
run: echo "CACHE_VERSION=$(< .github/.cache_version)" >> $GITHUB_ENV
1116

1217
- name: Install Java
18+
if: inputs.type != 'minimal'
1319
uses: actions/setup-java@v2
1420
with:
1521
distribution: zulu
1622
java-version: 11.0.4
1723
cache: gradle
1824

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

2229
- name: Install Node
@@ -29,6 +36,7 @@ runs:
2936
uses: ./.github/actions/cache
3037

3138
- name: Setting diff outputs variables
39+
if: inputs.type != 'minimal'
3240
id: diff
3341
shell: bash
3442
run: |
@@ -61,6 +69,7 @@ runs:
6169
echo "::set-output name=ORIGIN_BRANCH::$origin"
6270
6371
- name: Compute specs matrix
72+
if: inputs.type != 'minimal'
6473
id: spec-matrix
6574
shell: bash
6675
run: |
@@ -81,6 +90,7 @@ runs:
8190
echo "::set-output name=RUN_SPECS::$run"
8291
8392
- name: Compute the JS client build matrix
93+
if: inputs.type != 'minimal'
8494
id: js-matrix
8595
shell: bash
8696
run: |
@@ -117,6 +127,7 @@ runs:
117127
fi
118128
119129
- name: Compute the Java client build matrix
130+
if: inputs.type != 'minimal'
120131
id: java-matrix
121132
shell: bash
122133
run: |
@@ -137,6 +148,7 @@ runs:
137148
echo "::set-output name=RUN_CLIENT::$run"
138149
139150
- name: Compute the PHP client build matrix
151+
if: inputs.type != 'minimal'
140152
id: php-matrix
141153
shell: bash
142154
run: |

.github/workflows/codegen-cleanup.yml

Lines changed: 0 additions & 21 deletions
This file was deleted.

.github/workflows/codegen.yml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: Codegen
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, closed]
6+
7+
jobs:
8+
notification:
9+
runs-on: ubuntu-20.04
10+
timeout-minutes: 10
11+
if: (github.event.action == 'opened' || github.event.action == 'synchronize') && github.event.number
12+
steps:
13+
- uses: actions/checkout@v2
14+
with:
15+
fetch-depth: 0
16+
ref: ${{ github.event.pull_request.head.ref }}
17+
18+
- name: Setup
19+
uses: ./.github/actions/setup
20+
with:
21+
type: minimal
22+
23+
- name: Add notification comment
24+
run: yarn workspace scripts upsertGenerationComment notification
25+
env:
26+
GITHUB_TOKEN: ${{ secrets.TOKEN_GENERATE_BOT }}
27+
PR_NUMBER: ${{ github.event.number }}
28+
29+
cleanup:
30+
runs-on: ubuntu-20.04
31+
timeout-minutes: 10
32+
if: github.event.action == 'closed'
33+
steps:
34+
- uses: actions/checkout@v2
35+
with:
36+
fetch-depth: 0
37+
ref: main
38+
39+
- name: Setup
40+
uses: ./.github/actions/setup
41+
with:
42+
type: minimal
43+
44+
- name: Clean previously generated branch
45+
run: yarn workspace scripts cleanGeneratedBranch ${{ github.head_ref }}
46+
47+
- name: Add cleanup comment
48+
run: yarn workspace scripts upsertGenerationComment cleanup
49+
env:
50+
GITHUB_TOKEN: ${{ secrets.TOKEN_GENERATE_BOT }}
51+
PR_NUMBER: ${{ github.event.number }}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { cleanGeneratedBranch } from '../cleanGeneratedBranch';
2+
import { pushGeneratedCode } from '../pushGeneratedCode';
3+
import commentText from '../text';
4+
import {
5+
getCommentBody,
6+
upsertGenerationComment,
7+
} from '../upsertGenerationComment';
8+
9+
describe('codegen', () => {
10+
describe('cleanGeneratedBranch', () => {
11+
it('throws without parameters', async () => {
12+
await expect(
13+
// @ts-expect-error a parameter is required
14+
cleanGeneratedBranch()
15+
).rejects.toThrow(
16+
'The base branch should be passed as a cli parameter of the `cleanGeneratedBranch` script.'
17+
);
18+
});
19+
});
20+
21+
describe('pushGeneratedCode', () => {
22+
it('throws without GITHUB_TOKEN environment variable', async () => {
23+
await expect(pushGeneratedCode()).rejects.toThrow(
24+
'Environment variable `GITHUB_TOKEN` does not exist.'
25+
);
26+
});
27+
});
28+
29+
describe('upsertGenerationComment', () => {
30+
it('throws without parameter', async () => {
31+
await expect(
32+
// @ts-expect-error a parameter is required
33+
upsertGenerationComment()
34+
).rejects.toThrow(
35+
'`upsertGenerationComment` requires a `trigger` parameter (`codegen` | `notification`).'
36+
);
37+
});
38+
39+
it('throws without PR_NUMBER environment variable', async () => {
40+
process.env.GITHUB_TOKEN = 'foo';
41+
42+
await expect(upsertGenerationComment('codegen')).rejects.toThrow(
43+
'`upsertGenerationComment` requires a `PR_NUMBER` environment variable.'
44+
);
45+
});
46+
});
47+
48+
describe('getCommentBody', () => {
49+
it('returns the right comment for a `notification` trigger', async () => {
50+
expect(await getCommentBody('notification')).toMatchInlineSnapshot(`
51+
"🔨 The codegen job will run at the end of the CI.
52+
53+
_Make sure your last commit does not contains generated code, it will be automatically pushed by our CI._"
54+
`);
55+
});
56+
57+
it('returns the right comment for a `noGen` trigger', async () => {
58+
expect(await getCommentBody('noGen')).toMatchInlineSnapshot(`
59+
"✗ No code generated.
60+
61+
_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)._"
62+
`);
63+
});
64+
65+
it('returns the right comment for a `cleanup` trigger', async () => {
66+
expect(await getCommentBody('cleanup')).toMatchInlineSnapshot(`
67+
"✗ The generated branch has been deleted.
68+
69+
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)."
70+
`);
71+
});
72+
73+
describe('text', () => {
74+
it('creates a comment body for the parameters', () => {
75+
expect(
76+
commentText.codegen.body(
77+
'myBranch',
78+
'myCommit',
79+
42,
80+
'theGeneratedCommit'
81+
)
82+
).toMatchInlineSnapshot(`
83+
"🔨 Triggered by commit [myCommit](https://github.com/algolia/api-clients-automation/pull/42/commits/myCommit).
84+
🔍 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)."
85+
`);
86+
});
87+
});
88+
});
89+
});

scripts/ci/codegen/cleanGeneratedBranch.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import { run } from '../../common';
44
/**
55
* Deletes a branch for its `generated/${headRef}` name on origin.
66
*/
7-
async function cleanGeneratedBranch(headRef: string): Promise<void> {
7+
export async function cleanGeneratedBranch(headRef: string): Promise<void> {
8+
if (!headRef) {
9+
throw new Error(
10+
'The base branch should be passed as a cli parameter of the `cleanGeneratedBranch` script.'
11+
);
12+
}
13+
814
const generatedCodeBranch = `generated/${headRef}`;
915

1016
if (!(await run(`git ls-remote --heads origin ${generatedCodeBranch}`))) {
@@ -22,10 +28,6 @@ async function cleanGeneratedBranch(headRef: string): Promise<void> {
2228

2329
const args = process.argv.slice(2);
2430

25-
if (!args || args.length === 0) {
26-
throw new Error(
27-
'The base branch should be passed as a cli parameter of the `cleanGeneratedBranch` script.'
28-
);
31+
if (require.main === module) {
32+
cleanGeneratedBranch(args[0]);
2933
}
30-
31-
cleanGeneratedBranch(args[0]);

0 commit comments

Comments
 (0)