Skip to content

Commit

Permalink
fix(scripts): update release issue process (#537)
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts authored May 25, 2022
1 parent 4c0c681 commit 1b491e5
Show file tree
Hide file tree
Showing 16 changed files with 313 additions and 453 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,11 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.TOKEN_GENERATE_BOT }}
PR_NUMBER: ${{ github.event.number }}
HEAD_COMMIT_MESSAGE: ${{ github.event.head_commit.message }}

- name: Spread generation to each repository
if: ${{ steps.pushGeneratedCode.exitcode == 0 && github.ref == 'refs/heads/main' }}
run: yarn workspace scripts spreadGeneration
env:
GITHUB_TOKEN: ${{ secrets.TOKEN_GENERATE_BOT }}
HEAD_COMMIT_MESSAGE: ${{ github.event.head_commit.message }}
29 changes: 0 additions & 29 deletions .github/workflows/process-release.yml

This file was deleted.

26 changes: 20 additions & 6 deletions scripts/ci/codegen/pushGeneratedCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import { getNbGitDiff } from '../utils';
import text from './text';

const PR_NUMBER = parseInt(process.env.PR_NUMBER || '0', 10);
const IS_RELEASE_COMMIT =
process.env.HEAD_COMMIT_MESSAGE?.startsWith(
text.commitPrepareReleaseMessage
) || false;

