This commit is contained in:
Paulo Cesar 2024-04-08 11:21:40 +01:00 committed by GitHub
commit 98865b39e5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 1768 additions and 4481 deletions

View file

@ -53,6 +53,7 @@ describe("util", () => {
input_discussion_category_name: undefined,
input_generate_release_notes: false,
input_make_latest: undefined,
input_previous_tag: undefined,
})
);
});
@ -74,6 +75,7 @@ describe("util", () => {
input_discussion_category_name: undefined,
input_generate_release_notes: false,
input_make_latest: undefined,
input_previous_tag: undefined,
})
);
});
@ -95,6 +97,7 @@ describe("util", () => {
input_discussion_category_name: undefined,
input_generate_release_notes: false,
input_make_latest: undefined,
input_previous_tag: undefined,
})
);
});
@ -129,6 +132,7 @@ describe("util", () => {
input_discussion_category_name: undefined,
input_generate_release_notes: false,
input_make_latest: undefined,
input_previous_tag: undefined,
}
);
});
@ -155,6 +159,7 @@ describe("util", () => {
input_discussion_category_name: undefined,
input_generate_release_notes: false,
input_make_latest: undefined,
input_previous_tag: undefined,
}
);
});
@ -180,6 +185,7 @@ describe("util", () => {
input_discussion_category_name: "releases",
input_generate_release_notes: false,
input_make_latest: undefined,
input_previous_tag: undefined,
}
);
});
@ -206,6 +212,7 @@ describe("util", () => {
input_discussion_category_name: undefined,
input_generate_release_notes: true,
input_make_latest: undefined,
input_previous_tag: undefined,
}
);
});
@ -235,6 +242,7 @@ describe("util", () => {
input_discussion_category_name: undefined,
input_generate_release_notes: false,
input_make_latest: undefined,
input_previous_tag: undefined,
}
);
});
@ -262,6 +270,7 @@ describe("util", () => {
input_discussion_category_name: undefined,
input_generate_release_notes: false,
input_make_latest: undefined,
input_previous_tag: undefined,
}
);
});
@ -313,6 +322,7 @@ describe("util", () => {
input_discussion_category_name: undefined,
input_generate_release_notes: false,
input_make_latest: "false",
input_previous_tag: undefined,
}
);
});
@ -338,6 +348,7 @@ describe("util", () => {
input_discussion_category_name: undefined,
input_generate_release_notes: false,
input_make_latest: undefined,
input_previous_tag: undefined,
}
);
});

View file

@ -43,14 +43,16 @@ inputs:
generate_release_notes:
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."
required: false
previous_tag:
description: "The tag name of the previous release. If not specified, the previous tag will be detected automatically."
required: false
default: ""
append_body:
description: "Append to existing body instead of overwriting it. Default is false."
required: false
make_latest:
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"
required: false
env:
"GITHUB_TOKEN": "As provided by Github Actions"
outputs:
url:
description: "URL to the Release HTML Page"

10
dist/index.js vendored

File diff suppressed because one or more lines are too long

5995
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,6 @@
{
"name": "action-gh-release",
"version": "2.0.3",
"version": "2.0.5",
"private": true,
"description": "GitHub Action for creating GitHub Releases",
"main": "lib/main.js",
@ -20,24 +20,24 @@
],
"author": "softprops",
"dependencies": {
"@actions/core": "^1.10.0",
"@actions/github": "^5.1.1",
"@octokit/plugin-retry": "^4.0.3",
"@octokit/plugin-throttling": "^4.3.2",
"glob": "^8.0.3",
"mime": "^3.0.0"
"@actions/core": "^1.10.1",
"@actions/github": "^6.0.0",
"@octokit/plugin-retry": "^7.0.3",
"@octokit/plugin-throttling": "^9.0.3",
"glob": "^10.3.10",
"mime": "^4.0.1"
},
"devDependencies": {
"@types/glob": "^8.0.0",
"@types/jest": "^29.2.3",
"@types/mime": "^3.0.1",
"@types/node": "^18.11.9",
"@vercel/ncc": "^0.34.0",
"jest": "^29.3.1",
"jest-circus": "^29.3.1",
"prettier": "2.8.0",
"ts-jest": "^29.0.3",
"typescript": "^4.9.3",
"@types/glob": "^8.1.0",
"@types/jest": "^29.5.12",
"@types/mime": "^3.0.4",
"@types/node": "^20.11.28",
"@vercel/ncc": "^0.38.1",
"jest": "^29.7.0",
"jest-circus": "^29.7.0",
"prettier": "3.2.5",
"ts-jest": "^29.1.2",
"typescript": "^5.4.2",
"typescript-formatter": "^7.2.2"
}
}

