Skip to content

Commit 1b491e5

Browse files
authored
fix(scripts): update release issue process (#537)
1 parent 4c0c681 commit 1b491e5

16 files changed

+313
-453
lines changed

.github/workflows/check.yml

+2
Original file line numberDiff line numberDiff line change
@@ -325,9 +325,11 @@ jobs:
325325
env:
326326
GITHUB_TOKEN: ${{ secrets.TOKEN_GENERATE_BOT }}
327327
PR_NUMBER: ${{ github.event.number }}
328+
HEAD_COMMIT_MESSAGE: ${{ github.event.head_commit.message }}
328329

329330
- name: Spread generation to each repository
330331
if: ${{ steps.pushGeneratedCode.exitcode == 0 && github.ref == 'refs/heads/main' }}
331332
run: yarn workspace scripts spreadGeneration
332333
env:
333334
GITHUB_TOKEN: ${{ secrets.TOKEN_GENERATE_BOT }}
335+
HEAD_COMMIT_MESSAGE: ${{ github.event.head_commit.message }}

.github/workflows/process-release.yml

-29
This file was deleted.

scripts/ci/codegen/pushGeneratedCode.ts

+20-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import { getNbGitDiff } from '../utils';
66
import text from './text';
77

88
const PR_NUMBER = parseInt(process.env.PR_NUMBER || '0', 10);
9+
const IS_RELEASE_COMMIT =
10+
process.env.HEAD_COMMIT_MESSAGE?.startsWith(
11+
text.commitPrepareReleaseMessage
12+
) || false;
913

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

63-
const commitMessage = await run(`git show -s ${baseBranch} --format="${
64-
text.commitStartMessage
65-
} %H. ${isMainBranch ? '[skip ci]' : ''}
67+
const skipCi = isMainBranch ? '[skip ci]' : '';
68+
let message = await run(
69+
`git show -s ${baseBranch} --format="${text.commitStartMessage} %H. ${skipCi}"`
70+
);
71+
const authors = await run(
72+
`git show -s ${baseBranch} --format="
6673
6774
Co-authored-by: %an <%ae>
68-
%(trailers:key=Co-authored-by)"`);
75+
%(trailers:key=Co-authored-by)"`
76+
);
77+
78+
if (IS_RELEASE_COMMIT && isMainBranch) {
79+
message = text.commitReleaseMessage;
80+
}
81+
82+
message += authors;
6983

7084
console.log(`Pushing code to generated branch: '${branchToPush}'`);
71-
await run(`git add .`);
72-
await run(`git commit -m "${commitMessage}"`);
85+
await run('git add .');
86+
await run(`git commit -m "${message}"`);
7387
await run(`git push origin ${branchToPush}`);
7488

7589
if (PR_NUMBER) {

scripts/ci/codegen/spreadGeneration.ts

+39-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-disable no-console */
2+
import execa from 'execa';
23
import { copy } from 'fs-extra';
34

45
import {
@@ -10,13 +11,22 @@ import {
1011
REPO_URL,
1112
ensureGitHubToken,
1213
} from '../../common';
13-
import { getLanguageFolder } from '../../config';
14-
import { cloneRepository, configureGitHubAuthor } from '../../release/common';
14+
import { getLanguageFolder, getPackageVersionDefault } from '../../config';
15+
import {
16+
cloneRepository,
17+
configureGitHubAuthor,
18+
RELEASED_TAG,
19+
} from '../../release/common';
1520
import type { Language } from '../../types';
1621
import { getNbGitDiff } from '../utils';
1722

1823
import text from './text';
1924

25+
const IS_RELEASE_COMMIT =
26+
process.env.HEAD_COMMIT_MESSAGE?.startsWith(
27+
text.commitPrepareReleaseMessage
28+
) || false;
29+
2030
export function decideWhereToSpread(commitMessage: string): Language[] {
2131
if (commitMessage.startsWith('chore: release')) {
2232
return [];
@@ -77,6 +87,21 @@ async function spreadGeneration(): Promise<void> {
7787
const commitMessage = cleanUpCommitMessage(lastCommitMessage);
7888
const langs = decideWhereToSpread(lastCommitMessage);
7989

90+
// At this point, we know the release will happen on every clients
91+
// So we want to set the released tag at the monorepo level too.
92+
if (IS_RELEASE_COMMIT) {
93+
// remove old `released` tag
94+
await run(
95+
`git fetch origin refs/tags/${RELEASED_TAG}:refs/tags/${RELEASED_TAG}`
96+
);
97+
await run(`git tag -d ${RELEASED_TAG}`);
98+
await run(`git push --delete origin ${RELEASED_TAG}`);
99+
100+
// create new `released` tag
101+
await run(`git tag released`);
102+
await run(`git push --tags`);
103+
}
104+
80105
for (const lang of langs) {
81106
const { tempGitDir } = await cloneRepository({
82107
lang,
@@ -100,14 +125,24 @@ async function spreadGeneration(): Promise<void> {
100125
continue;
101126
}
102127

128+
const version = getPackageVersionDefault(lang);
129+
const message = IS_RELEASE_COMMIT
130+
? `chore: release ${version}`
131+
: commitMessage;
132+
103133
await configureGitHubAuthor(tempGitDir);
104134
await run(`git add .`, { cwd: tempGitDir });
105135
await gitCommit({
106-
message: commitMessage,
136+
message,
107137
coAuthors: [author, ...coAuthors],
108138
cwd: tempGitDir,
109139
});
110-
await run(`git push`, { cwd: tempGitDir });
140+
await execa('git', ['tag', version], {
141+
cwd: tempGitDir,
142+
});
143+
await run(IS_RELEASE_COMMIT ? 'git push --follow-tags' : 'git push', {
144+
cwd: tempGitDir,
145+
});
111146
console.log(`✅ Spread the generation to ${lang} repository.`);
112147
}
113148
}

scripts/ci/codegen/text.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { MAIN_BRANCH, REPO_URL } from '../../common';
1+
import { MAIN_BRANCH, REPO_URL, TODAY } from '../../common';
22

33
export default {
44
commitStartMessage: 'chore: generated code for commit',
5+
commitPrepareReleaseMessage: 'chore: prepare-release-',
6+
commitReleaseMessage: `chore: release ${TODAY}`,
57
notification: {
68
header: '### 🔨 The codegen job will run at the end of the CI.',
79
body: (): string =>

scripts/ci/husky/pre-commit.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ async function preCommit() {
6464
}
6565

6666
console.log(
67-
chalk.bgYellow('[INFO]'),
67+
chalk.black.bgYellow('[INFO]'),
6868
`Generated file found, unstaging: ${stagedFile}`
6969
);
7070

scripts/common.ts

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export const MAIN_BRANCH = releaseConfig.mainBranch;
2323
export const OWNER = releaseConfig.owner;
2424
export const REPO = releaseConfig.repo;
2525
export const REPO_URL = `https://github.com/${OWNER}/${REPO}`;
26+
export const TODAY = new Date().toISOString().split('T')[0];
2627

2728
export const CI = Boolean(process.env.CI);
2829
export const DOCKER = Boolean(process.env.DOCKER);

scripts/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"createMatrix": "ts-node ci/githubActions/createMatrix.ts",
77
"createReleaseIssue": "ts-node release/create-release-issue.ts",
88
"pre-commit": "./ci/husky/pre-commit.js",
9-
"processRelease": "ts-node release/process-release.ts",
109
"pushGeneratedCode": "ts-node ci/codegen/pushGeneratedCode.ts",
1110
"renovateWeeklyPR": "ts-node ci/githubActions/renovateWeeklyPR.ts",
1211
"setRunVariables": "ts-node ci/githubActions/setRunVariables.ts",

scripts/release/__tests__/common.test.ts

-63
This file was deleted.

scripts/release/__tests__/create-release-issue.test.ts

+22-10
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from '../create-release-issue';
99

1010
describe('create release issue', () => {
11-
it('reads versions openapitools.json', () => {
11+
it('reads versions of the current language', () => {
1212
expect(readVersions()).toEqual({
1313
java: {
1414
current: expect.any(String),
@@ -53,7 +53,19 @@ describe('create release issue', () => {
5353

5454
it('returns error when it is a generated commit', () => {
5555
expect(
56-
parseCommit(`${generationCommitText.commitStartMessage} ABCDEF`)
56+
parseCommit(
57+
`49662518 ${generationCommitText.commitStartMessage} ABCDEF`
58+
)
59+
).toEqual({
60+
error: 'generation-commit',
61+
});
62+
});
63+
64+
it('returns error when it is a generated commit, even with other casing', () => {
65+
expect(
66+
parseCommit(
67+
`49662518 ${generationCommitText.commitStartMessage.toLocaleUpperCase()} ABCDEF`
68+
)
5769
).toEqual({
5870
error: 'generation-commit',
5971
});
@@ -80,9 +92,9 @@ describe('create release issue', () => {
8092
},
8193
})
8294
).toMatchInlineSnapshot(`
83-
"- [x] javascript: 0.0.1 -> \`patch\` _(e.g. 0.0.2)_
84-
- [x] java: 0.0.1 -> \`patch\` _(e.g. 0.0.2)_
85-
- [x] php: 0.0.1 -> \`patch\` _(e.g. 0.0.2)_"
95+
"- javascript: 0.0.1 -> **\`patch\` _(e.g. 0.0.2)_**
96+
- java: 0.0.1 -> **\`patch\` _(e.g. 0.0.2)_**
97+
- php: 0.0.1 -> **\`patch\` _(e.g. 0.0.2)_**"
8698
`);
8799
});
88100

@@ -106,8 +118,8 @@ describe('create release issue', () => {
106118
},
107119
})
108120
).toMatchInlineSnapshot(`
109-
"- [x] javascript: 0.0.1 -> \`patch\` _(e.g. 0.0.2)_
110-
- [x] java: 0.0.1 -> \`patch\` _(e.g. 0.0.2)_
121+
"- javascript: 0.0.1 -> **\`patch\` _(e.g. 0.0.2)_**
122+
- java: 0.0.1 -> **\`patch\` _(e.g. 0.0.2)_**
111123
- ~php: 0.0.1 (no commit)~"
112124
`);
113125
});
@@ -132,10 +144,10 @@ describe('create release issue', () => {
132144
},
133145
})
134146
).toMatchInlineSnapshot(`
135-
"- [x] javascript: 0.0.1 -> \`patch\` _(e.g. 0.0.2)_
136-
- [ ] java: 0.0.1 -> \`patch\` _(e.g. 0.0.2)_
147+
"- javascript: 0.0.1 -> **\`patch\` _(e.g. 0.0.2)_**
148+
- ~java: 0.0.1 -> **\`patch\` _(e.g. 0.0.2)_**~
137149
- No \`feat\` or \`fix\` commit, thus unchecked by default.
138-
- [x] php: 0.0.1 -> \`minor\` _(e.g. 0.1.0)_"
150+
- php: 0.0.1 -> **\`minor\` _(e.g. 0.1.0)_**"
139151
`);
140152
});
141153
});

scripts/release/__tests__/process-release.test.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ describe('process release', () => {
77
const versions = getVersionsToRelease(`
88
## Version Changes
99
10-
- [x] javascript: 1.0.0 -> \`minor\` (e.g. 1.1.0)
11-
- [x] php: 2.0.0 -> \`patch\` (e.g. 2.0.1)
12-
- [ ] java: 3.0.0 -> \`patch\` (e.g. 3.0.1)
10+
- javascript: 1.0.0 -> **\`minor\` _(e.g. 1.1.0)_**
11+
- ~java: 3.0.0 -> **\`patch\` _(e.g. 3.0.1)_**~
12+
- No \`feat\` or \`fix\` commit, thus unchecked by default.
13+
- php: 2.0.0 -> **\`patch\` _(e.g. 2.0.1)_**
1314
`);
1415

1516
expect(Object.keys(versions)).toEqual(['javascript', 'php']);

scripts/release/common.ts

-16
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,6 @@ export function getGitAuthor(): { name: string; email: string } {
1616
return config.gitAuthor;
1717
}
1818

19-
export function getMarkdownSection(markdown: string, title: string): string {
20-
const levelIndicator = title.split(' ')[0]; // e.g. `##`
21-
const lines = markdown
22-
.slice(markdown.indexOf(title))
23-
.split('\n')
24-
.map((line) => line.trim());
25-
let endIndex = lines.length;
26-
for (let i = 1; i < lines.length; i++) {
27-
if (lines[i].startsWith(`${levelIndicator} `)) {
28-
endIndex = i;
29-
break;
30-
}
31-
}
32-
return lines.slice(0, endIndex).join('\n');
33-
}
34-
3519
export async function configureGitHubAuthor(cwd?: string): Promise<void> {
3620
const { name, email } = getGitAuthor();
3721

0 commit comments

Comments
 (0)