async function isUpToDate(baseBranch: string): Promise<boolean> {
await run('git fetch origin');
Expand Down Expand Up @@ -60,16 +64,26 @@ export async function pushGeneratedCode(): Promise<void> {
return;
}

const commitMessage = await run(`git show -s ${baseBranch} --format="${
text.commitStartMessage
} %H. ${isMainBranch ? '[skip ci]' : ''}
const skipCi = isMainBranch ? '[skip ci]' : '';
let message = await run(
`git show -s ${baseBranch} --format="${text.commitStartMessage} %H. ${skipCi}"`
);
const authors = await run(
`git show -s ${baseBranch} --format="
Co-authored-by: %an <%ae>
%(trailers:key=Co-authored-by)"`);
%(trailers:key=Co-authored-by)"`
);

if (IS_RELEASE_COMMIT && isMainBranch) {
message = text.commitReleaseMessage;
}

message += authors;

console.log(`Pushing code to generated branch: '${branchToPush}'`);
await run(`git add .`);
await run(`git commit -m "${commitMessage}"`);
await run('git add .');
await run(`git commit -m "${message}"`);
await run(`git push origin ${branchToPush}`);

if (PR_NUMBER) {
Expand Down
43 changes: 39 additions & 4 deletions scripts/ci/codegen/spreadGeneration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable no-console */
import execa from 'execa';
import { copy } from 'fs-extra';

import {
Expand All @@ -10,13 +11,22 @@ import {
REPO_URL,
ensureGitHubToken,
} from '../../common';
import { getLanguageFolder } from '../../config';
import { cloneRepository, configureGitHubAuthor } from '../../release/common';
import { getLanguageFolder, getPackageVersionDefault } from '../../config';
import {
cloneRepository,
configureGitHubAuthor,
RELEASED_TAG,
} from '../../release/common';
import type { Language } from '../../types';
import { getNbGitDiff } from '../utils';

import text from './text';

const IS_RELEASE_COMMIT =
process.env.HEAD_COMMIT_MESSAGE?.startsWith(
text.commitPrepareReleaseMessage
) || false;

export function decideWhereToSpread(commitMessage: string): Language[] {
if (commitMessage.startsWith('chore: release')) {
return [];
Expand Down Expand Up @@ -77,6 +87,21 @@ async function spreadGeneration(): Promise<void> {
const commitMessage = cleanUpCommitMessage(lastCommitMessage);
const langs = decideWhereToSpread(lastCommitMessage);

// At this point, we know the release will happen on every clients
// So we want to set the released tag at the monorepo level too.
if (IS_RELEASE_COMMIT) {
// remove old `released` tag
await run(
`git fetch origin refs/tags/${RELEASED_TAG}:refs/tags/${RELEASED_TAG}`
);
await run(`git tag -d ${RELEASED_TAG}`);
await run(`git push --delete origin ${RELEASED_TAG}`);

// create new `released` tag
await run(`git tag released`);
await run(`git push --tags`);
}

for (const lang of langs) {
const { tempGitDir } = await cloneRepository({
lang,
Expand All @@ -100,14 +125,24 @@ async function spreadGeneration(): Promise<void> {
continue;
}

const version = getPackageVersionDefault(lang);
const message = IS_RELEASE_COMMIT
? `chore: release ${version}`
: commitMessage;

await configureGitHubAuthor(tempGitDir);
await run(`git add .`, { cwd: tempGitDir });
await gitCommit({
message: commitMessage,
message,
coAuthors: [author, ...coAuthors],
cwd: tempGitDir,
});
await run(`git push`, { cwd: tempGitDir });
await execa('git', ['tag', version], {
cwd: tempGitDir,
});
await run(IS_RELEASE_COMMIT ? 'git push --follow-tags' : 'git push', {
cwd: tempGitDir,
});
console.log(`✅ Spread the generation to ${lang} repository.`);
}
}
Expand Down
4 changes: 3 additions & 1 deletion scripts/ci/codegen/text.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { MAIN_BRANCH, REPO_URL } from '../../common';
import { MAIN_BRANCH, REPO_URL, TODAY } from '../../common';

export default {
commitStartMessage: 'chore: generated code for commit',
commitPrepareReleaseMessage: 'chore: prepare-release-',
commitReleaseMessage: `chore: release ${TODAY}`,
notification: {
header: '### 🔨 The codegen job will run at the end of the CI.',
body: (): string =>
Expand Down
2 changes: 1 addition & 1 deletion scripts/ci/husky/pre-commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ async function preCommit() {
}

console.log(
chalk.bgYellow('[INFO]'),
chalk.black.bgYellow('[INFO]'),
`Generated file found, unstaging: ${stagedFile}`
);

Expand Down
1 change: 1 addition & 0 deletions scripts/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const MAIN_BRANCH = releaseConfig.mainBranch;
export const OWNER = releaseConfig.owner;
export const REPO = releaseConfig.repo;
export const REPO_URL = `https://github.com/${OWNER}/${REPO}`;
export const TODAY = new Date().toISOString().split('T')[0];

export const CI = Boolean(process.env.CI);
export const DOCKER = Boolean(process.env.DOCKER);
Expand Down
1 change: 0 additions & 1 deletion scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"createMatrix": "ts-node ci/githubActions/createMatrix.ts",
"createReleaseIssue": "ts-node release/create-release-issue.ts",
"pre-commit": "./ci/husky/pre-commit.js",
"processRelease": "ts-node release/process-release.ts",
"pushGeneratedCode": "ts-node ci/codegen/pushGeneratedCode.ts",
"renovateWeeklyPR": "ts-node ci/githubActions/renovateWeeklyPR.ts",
"setRunVariables": "ts-node ci/githubActions/setRunVariables.ts",
Expand Down
63 changes: 0 additions & 63 deletions scripts/release/__tests__/common.test.ts

This file was deleted.

32 changes: 22 additions & 10 deletions scripts/release/__tests__/create-release-issue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '../create-release-issue';

describe('create release issue', () => {
it('reads versions openapitools.json', () => {
it('reads versions of the current language', () => {
expect(readVersions()).toEqual({
java: {
current: expect.any(String),
Expand Down Expand Up @@ -53,7 +53,19 @@ describe('create release issue', () => {

it('returns error when it is a generated commit', () => {
expect(
parseCommit(`${generationCommitText.commitStartMessage} ABCDEF`)
parseCommit(
`49662518 ${generationCommitText.commitStartMessage} ABCDEF`
)
).toEqual({
error: 'generation-commit',
});
});

it('returns error when it is a generated commit, even with other casing', () => {
expect(
parseCommit(
`49662518 ${generationCommitText.commitStartMessage.toLocaleUpperCase()} ABCDEF`
)
).toEqual({
error: 'generation-commit',
});
Expand All @@ -80,9 +92,9 @@ describe('create release issue', () => {
},
})
).toMatchInlineSnapshot(`
"- [x] javascript: 0.0.1 -> \`patch\` _(e.g. 0.0.2)_
- [x] java: 0.0.1 -> \`patch\` _(e.g. 0.0.2)_
- [x] php: 0.0.1 -> \`patch\` _(e.g. 0.0.2)_"
"- javascript: 0.0.1 -> **\`patch\` _(e.g. 0.0.2)_**
- java: 0.0.1 -> **\`patch\` _(e.g. 0.0.2)_**
- php: 0.0.1 -> **\`patch\` _(e.g. 0.0.2)_**"
`);
});

Expand All @@ -106,8 +118,8 @@ describe('create release issue', () => {
},
})
).toMatchInlineSnapshot(`
"- [x] javascript: 0.0.1 -> \`patch\` _(e.g. 0.0.2)_
- [x] java: 0.0.1 -> \`patch\` _(e.g. 0.0.2)_
"- javascript: 0.0.1 -> **\`patch\` _(e.g. 0.0.2)_**
- java: 0.0.1 -> **\`patch\` _(e.g. 0.0.2)_**
- ~php: 0.0.1 (no commit)~"
`);
});
Expand All @@ -132,10 +144,10 @@ describe('create release issue', () => {
},
})
).toMatchInlineSnapshot(`
"- [x] javascript: 0.0.1 -> \`patch\` _(e.g. 0.0.2)_
- [ ] java: 0.0.1 -> \`patch\` _(e.g. 0.0.2)_
"- javascript: 0.0.1 -> **\`patch\` _(e.g. 0.0.2)_**
- ~java: 0.0.1 -> **\`patch\` _(e.g. 0.0.2)_**~
- No \`feat\` or \`fix\` commit, thus unchecked by default.
- [x] php: 0.0.1 -> \`minor\` _(e.g. 0.1.0)_"
- php: 0.0.1 -> **\`minor\` _(e.g. 0.1.0)_**"
`);
});
});
Expand Down
7 changes: 4 additions & 3 deletions scripts/release/__tests__/process-release.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ describe('process release', () => {
const versions = getVersionsToRelease(`
## Version Changes
- [x] javascript: 1.0.0 -> \`minor\` (e.g. 1.1.0)
- [x] php: 2.0.0 -> \`patch\` (e.g. 2.0.1)
- [ ] java: 3.0.0 -> \`patch\` (e.g. 3.0.1)
- javascript: 1.0.0 -> **\`minor\` _(e.g. 1.1.0)_**
- ~java: 3.0.0 -> **\`patch\` _(e.g. 3.0.1)_**~
- No \`feat\` or \`fix\` commit, thus unchecked by default.
- php: 2.0.0 -> **\`patch\` _(e.g. 2.0.1)_**
`);

expect(Object.keys(versions)).toEqual(['javascript', 'php']);
Expand Down
16 changes: 0 additions & 16 deletions scripts/release/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,6 @@ export function getGitAuthor(): { name: string; email: string } {
return config.gitAuthor;
}

export function getMarkdownSection(markdown: string, title: string): string {
const levelIndicator = title.split(' ')[0]; // e.g. `##`
const lines = markdown
.slice(markdown.indexOf(title))
.split('\n')
.map((line) => line.trim());
let endIndex = lines.length;
for (let i = 1; i < lines.length; i++) {
if (lines[i].startsWith(`${levelIndicator} `)) {
endIndex = i;
break;
}
}
return lines.slice(0, endIndex).join('\n');
}

export async function configureGitHubAuthor(cwd?: string): Promise<void> {
const { name, email } = getGitAuthor();

Expand Down
Loading

0 comments on commit 1b491e5

Please sign in to comment.