Skip to content

Commit

Permalink
chore(ci): close generation comment with last commit
Browse files Browse the repository at this point in the history
  • Loading branch information
millotp committed May 19, 2022
1 parent c971410 commit 2ba72c1
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 37 deletions.
1 change: 0 additions & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,6 @@ jobs:

- name: Push generated code
id: pushGeneratedCode
if: ${{ needs.setup.outputs.RUN_CODEGEN == 'true' }}
run: yarn workspace scripts pushGeneratedCode
env:
GITHUB_TOKEN: ${{ secrets.TOKEN_GENERATE_BOT }}
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/codegen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ jobs:
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 }}

- name: Clean previously generated branch
run: yarn workspace scripts cleanGeneratedBranch ${{ github.head_ref }}
12 changes: 9 additions & 3 deletions scripts/ci/codegen/__tests__/codegen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import {
upsertGenerationComment,
} 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 @@ -67,18 +72,19 @@ describe('codegen', () => {
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 [\`${MAIN_BRANCH}\` branch](https://github.com/algolia/api-clients-automation/tree/${MAIN_BRANCH})."
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)."
`);
});

describe('text', () => {
it('creates a comment body for the parameters', () => {
expect(
commentText.codegen.body(
'theGeneratedCommit',
'myBranch',
'myCommit',
42,
'theGeneratedCommit'
42
)
).toMatchInlineSnapshot(`
"
Expand Down
3 changes: 1 addition & 2 deletions scripts/ci/codegen/cleanGeneratedBranch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ export async function cleanGeneratedBranch(headRef: string): Promise<void> {
await run(`git push -d origin ${generatedCodeBranch}`);
}

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

if (require.main === module) {
const args = process.argv.slice(2);
cleanGeneratedBranch(args[0]);
}
23 changes: 13 additions & 10 deletions scripts/ci/codegen/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,34 @@ export default {
commitStartMessage: 'chore: generated code for commit',
notification: {
header: '### 🔨 The codegen job will run at the end of the CI.',
body: '_Make sure your last commit does not contains generated code, it will be automatically pushed by our CI._',
body: (): string =>
'_Make sure your last commit does not contains generated code, it will be automatically pushed by our CI._',
},
noGen: {
header: '### ✗ No code generated.',
body: `_If you believe this is an issue on our side, please [open an issue](${REPO_URL}/issues/new?template=Bug_report.md)._`,
body: (): string =>
`_If you believe this is an issue on our side, please [open an issue](${REPO_URL}/issues/new?template=Bug_report.md)._`,
},
cleanup: {
header: '### ✗ The generated branch has been deleted.',
body: `If the PR has been merged, you can check the generated code on the [\`${MAIN_BRANCH}\` branch](${REPO_URL}/tree/${MAIN_BRANCH}).`,
body: (
generatedCommit: 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}).`,
},
codegen: {
header: '### ✔️ Code generated!',
body(
body: (
generatedCommit: string,
branch: string,
commit: string,
eventNumber: number,
generatedCommit: string
): string {
return `
eventNumber: number
): string => `
| Name | Link |
|---------------------------------|------------------------|
| 🔨 Triggered by | [\`${commit}\`](${REPO_URL}/pull/${eventNumber}/commits/${commit}) |
| 🔍 Generated code | [\`${generatedCommit}\`](${REPO_URL}/commit/${generatedCommit}) |
| 🌲 Generated branch | [\`${branch}\`](${REPO_URL}/tree/${branch}) |
`;
},
`,
},
};
29 changes: 11 additions & 18 deletions scripts/ci/codegen/upsertGenerationComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,16 @@ const PR_NUMBER = parseInt(process.env.PR_NUMBER || '0', 10);
const octokit = getOctokit(process.env.GITHUB_TOKEN!);

const args = process.argv.slice(2);
const allowedTriggers = ['notification', 'codegen', 'noGen', 'cleanup'];
const allowedTriggers = [
'notification',
'codegen',
'noGen',
'cleanup',
] as const;

type Trigger = keyof typeof commentText;
type Trigger = typeof allowedTriggers[number];

export async function getCommentBody(trigger: Trigger): Promise<string> {
// All of the case where we are not pushing generated code.
if (
trigger === 'notification' ||
trigger === 'noGen' ||
trigger === 'cleanup'
) {
return `${commentText[trigger].header}
${commentText[trigger].body}`;
}

// We are on a codegen step on a pull request here
const generatedBranch = await run('git branch --show-current');
const baseBranch = generatedBranch.replace('generated/', '');
const baseCommit = await run(`git show ${baseBranch} -s --format=%H`);
Expand All @@ -34,13 +27,13 @@ ${commentText[trigger].body}`;
`git show ${generatedBranch} -s --format=%H`
);

return `${commentText.codegen.header}
return `${commentText[trigger].header}
${commentText.codegen.body(
${commentText[trigger].body(
generatedCommit,
generatedBranch,
baseCommit,
PR_NUMBER,
generatedCommit
PR_NUMBER
)}`;
}

Expand Down

0 comments on commit 2ba72c1

Please sign in to comment.