Skip to content

Commit

Permalink
chore(ci): fix release patch version (#17278)
Browse files Browse the repository at this point in the history
  • Loading branch information
everpcpc authored Jan 14, 2025
1 parent 5faf4a6 commit c8b8a28
Showing 1 changed file with 119 additions and 79 deletions.
198 changes: 119 additions & 79 deletions .github/scripts/bump_version.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,118 @@ module.exports = async ({ github, context, core }) => {
const RE_TAG_NIGHTLY = /^v(\d+)\.(\d+)\.(\d+)-nightly$/;
const RE_TAG_PATCH = /^v(\d+)\.(\d+)\.(\d+)-p(\d+)$/;

switch (TYPE) {
case "":
case "nightly": {
core.setOutput("sha", context.sha);
core.info(`Nightly release triggered by (${context.sha})`);
async function getPreviousNightlyRelease(github, context) {
const releases = await github.rest.repos.listReleases({
owner: context.repo.owner,
repo: context.repo.repo,
});
for (const release of releases.data) {
const ret = RE_TAG_NIGHTLY.exec(release.tag_name);
if (ret) {
return release.tag_name;
}
}
}

function getNextNightlyRelease(previous) {
const nightly = RE_TAG_NIGHTLY.exec(previous);
if (nightly) {
const major = nightly[1];
const minor = nightly[2];
const patch = parseInt(nightly[3]) + 1;
return `v${major}.${minor}.${patch}-nightly`;
}
}

let previous = null;
async function getPreviousStableRelease(github, context) {
let page = 1;
while (true) {
const releases = await github.rest.repos.listReleases({
owner: context.repo.owner,
repo: context.repo.repo,
page,
});
if (releases.data.length === 0) {
break;
}
page++;
for (const release of releases.data) {
const ret = RE_TAG_NIGHTLY.exec(release.tag_name);
const ret = RE_TAG_STABLE.exec(release.tag_name);
if (ret) {
previous = release.tag_name;
break;
return release.tag_name;
}
}
}
}

function getNextStableRelease() {
const nightly = RE_TAG_NIGHTLY.exec(TAG);
if (nightly) {
const major = nightly[1];
const minor = nightly[2];
const patch = nightly[3];
return `v${major}.${minor}.${patch}`;
}
}

async function getPreviousPatchRelease(github, context) {
let page = 1;
while (true) {
const releases = await github.rest.repos.listReleases({
owner: context.repo.owner,
repo: context.repo.repo,
page,
});
if (releases.data.length === 0) {
break;
}
page++;
for (const release of releases.data) {
if (!release.tag_name.startsWith(TAG)) {
continue;
}
if (release.tag_name === TAG) {
// no previous patch release, use the previous stable release
return release.tag_name;
}
const ret = RE_TAG_PATCH.exec(release.tag_name);
if (!ret) {
core.warning(`Ignore invalid patch release ${release.tag_name}`);
continue;
}
return release.tag_name;
}
}
}

function getNextPatchRelease(previous) {
const stable = RE_TAG_STABLE.exec(previous);
if (stable) {
const major = stable[1];
const minor = stable[2];
const patch = stable[3];
return `v${major}.${minor}.${patch}-p1`;
}
const patch = RE_TAG_PATCH.exec(previous);
if (patch) {
const major = patch[1];
const minor = patch[2];
const patch = patch[3];
const pv = parseInt(patch[4]);
return `v${major}.${minor}.${patch}-p${pv + 1}`;
}
}

switch (TYPE) {
case "":
case "nightly": {
core.setOutput("sha", context.sha);
core.info(`Nightly release triggered by (${context.sha})`);

const previous = await getPreviousNightlyRelease(github, context);
if (!previous) {
core.setFailed(`No previous nightly release found, ignoring`);
return;
}
core.setOutput("previous", previous);
core.info(`Nightly release with previous release: ${previous}`);
Expand All @@ -37,15 +132,11 @@ module.exports = async ({ github, context, core }) => {
core.info(`Release create manually with tag ${TAG}`);
return;
}
const result = RE_TAG_NIGHTLY.exec(previous);
if (!result) {
core.setFailed(`The previous tag ${previous} is invalid.`);
const nextTag = getNextNightlyRelease(previous);
if (!nextTag) {
core.setFailed(`No next nightly release from ${previous}`);
return;
}
const major = result[1];
const minor = result[2];
const patch = (parseInt(result[3]) + 1).toString();
const nextTag = `v${major}.${minor}.${patch}-nightly`;
core.setOutput("tag", nextTag);
core.info(`Release create new nightly ${nextTag}`);
return;
Expand All @@ -58,38 +149,15 @@ module.exports = async ({ github, context, core }) => {
return;
}
core.info(`Stable release triggered by ${TAG} (${context.sha})`);
const result = RE_TAG_NIGHTLY.exec(TAG);
if (!result) {
core.setFailed(`The tag ${TAG} is invalid, ignoring`);
const nextTag = getNextStableRelease();
if (!nextTag) {
core.setFailed(`No stable release from ${TAG}`);
return;
}
const major = result[1];
const minor = result[2];
const patch = result[3];
const nextTag = `v${major}.${minor}.${patch}`;
core.setOutput("tag", nextTag);
core.info(`Stable release ${nextTag} from ${TAG}`);

let previous = null;
let page = 1;
while (true) {
const releases = await github.rest.repos.listReleases({
owner: context.repo.owner,
repo: context.repo.repo,
page,
});
if (releases.data.length === 0) {
break;
}
page++;
for (const release of releases.data) {
const ret = RE_TAG_STABLE.exec(release.tag_name);
if (ret) {
previous = release.tag_name;
break;
}
}
}
const previous = await getPreviousStableRelease(github, context);
if (!previous) {
core.setFailed(`No previous stable release found, ignoring`);
return;
Expand Down Expand Up @@ -120,49 +188,21 @@ module.exports = async ({ github, context, core }) => {
`Patch release triggered by ${TAG} (${branch.data.commit.sha})`
);

let pv = 1;
let previous = null;
let page = 1;
while (true) {
const releases = await github.rest.repos.listReleases({
owner: context.repo.owner,
repo: context.repo.repo,
page,
});
if (releases.data.length === 0) {
break;
}
page++;
for (const release of releases.data) {
if (!release.tag_name.startsWith(TAG)) {
continue;
}
if (release.tag_name === TAG) {
previous = release.tag_name;
break;
}
const ret = RE_TAG_PATCH.exec(release.tag_name);
if (!ret) {
core.warning(`Ignore previous release ${release.tag_name}`);
continue;
}
pv = parseInt(result[4]) + 1;
previous = release.tag_name;
}
}
const previous = await getPreviousPatchRelease(github, context);
if (!previous) {
core.setFailed(`No previous stable release found, ignoring`);
core.setFailed(`No previous patch release found, ignoring`);
return;
}
core.setOutput("previous", previous);
core.info(`Patch release with previous release: ${previous}`);

const major = result[1];
const minor = result[2];
const patch = result[3];
const nextTag = `v${major}.${minor}.${patch}-p${pv}`;
const nextTag = getNextPatchRelease(previous);
if (!nextTag) {
core.setFailed(`No next patch release from ${previous}`);
return;
}
core.setOutput("tag", nextTag);
core.info(`Patch release ${nextTag} from ${TAG}`);
core.info(`Patch release ${nextTag} from ${previous}`);
return;
}

Expand Down

0 comments on commit c8b8a28

Please sign in to comment.