Skip to content

Commit c7b9e12

Browse files
authored
fix(scripts/release): fetch origin/main, try pr create first, error if spawn() returns non-zero status (#24914)
* fix(scripts/release): error if spawn() command returns non-zero status * fix(workflows/release-pr): remove unnecessary "main" ref * chore(scripts/release): print gh command output * fix(scripts/release): create PR, fallback to edit PR * fix(scripts/release): start by fetching main * fix(scripts/release): diff against origin/main, not main * fix(scripts/release): create release branch from origin/main * chore(scripts/release): refine log messages
1 parent 8d69895 commit c7b9e12

File tree

4 files changed

+27
-10
lines changed

4 files changed

+27
-10
lines changed

.github/workflows/release-pr.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ jobs:
2323
with:
2424
fetch-depth: 0
2525
fetch-tags: true
26-
ref: main
2726

2827
- name: Setup Node
2928
uses: actions/setup-node@v4

scripts/release/index.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
getRefDate,
1818
keypress,
1919
spawn,
20+
fetchMain,
2021
} from './utils.js';
2122

2223
/**
@@ -53,25 +54,31 @@ const commitAndPR = async (
5354
console.log('');
5455
}
5556

57+
console.log(chalk`{blue Preparing release branch...}`);
5658
exec(`
5759
git stash
58-
git switch -C ${branch} main
60+
git switch -C ${branch} origin/main
5961
git stash pop
6062
git add package.json package-lock.json RELEASE_NOTES.md
6163
`);
6264

65+
console.log(chalk`{blue Committing changes...}`);
6366
await temporaryWriteTask(message, (commitFile) =>
6467
exec(`git commit --file ${commitFile}`),
6568
);
6669

70+
console.log(chalk`{blue Pushing release branch...}`);
6771
exec(`git push --force --set-upstream origin ${branch}`);
6872

73+
console.log(chalk`{blue Creating/editing pull request...}`);
6974
await temporaryWriteTask(pr.body, (bodyFile) => {
7075
const commonArgs = ['--title', pr.title, '--body-file', bodyFile];
7176
try {
72-
spawn('gh', ['pr', 'edit', ...commonArgs]);
77+
const stdout = spawn('gh', ['pr', 'create', ...commonArgs]);
78+
console.log(stdout);
7379
} catch (e) {
74-
spawn('gh', ['pr', 'create', ...commonArgs]);
80+
const stdout = spawn('gh', ['pr', 'edit', ...commonArgs]);
81+
console.log(stdout);
7582
}
7683
});
7784

@@ -88,6 +95,9 @@ const main = async () => {
8895
requireGitHubCLI();
8996
requireWriteAccess();
9097

98+
console.log(chalk`{blue Fetching main branch...}`);
99+
fetchMain();
100+
91101
console.log(chalk`{blue Getting last version...}`);
92102
const lastVersion = getLatestTag();
93103
const lastVersionDate = getRefDate(lastVersion);
@@ -118,11 +128,10 @@ const main = async () => {
118128
console.log(chalk`{blue Getting lists of added/removed features...}`);
119129
const changes = await getChanges(lastVersionDate);
120130

121-
console.log(chalk`{blue Applying changelog...}`);
131+
console.log(chalk`{blue Updating release notes...}`);
122132
const notes = getNotes(thisVersion, changes, stats, versionBump);
123133
await addNotes(notes, versionBump, lastVersion);
124134

125-
console.log(chalk`{blue Creating pull request...}`);
126135
const title = `Release ${thisVersion}`;
127136
const body = `(This release was generated by the project's release script.)\n\n${notes}`;
128137
await commitAndPR(

scripts/release/stats.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const contributors = (): number => {
5454
*/
5555
const stats = (start: string): ChangeStats => {
5656
// Get just the diff stats summary
57-
const diff = exec(`git diff --shortstat ${start}...main`);
57+
const diff = exec(`git diff --shortstat ${start}...origin/main`);
5858
if (diff === '') {
5959
console.error(chalk`{red No changes for which to generate statistics.}`);
6060
process.exit(1);
@@ -73,7 +73,7 @@ const stats = (start: string): ChangeStats => {
7373
const { changed, insertions, deletions } = match.groups as any;
7474

7575
// Get the number of commits
76-
const commits = exec(`git rev-list --count ${start}...main`);
76+
const commits = exec(`git rev-list --count ${start}...origin/main`);
7777

7878
return {
7979
commits: Number(commits),

scripts/release/utils.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ export const spawn = (
3737
throw result.error;
3838
}
3939

40-
if (result.stderr) {
41-
throw new Error(result.stderr);
40+
if (result.status !== 0) {
41+
throw new Error(
42+
`The command '${command}' returned non-zero exit status ${result.status}: ${result.stderr}`,
43+
);
4244
}
4345

4446
return result.stdout.trim();
@@ -102,6 +104,13 @@ export const queryPRs = (queryArgs: any): any => {
102104
return JSON.parse(exec(command));
103105
};
104106

107+
/**
108+
* Ensures main is fetched.
109+
*/
110+
export const fetchMain = (): void => {
111+
exec('git fetch origin main');
112+
};
113+
105114
/**
106115
* Get the latest Git tag
107116
* @returns The latest Git tag

0 commit comments

Comments
 (0)