Skip to content

Commit be213f8

Browse files
authored
chore: make spread codegen commit cleaner (#412)
1 parent f360137 commit be213f8

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { LANGUAGES } from '../../../common';
22
import { decideWhereToSpread, cleanUpCommitMessage } from '../spreadGeneration';
3+
import text from '../text';
34

45
describe('spread generation', () => {
56
it('skips in case of release commit', () => {
@@ -45,4 +46,13 @@ describe('spread generation', () => {
4546
https://github.com/algolia/api-clients-automation/pull/200"
4647
`);
4748
});
49+
50+
it('provides a link to the automation repo for commit with hash', () => {
51+
const commitMessage = `${text.commitStartMessage} ed33e02f3e45fd72b4f420a56e4be7c6929fca9f. [skip ci]`;
52+
expect(cleanUpCommitMessage(commitMessage)).toMatchInlineSnapshot(`
53+
"chore: generated code for commit ed33e02f. [skip ci]
54+
55+
https://github.com/algolia/api-clients-automation/commit/ed33e02f3e45fd72b4f420a56e4be7c6929fca9f"
56+
`);
57+
});
4858
});

scripts/ci/codegen/pushGeneratedCode.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { MAIN_BRANCH, run } from '../../common';
33
import { configureGitHubAuthor } from '../../release/common';
44
import { getNbGitDiff } from '../utils';
55

6+
import text from './text';
7+
68
const PR_NUMBER = parseInt(process.env.PR_NUMBER || '0', 10);
79
const FOLDERS_TO_CHECK = 'yarn.lock openapitools.json clients specs/bundled';
810

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

51-
const commitMessage =
52-
await run(`git show -s ${baseBranch} --format="chore: generated code for commit %H. ${
53-
isMainBranch ? '[skip ci]' : ''
54-
}
53+
const commitMessage = await run(`git show -s ${baseBranch} --format="${
54+
text.commitStartMessage
55+
} %H. ${isMainBranch ? '[skip ci]' : ''}
5556
5657
Co-authored-by: %an <%ae>
5758
%(trailers:key=Co-authored-by)"`);

scripts/ci/codegen/spreadGeneration.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import { getLanguageFolder } from '../../config';
1313
import { cloneRepository, configureGitHubAuthor } from '../../release/common';
1414
import { getNbGitDiff } from '../utils';
1515

16+
import text from './text';
17+
1618
export function decideWhereToSpread(commitMessage: string): string[] {
1719
if (commitMessage.startsWith('chore: release')) {
1820
return [];
@@ -29,12 +31,31 @@ export function decideWhereToSpread(commitMessage: string): string[] {
2931
}
3032

3133
export function cleanUpCommitMessage(commitMessage: string): string {
32-
const result = commitMessage.match(/(.+)\s\(#(\d+)\)$/);
33-
if (!result) {
34+
const isCodeGenCommit = commitMessage.startsWith(text.commitStartMessage);
35+
36+
if (isCodeGenCommit) {
37+
const hash = commitMessage
38+
.split(text.commitStartMessage)[1]
39+
.replace('. [skip ci]', '')
40+
.trim();
41+
42+
if (!hash) {
43+
return commitMessage;
44+
}
45+
46+
return [
47+
`${text.commitStartMessage} ${hash.substring(0, 8)}. [skip ci]`,
48+
`${REPO_URL}/commit/${hash}`,
49+
].join('\n\n');
50+
}
51+
52+
const prCommit = commitMessage.match(/(.+)\s\(#(\d+)\)$/);
53+
54+
if (!prCommit) {
3455
return commitMessage;
3556
}
3657

37-
return [result[1], `${REPO_URL}/pull/${result[2]}`].join('\n\n');
58+
return [prCommit[1], `${REPO_URL}/pull/${prCommit[2]}`].join('\n\n');
3859
}
3960

4061
async function spreadGeneration(): Promise<void> {

scripts/ci/codegen/text.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { MAIN_BRANCH, REPO_URL } from '../../common';
22

33
export default {
4+
commitStartMessage: 'chore: generated code for commit',
45
notification: {
56
header: '### 🔨 The codegen job will run at the end of the CI.',
67
body: '_Make sure your last commit does not contains generated code, it will be automatically pushed by our CI._',

0 commit comments

Comments
 (0)