Skip to content
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

fix(scripts): codegen cleanup #517

Merged
merged 1 commit into from
May 19, 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
1 change: 1 addition & 0 deletions .github/workflows/codegen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.TOKEN_GENERATE_BOT }}
PR_NUMBER: ${{ github.event.number }}
HEAD_BRANCH: ${{ github.head_ref }}

cleanup:
runs-on: ubuntu-20.04
Expand Down
49 changes: 36 additions & 13 deletions scripts/ci/codegen/__tests__/codegen.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import { MAIN_BRANCH } from '../../../common';
import * as common from '../../../common';
import { cleanGeneratedBranch } from '../cleanGeneratedBranch';
import { pushGeneratedCode } from '../pushGeneratedCode';
import commentText from '../text';
import {
getCommentBody,
upsertGenerationComment,
getCommentBody,
} from '../upsertGenerationComment';

jest.mock('../../../common', () => ({
...(jest.requireActual('../../../common') as any),
run: jest.fn().mockResolvedValue('mocked'),
}));

describe('codegen', () => {
describe('cleanGeneratedBranch', () => {
it('throws without parameters', async () => {
Expand Down Expand Up @@ -68,13 +63,41 @@ describe('codegen', () => {
`);
});

it('returns the right comment for a `cleanup` trigger', async () => {
expect(await getCommentBody('cleanup')).toMatchInlineSnapshot(`
"### ✗ The generated branch has been deleted.
describe('cleanup', () => {
let mockedResolvedValue: string;
beforeEach(() => {
jest.spyOn(common, 'run').mockImplementation(() => {
return Promise.resolve(mockedResolvedValue);
});
Comment on lines +69 to +71
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
jest.spyOn(common, 'run').mockImplementation(() => {
return Promise.resolve(mockedResolvedValue);
});
jest.spyOn(common, 'run').mockResolvedValue(mockedResolvedValue);

Copy link
Member Author

Choose a reason for hiding this comment

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

idk why I did not managed to make it work at first so went with the non sugar implem

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah yes it's because the mockedResolvedValue variable changes and need to be reevaluated everytime, while mockResolvedValue function only evaluate at the start.

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 make sense

});

afterEach(() => {
jest.spyOn(common, 'run').mockRestore();
});

afterEach(() => {});
it('returns the right comment for a `cleanup` trigger', async () => {
mockedResolvedValue = 'mocked';

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)."
`);
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 [\`${common.MAIN_BRANCH}\` branch](https://github.com/algolia/api-clients-automation/tree/${common.MAIN_BRANCH}).
You can still access the code generated on \`mocked\` via [this commit](https://github.com/algolia/api-clients-automation/commit/mocked)."
`);
});

it('fallbacks to the env variable HEAD_BRANCH if found when we are on `main`', async () => {
process.env.HEAD_BRANCH = 'myFakeBranch';
mockedResolvedValue = 'main';

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 [\`${common.MAIN_BRANCH}\` branch](https://github.com/algolia/api-clients-automation/tree/${common.MAIN_BRANCH}).
You can still access the code generated on \`generated/myFakeBranch\` via [this commit](https://github.com/algolia/api-clients-automation/commit/main)."
`);
});
});

describe('text', () => {
Expand Down
5 changes: 3 additions & 2 deletions scripts/ci/codegen/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ export default {
cleanup: {
header: '### ✗ The generated branch has been deleted.',
body: (
generatedCommit: string
generatedCommit: string,
branch: 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}).`,
You can still access the code generated on \`${branch}\` via [this commit](${REPO_URL}/commit/${generatedCommit}).`,
},
codegen: {
header: '### ✔️ Code generated!',
Expand Down
10 changes: 8 additions & 2 deletions scripts/ci/codegen/upsertGenerationComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ const allowedTriggers = [
type Trigger = typeof allowedTriggers[number];

export async function getCommentBody(trigger: Trigger): Promise<string> {
const generatedBranch = await run('git branch --show-current');
let generatedBranch = await run('git branch --show-current');

// `cleanup` is triggered on PR close, which runs on `main`, so we lose the
// branch name context at this point
if (generatedBranch === 'main' && process.env.HEAD_BRANCH) {
generatedBranch = `generated/${process.env.HEAD_BRANCH}`;
}

const baseBranch = generatedBranch.replace('generated/', '');
const baseCommit = await run(`git show ${baseBranch} -s --format=%H`);

const generatedCommit = await run(
`git show ${generatedBranch} -s --format=%H`
);
Expand Down