From f348b205da50742c53f4f023317334053f780290 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Vannicatte?= Date: Thu, 7 Apr 2022 14:37:50 +0200 Subject: [PATCH 1/5] chore: keep `generated/main` alive --- scripts/ci/codegen/pushGeneratedCode.ts | 49 +++++++++++++++++-------- scripts/ci/codegen/text.ts | 2 +- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/scripts/ci/codegen/pushGeneratedCode.ts b/scripts/ci/codegen/pushGeneratedCode.ts index 0d58ddc68be..1d896261ec3 100644 --- a/scripts/ci/codegen/pushGeneratedCode.ts +++ b/scripts/ci/codegen/pushGeneratedCode.ts @@ -3,6 +3,8 @@ import { run } from '../../common'; import { configureGitHubAuthor } from '../../release/common'; import { getNbGitDiff } from '../utils'; +import { GENERATED_MAIN_BRANCH } from './text'; + const PR_NUMBER = parseInt(process.env.PR_NUMBER || '0', 10); const FOLDERS_TO_CHECK = 'yarn.lock openapitools.json clients specs/bundled'; @@ -19,16 +21,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: 'noHead', + 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 +37,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/text.ts b/scripts/ci/codegen/text.ts index 318f4bde5ef..c680d5c292c 100644 --- a/scripts/ci/codegen/text.ts +++ b/scripts/ci/codegen/text.ts @@ -28,7 +28,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}) | `; }, From 3b091761fb5e60ec1e1893c05f50e8ef1fa50b81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Vannicatte?= Date: Thu, 7 Apr 2022 17:00:47 +0200 Subject: [PATCH 2/5] fix: test --- scripts/ci/codegen/__tests__/codegen.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/ci/codegen/__tests__/codegen.test.ts b/scripts/ci/codegen/__tests__/codegen.test.ts index 32df0daf0e4..e115d8fa1f3 100644 --- a/scripts/ci/codegen/__tests__/codegen.test.ts +++ b/scripts/ci/codegen/__tests__/codegen.test.ts @@ -84,7 +84,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) | " `); From a72f8e21bbd6a4f159d5eeba078fd2463bbc5260 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Vannicatte?= Date: Thu, 7 Apr 2022 17:20:18 +0200 Subject: [PATCH 3/5] fix merge issue --- scripts/ci/codegen/pushGeneratedCode.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/ci/codegen/pushGeneratedCode.ts b/scripts/ci/codegen/pushGeneratedCode.ts index 1d896261ec3..d94d38aeb14 100644 --- a/scripts/ci/codegen/pushGeneratedCode.ts +++ b/scripts/ci/codegen/pushGeneratedCode.ts @@ -23,7 +23,7 @@ export async function pushGeneratedCode(): Promise { const nbDiff = await getNbGitDiff({ branch: baseBranch, - head: 'noHead', + head: null, path: FOLDERS_TO_CHECK, }); From f1aec3f747fd1c9b73795d083c4d8d88aa7c0331 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Vannicatte?= Date: Fri, 8 Apr 2022 11:33:24 +0200 Subject: [PATCH 4/5] move vars around --- scripts/ci/codegen/text.ts | 5 +---- scripts/common.ts | 7 +++++++ scripts/release/common.ts | 3 --- scripts/release/process-release.ts | 4 ++-- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/scripts/ci/codegen/text.ts b/scripts/ci/codegen/text.ts index c680d5c292c..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: { 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/process-release.ts b/scripts/release/process-release.ts index 6f336b5feca..ee0e509bf99 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, From e26ffbc86943e330bd5c73fde8cefde98e25c96f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Vannicatte?= Date: Fri, 8 Apr 2022 11:38:29 +0200 Subject: [PATCH 5/5] fix paths --- scripts/ci/codegen/__tests__/codegen.test.ts | 3 ++- scripts/ci/codegen/pushGeneratedCode.ts | 4 +--- scripts/ci/codegen/spreadGeneration.ts | 4 ++-- scripts/ci/codegen/upsertGenerationComment.ts | 4 ++-- scripts/release/create-release-issue.ts | 15 ++++++++------- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/scripts/ci/codegen/__tests__/codegen.test.ts b/scripts/ci/codegen/__tests__/codegen.test.ts index e115d8fa1f3..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, diff --git a/scripts/ci/codegen/pushGeneratedCode.ts b/scripts/ci/codegen/pushGeneratedCode.ts index d94d38aeb14..78dde3b53ad 100644 --- a/scripts/ci/codegen/pushGeneratedCode.ts +++ b/scripts/ci/codegen/pushGeneratedCode.ts @@ -1,10 +1,8 @@ /* eslint-disable no-console */ -import { run } from '../../common'; +import { run, GENERATED_MAIN_BRANCH } from '../../common'; import { configureGitHubAuthor } from '../../release/common'; import { getNbGitDiff } from '../utils'; -import { GENERATED_MAIN_BRANCH } from './text'; - const PR_NUMBER = parseInt(process.env.PR_NUMBER || '0', 10); const FOLDERS_TO_CHECK = 'yarn.lock openapitools.json clients specs/bundled'; 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/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/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,