Skip to content

Commit

Permalink
chore: make spread codegen commit cleaner (#412)
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts authored Apr 21, 2022
1 parent f705498 commit a2a40e8
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
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]
.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

0 comments on commit a2a40e8

Please sign in to comment.