Skip to content

Commit fb2d031

Browse files
Fix missing update release body (#365)
* Update json5 deps * Remove quote around env var `GITHUB_TOKEN` * Factorize search by draft & tag * Correct `_release` undefined when creating a new release * Cleanup package-lock
1 parent 08d85b1 commit fb2d031

File tree

4 files changed

+108
-67
lines changed

4 files changed

+108
-67
lines changed

action.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ inputs:
5050
description: "Specifies whether this release should be set as the latest release for the repository. Drafts and prereleases cannot be set as latest. Can be `true`, `false`, or `legacy`. Uses GitHub api default if not provided"
5151
required: false
5252
env:
53-
"GITHUB_TOKEN": "As provided by Github Actions"
53+
GITHUB_TOKEN: "As provided by Github Actions"
5454
outputs:
5555
url:
5656
description: "URL to the Release HTML Page"

dist/index.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"main": "lib/main.js",
77
"scripts": {
88
"build": "ncc build src/main.ts --minify",
9+
"build-debug": "ncc build src/main.ts --v8-cache --source-map",
910
"test": "jest",
1011
"fmt": "prettier --write \"src/**/*.ts\" \"__tests__/**/*.ts\"",
1112
"fmtcheck": "prettier --check \"src/**/*.ts\" \"__tests__/**/*.ts\"",

src/github.ts

+105-65
Original file line numberDiff line numberDiff line change
@@ -208,45 +208,63 @@ export const release = async (
208208
try {
209209
// you can't get a an existing draft by tag
210210
// so we must find one in the list of all releases
211+
let _release: Release | undefined = undefined;
211212
if (config.input_draft) {
212213
for await (const response of releaser.allReleases({
213214
owner,
214215
repo,
215216
})) {
216-
let release = response.data.find((release) => release.tag_name === tag);
217-
if (release) {
218-
return release;
219-
}
217+
_release = response.data.find((release) => release.tag_name === tag);
220218
}
219+
} else {
220+
_release = (
221+
await releaser.getReleaseByTag({
222+
owner,
223+
repo,
224+
tag,
225+
})
226+
).data;
221227
}
222-
let existingRelease = await releaser.getReleaseByTag({
223-
owner,
224-
repo,
225-
tag,
226-
});
228+
if (_release === null || _release === undefined) {
229+
return await createRelease(
230+
tag,
231+
config,
232+
releaser,
233+
owner,
234+
repo,
235+
discussion_category_name,
236+
generate_release_notes,
237+
maxRetries
238+
);
239+
}
240+
241+
let existingRelease: Release = _release!;
242+
console.log(
243+
`Found release ${existingRelease.name} (with id=${existingRelease.id})`
244+
);
227245

228-
const release_id = existingRelease.data.id;
246+
const release_id = existingRelease.id;
229247
let target_commitish: string;
230248
if (
231249
config.input_target_commitish &&
232-
config.input_target_commitish !== existingRelease.data.target_commitish
250+
config.input_target_commitish !== existingRelease.target_commitish
233251
) {
234252
console.log(
235-
`Updating commit from "${existingRelease.data.target_commitish}" to "${config.input_target_commitish}"`
253+
`Updating commit from "${existingRelease.target_commitish}" to "${config.input_target_commitish}"`
236254
);
237255
target_commitish = config.input_target_commitish;
238256
} else {
239-
target_commitish = existingRelease.data.target_commitish;
257+
target_commitish = existingRelease.target_commitish;
240258
}
241259

242260
const tag_name = tag;
243-
const name = config.input_name || existingRelease.data.name || tag;
261+
const name = config.input_name || existingRelease.name || tag;
244262
// revisit: support a new body-concat-strategy input for accumulating
245263
// body parts as a release gets updated. some users will likely want this while
246264
// others won't previously this was duplicating content for most which
247265
// no one wants
248266
const workflowBody = releaseBody(config) || "";
249-
const existingReleaseBody = existingRelease.data.body || "";
267+
const existingReleaseBody = existingRelease.body || "";
250268
let body: string;
251269
if (config.input_append_body && workflowBody && existingReleaseBody) {
252270
body = existingReleaseBody + "\n" + workflowBody;
@@ -257,11 +275,11 @@ export const release = async (
257275
const draft =
258276
config.input_draft !== undefined
259277
? config.input_draft
260-
: existingRelease.data.draft;
278+
: existingRelease.draft;
261279
const prerelease =
262280
config.input_prerelease !== undefined
263281
? config.input_prerelease
264-
: existingRelease.data.prerelease;
282+
: existingRelease.prerelease;
265283

266284
const make_latest = config.input_make_latest;
267285

@@ -288,58 +306,80 @@ export const release = async (
288306
throw error;
289307
}
290308

291-
const tag_name = tag;
292-
const name = config.input_name || tag;
293-
const body = releaseBody(config);
294-
const draft = config.input_draft;
295-
const prerelease = config.input_prerelease;
296-
const target_commitish = config.input_target_commitish;
297-
const make_latest = config.input_make_latest;
298-
let commitMessage: string = "";
299-
if (target_commitish) {
300-
commitMessage = ` using commit "${target_commitish}"`;
301-
}
302-
console.log(
303-
`👩‍🏭 Creating new GitHub release for tag ${tag_name}${commitMessage}...`
309+
return await createRelease(
310+
tag,
311+
config,
312+
releaser,
313+
owner,
314+
repo,
315+
discussion_category_name,
316+
generate_release_notes,
317+
maxRetries
304318
);
305-
try {
306-
let release = await releaser.createRelease({
307-
owner,
308-
repo,
309-
tag_name,
310-
name,
311-
body,
312-
draft,
313-
prerelease,
314-
target_commitish,
315-
discussion_category_name,
316-
generate_release_notes,
317-
make_latest,
318-
});
319-
return release.data;
320-
} catch (error) {
321-
// presume a race with competing matrix runs
322-
console.log(`⚠️ GitHub release failed with status: ${error.status}`);
323-
console.log(`${JSON.stringify(error.response.data)}`);
319+
}
320+
};
324321

325-
switch (error.status) {
326-
case 403:
327-
console.log(
328-
"Skip retry — your GitHub token/PAT does not have the required permission to create a release"
329-
);
330-
throw error;
322+
async function createRelease(
323+
tag: string,
324+
config: Config,
325+
releaser: Releaser,
326+
owner: string,
327+
repo: string,
328+
discussion_category_name: string | undefined,
329+
generate_release_notes: boolean | undefined,
330+
maxRetries: number
331+
) {
332+
const tag_name = tag;
333+
const name = config.input_name || tag;
334+
const body = releaseBody(config);
335+
const draft = config.input_draft;
336+
const prerelease = config.input_prerelease;
337+
const target_commitish = config.input_target_commitish;
338+
const make_latest = config.input_make_latest;
339+
let commitMessage: string = "";
340+
if (target_commitish) {
341+
commitMessage = ` using commit "${target_commitish}"`;
342+
}
343+
console.log(
344+
`👩‍🏭 Creating new GitHub release for tag ${tag_name}${commitMessage}...`
345+
);
346+
try {
347+
let release = await releaser.createRelease({
348+
owner,
349+
repo,
350+
tag_name,
351+
name,
352+
body,
353+
draft,
354+
prerelease,
355+
target_commitish,
356+
discussion_category_name,
357+
generate_release_notes,
358+
make_latest,
359+
});
360+
return release.data;
361+
} catch (error) {
362+
// presume a race with competing matrix runs
363+
console.log(`⚠️ GitHub release failed with status: ${error.status}`);
364+
console.log(`${JSON.stringify(error.response.data)}`);
331365

332-
case 404:
333-
console.log("Skip retry - discussion category mismatch");
334-
throw error;
366+
switch (error.status) {
367+
case 403:
368+
console.log(
369+
"Skip retry — your GitHub token/PAT does not have the required permission to create a release"
370+
);
371+
throw error;
335372

336-
case 422:
337-
console.log("Skip retry - validation failed");
338-
throw error;
339-
}
373+
case 404:
374+
console.log("Skip retry - discussion category mismatch");
375+
throw error;
340376

341-
console.log(`retrying... (${maxRetries - 1} retries remaining)`);
342-
return release(config, releaser, maxRetries - 1);
377+
case 422:
378+
console.log("Skip retry - validation failed");
379+
throw error;
343380
}
381+
382+
console.log(`retrying... (${maxRetries - 1} retries remaining)`);
383+
return release(config, releaser, maxRetries - 1);
344384
}
345-
};
385+
}

0 commit comments

Comments
 (0)