View file

@ -1,7 +1,7 @@
import { GitHub } from "@actions/github/lib/utils";
import { Config, isTag, releaseBody } from "./util";
import { statSync, readFileSync } from "fs";
import { getType } from "mime";
import mime from "mime";
import { basename } from "path";
type GitHub = InstanceType<typeof GitHub>;
@ -13,6 +13,10 @@ export interface ReleaseAsset {
data: Buffer;
}
export type GenerateReleaseNotesParams = Partial<Parameters<GitHub["rest"]["repos"]["generateReleaseNotes"]['defaults']>[0]>;
export type CreateReleaseParams = Partial<Parameters<GitHub["rest"]["repos"]["createRelease"]>[0]>;
export type UpdateReleaseParams = Partial<Parameters<GitHub["rest"]["repos"]["updateRelease"]>[0]>;
export interface Release {
id: number;
upload_url: string;
@ -33,39 +37,23 @@ export interface Releaser {
tag: string;
}): Promise<{ data: Release }>;
createRelease(params: {
owner: string;
repo: string;
tag_name: string;
name: string;
body: string | undefined;
draft: boolean | undefined;
prerelease: boolean | undefined;
target_commitish: string | undefined;
discussion_category_name: string | undefined;
generate_release_notes: boolean | undefined;
make_latest: string | undefined;
}): Promise<{ data: Release }>;
createRelease(params: CreateReleaseParams): Promise<{ data: Release }>;
updateRelease(params: {
owner: string;
repo: string;
release_id: number;
tag_name: string;
target_commitish: string;
name: string;
body: string | undefined;
draft: boolean | undefined;
prerelease: boolean | undefined;
discussion_category_name: string | undefined;
generate_release_notes: boolean | undefined;
make_latest: string | undefined;
}): Promise<{ data: Release }>;
updateRelease(params: UpdateReleaseParams): Promise<{ data: Release }>;
allReleases(params: {
owner: string;
repo: string;
}): AsyncIterableIterator<{ data: Release[] }>;
getLatestTag(params: {
owner: string;
repo: string;
}): Promise<undefined | string>;
generateReleaseBody(
params: GenerateReleaseNotesParams
): Promise<string>;
}
export class GitHubReleaser implements Releaser {
@ -79,40 +67,21 @@ export class GitHubReleaser implements Releaser {
repo: string;
tag: string;
}): Promise<{ data: Release }> {
return this.github.rest.repos.getReleaseByTag(params);
return this.github.rest.repos.getReleaseByTag(params as any);
}
createRelease(params: {
owner: string;
repo: string;
tag_name: string;
name: string;
body: string | undefined;
draft: boolean | undefined;
prerelease: boolean | undefined;
target_commitish: string | undefined;
discussion_category_name: string | undefined;
generate_release_notes: boolean | undefined;
make_latest: string | undefined;
}): Promise<{ data: Release }> {
return this.github.rest.repos.createRelease(params);
createRelease(params: CreateReleaseParams): Promise<{ data: Release }> {
return this.github.rest.repos.createRelease({
...params,
generate_release_notes: false
} as any);
}
updateRelease(params: {
owner: string;
repo: string;
release_id: number;
tag_name: string;
target_commitish: string;
name: string;
body: string | undefined;
draft: boolean | undefined;
prerelease: boolean | undefined;
discussion_category_name: string | undefined;
generate_release_notes: boolean | undefined;
make_latest: string | undefined;
}): Promise<{ data: Release }> {
return this.github.rest.repos.updateRelease(params);
updateRelease(params: UpdateReleaseParams): Promise<{ data: Release }> {
return this.github.rest.repos.updateRelease({
...params,
generate_release_notes: false,
} as any);
}
allReleases(params: {
@ -121,9 +90,44 @@ export class GitHubReleaser implements Releaser {
}): AsyncIterableIterator<{ data: Release[] }> {
const updatedParams = { per_page: 100, ...params };
return this.github.paginate.iterator(
this.github.rest.repos.listReleases.endpoint.merge(updatedParams)
this.github.rest.repos.listReleases.endpoint.merge(updatedParams as any)
);
}
async getLatestTag(params: {
owner: string;
repo: string;
}): Promise<undefined | string> {
try {
const release = await this.github.rest.repos.getLatestRelease(params as any);
if (!release?.data) {
return;
}
return release.data.tag_name;
} catch (e) {
console.error(e);
return;
}
}
async generateReleaseBody(params: GenerateReleaseNotesParams): Promise<string> {
try {
const { data } = await this.github.rest.repos.generateReleaseNotes(
params as any
);
if (!data.body) {
throw new Error("No release body generated");
}
return data.body;
} catch (e) {
throw e;
}
}
}
export const asset = (path: string): ReleaseAsset => {
@ -136,7 +140,7 @@ export const asset = (path: string): ReleaseAsset => {
};
export const mimeOrDefault = (path: string): string => {
return getType(path) || "application/octet-stream";
return mime.getType(path) || "application/octet-stream";
};
export const upload = async (
@ -200,8 +204,44 @@ export const release = async (
? config.github_ref.replace("refs/tags/", "")
: "");
const previous_tag = config.input_previous_tag;
const discussion_category_name = config.input_discussion_category_name;
const generate_release_notes = config.input_generate_release_notes;
const latestTag: string | undefined = !previous_tag
? await releaser.getLatestTag({
owner,
repo,
})
: undefined;
if (latestTag) {
console.log(`🏷️ Latest tag related to a release is ${latestTag}`);
} else if (previous_tag) {
console.log(`🏷️ Previous tag is ${previous_tag}`);
}
const tag_name = tag;
let body: string = generate_release_notes
? await releaser.generateReleaseBody({
owner,
repo,
tag_name,
previous_tag_name: previous_tag || latestTag,
} as GenerateReleaseNotesParams)
: "";
if ((generate_release_notes && previous_tag) || latestTag) {
console.log(
`Will generate release notes using ${
previous_tag || latestTag
} as previous tag`
);
}
body = body ? `${body}\n` : "";
try {
// you can't get a an existing draft by tag
// so we must find one in the list of all releases
@ -236,7 +276,6 @@ export const release = async (
target_commitish = existingRelease.data.target_commitish;
}
const tag_name = tag;
const name = config.input_name || existingRelease.data.name || tag;
// revisit: support a new body-concat-strategy input for accumulating
// body parts as a release gets updated. some users will likely want this while
@ -244,11 +283,15 @@ export const release = async (
// no one wants
const workflowBody = releaseBody(config) || "";
const existingReleaseBody = existingRelease.data.body || "";
let body: string;
if (config.input_append_body && workflowBody && existingReleaseBody) {
body = existingReleaseBody + "\n" + workflowBody;
console.log(" Appending existing release body");
body = body + existingReleaseBody + "\n" + workflowBody;
} else {
body = workflowBody || existingReleaseBody;
console.log(
` Using ${workflowBody ? "workflow body" : "existing release body"}`
);
body = body + (workflowBody || existingReleaseBody);
}
const draft =
@ -260,7 +303,7 @@ export const release = async (
? config.input_prerelease
: existingRelease.data.prerelease;
const make_latest = config.input_make_latest;
const make_latest = config.input_make_latest!;
const release = await releaser.updateRelease({
owner,
@ -273,19 +316,24 @@ export const release = async (
draft,
prerelease,
discussion_category_name,
generate_release_notes,
make_latest,
});
} as UpdateReleaseParams);
return release.data;
} catch (error) {
if (error.status === 404) {
const tag_name = tag;
const name = config.input_name || tag;
const body = releaseBody(config);
const workflowBody = releaseBody(config) || "";
if (config.input_append_body && workflowBody) {
console.log(" Appending existing release body");
body = body + workflowBody;
}
const draft = config.input_draft;
const prerelease = config.input_prerelease;
const target_commitish = config.input_target_commitish;
const make_latest = config.input_make_latest;
const make_latest = config.input_make_latest!;
let commitMessage: string = "";
if (target_commitish) {
commitMessage = ` using commit "${target_commitish}"`;
@ -304,9 +352,8 @@ export const release = async (
prerelease,
target_commitish,
discussion_category_name,
generate_release_notes,
make_latest,
});
} as CreateReleaseParams);
return release.data;
} catch (error) {
// presume a race with competing metrix runs

View file

@ -19,7 +19,8 @@ export interface Config {
input_discussion_category_name?: string;
input_generate_release_notes?: boolean;
input_append_body?: boolean;
input_make_latest: string | undefined;
input_previous_tag?: string;
input_make_latest?: string;
}
export const uploadUrl = (url: string): string => {
@ -74,6 +75,7 @@ export const parseConfig = (env: Env): Config => {
input_make_latest: env.INPUT_MAKE_LATEST
? env.INPUT_MAKE_LATEST
: undefined,
input_previous_tag: env.INPUT_PREVIOUS_TAG?.trim() || undefined,
};
};