Skip to content

Commit 3771562

Browse files
authored
Merge pull request #3 from secondlife-3p/feat/add-generateReleaseNotes
Adopt new `previous_tag` input from softprops#372.
2 parents 975c1b2 + dc193b8 commit 3771562

File tree

5 files changed

+122
-14
lines changed

5 files changed

+122
-14
lines changed

__tests__/util.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ describe("util", () => {
5252
input_target_commitish: undefined,
5353
input_discussion_category_name: undefined,
5454
input_generate_release_notes: false,
55+
input_previous_tag: undefined,
5556
})
5657
);
5758
});
@@ -72,6 +73,7 @@ describe("util", () => {
7273
input_target_commitish: undefined,
7374
input_discussion_category_name: undefined,
7475
input_generate_release_notes: false,
76+
input_previous_tag: undefined,
7577
})
7678
);
7779
});
@@ -92,6 +94,7 @@ describe("util", () => {
9294
input_target_commitish: undefined,
9395
input_discussion_category_name: undefined,
9496
input_generate_release_notes: false,
97+
input_previous_tag: undefined,
9598
})
9699
);
97100
});
@@ -125,6 +128,7 @@ describe("util", () => {
125128
input_target_commitish: undefined,
126129
input_discussion_category_name: undefined,
127130
input_generate_release_notes: false,
131+
input_previous_tag: undefined,
128132
}
129133
);
130134
});
@@ -150,6 +154,7 @@ describe("util", () => {
150154
input_target_commitish: "affa18ef97bc9db20076945705aba8c516139abd",
151155
input_discussion_category_name: undefined,
152156
input_generate_release_notes: false,
157+
input_previous_tag: undefined,
153158
}
154159
);
155160
});
@@ -174,6 +179,7 @@ describe("util", () => {
174179
input_target_commitish: undefined,
175180
input_discussion_category_name: "releases",
176181
input_generate_release_notes: false,
182+
input_previous_tag: undefined,
177183
}
178184
);
179185
});
@@ -199,6 +205,7 @@ describe("util", () => {
199205
input_target_commitish: undefined,
200206
input_discussion_category_name: undefined,
201207
input_generate_release_notes: true,
208+
input_previous_tag: undefined,
202209
}
203210
);
204211
});
@@ -227,6 +234,7 @@ describe("util", () => {
227234
input_target_commitish: undefined,
228235
input_discussion_category_name: undefined,
229236
input_generate_release_notes: false,
237+
input_previous_tag: undefined,
230238
}
231239
);
232240
});
@@ -253,6 +261,7 @@ describe("util", () => {
253261
input_target_commitish: undefined,
254262
input_discussion_category_name: undefined,
255263
input_generate_release_notes: false,
264+
input_previous_tag: undefined,
256265
}
257266
);
258267
});
@@ -278,6 +287,7 @@ describe("util", () => {
278287
input_target_commitish: undefined,
279288
input_discussion_category_name: undefined,
280289
input_generate_release_notes: false,
290+
input_previous_tag: undefined,
281291
}
282292
);
283293
});
@@ -302,6 +312,7 @@ describe("util", () => {
302312
input_target_commitish: undefined,
303313
input_discussion_category_name: undefined,
304314
input_generate_release_notes: false,
315+
input_previous_tag: undefined,
305316
}
306317
);
307318
});

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ inputs:
4343
generate_release_notes:
4444
description: "Whether to automatically generate the name and body for this release. If name is specified, the specified name will be used; otherwise, a name will be automatically generated. If body is specified, the body will be pre-pended to the automatically generated notes."
4545
required: false
46+
previous_tag:
47+
description: "The tag name of the previous release. If not specified, the previous tag will be detected automatically."
48+
required: false
49+
default: ""
4650
append_body:
4751
description: "Append to existing body instead of overwriting it. Default is false."
4852
required: false

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/github.ts

Lines changed: 104 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ export interface Releaser {
4444
prerelease: boolean | undefined;
4545
target_commitish: string | undefined;
4646
discussion_category_name: string | undefined;
47-
generate_release_notes: boolean | undefined;
4847
}): Promise<{ data: Release }>;
4948

5049
updateRelease(params: {
@@ -58,13 +57,21 @@ export interface Releaser {
5857
draft: boolean | undefined;
5958
prerelease: boolean | undefined;
6059
discussion_category_name: string | undefined;
61-
generate_release_notes: boolean | undefined;
6260
}): Promise<{ data: Release }>;
6361

6462
allReleases(params: {
6563
owner: string;
6664
repo: string;
6765
}): AsyncIterableIterator<{ data: Release[] }>;
66+
67+
getLatestTag(params: {
68+
owner: string;
69+
repo: string;
70+
}): Promise<undefined | string>;
71+
72+
generateReleaseBody(
73+
params: Parameters<GitHub["rest"]["repos"]["generateReleaseNotes"]>[0]
74+
): Promise<string>;
6875
}
6976

7077
export class GitHubReleaser implements Releaser {
@@ -91,9 +98,11 @@ export class GitHubReleaser implements Releaser {
9198
prerelease: boolean | undefined;
9299
target_commitish: string | undefined;
93100
discussion_category_name: string | undefined;
94-
generate_release_notes: boolean | undefined;
95101
}): Promise<{ data: Release }> {
96-
return this.github.rest.repos.createRelease(params);
102+
return this.github.rest.repos.createRelease({
103+
...params,
104+
generate_release_notes: false,
105+
});
97106
}
98107

