diff --git a/scripts/ci/codegen/__tests__/codegen.test.ts b/scripts/ci/codegen/__tests__/codegen.test.ts index 17fa43a42f..ef09391967 100644 --- a/scripts/ci/codegen/__tests__/codegen.test.ts +++ b/scripts/ci/codegen/__tests__/codegen.test.ts @@ -1,4 +1,4 @@ -import { GENERATED_MAIN_BRANCH } from '../../../common'; +import { MAIN_BRANCH } from '../../../common'; import { cleanGeneratedBranch } from '../cleanGeneratedBranch'; import { pushGeneratedCode } from '../pushGeneratedCode'; import commentText from '../text'; @@ -67,7 +67,7 @@ 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 [\`${GENERATED_MAIN_BRANCH}\` branch](https://github.com/algolia/api-clients-automation/tree/${GENERATED_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})." `); }); @@ -85,7 +85,7 @@ describe('codegen', () => { | Name | Link | |---------------------------------|------------------------| | 🔨 Triggered by | [\`myCommit\`](https://github.com/algolia/api-clients-automation/pull/42/commits/myCommit) | - | 🔍 Generated code | [\`theGeneratedCommit\`](https://github.com/algolia/api-clients-automation/compare/generated/main...theGeneratedCommit) | + | 🔍 Generated code | [\`theGeneratedCommit\`](https://github.com/algolia/api-clients-automation/commit/theGeneratedCommit) | | 🌲 Generated branch | [\`myBranch\`](https://github.com/algolia/api-clients-automation/tree/myBranch) | " `); diff --git a/scripts/ci/codegen/pushGeneratedCode.ts b/scripts/ci/codegen/pushGeneratedCode.ts index e0b2811095..6090e42c79 100644 --- a/scripts/ci/codegen/pushGeneratedCode.ts +++ b/scripts/ci/codegen/pushGeneratedCode.ts @@ -1,5 +1,5 @@ /* eslint-disable no-console */ -import { run } from '../../common'; +import { MAIN_BRANCH, run } from '../../common'; import { configureGitHubAuthor } from '../../release/common'; import { getNbGitDiff } from '../utils'; @@ -17,10 +17,11 @@ export async function pushGeneratedCode(): Promise { await configureGitHubAuthor(); const baseBranch = await run('git branch --show-current'); + const isMainBranch = baseBranch === MAIN_BRANCH; console.log(`Checking codegen status on '${baseBranch}'.`); const nbDiff = await getNbGitDiff({ - branch: 'origin/generated/main', + branch: baseBranch, head: null, path: FOLDERS_TO_CHECK, }); @@ -38,22 +39,13 @@ export async function pushGeneratedCode(): Promise { console.log(`${nbDiff} changes found for ${FOLDERS_TO_CHECK}`); // determine generated branch name based on current branch - const generatedCodeBranch = `generated/${baseBranch}`; + const branchToPush = isMainBranch ? baseBranch : `generated/${baseBranch}`; - // We don't re-create GENERATED_MAIN_BRANCH - if (baseBranch !== 'main') { + if (!isMainBranch) { await run(`yarn workspace scripts cleanGeneratedBranch ${baseBranch}`); - console.log(`Creating branch for generated code: '${generatedCodeBranch}'`); - await run(`git branch ${generatedCodeBranch}`); - } - - await run(`git checkout ${generatedCodeBranch}`); - - // For the GENERATED_MAIN_BRANCH, we take the latest commit on main and generate code - if (baseBranch === 'main') { - console.log(`Merging '${baseBranch}' in '${generatedCodeBranch}'`); - await run(`git merge --no-commit ${baseBranch}`); + console.log(`Creating branch for generated code: '${branchToPush}'`); + await run(`git checkout -b ${branchToPush}`); } const commitMessage = @@ -62,11 +54,11 @@ export async function pushGeneratedCode(): Promise { Co-authored-by: %an <%ae>"`); console.log( - `Pushing code for folders '${FOLDERS_TO_CHECK}' to generated branch: '${generatedCodeBranch}'` + `Pushing code for folders '${FOLDERS_TO_CHECK}' to generated branch: '${branchToPush}'` ); await run(`git add ${FOLDERS_TO_CHECK}`); await run(`git commit -m "${commitMessage}"`); - await run(`git push origin ${generatedCodeBranch}`); + await run(`git push origin ${branchToPush}`); if (PR_NUMBER) { await run(`git checkout ${baseBranch}`); diff --git a/scripts/ci/codegen/spreadGeneration.ts b/scripts/ci/codegen/spreadGeneration.ts index 5157d0e564..d3822cc1d9 100644 --- a/scripts/ci/codegen/spreadGeneration.ts +++ b/scripts/ci/codegen/spreadGeneration.ts @@ -3,12 +3,10 @@ import { copy } from 'fs-extra'; import { emptyDirExceptForDotGit, - gitBranchExists, gitCommit, LANGUAGES, run, toAbsolutePath, - GENERATED_MAIN_BRANCH, REPO_URL, } from '../../common'; import { getLanguageFolder } from '../../config'; @@ -44,21 +42,12 @@ async function spreadGeneration(): Promise { throw new Error('Environment variable `GITHUB_TOKEN` does not exist.'); } - if (!(await gitBranchExists(GENERATED_MAIN_BRANCH))) { - console.log( - `Skiping because the branch \`${GENERATED_MAIN_BRANCH}\` does not exist.` - ); - return; - } - const lastCommitMessage = await run(`git log -1 --format="%s"`); const name = (await run(`git log -1 --format="%an"`)).trim(); const email = (await run(`git log -1 --format="%ae"`)).trim(); const commitMessage = cleanUpCommitMessage(lastCommitMessage); const langs = decideWhereToSpread(lastCommitMessage); - await run(`git checkout ${GENERATED_MAIN_BRANCH}`); - for (const lang of langs) { const { tempGitDir } = await cloneRepository({ lang, diff --git a/scripts/ci/codegen/text.ts b/scripts/ci/codegen/text.ts index ec89ea68d6..5997c3ff2d 100644 --- a/scripts/ci/codegen/text.ts +++ b/scripts/ci/codegen/text.ts @@ -1,4 +1,4 @@ -import { REPO_URL, GENERATED_MAIN_BRANCH } from '../../common'; +import { MAIN_BRANCH, REPO_URL } from '../../common'; export default { notification: { @@ -11,7 +11,7 @@ export default { }, cleanup: { header: '### ✗ The generated branch has been deleted.', - body: `If the PR has been merged, you can check the generated code on the [\`${GENERATED_MAIN_BRANCH}\` branch](${REPO_URL}/tree/${GENERATED_MAIN_BRANCH}).`, + body: `If the PR has been merged, you can check the generated code on the [\`${MAIN_BRANCH}\` branch](${REPO_URL}/tree/${MAIN_BRANCH}).`, }, codegen: { header: '### ✔️ Code generated!', @@ -25,7 +25,7 @@ export default { | Name | Link | |---------------------------------|------------------------| | 🔨 Triggered by | [\`${commit}\`](${REPO_URL}/pull/${eventNumber}/commits/${commit}) | -| 🔍 Generated code | [\`${generatedCommit}\`](${REPO_URL}/compare/${GENERATED_MAIN_BRANCH}...${generatedCommit}) | +| 🔍 Generated code | [\`${generatedCommit}\`](${REPO_URL}/commit/${generatedCommit}) | | 🌲 Generated branch | [\`${branch}\`](${REPO_URL}/tree/${branch}) | `; }, diff --git a/scripts/common.ts b/scripts/common.ts index 502bd1bf5e..4fb3c9e976 100644 --- a/scripts/common.ts +++ b/scripts/common.ts @@ -18,7 +18,6 @@ import type { } from './types'; export const MAIN_BRANCH = config.mainBranch; -export const GENERATED_MAIN_BRANCH = `generated/${MAIN_BRANCH}`; export const OWNER = config.owner; export const REPO = config.repo; export const REPO_URL = `https://github.com/${OWNER}/${REPO}`;