From be213f802906ad868ee1906066371e18acc641e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Vannicatte?= Date: Thu, 21 Apr 2022 14:09:00 +0200 Subject: [PATCH] chore: make spread codegen commit cleaner (#412) --- .../__tests__/spreadGeneration.test.ts | 10 +++++++ scripts/ci/codegen/pushGeneratedCode.ts | 9 ++++--- scripts/ci/codegen/spreadGeneration.ts | 27 ++++++++++++++++--- scripts/ci/codegen/text.ts | 1 + 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/scripts/ci/codegen/__tests__/spreadGeneration.test.ts b/scripts/ci/codegen/__tests__/spreadGeneration.test.ts index 454322cd2d..ffe402d759 100644 --- a/scripts/ci/codegen/__tests__/spreadGeneration.test.ts +++ b/scripts/ci/codegen/__tests__/spreadGeneration.test.ts @@ -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', () => { @@ -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" + `); + }); }); diff --git a/scripts/ci/codegen/pushGeneratedCode.ts b/scripts/ci/codegen/pushGeneratedCode.ts index 276013ea8d..13935b6860 100644 --- a/scripts/ci/codegen/pushGeneratedCode.ts +++ b/scripts/ci/codegen/pushGeneratedCode.ts @@ -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'; @@ -48,10 +50,9 @@ export async function pushGeneratedCode(): Promise { 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)"`); diff --git a/scripts/ci/codegen/spreadGeneration.ts b/scripts/ci/codegen/spreadGeneration.ts index c9d847357d..12119c57af 100644 --- a/scripts/ci/codegen/spreadGeneration.ts +++ b/scripts/ci/codegen/spreadGeneration.ts @@ -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 []; @@ -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 { diff --git a/scripts/ci/codegen/text.ts b/scripts/ci/codegen/text.ts index 5997c3ff2d..e90f8eb4a3 100644 --- a/scripts/ci/codegen/text.ts +++ b/scripts/ci/codegen/text.ts @@ -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._',