Skip to content

chore: keep generated/main alive #349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions scripts/ci/codegen/__tests__/codegen.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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) |
"
`);
Expand Down
49 changes: 32 additions & 17 deletions scripts/ci/codegen/pushGeneratedCode.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -19,16 +19,13 @@ export async function pushGeneratedCode(): Promise<void> {
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) {
Expand All @@ -38,25 +35,43 @@ export async function pushGeneratedCode(): Promise<void> {
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}`);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So generated/main will move by 2 commits for each PR ? Is it possible to squash the merge and the changes of the PR ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't create a merge commit, so it should only be ahead of one generation commit for each PR merged to main, which keeps the history correct

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Squashing was my first option, but can lead to wrong authoring if multiple commits are merged to main. Which could also be solved by avoid cancelling concurrent jobs on main (2nd point in the PR body)

}

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`);
}
}
Expand Down
4 changes: 2 additions & 2 deletions scripts/ci/codegen/spreadGeneration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
Expand Down
7 changes: 2 additions & 5 deletions scripts/ci/codegen/text.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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}) |
`;
},
Expand Down
4 changes: 2 additions & 2 deletions scripts/ci/codegen/upsertGenerationComment.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
7 changes: 7 additions & 0 deletions scripts/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -16,6 +17,12 @@ import type {
RunOptions,
} from './types';

export const MAIN_BRANCH = config.mainBranch;
export const GENERATED_MAIN_BRANCH = `generated/${MAIN_BRANCH}`;
Comment on lines +20 to +21
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this make more sense 👍

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);

Expand Down
3 changes: 0 additions & 3 deletions scripts/release/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
15 changes: 8 additions & 7 deletions scripts/release/create-release-issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions scripts/release/process-release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down