Skip to content

Commit c8b8a28

Browse files
authored
chore(ci): fix release patch version (#17278)
1 parent 5faf4a6 commit c8b8a28

File tree

1 file changed

+119
-79
lines changed

1 file changed

+119
-79
lines changed

.github/scripts/bump_version.js

+119-79
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,118 @@ module.exports = async ({ github, context, core }) => {
1111
const RE_TAG_NIGHTLY = /^v(\d+)\.(\d+)\.(\d+)-nightly$/;
1212
const RE_TAG_PATCH = /^v(\d+)\.(\d+)\.(\d+)-p(\d+)$/;
1313

14-
switch (TYPE) {
15-
case "":
16-
case "nightly": {
17-
core.setOutput("sha", context.sha);
18-
core.info(`Nightly release triggered by (${context.sha})`);
14+
async function getPreviousNightlyRelease(github, context) {
15+
const releases = await github.rest.repos.listReleases({
16+
owner: context.repo.owner,
17+
repo: context.repo.repo,
18+
});
19+
for (const release of releases.data) {
20+
const ret = RE_TAG_NIGHTLY.exec(release.tag_name);
21+
if (ret) {
22+
return release.tag_name;
23+
}
24+
}
25+
}
26+
27+
function getNextNightlyRelease(previous) {
28+
const nightly = RE_TAG_NIGHTLY.exec(previous);
29+
if (nightly) {
30+
const major = nightly[1];
31+
const minor = nightly[2];
32+
const patch = parseInt(nightly[3]) + 1;
33+
return `v${major}.${minor}.${patch}-nightly`;
34+
}
35+
}
1936

20-
let previous = null;
37+
async function getPreviousStableRelease(github, context) {
38+
let page = 1;
39+
while (true) {
2140
const releases = await github.rest.repos.listReleases({
2241
owner: context.repo.owner,
2342
repo: context.repo.repo,
43+
page,
2444
});
45+
if (releases.data.length === 0) {
46+
break;
47+
}
48+
page++;
2549
for (const release of releases.data) {
26-
const ret = RE_TAG_NIGHTLY.exec(release.tag_name);
50+
const ret = RE_TAG_STABLE.exec(release.tag_name);
2751
if (ret) {
28-
previous = release.tag_name;
29-
break;
52+
return release.tag_name;
53+
}
54+
}
55+
}
56+
}
57+
58+
function getNextStableRelease() {
59+
const nightly = RE_TAG_NIGHTLY.exec(TAG);
60+
if (nightly) {
61+
const major = nightly[1];
62+
const minor = nightly[2];
63+
const patch = nightly[3];
64+
return `v${major}.${minor}.${patch}`;
65+
}
66+
}
67+
68+
async function getPreviousPatchRelease(github, context) {
69+
let page = 1;
70+
while (true) {
71+
const releases = await github.rest.repos.listReleases({
72+
owner: context.repo.owner,
73+
repo: context.repo.repo,
74+
page,
75+
});
76+
if (releases.data.length === 0) {
77+
break;
78+
}
79+
page++;
80+
for (const release of releases.data) {
81+
if (!release.tag_name.startsWith(TAG)) {
82+
continue;
83+
}
84+
if (release.tag_name === TAG) {
85+
// no previous patch release, use the previous stable release
86+
return release.tag_name;
87+
}
88+
const ret = RE_TAG_PATCH.exec(release.tag_name);
89+
if (!ret) {
90+
core.warning(`Ignore invalid patch release ${release.tag_name}`);
91+
continue;
3092
}
93+
return release.tag_name;
94+
}
95+
}
96+
}
97+
98+
function getNextPatchRelease(previous) {
99+
const stable = RE_TAG_STABLE.exec(previous);
100+
if (stable) {
101+
const major = stable[1];
102+
const minor = stable[2];
103+
const patch = stable[3];
104+
return `v${major}.${minor}.${patch}-p1`;
105+
}
106+
const patch = RE_TAG_PATCH.exec(previous);
107+
if (patch) {
108+
const major = patch[1];
109+
const minor = patch[2];
110+
const patch = patch[3];
111+
const pv = parseInt(patch[4]);
112+
return `v${major}.${minor}.${patch}-p${pv + 1}`;
113+
}
114+
}
115+
116+
switch (TYPE) {
117+
case "":
118+
case "nightly": {
119+
core.setOutput("sha", context.sha);
120+
core.info(`Nightly release triggered by (${context.sha})`);
121+
122+
const previous = await getPreviousNightlyRelease(github, context);
123+
if (!previous) {
124+
core.setFailed(`No previous nightly release found, ignoring`);
125+
return;
31126
}
32127
core.setOutput("previous", previous);
33128
core.info(`Nightly release with previous release: ${previous}`);
@@ -37,15 +132,11 @@ module.exports = async ({ github, context, core }) => {
37132
core.info(`Release create manually with tag ${TAG}`);
38133
return;
39134
}
40-
const result = RE_TAG_NIGHTLY.exec(previous);
41-
if (!result) {
42-
core.setFailed(`The previous tag ${previous} is invalid.`);
135+
const nextTag = getNextNightlyRelease(previous);
136+
if (!nextTag) {
137+
core.setFailed(`No next nightly release from ${previous}`);
43138
return;
44139
}
45-
const major = result[1];
46-
const minor = result[2];
47-
const patch = (parseInt(result[3]) + 1).toString();
48-
const nextTag = `v${major}.${minor}.${patch}-nightly`;
49140
core.setOutput("tag", nextTag);
50141
core.info(`Release create new nightly ${nextTag}`);
51142
return;
@@ -58,38 +149,15 @@ module.exports = async ({ github, context, core }) => {
58149
return;
59150
}
60151
core.info(`Stable release triggered by ${TAG} (${context.sha})`);
61-
const result = RE_TAG_NIGHTLY.exec(TAG);
62-
if (!result) {
63-
core.setFailed(`The tag ${TAG} is invalid, ignoring`);
152+
const nextTag = getNextStableRelease();
153+
if (!nextTag) {
154+
core.setFailed(`No stable release from ${TAG}`);
64155
return;
65156
}
66-
const major = result[1];
67-
const minor = result[2];
68-
const patch = result[3];
69-
const nextTag = `v${major}.${minor}.${patch}`;
70157
core.setOutput("tag", nextTag);
71158
core.info(`Stable release ${nextTag} from ${TAG}`);
72159

73-
let previous = null;
74-
let page = 1;
75-
while (true) {
76-
const releases = await github.rest.repos.listReleases({
77-
owner: context.repo.owner,
78-
repo: context.repo.repo,
79-
page,
80-
});
81-
if (releases.data.length === 0) {
82-
break;
83-
}
84-
page++;
85-
for (const release of releases.data) {
86-
const ret = RE_TAG_STABLE.exec(release.tag_name);
87-
if (ret) {
88-
previous = release.tag_name;
89-
break;
90-
}
91-
}
92-
}
160+
const previous = await getPreviousStableRelease(github, context);
93161
if (!previous) {
94162
core.setFailed(`No previous stable release found, ignoring`);
95163
return;
@@ -120,49 +188,21 @@ module.exports = async ({ github, context, core }) => {
120188
`Patch release triggered by ${TAG} (${branch.data.commit.sha})`
121189
);
122190

123-
let pv = 1;
124-
let previous = null;
125-
let page = 1;
126-
while (true) {
127-
const releases = await github.rest.repos.listReleases({
128-
owner: context.repo.owner,
129-
repo: context.repo.repo,
130-
page,
131-
});
132-
if (releases.data.length === 0) {
133-
break;
134-
}
135-
page++;
136-
for (const release of releases.data) {
137-
if (!release.tag_name.startsWith(TAG)) {
138-
continue;
139-
}
140-
if (release.tag_name === TAG) {
141-
previous = release.tag_name;
142-
break;
143-
}
144-
const ret = RE_TAG_PATCH.exec(release.tag_name);
145-
if (!ret) {
146-
core.warning(`Ignore previous release ${release.tag_name}`);
147-
continue;
148-
}
149-
pv = parseInt(result[4]) + 1;
150-
previous = release.tag_name;
151-
}
152-
}
191+
const previous = await getPreviousPatchRelease(github, context);
153192
if (!previous) {
154-
core.setFailed(`No previous stable release found, ignoring`);
193+
core.setFailed(`No previous patch release found, ignoring`);
155194
return;
156195
}
157196
core.setOutput("previous", previous);
158197
core.info(`Patch release with previous release: ${previous}`);
159198

160-
const major = result[1];
161-
const minor = result[2];
162-
const patch = result[3];
163-
const nextTag = `v${major}.${minor}.${patch}-p${pv}`;
199+
const nextTag = getNextPatchRelease(previous);
200+
if (!nextTag) {
201+
core.setFailed(`No next patch release from ${previous}`);
202+
return;
203+
}
164204
core.setOutput("tag", nextTag);
165-
core.info(`Patch release ${nextTag} from ${TAG}`);
205+
core.info(`Patch release ${nextTag} from ${previous}`);
166206
return;
167207
}
168208

0 commit comments

Comments
 (0)