Skip to content

Commit 6f17c84

Browse files
authored
chore: keep generated/main alive (#349)
1 parent f21a038 commit 6f17c84

9 files changed

+58
-40
lines changed

scripts/ci/codegen/__tests__/codegen.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import { GENERATED_MAIN_BRANCH } from '../../../common';
12
import { cleanGeneratedBranch } from '../cleanGeneratedBranch';
23
import { pushGeneratedCode } from '../pushGeneratedCode';
3-
import commentText, { GENERATED_MAIN_BRANCH } from '../text';
4+
import commentText from '../text';
45
import {
56
getCommentBody,
67
upsertGenerationComment,
@@ -84,7 +85,7 @@ describe('codegen', () => {
8485
| Name | Link |
8586
|---------------------------------|------------------------|
8687
| 🔨 Triggered by | [\`myCommit\`](https://github.com/algolia/api-clients-automation/pull/42/commits/myCommit) |
87-
| 🔍 Generated code | [\`theGeneratedCommit\`](https://github.com/algolia/api-clients-automation/commit/theGeneratedCommit) |
88+
| 🔍 Generated code | [\`theGeneratedCommit\`](https://github.com/algolia/api-clients-automation/compare/generated/main...theGeneratedCommit) |
8889
| 🌲 Generated branch | [\`myBranch\`](https://github.com/algolia/api-clients-automation/tree/myBranch) |
8990
"
9091
`);

scripts/ci/codegen/pushGeneratedCode.ts

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable no-console */
2-
import { run } from '../../common';
2+
import { run, GENERATED_MAIN_BRANCH } from '../../common';
33
import { configureGitHubAuthor } from '../../release/common';
44
import { getNbGitDiff } from '../utils';
55

@@ -19,16 +19,13 @@ export async function pushGeneratedCode(): Promise<void> {
1919
const baseBranch = await run('git branch --show-current');
2020
console.log(`Checking codegen status on '${baseBranch}'.`);
2121

22-
// determine generated branch name based on current branch
23-
const generatedCodeBranch = `generated/${baseBranch}`;
22+
const nbDiff = await getNbGitDiff({
23+
branch: baseBranch,
24+
head: null,
25+
path: FOLDERS_TO_CHECK,
26+
});
2427

25-
if (
26-
(await getNbGitDiff({
27-
branch: baseBranch,
28-
head: null,
29-
path: FOLDERS_TO_CHECK,
30-
})) === 0
31-
) {
28+
if (nbDiff === 0) {
3229
console.log(`No generated code changes found for '${baseBranch}'.`);
3330

3431
if (PR_NUMBER) {
@@ -38,25 +35,43 @@ export async function pushGeneratedCode(): Promise<void> {
3835
return;
3936
}
4037

41-
await run(`yarn workspace scripts cleanGeneratedBranch ${baseBranch}`);
38+
console.log(`${nbDiff} changes found for ${FOLDERS_TO_CHECK}`);
39+
40+
// determine generated branch name based on current branch
41+
const generatedCodeBranch = `generated/${baseBranch}`;
42+
43+
// We don't re-create GENERATED_MAIN_BRANCH
44+
if (baseBranch !== 'main') {
45+
await run(`yarn workspace scripts cleanGeneratedBranch ${baseBranch}`);
46+
47+
console.log(`Creating branch for generated code: '${generatedCodeBranch}'`);
48+
await run(
49+
`git branch ${generatedCodeBranch} origin/${GENERATED_MAIN_BRANCH}`
50+
);
51+
}
52+
53+
await run(`git checkout ${generatedCodeBranch}`);
54+
55+
// For the GENERATED_MAIN_BRANCH, we take the latest commit on main and generate code
56+
if (baseBranch === 'main') {
57+
console.log(`Merging '${baseBranch}' in '${generatedCodeBranch}'`);
58+
await run(`git merge --no-commit ${baseBranch}`);
59+
}
4260

43-
console.log(`Creating branch for generated code: '${generatedCodeBranch}'`);
44-
await run(`git branch ${generatedCodeBranch}`);
4561
const commitMessage =
46-
await run(`git show -s --format="Generated code for commit %H.
62+
await run(`git show -s ${baseBranch} --format="Generated code for commit %H.
4763
4864
Co-authored-by: %an <%ae>"`);
4965

5066
console.log(
51-
`Pushing code for folders '${FOLDERS_TO_CHECK}' to generated branch: ${generatedCodeBranch}`
67+
`Pushing code for folders '${FOLDERS_TO_CHECK}' to generated branch: '${generatedCodeBranch}'`
5268
);
53-
await run(`git checkout ${generatedCodeBranch}`);
5469
await run(`git add ${FOLDERS_TO_CHECK}`);
5570
await run(`git commit -m "${commitMessage}"`);
5671
await run(`git push origin ${generatedCodeBranch}`);
57-
await run(`git checkout ${baseBranch}`);
5872

5973
if (PR_NUMBER) {
74+
await run(`git checkout ${baseBranch}`);
6075
await run(`yarn workspace scripts upsertGenerationComment codegen`);
6176
}
6277
}

scripts/ci/codegen/spreadGeneration.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import {
88
LANGUAGES,
99
run,
1010
toAbsolutePath,
11+
GENERATED_MAIN_BRANCH,
12+
REPO_URL,
1113
} from '../../common';
1214
import { getLanguageFolder } from '../../config';
1315
import { cloneRepository, configureGitHubAuthor } from '../../release/common';
1416

15-
import { GENERATED_MAIN_BRANCH, REPO_URL } from './text';
16-
1717
export function decideWhereToSpread(commitMessage: string): string[] {
1818
if (commitMessage.startsWith('chore: release')) {
1919
return [];

scripts/ci/codegen/text.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import { MAIN_BRANCH, OWNER, REPO } from '../../release/common';
2-
3-
export const REPO_URL = `https://github.com/${OWNER}/${REPO}`;
4-
export const GENERATED_MAIN_BRANCH = `generated/${MAIN_BRANCH}`;
1+
import { REPO_URL, GENERATED_MAIN_BRANCH } from '../../common';
52

63
export default {
74
notification: {
@@ -28,7 +25,7 @@ export default {
2825
| Name | Link |
2926
|---------------------------------|------------------------|
3027
| 🔨 Triggered by | [\`${commit}\`](${REPO_URL}/pull/${eventNumber}/commits/${commit}) |
31-
| 🔍 Generated code | [\`${generatedCommit}\`](${REPO_URL}/commit/${generatedCommit}) |
28+
| 🔍 Generated code | [\`${generatedCommit}\`](${REPO_URL}/compare/${GENERATED_MAIN_BRANCH}...${generatedCommit}) |
3229
| 🌲 Generated branch | [\`${branch}\`](${REPO_URL}/tree/${branch}) |
3330
`;
3431
},

scripts/ci/codegen/upsertGenerationComment.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable no-console */
2-
import { run } from '../../common';
3-
import { getOctokit, OWNER, REPO } from '../../release/common';
2+
import { run, OWNER, REPO } from '../../common';
3+
import { getOctokit } from '../../release/common';
44

55
import commentText from './text';
66

scripts/common.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { hashElement } from 'folder-hash';
66
import { remove } from 'fs-extra';
77

88
import clientsConfig from '../config/clients.config.json';
9+
import config from '../config/release.config.json';
910
import openapitools from '../openapitools.json';
1011

1112
import { createSpinner } from './oraLog';
@@ -16,6 +17,12 @@ import type {
1617
RunOptions,
1718
} from './types';
1819

20+
export const MAIN_BRANCH = config.mainBranch;
21+
export const GENERATED_MAIN_BRANCH = `generated/${MAIN_BRANCH}`;
22+
export const OWNER = config.owner;
23+
export const REPO = config.repo;
24+
export const REPO_URL = `https://github.com/${OWNER}/${REPO}`;
25+
1926
export const CI = Boolean(process.env.CI);
2027
export const DOCKER = Boolean(process.env.DOCKER);
2128

scripts/release/common.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ import config from '../../config/release.config.json';
77
import { getGitHubUrl, run } from '../common';
88

99
export const RELEASED_TAG = config.releasedTag;
10-
export const MAIN_BRANCH = config.mainBranch;
11-
export const OWNER = config.owner;
12-
export const REPO = config.repo;
1310
export const TEAM_SLUG = config.teamSlug;
1411
export const MAIN_PACKAGE = Object.keys(clientsConfig).reduce(
1512
(mainPackage: { [lang: string]: string }, lang: string) => {

scripts/release/create-release-issue.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22
import dotenv from 'dotenv';
33
import semver from 'semver';
44

5-
import { LANGUAGES, ROOT_ENV_PATH, run, getPackageVersion } from '../common';
6-
import type { Language } from '../types';
7-
85
import {
9-
RELEASED_TAG,
6+
LANGUAGES,
7+
ROOT_ENV_PATH,
8+
run,
9+
getPackageVersion,
1010
MAIN_BRANCH,
1111
OWNER,
1212
REPO,
13-
MAIN_PACKAGE,
14-
getOctokit,
15-
} from './common';
13+
} from '../common';
14+
import type { Language } from '../types';
15+
16+
import { RELEASED_TAG, MAIN_PACKAGE, getOctokit } from './common';
1617
import TEXT from './text';
1718
import type {
1819
Versions,

scripts/release/process-release.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ import {
1515
exists,
1616
getGitHubUrl,
1717
gitCommit,
18+
OWNER,
19+
REPO,
1820
emptyDirExceptForDotGit,
1921
} from '../common';
2022
import { getLanguageFolder } from '../config';
2123

2224
import {
2325
RELEASED_TAG,
24-
OWNER,
25-
REPO,
2626
TEAM_SLUG,
2727
getMarkdownSection,
2828
configureGitHubAuthor,

0 commit comments

Comments
 (0)