Skip to content

chore: make CI push code to main #369

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 2 commits into from
Apr 12, 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
6 changes: 3 additions & 3 deletions scripts/ci/codegen/__tests__/codegen.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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})."
`);
});

Expand All @@ -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) |
"
`);
Expand Down
26 changes: 9 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 { MAIN_BRANCH, run } from '../../common';
import { configureGitHubAuthor } from '../../release/common';
import { getNbGitDiff } from '../utils';

Expand All @@ -17,10 +17,11 @@ export async function pushGeneratedCode(): Promise<void> {
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,
Copy link
Collaborator

Choose a reason for hiding this comment

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

baseBranch is not very true, it should be currentBranch, but it's not important

head: null,
path: FOLDERS_TO_CHECK,
});
Expand All @@ -38,22 +39,13 @@ export async function pushGeneratedCode(): Promise<void> {
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 =
Expand All @@ -62,11 +54,11 @@ export async function pushGeneratedCode(): Promise<void> {
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}`);
Expand Down
11 changes: 0 additions & 11 deletions scripts/ci/codegen/spreadGeneration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -44,21 +42,12 @@ async function spreadGeneration(): Promise<void> {
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,
Expand Down
6 changes: 3 additions & 3 deletions scripts/ci/codegen/text.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { REPO_URL, GENERATED_MAIN_BRANCH } from '../../common';
import { MAIN_BRANCH, REPO_URL } from '../../common';

export default {
notification: {
Expand All @@ -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!',
Expand All @@ -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}) |
`;
},
Expand Down
1 change: 0 additions & 1 deletion scripts/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
Expand Down