Skip to content

chore: make spread codegen commit cleaner #412

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

Merged
merged 1 commit into from
Apr 21, 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
10 changes: 10 additions & 0 deletions scripts/ci/codegen/__tests__/spreadGeneration.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { LANGUAGES } from '../../../common';
import { decideWhereToSpread, cleanUpCommitMessage } from '../spreadGeneration';
import text from '../text';

describe('spread generation', () => {
it('skips in case of release commit', () => {
Expand Down Expand Up @@ -45,4 +46,13 @@ describe('spread generation', () => {
https://github.com/algolia/api-clients-automation/pull/200"
`);
});

it('provides a link to the automation repo for commit with hash', () => {
const commitMessage = `${text.commitStartMessage} ed33e02f3e45fd72b4f420a56e4be7c6929fca9f. [skip ci]`;
expect(cleanUpCommitMessage(commitMessage)).toMatchInlineSnapshot(`
"chore: generated code for commit ed33e02f. [skip ci]

https://github.com/algolia/api-clients-automation/commit/ed33e02f3e45fd72b4f420a56e4be7c6929fca9f"
`);
});
});
9 changes: 5 additions & 4 deletions scripts/ci/codegen/pushGeneratedCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { MAIN_BRANCH, run } from '../../common';
import { configureGitHubAuthor } from '../../release/common';
import { getNbGitDiff } from '../utils';

import text from './text';

const PR_NUMBER = parseInt(process.env.PR_NUMBER || '0', 10);
const FOLDERS_TO_CHECK = 'yarn.lock openapitools.json clients specs/bundled';

Expand Down Expand Up @@ -48,10 +50,9 @@ export async function pushGeneratedCode(): Promise<void> {
await run(`git checkout -b ${branchToPush}`);
}

const commitMessage =
await run(`git show -s ${baseBranch} --format="chore: generated code for commit %H. ${
isMainBranch ? '[skip ci]' : ''
}
const commitMessage = await run(`git show -s ${baseBranch} --format="${
text.commitStartMessage
} %H. ${isMainBranch ? '[skip ci]' : ''}

Co-authored-by: %an <%ae>
%(trailers:key=Co-authored-by)"`);
Expand Down
27 changes: 24 additions & 3 deletions scripts/ci/codegen/spreadGeneration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { getLanguageFolder } from '../../config';
import { cloneRepository, configureGitHubAuthor } from '../../release/common';
import { getNbGitDiff } from '../utils';

import text from './text';

export function decideWhereToSpread(commitMessage: string): string[] {
if (commitMessage.startsWith('chore: release')) {
return [];
Expand All @@ -29,12 +31,31 @@ export function decideWhereToSpread(commitMessage: string): string[] {
}

export function cleanUpCommitMessage(commitMessage: string): string {
const result = commitMessage.match(/(.+)\s\(#(\d+)\)$/);
if (!result) {
const isCodeGenCommit = commitMessage.startsWith(text.commitStartMessage);

if (isCodeGenCommit) {
const hash = commitMessage
.split(text.commitStartMessage)[1]
Copy link
Collaborator

Choose a reason for hiding this comment

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

won't that split on spaces by default ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry I'm not sure to understand, could you elaborate pls? D:

Copy link
Collaborator

Choose a reason for hiding this comment

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

for me this split will juste return generated because you split on spaces

Copy link
Collaborator

Choose a reason for hiding this comment

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

ah no ok that's a weird use of split ahah, I get it

Copy link
Member Author

Choose a reason for hiding this comment

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

I split on text.commitStartMessage which is chore: generated code for commit

Copy link
Member Author

Choose a reason for hiding this comment

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

I can change it to a regex if you prefer but it seemed easy enough like this

.replace('. [skip ci]', '')
.trim();

if (!hash) {
return commitMessage;
}

return [
`${text.commitStartMessage} ${hash.substring(0, 8)}. [skip ci]`,
`${REPO_URL}/commit/${hash}`,
].join('\n\n');
}

const prCommit = commitMessage.match(/(.+)\s\(#(\d+)\)$/);

if (!prCommit) {
return commitMessage;
}

return [result[1], `${REPO_URL}/pull/${result[2]}`].join('\n\n');
return [prCommit[1], `${REPO_URL}/pull/${prCommit[2]}`].join('\n\n');
}

async function spreadGeneration(): Promise<void> {
Expand Down
1 change: 1 addition & 0 deletions scripts/ci/codegen/text.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { MAIN_BRANCH, REPO_URL } from '../../common';

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._',
Expand Down