Skip to content

Commit 22f77f2

Browse files
millotpshortcuts
andauthored
chore(ci): close generation comment with last commit (#513)
Co-authored-by: Clément Vannicatte <[email protected]>
1 parent 3f9309f commit 22f77f2

File tree

6 files changed

+38
-38
lines changed

6 files changed

+38
-38
lines changed

.github/workflows/check.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,6 @@ jobs:
316316

317317
- name: Push generated code
318318
id: pushGeneratedCode
319-
if: ${{ needs.setup.outputs.RUN_CODEGEN == 'true' }}
320319
run: yarn workspace scripts pushGeneratedCode
321320
env:
322321
GITHUB_TOKEN: ${{ secrets.TOKEN_GENERATE_BOT }}

.github/workflows/codegen.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ jobs:
4141
with:
4242
type: minimal
4343

44-
- name: Clean previously generated branch
45-
run: yarn workspace scripts cleanGeneratedBranch ${{ github.head_ref }}
46-
4744
- name: Add cleanup comment
4845
run: yarn workspace scripts upsertGenerationComment cleanup
4946
env:
5047
GITHUB_TOKEN: ${{ secrets.TOKEN_GENERATE_BOT }}
5148
PR_NUMBER: ${{ github.event.number }}
49+
50+
- name: Clean previously generated branch
51+
run: yarn workspace scripts cleanGeneratedBranch ${{ github.head_ref }}

scripts/ci/codegen/__tests__/codegen.test.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ import {
77
upsertGenerationComment,
88
} from '../upsertGenerationComment';
99

10+
jest.mock('../../../common', () => ({
11+
...(jest.requireActual('../../../common') as any),
12+
run: jest.fn().mockResolvedValue('mocked'),
13+
}));
14+
1015
describe('codegen', () => {
1116
describe('cleanGeneratedBranch', () => {
1217
it('throws without parameters', async () => {
@@ -51,7 +56,7 @@ describe('codegen', () => {
5156
expect(await getCommentBody('notification')).toMatchInlineSnapshot(`
5257
"### 🔨 The codegen job will run at the end of the CI.
5358
54-
_Make sure your last commit does not contains generated code, it will be automatically pushed by our CI._"
59+
_Make sure your last commit does not contain generated code, it will be automatically pushed by our CI._"
5560
`);
5661
});
5762

@@ -67,18 +72,19 @@ describe('codegen', () => {
6772
expect(await getCommentBody('cleanup')).toMatchInlineSnapshot(`
6873
"### ✗ The generated branch has been deleted.
6974
70-
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})."
75+
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}).
76+
You can still access [the last generated commit](https://github.com/algolia/api-clients-automation/commit/mocked)."
7177
`);
7278
});
7379

7480
describe('text', () => {
7581
it('creates a comment body for the parameters', () => {
7682
expect(
7783
commentText.codegen.body(
84+
'theGeneratedCommit',
7885
'myBranch',
7986
'myCommit',
80-
42,
81-
'theGeneratedCommit'
87+
42
8288
)
8389
).toMatchInlineSnapshot(`
8490
"

scripts/ci/codegen/cleanGeneratedBranch.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ export async function cleanGeneratedBranch(headRef: string): Promise<void> {
2626
await run(`git push -d origin ${generatedCodeBranch}`);
2727
}
2828

29-
const args = process.argv.slice(2);
30-
3129
if (require.main === module) {
30+
const args = process.argv.slice(2);
3231
cleanGeneratedBranch(args[0]);
3332
}

scripts/ci/codegen/text.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,34 @@ export default {
44
commitStartMessage: 'chore: generated code for commit',
55
notification: {
66
header: '### 🔨 The codegen job will run at the end of the CI.',
7-
body: '_Make sure your last commit does not contains generated code, it will be automatically pushed by our CI._',
7+
body: (): string =>
8+
'_Make sure your last commit does not contain generated code, it will be automatically pushed by our CI._',
89
},
910
noGen: {
1011
header: '### ✗ No code generated.',
11-
body: `_If you believe this is an issue on our side, please [open an issue](${REPO_URL}/issues/new?template=Bug_report.md)._`,
12+
body: (): string =>
13+
`_If you believe this is an issue on our side, please [open an issue](${REPO_URL}/issues/new?template=Bug_report.md)._`,
1214
},
1315
cleanup: {
1416
header: '### ✗ The generated branch has been deleted.',
15-
body: `If the PR has been merged, you can check the generated code on the [\`${MAIN_BRANCH}\` branch](${REPO_URL}/tree/${MAIN_BRANCH}).`,
17+
body: (
18+
generatedCommit: string
19+
): string => `If the PR has been merged, you can check the generated code on the [\`${MAIN_BRANCH}\` branch](${REPO_URL}/tree/${MAIN_BRANCH}).
20+
You can still access [the last generated commit](${REPO_URL}/commit/${generatedCommit}).`,
1621
},
1722
codegen: {
1823
header: '### ✔️ Code generated!',
19-
body(
24+
body: (
25+
generatedCommit: string,
2026
branch: string,
2127
commit: string,
22-
eventNumber: number,
23-
generatedCommit: string
24-
): string {
25-
return `
28+
eventNumber: number
29+
): string => `
2630
| Name | Link |
2731
|---------------------------------|------------------------|
2832
| 🔨 Triggered by | [\`${commit}\`](${REPO_URL}/pull/${eventNumber}/commits/${commit}) |
2933
| 🔍 Generated code | [\`${generatedCommit}\`](${REPO_URL}/commit/${generatedCommit}) |
3034
| 🌲 Generated branch | [\`${branch}\`](${REPO_URL}/tree/${branch}) |
31-
`;
32-
},
35+
`,
3336
},
3437
};

scripts/ci/codegen/upsertGenerationComment.ts

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,16 @@ const PR_NUMBER = parseInt(process.env.PR_NUMBER || '0', 10);
99
const octokit = getOctokit(process.env.GITHUB_TOKEN!);
1010

1111
const args = process.argv.slice(2);
12-
const allowedTriggers = ['notification', 'codegen', 'noGen', 'cleanup'];
12+
const allowedTriggers = [
13+
'notification',
14+
'codegen',
15+
'noGen',
16+
'cleanup',
17+
] as const;
1318

14-
type Trigger = keyof typeof commentText;
19+
type Trigger = typeof allowedTriggers[number];
1520

1621
export async function getCommentBody(trigger: Trigger): Promise<string> {
17-
// All of the case where we are not pushing generated code.
18-
if (
19-
trigger === 'notification' ||
20-
trigger === 'noGen' ||
21-
trigger === 'cleanup'
22-
) {
23-
return `${commentText[trigger].header}
24-
25-
${commentText[trigger].body}`;
26-
}
27-
28-
// We are on a codegen step on a pull request here
2922
const generatedBranch = await run('git branch --show-current');
3023
const baseBranch = generatedBranch.replace('generated/', '');
3124
const baseCommit = await run(`git show ${baseBranch} -s --format=%H`);
@@ -34,13 +27,13 @@ ${commentText[trigger].body}`;
3427
`git show ${generatedBranch} -s --format=%H`
3528
);
3629

37-
return `${commentText.codegen.header}
30+
return `${commentText[trigger].header}
3831
39-
${commentText.codegen.body(
32+
${commentText[trigger].body(
33+
generatedCommit,
4034
generatedBranch,
4135
baseCommit,
42-
PR_NUMBER,
43-
generatedCommit
36+
PR_NUMBER
4437
)}`;
4538
}
4639

0 commit comments

Comments
 (0)