diff --git a/scripts/ci/codegen/__tests__/codegen.test.ts b/scripts/ci/codegen/__tests__/codegen.test.ts index 32df0daf0e4..17fa43a42f2 100644 --- a/scripts/ci/codegen/__tests__/codegen.test.ts +++ b/scripts/ci/codegen/__tests__/codegen.test.ts @@ -1,6 +1,7 @@ +import { GENERATED_MAIN_BRANCH } from '../../../common'; import { cleanGeneratedBranch } from '../cleanGeneratedBranch'; import { pushGeneratedCode } from '../pushGeneratedCode'; -import commentText, { GENERATED_MAIN_BRANCH } from '../text'; +import commentText from '../text'; import { getCommentBody, upsertGenerationComment, @@ -84,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/commit/theGeneratedCommit) | + | 🔍 Generated code | [\`theGeneratedCommit\`](https://github.com/algolia/api-clients-automation/compare/generated/main...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 0d58ddc68be..78dde3b53ad 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 { run, GENERATED_MAIN_BRANCH } from '../../common'; import { configureGitHubAuthor } from '../../release/common'; import { getNbGitDiff } from '../utils'; @@ -19,16 +19,13 @@ export async function pushGeneratedCode(): Promise { const baseBranch = await run('git branch --show-current'); console.log(`Checking codegen status on '${baseBranch}'.`); - // determine generated branch name based on current branch - const generatedCodeBranch = `generated/${baseBranch}`; + const nbDiff = await getNbGitDiff({ + branch: baseBranch, + head: null, + path: FOLDERS_TO_CHECK, + }); - if ( - (await getNbGitDiff({ - branch: baseBranch, - head: null, - path: FOLDERS_TO_CHECK, - })) === 0 - ) { + if (nbDiff === 0) { console.log(`No generated code changes found for '${baseBranch}'.`); if (PR_NUMBER) { @@ -38,25 +35,43 @@ export async function pushGeneratedCode(): Promise { return; } - await run(`yarn workspace scripts cleanGeneratedBranch ${baseBranch}`); + console.log(`${nbDiff} changes found for ${FOLDERS_TO_CHECK}`); + + // determine generated branch name based on current branch + const generatedCodeBranch = `generated/${baseBranch}`; + + // We don't re-create GENERATED_MAIN_BRANCH + if (baseBranch !== 'main') { + await run(`yarn workspace scripts cleanGeneratedBranch ${baseBranch}`); + + console.log(`Creating branch for generated code: '${generatedCodeBranch}'`); + await run( + `git branch ${generatedCodeBranch} origin/${GENERATED_MAIN_BRANCH}` + ); + } + + 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: '${generatedCodeBranch}'`); - await run(`git branch ${generatedCodeBranch}`); const commitMessage = - await run(`git show -s --format="Generated code for commit %H. + await run(`git show -s ${baseBranch} --format="Generated code for commit %H. 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: '${generatedCodeBranch}'` ); - await run(`git checkout ${generatedCodeBranch}`); await run(`git add ${FOLDERS_TO_CHECK}`); await run(`git commit -m "${commitMessage}"`); await run(`git push origin ${generatedCodeBranch}`); - await run(`git checkout ${baseBranch}`); if (PR_NUMBER) { + await run(`git checkout ${baseBranch}`); await run(`yarn workspace scripts upsertGenerationComment codegen`); } } diff --git a/scripts/ci/codegen/spreadGeneration.ts b/scripts/ci/codegen/spreadGeneration.ts index 28177e0aed7..aca4ccd45c2 100644 --- a/scripts/ci/codegen/spreadGeneration.ts +++ b/scripts/ci/codegen/spreadGeneration.ts @@ -8,12 +8,12 @@ import { LANGUAGES, run, toAbsolutePath, + GENERATED_MAIN_BRANCH, + REPO_URL, } from '../../common'; import { getLanguageFolder } from '../../config'; import { cloneRepository, configureGitHubAuthor } from '../../release/common'; -import { GENERATED_MAIN_BRANCH, REPO_URL } from './text'; - export function decideWhereToSpread(commitMessage: string): string[] { if (commitMessage.startsWith('chore: release')) { return []; diff --git a/scripts/ci/codegen/text.ts b/scripts/ci/codegen/text.ts index 318f4bde5ef..ec89ea68d62 100644 --- a/scripts/ci/codegen/text.ts +++ b/scripts/ci/codegen/text.ts @@ -1,7 +1,4 @@ -import { MAIN_BRANCH, OWNER, REPO } from '../../release/common'; - -export const REPO_URL = `https://github.com/${OWNER}/${REPO}`; -export const GENERATED_MAIN_BRANCH = `generated/${MAIN_BRANCH}`; +import { REPO_URL, GENERATED_MAIN_BRANCH } from '../../common'; export default { notification: { @@ -28,7 +25,7 @@ export default { | Name | Link | |---------------------------------|------------------------| | 🔨 Triggered by | [\`${commit}\`](${REPO_URL}/pull/${eventNumber}/commits/${commit}) | -| 🔍 Generated code | [\`${generatedCommit}\`](${REPO_URL}/commit/${generatedCommit}) | +| 🔍 Generated code | [\`${generatedCommit}\`](${REPO_URL}/compare/${GENERATED_MAIN_BRANCH}...${generatedCommit}) | | 🌲 Generated branch | [\`${branch}\`](${REPO_URL}/tree/${branch}) | `; }, diff --git a/scripts/ci/codegen/upsertGenerationComment.ts b/scripts/ci/codegen/upsertGenerationComment.ts index 2ca3c803584..28c115d03e0 100644 --- a/scripts/ci/codegen/upsertGenerationComment.ts +++ b/scripts/ci/codegen/upsertGenerationComment.ts @@ -1,6 +1,6 @@ /* eslint-disable no-console */ -import { run } from '../../common'; -import { getOctokit, OWNER, REPO } from '../../release/common'; +import { run, OWNER, REPO } from '../../common'; +import { getOctokit } from '../../release/common'; import commentText from './text'; diff --git a/scripts/common.ts b/scripts/common.ts index fd7cdf85107..502bd1bf5eb 100644 --- a/scripts/common.ts +++ b/scripts/common.ts @@ -6,6 +6,7 @@ import { hashElement } from 'folder-hash'; import { remove } from 'fs-extra'; import clientsConfig from '../config/clients.config.json'; +import config from '../config/release.config.json'; import openapitools from '../openapitools.json'; import { createSpinner } from './oraLog'; @@ -16,6 +17,12 @@ import type { RunOptions, } 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}`; + export const CI = Boolean(process.env.CI); export const DOCKER = Boolean(process.env.DOCKER); diff --git a/scripts/release/common.ts b/scripts/release/common.ts index ab0e6adf30c..6c3e65b2958 100644 --- a/scripts/release/common.ts +++ b/scripts/release/common.ts @@ -7,9 +7,6 @@ import config from '../../config/release.config.json'; import { getGitHubUrl, run } from '../common'; export const RELEASED_TAG = config.releasedTag; -export const MAIN_BRANCH = config.mainBranch; -export const OWNER = config.owner; -export const REPO = config.repo; export const TEAM_SLUG = config.teamSlug; export const MAIN_PACKAGE = Object.keys(clientsConfig).reduce( (mainPackage: { [lang: string]: string }, lang: string) => { diff --git a/scripts/release/create-release-issue.ts b/scripts/release/create-release-issue.ts index 0d88bf458f5..99785d6e99f 100755 --- a/scripts/release/create-release-issue.ts +++ b/scripts/release/create-release-issue.ts @@ -2,17 +2,18 @@ import dotenv from 'dotenv'; import semver from 'semver'; -import { LANGUAGES, ROOT_ENV_PATH, run, getPackageVersion } from '../common'; -import type { Language } from '../types'; - import { - RELEASED_TAG, + LANGUAGES, + ROOT_ENV_PATH, + run, + getPackageVersion, MAIN_BRANCH, OWNER, REPO, - MAIN_PACKAGE, - getOctokit, -} from './common'; +} from '../common'; +import type { Language } from '../types'; + +import { RELEASED_TAG, MAIN_PACKAGE, getOctokit } from './common'; import TEXT from './text'; import type { Versions, diff --git a/scripts/release/process-release.ts b/scripts/release/process-release.ts index dacd3051d6c..cc1d11e929e 100755 --- a/scripts/release/process-release.ts +++ b/scripts/release/process-release.ts @@ -15,14 +15,14 @@ import { exists, getGitHubUrl, gitCommit, + OWNER, + REPO, emptyDirExceptForDotGit, } from '../common'; import { getLanguageFolder } from '../config'; import { RELEASED_TAG, - OWNER, - REPO, TEAM_SLUG, getMarkdownSection, configureGitHubAuthor,