99108
updateRelease(params: {
@@ -107,9 +116,11 @@ export class GitHubReleaser implements Releaser {
107116
draft: boolean | undefined;
108117
prerelease: boolean | undefined;
109118
discussion_category_name: string | undefined;
110-
generate_release_notes: boolean | undefined;
111119
}): Promise<{ data: Release }> {
112-
return this.github.rest.repos.updateRelease(params);
120+
return this.github.rest.repos.updateRelease({
121+
...params,
122+
generate_release_notes: false,
123+
});
113124
}
114125

115126
allReleases(params: {
@@ -121,6 +132,43 @@ export class GitHubReleaser implements Releaser {
121132
this.github.rest.repos.listReleases.endpoint.merge(updatedParams)
122133
);
123134
}
135+
136+
async getLatestTag(params: {
137+
owner: string;
138+
repo: string;
139+
}): Promise<undefined | string> {
140+
try {
141+
const release = await this.github.rest.repos.getLatestRelease(params);
142+
143+
if (!release?.data) {
144+
return;
145+
}
146+
147+
return release.data.tag_name;
148+
} catch (e) {
149+
console.error(e);
150+
151+
return;
152+
}
153+
}
154+
155+
async generateReleaseBody(
156+
params: Parameters<GitHub["rest"]["repos"]["generateReleaseNotes"]>[0]
157+
): Promise<string> {
158+
try {
159+
const { data } = await this.github.rest.repos.generateReleaseNotes(
160+
params
161+
);
162+
163+
if (!data.body) {
164+
throw new Error("No release body generated");
165+
}
166+
167+
return data.body;
168+
} catch (e) {
169+
throw e;
170+
}
171+
}
124172
}
125173

126174
export const asset = (path: string): ReleaseAsset => {
@@ -196,8 +244,44 @@ export const release = async (
196244
? config.github_ref.replace("refs/tags/", "")
197245
: "");
198246

247+
const previous_tag = config.input_previous_tag;
199248
const discussion_category_name = config.input_discussion_category_name;
200249
const generate_release_notes = config.input_generate_release_notes;
250+
251+
const latestTag: string | undefined = !previous_tag
252+
? await releaser.getLatestTag({
253+
owner,
254+
repo,
255+
})
256+
: undefined;
257+
258+
if (latestTag) {
259+
console.log(`🏷️ Latest tag related to a release is ${latestTag}`);
260+
} else if (previous_tag) {
261+
console.log(`🏷️ Previous tag is ${previous_tag}`);
262+
}
263+
264+
const tag_name = tag;
265+
266+
let body: string = generate_release_notes
267+
? await releaser.generateReleaseBody({
268+
owner,
269+
repo,
270+
tag_name,
271+
previous_tag_name: previous_tag || latestTag,
272+
})
273+
: "";
274+
275+
if ((generate_release_notes && previous_tag) || latestTag) {
276+
console.log(
277+
`Will generate release notes using ${
278+
previous_tag || latestTag
279+
} as previous tag`
280+
);
281+
}
282+
283+
body = body ? `${body}\n` : "";
284+
201285
try {
202286
// you can't get a an existing draft by tag
203287
// so we must find one in the list of all releases
@@ -232,19 +316,22 @@ export const release = async (
232316
target_commitish = existingRelease.data.target_commitish;
233317
}
234318

235-
const tag_name = tag;
236319
const name = config.input_name || existingRelease.data.name || tag;
237320
// revisit: support a new body-concat-strategy input for accumulating
238321
// body parts as a release gets updated. some users will likely want this while
239322
// others won't previously this was duplicating content for most which
240323
// no one wants
241324
const workflowBody = releaseBody(config) || "";
242325
const existingReleaseBody = existingRelease.data.body || "";
243-
let body: string;
326+
244327
if (config.input_append_body && workflowBody && existingReleaseBody) {
245-
body = existingReleaseBody + "\n" + workflowBody;
328+
console.log("➕ Appending existing release body");
329+
body = body + existingReleaseBody + "\n" + workflowBody;
246330
} else {
247-
body = workflowBody || existingReleaseBody;
331+
console.log(
332+
`➕ Using ${workflowBody ? "workflow body" : "existing release body"}`
333+
);
334+
body = body + (workflowBody || existingReleaseBody);
248335
}
249336

250337
const draft =
@@ -267,14 +354,19 @@ export const release = async (
267354
draft,
268355
prerelease,
269356
discussion_category_name,
270-
generate_release_notes,
271357
});
272358
return release.data;
273359
} catch (error) {
274360
if (error.status === 404) {
275361
const tag_name = tag;
276362
const name = config.input_name || tag;
277-
const body = releaseBody(config);
363+
const workflowBody = releaseBody(config) || "";
364+
365+
if (config.input_append_body && workflowBody) {
366+
console.log("➕ Appending existing release body");
367+
body = body + workflowBody;
368+
}
369+
278370
const draft = config.input_draft;
279371
const prerelease = config.input_prerelease;
280372
const target_commitish = config.input_target_commitish;
@@ -296,7 +388,6 @@ export const release = async (
296388
prerelease,
297389
target_commitish,
298390
discussion_category_name,
299-
generate_release_notes,
300391
});
301392
return release.data;
302393
} catch (error) {

src/util.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export interface Config {
1919
input_discussion_category_name?: string;
2020
input_generate_release_notes?: boolean;
2121
input_append_body?: boolean;
22+
input_previous_tag?: string;
2223
}
2324

2425
export const uploadUrl = (url: string): string => {
@@ -70,6 +71,7 @@ export const parseConfig = (env: Env): Config => {
7071
env.INPUT_DISCUSSION_CATEGORY_NAME || undefined,
7172
input_generate_release_notes: env.INPUT_GENERATE_RELEASE_NOTES == "true",
7273
input_append_body: env.INPUT_APPEND_BODY == "true",
74+
input_previous_tag: env.INPUT_PREVIOUS_TAG?.trim() || undefined,
7375
};
7476
};
7577

0 commit comments

Comments
 (0)