Skip to content

Commit 10cdbbd

Browse files
Copilotdibarbet
andcommitted
Address code review feedback: refactor version helpers and workflow
Co-authored-by: dibarbet <5749229+dibarbet@users.noreply.github.com>
1 parent 10b97f0 commit 10cdbbd

File tree

2 files changed

+44
-30
lines changed

2 files changed

+44
-30
lines changed

.github/workflows/branch-snap.yml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,12 @@ jobs:
6363
- name: Install dependencies
6464
run: npm ci
6565
- name: Update version.json for release
66-
run: npx gulp updateVersionForRelease
67-
- name: Commit and push version update
68-
run: |
69-
git config --local user.name "github-actions[bot]"
70-
git config --local user.email "github-actions[bot]@users.noreply.github.com"
71-
git add version.json
72-
git commit -m "Update version for stable release"
73-
git push
66+
run: npx gulp updateVersionForStableRelease
67+
- name: Create PR with version update
68+
uses: peter-evans/create-pull-request@v4
69+
with:
70+
token: ${{ secrets.GITHUB_TOKEN }}
71+
commit-message: Update version for stable release
72+
title: '[automated] Update version for stable release'
73+
branch: merge/prerelease-to-release
74+
base: release

tasks/snapTasks.ts

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,41 +38,59 @@ export function getNextReleaseVersion(currentVersion: string): string {
3838
return `${major}.${nextTensMinor}`;
3939
}
4040

41+
/**
42+
* Read and parse version.json
43+
* @returns The parsed version.json object
44+
*/
45+
function readVersionJson(): { version: string; [key: string]: unknown } {
46+
const versionFilePath = path.join(path.resolve(__dirname, '..'), 'version.json');
47+
const file = fs.readFileSync(versionFilePath, 'utf8');
48+
return JSON.parse(file);
49+
}
50+
51+
/**
52+
* Write version.json with the given version
53+
* @param versionJson The version.json object to write
54+
*/
55+
function writeVersionJson(versionJson: { version: string; [key: string]: unknown }): void {
56+
const versionFilePath = path.join(path.resolve(__dirname, '..'), 'version.json');
57+
const newJson = JSON.stringify(versionJson, null, 4);
58+
console.log(`New json: ${newJson}`);
59+
fs.writeFileSync(versionFilePath, newJson);
60+
}
61+
4162
gulp.task('incrementVersion', async (): Promise<void> => {
4263
const argv = minimist(process.argv.slice(2));
4364
const isReleaseCandidate = argv['releaseCandidate'] === true || argv['releaseCandidate'] === 'true';
4465

4566
// Get the current version from version.json
46-
const versionFilePath = path.join(path.resolve(__dirname, '..'), 'version.json');
47-
const file = fs.readFileSync(versionFilePath, 'utf8');
48-
const versionJson = JSON.parse(file);
67+
const versionJson = readVersionJson();
4968

5069
// Calculate new version
5170
const version = versionJson.version as string;
52-
const split = version.split('.');
53-
let newVersion: string;
71+
let split: string[];
5472

5573
if (isReleaseCandidate) {
5674
// If this is a release candidate, increment to be higher than the next stable version
5775
// e.g., if current is 2.74, next stable is 2.80, so main should be 2.81
5876
const nextStableVersion = getNextReleaseVersion(version);
59-
const stableSplit = nextStableVersion.split('.');
60-
newVersion = `${stableSplit[0]}.${parseInt(stableSplit[1]) + 1}`;
77+
split = nextStableVersion.split('.');
6178
console.log(
62-
`Release candidate mode: Updating ${version} to ${newVersion} (next stable would be ${nextStableVersion})`
79+
`Release candidate mode: Updating ${version} to ${split[0]}.${
80+
parseInt(split[1]) + 1
81+
} (next stable would be ${nextStableVersion})`
6382
);
6483
} else {
6584
// Normal increment: just increment the minor version
66-
newVersion = `${split[0]}.${parseInt(split[1]) + 1}`;
67-
console.log(`Updating ${version} to ${newVersion}`);
85+
split = version.split('.');
86+
console.log(`Updating ${version} to ${split[0]}.${parseInt(split[1]) + 1}`);
6887
}
6988

89+
const newVersion = `${split[0]}.${parseInt(split[1]) + 1}`;
90+
7091
// Write the new version back to version.json
7192
versionJson.version = newVersion;
72-
const newJson = JSON.stringify(versionJson, null, 4);
73-
console.log(`New json: ${newJson}`);
74-
75-
fs.writeFileSync(versionFilePath, newJson);
93+
writeVersionJson(versionJson);
7694

7795
// Add a new changelog section for the new version.
7896
console.log('Adding new version header to changelog');
@@ -226,11 +244,9 @@ async function generatePRList(startSHA: string, endSHA: string): Promise<string[
226244
* This task is used when snapping from prerelease to release.
227245
* It updates the version to round up to the next tens version (e.g., 2.74 -> 2.80).
228246
*/
229-
gulp.task('updateVersionForRelease', async (): Promise<void> => {
247+
gulp.task('updateVersionForStableRelease', async (): Promise<void> => {
230248
// Get the current version from version.json
231-
const versionFilePath = path.join(path.resolve(__dirname, '..'), 'version.json');
232-
const file = fs.readFileSync(versionFilePath, 'utf8');
233-
const versionJson = JSON.parse(file);
249+
const versionJson = readVersionJson();
234250

235251
const currentVersion = versionJson.version as string;
236252
const releaseVersion = getNextReleaseVersion(currentVersion);
@@ -239,8 +255,5 @@ gulp.task('updateVersionForRelease', async (): Promise<void> => {
239255

240256
// Write the new version back to version.json
241257
versionJson.version = releaseVersion;
242-
const newJson = JSON.stringify(versionJson, null, 4);
243-
console.log(`New json: ${newJson}`);
244-
245-
fs.writeFileSync(versionFilePath, newJson);
258+
writeVersionJson(versionJson);
246259
});

0 commit comments

Comments
 (0)