Skip to content

Commit 96fddef

Browse files
authored
fix(scripts): codegen cleanup (#517)
1 parent 4bc1931 commit 96fddef

File tree

4 files changed

+48
-17
lines changed

4 files changed

+48
-17
lines changed

.github/workflows/codegen.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ jobs:
2525
env:
2626
GITHUB_TOKEN: ${{ secrets.TOKEN_GENERATE_BOT }}
2727
PR_NUMBER: ${{ github.event.number }}
28+
HEAD_BRANCH: ${{ github.head_ref }}
2829

2930
cleanup:
3031
runs-on: ubuntu-20.04

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

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
import { MAIN_BRANCH } from '../../../common';
1+
import * as common from '../../../common';
22
import { cleanGeneratedBranch } from '../cleanGeneratedBranch';
33
import { pushGeneratedCode } from '../pushGeneratedCode';
44
import commentText from '../text';
55
import {
6-
getCommentBody,
76
upsertGenerationComment,
7+
getCommentBody,
88
} from '../upsertGenerationComment';
99

10-
jest.mock('../../../common', () => ({
11-
...(jest.requireActual('../../../common') as any),
12-
run: jest.fn().mockResolvedValue('mocked'),
13-
}));
14-
1510
describe('codegen', () => {
1611
describe('cleanGeneratedBranch', () => {
1712
it('throws without parameters', async () => {
@@ -68,13 +63,41 @@ describe('codegen', () => {
6863
`);
6964
});
7065

71-
it('returns the right comment for a `cleanup` trigger', async () => {
72-
expect(await getCommentBody('cleanup')).toMatchInlineSnapshot(`
73-
"### ✗ The generated branch has been deleted.
66+
describe('cleanup', () => {
67+
let mockedResolvedValue: string;
68+
beforeEach(() => {
69+
jest.spyOn(common, 'run').mockImplementation(() => {
70+
return Promise.resolve(mockedResolvedValue);
71+
});
72+
});
73+
74+
afterEach(() => {
75+
jest.spyOn(common, 'run').mockRestore();
76+
});
77+
78+
afterEach(() => {});
79+
it('returns the right comment for a `cleanup` trigger', async () => {
80+
mockedResolvedValue = 'mocked';
7481

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)."
77-
`);
82+
expect(await getCommentBody('cleanup')).toMatchInlineSnapshot(`
83+
"### ✗ The generated branch has been deleted.
84+
85+
If the PR has been merged, you can check the generated code on the [\`${common.MAIN_BRANCH}\` branch](https://github.com/algolia/api-clients-automation/tree/${common.MAIN_BRANCH}).
86+
You can still access the code generated on \`mocked\` via [this commit](https://github.com/algolia/api-clients-automation/commit/mocked)."
87+
`);
88+
});
89+
90+
it('fallbacks to the env variable HEAD_BRANCH if found when we are on `main`', async () => {
91+
process.env.HEAD_BRANCH = 'myFakeBranch';
92+
mockedResolvedValue = 'main';
93+
94+
expect(await getCommentBody('cleanup')).toMatchInlineSnapshot(`
95+
"### ✗ The generated branch has been deleted.
96+
97+
If the PR has been merged, you can check the generated code on the [\`${common.MAIN_BRANCH}\` branch](https://github.com/algolia/api-clients-automation/tree/${common.MAIN_BRANCH}).
98+
You can still access the code generated on \`generated/myFakeBranch\` via [this commit](https://github.com/algolia/api-clients-automation/commit/main)."
99+
`);
100+
});
78101
});
79102

80103
describe('text', () => {

scripts/ci/codegen/text.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ export default {
1515
cleanup: {
1616
header: '### ✗ The generated branch has been deleted.',
1717
body: (
18-
generatedCommit: string
18+
generatedCommit: string,
19+
branch: string
1920
): 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}).`,
21+
You can still access the code generated on \`${branch}\` via [this commit](${REPO_URL}/commit/${generatedCommit}).`,
2122
},
2223
codegen: {
2324
header: '### ✔️ Code generated!',

scripts/ci/codegen/upsertGenerationComment.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,16 @@ const allowedTriggers = [
1919
type Trigger = typeof allowedTriggers[number];
2020

2121
export async function getCommentBody(trigger: Trigger): Promise<string> {
22-
const generatedBranch = await run('git branch --show-current');
22+
let generatedBranch = await run('git branch --show-current');
23+
24+
// `cleanup` is triggered on PR close, which runs on `main`, so we lose the
25+
// branch name context at this point
26+
if (generatedBranch === 'main' && process.env.HEAD_BRANCH) {
27+
generatedBranch = `generated/${process.env.HEAD_BRANCH}`;
28+
}
29+
2330
const baseBranch = generatedBranch.replace('generated/', '');
2431
const baseCommit = await run(`git show ${baseBranch} -s --format=%H`);
25-
2632
const generatedCommit = await run(
2733
`git show ${generatedBranch} -s --format=%H`
2834
);

0 commit comments

Comments
 (0)