chore: add prettier config

Signed-off-by: Rui Chen <rui@chenrui.dev>
This commit is contained in:
Rui Chen 2025-06-11 11:33:39 -04:00
parent 605f567f95
commit cb9d16a07e
No known key found for this signature in database
GPG key ID: 6577287BDCA70840
7 changed files with 273 additions and 355 deletions

16
.prettierignore Normal file
View file

@ -0,0 +1,16 @@
# Build outputs
dist/
lib/
coverage/
# Dependencies
node_modules/
# Misc
.github/
*.log
.DS_Store
__tests__/release.txt
# Package files
package-lock.json

11
.prettierrc.js Normal file
View file

@ -0,0 +1,11 @@
/**
* @type {import('prettier').Config}
*/
module.exports = {
trailingComma: 'all',
tabWidth: 2,
semi: true,
singleQuote: true,
printWidth: 100,
bracketSpacing: true,
};

View file

@ -1,62 +1,56 @@
import { import { asset, findTagFromReleases, mimeOrDefault, Release, Releaser } from '../src/github';
asset,
findTagFromReleases,
mimeOrDefault,
Release,
Releaser,
} from "../src/github";
import { assert, describe, it } from "vitest"; import { assert, describe, it } from 'vitest';
describe("github", () => { describe('github', () => {
describe("mimeOrDefault", () => { describe('mimeOrDefault', () => {
it("returns a specific mime for common path", async () => { it('returns a specific mime for common path', async () => {
assert.equal(mimeOrDefault("foo.tar.gz"), "application/gzip"); assert.equal(mimeOrDefault('foo.tar.gz'), 'application/gzip');
}); });
it("returns default mime for uncommon path", async () => { it('returns default mime for uncommon path', async () => {
assert.equal(mimeOrDefault("foo.uncommon"), "application/octet-stream"); assert.equal(mimeOrDefault('foo.uncommon'), 'application/octet-stream');
}); });
}); });
describe("asset", () => { describe('asset', () => {
it("derives asset info from a path", async () => { it('derives asset info from a path', async () => {
const { name, mime, size } = asset("tests/data/foo/bar.txt"); const { name, mime, size } = asset('tests/data/foo/bar.txt');
assert.equal(name, "bar.txt"); assert.equal(name, 'bar.txt');
assert.equal(mime, "text/plain"); assert.equal(mime, 'text/plain');
assert.equal(size, 10); assert.equal(size, 10);
}); });
}); });
describe("findTagFromReleases", () => { describe('findTagFromReleases', () => {
const owner = "owner"; const owner = 'owner';
const repo = "repo"; const repo = 'repo';
const mockRelease: Release = { const mockRelease: Release = {
id: 1, id: 1,
upload_url: `https://api.github.com/repos/${owner}/${repo}/releases/1/assets`, upload_url: `https://api.github.com/repos/${owner}/${repo}/releases/1/assets`,
html_url: `https://github.com/${owner}/${repo}/releases/tag/v1.0.0`, html_url: `https://github.com/${owner}/${repo}/releases/tag/v1.0.0`,
tag_name: "v1.0.0", tag_name: 'v1.0.0',
name: "Test Release", name: 'Test Release',
body: "Test body", body: 'Test body',
target_commitish: "main", target_commitish: 'main',
draft: false, draft: false,
prerelease: false, prerelease: false,
assets: [], assets: [],
} as const; } as const;
const mockReleaser: Releaser = { const mockReleaser: Releaser = {
getReleaseByTag: () => Promise.reject("Not implemented"), getReleaseByTag: () => Promise.reject('Not implemented'),
createRelease: () => Promise.reject("Not implemented"), createRelease: () => Promise.reject('Not implemented'),
updateRelease: () => Promise.reject("Not implemented"), updateRelease: () => Promise.reject('Not implemented'),
allReleases: async function* () { allReleases: async function* () {
yield { data: [mockRelease] }; yield { data: [mockRelease] };
}, },
} as const; } as const;
describe("when the tag_name is not an empty string", () => { describe('when the tag_name is not an empty string', () => {
const targetTag = "v1.0.0"; const targetTag = 'v1.0.0';
it("finds a matching release in first batch of results", async () => { it('finds a matching release in first batch of results', async () => {
const targetRelease = { const targetRelease = {
...mockRelease, ...mockRelease,
owner, owner,
@ -67,7 +61,7 @@ describe("github", () => {
...mockRelease, ...mockRelease,
owner, owner,
repo, repo,
tag_name: "v1.0.1", tag_name: 'v1.0.1',
}; };
const releaser = { const releaser = {
@ -78,17 +72,12 @@ describe("github", () => {
}, },
}; };
const result = await findTagFromReleases( const result = await findTagFromReleases(releaser, owner, repo, targetTag);
releaser,
owner,
repo,
targetTag,
);
assert.deepStrictEqual(result, targetRelease); assert.deepStrictEqual(result, targetRelease);
}); });
it("finds a matching release in second batch of results", async () => { it('finds a matching release in second batch of results', async () => {
const targetRelease = { const targetRelease = {
...mockRelease, ...mockRelease,
owner, owner,
@ -99,7 +88,7 @@ describe("github", () => {
...mockRelease, ...mockRelease,
owner, owner,
repo, repo,
tag_name: "v1.0.1", tag_name: 'v1.0.1',
}; };
const releaser = { const releaser = {
@ -110,21 +99,16 @@ describe("github", () => {
}, },
}; };
const result = await findTagFromReleases( const result = await findTagFromReleases(releaser, owner, repo, targetTag);
releaser,
owner,
repo,
targetTag,
);
assert.deepStrictEqual(result, targetRelease); assert.deepStrictEqual(result, targetRelease);
}); });
it("returns undefined when a release is not found in any batch", async () => { it('returns undefined when a release is not found in any batch', async () => {
const otherRelease = { const otherRelease = {
...mockRelease, ...mockRelease,
owner, owner,
repo, repo,
tag_name: "v1.0.1", tag_name: 'v1.0.1',
}; };
const releaser = { const releaser = {
...mockReleaser, ...mockReleaser,
@ -134,17 +118,12 @@ describe("github", () => {
}, },
}; };
const result = await findTagFromReleases( const result = await findTagFromReleases(releaser, owner, repo, targetTag);
releaser,
owner,
repo,
targetTag,
);
assert.strictEqual(result, undefined); assert.strictEqual(result, undefined);
}); });
it("returns undefined when no releases are returned", async () => { it('returns undefined when no releases are returned', async () => {
const releaser = { const releaser = {
...mockReleaser, ...mockReleaser,
allReleases: async function* () { allReleases: async function* () {
@ -152,21 +131,16 @@ describe("github", () => {
}, },
}; };
const result = await findTagFromReleases( const result = await findTagFromReleases(releaser, owner, repo, targetTag);
releaser,
owner,
repo,
targetTag,
);
assert.strictEqual(result, undefined); assert.strictEqual(result, undefined);
}); });
}); });
describe("when the tag_name is an empty string", () => { describe('when the tag_name is an empty string', () => {
const emptyTag = ""; const emptyTag = '';
it("finds a matching release in first batch of results", async () => { it('finds a matching release in first batch of results', async () => {
const targetRelease = { const targetRelease = {
...mockRelease, ...mockRelease,
owner, owner,
@ -177,7 +151,7 @@ describe("github", () => {
...mockRelease, ...mockRelease,
owner, owner,
repo, repo,
tag_name: "v1.0.1", tag_name: 'v1.0.1',
}; };
const releaser = { const releaser = {
@ -188,17 +162,12 @@ describe("github", () => {
}, },
}; };
const result = await findTagFromReleases( const result = await findTagFromReleases(releaser, owner, repo, emptyTag);
releaser,
owner,
repo,
emptyTag,
);
assert.deepStrictEqual(result, targetRelease); assert.deepStrictEqual(result, targetRelease);
}); });
it("finds a matching release in second batch of results", async () => { it('finds a matching release in second batch of results', async () => {
const targetRelease = { const targetRelease = {
...mockRelease, ...mockRelease,
owner, owner,
@ -209,7 +178,7 @@ describe("github", () => {
...mockRelease, ...mockRelease,
owner, owner,
repo, repo,
tag_name: "v1.0.1", tag_name: 'v1.0.1',
}; };
const releaser = { const releaser = {
@ -220,21 +189,16 @@ describe("github", () => {
}, },
}; };
const result = await findTagFromReleases( const result = await findTagFromReleases(releaser, owner, repo, emptyTag);
releaser,
owner,
repo,
emptyTag,
);
assert.deepStrictEqual(result, targetRelease); assert.deepStrictEqual(result, targetRelease);
}); });
it("returns undefined when a release is not found in any batch", async () => { it('returns undefined when a release is not found in any batch', async () => {
const otherRelease = { const otherRelease = {
...mockRelease, ...mockRelease,
owner, owner,
repo, repo,
tag_name: "v1.0.1", tag_name: 'v1.0.1',
}; };
const releaser = { const releaser = {
...mockReleaser, ...mockReleaser,
@ -244,17 +208,12 @@ describe("github", () => {
}, },
}; };
const result = await findTagFromReleases( const result = await findTagFromReleases(releaser, owner, repo, emptyTag);
releaser,
owner,
repo,
emptyTag,
);
assert.strictEqual(result, undefined); assert.strictEqual(result, undefined);
}); });
it("returns undefined when no releases are returned", async () => { it('returns undefined when no releases are returned', async () => {
const releaser = { const releaser = {
...mockReleaser, ...mockReleaser,
allReleases: async function* () { allReleases: async function* () {
@ -262,12 +221,7 @@ describe("github", () => {
}, },
}; };
const result = await findTagFromReleases( const result = await findTagFromReleases(releaser, owner, repo, emptyTag);
releaser,
owner,
repo,
emptyTag,
);
assert.strictEqual(result, undefined); assert.strictEqual(result, undefined);
}); });

View file

@ -7,44 +7,48 @@ import {
releaseBody, releaseBody,
unmatchedPatterns, unmatchedPatterns,
uploadUrl, uploadUrl,
} from "../src/util"; } from '../src/util';
import { assert, describe, expect, it } from "vitest"; import { assert, describe, expect, it } from 'vitest';
describe("util", () => { describe('util', () => {
describe("uploadUrl", () => { describe('uploadUrl', () => {
it("strips template", () => { it('strips template', () => {
assert.equal( assert.equal(
uploadUrl( uploadUrl(
"https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets{?name,label}", 'https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets{?name,label}',
), ),
"https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets", 'https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets',
); );
}); });
}); });
describe("parseInputFiles", () => { describe('parseInputFiles', () => {
it("parses empty strings", () => { it('parses empty strings', () => {
assert.deepStrictEqual(parseInputFiles(""), []); assert.deepStrictEqual(parseInputFiles(''), []);
}); });
it("parses comma-delimited strings", () => { it('parses comma-delimited strings', () => {
assert.deepStrictEqual(parseInputFiles("foo,bar"), ["foo", "bar"]); assert.deepStrictEqual(parseInputFiles('foo,bar'), ['foo', 'bar']);
}); });
it("parses newline and comma-delimited (and then some)", () => { it('parses newline and comma-delimited (and then some)', () => {
assert.deepStrictEqual( assert.deepStrictEqual(parseInputFiles('foo,bar\nbaz,boom,\n\ndoom,loom '), [
parseInputFiles("foo,bar\nbaz,boom,\n\ndoom,loom "), 'foo',
["foo", "bar", "baz", "boom", "doom", "loom"], 'bar',
); 'baz',
'boom',
'doom',
'loom',
]);
}); });
}); });
describe("releaseBody", () => { describe('releaseBody', () => {
it("uses input body", () => { it('uses input body', () => {
assert.equal( assert.equal(
"foo", 'foo',
releaseBody({ releaseBody({
github_ref: "", github_ref: '',
github_repository: "", github_repository: '',
github_token: "", github_token: '',
input_body: "foo", input_body: 'foo',
input_body_path: undefined, input_body_path: undefined,
input_draft: false, input_draft: false,
input_prerelease: false, input_prerelease: false,
@ -60,15 +64,15 @@ describe("util", () => {
}), }),
); );
}); });
it("uses input body path", () => { it('uses input body path', () => {
assert.equal( assert.equal(
"bar", 'bar',
releaseBody({ releaseBody({
github_ref: "", github_ref: '',
github_repository: "", github_repository: '',
github_token: "", github_token: '',
input_body: undefined, input_body: undefined,
input_body_path: "__tests__/release.txt", input_body_path: '__tests__/release.txt',
input_draft: false, input_draft: false,
input_prerelease: false, input_prerelease: false,
input_preserve_order: undefined, input_preserve_order: undefined,
@ -83,15 +87,15 @@ describe("util", () => {
}), }),
); );
}); });
it("defaults to body path when both body and body path are provided", () => { it('defaults to body path when both body and body path are provided', () => {
assert.equal( assert.equal(
"bar", 'bar',
releaseBody({ releaseBody({
github_ref: "", github_ref: '',
github_repository: "", github_repository: '',
github_token: "", github_token: '',
input_body: "foo", input_body: 'foo',
input_body_path: "__tests__/release.txt", input_body_path: '__tests__/release.txt',
input_draft: false, input_draft: false,
input_prerelease: false, input_prerelease: false,
input_preserve_order: undefined, input_preserve_order: undefined,
@ -107,8 +111,8 @@ describe("util", () => {
); );
}); });
}); });
describe("parseConfig", () => { describe('parseConfig', () => {
it("parses basic config", () => { it('parses basic config', () => {
assert.deepStrictEqual( assert.deepStrictEqual(
parseConfig({ parseConfig({
// note: inputs declared in actions.yml, even when declared not required, // note: inputs declared in actions.yml, even when declared not required,
@ -117,13 +121,13 @@ describe("util", () => {
// as an empty string !== undefined in terms of what we pass to the api // as an empty string !== undefined in terms of what we pass to the api
// so we cover that in a test case here to ensure undefined values are actually // so we cover that in a test case here to ensure undefined values are actually
// resolved as undefined and not empty strings // resolved as undefined and not empty strings
INPUT_TARGET_COMMITISH: "", INPUT_TARGET_COMMITISH: '',
INPUT_DISCUSSION_CATEGORY_NAME: "", INPUT_DISCUSSION_CATEGORY_NAME: '',
}), }),
{ {
github_ref: "", github_ref: '',
github_repository: "", github_repository: '',
github_token: "", github_token: '',
input_append_body: false, input_append_body: false,
input_body: undefined, input_body: undefined,
input_body_path: undefined, input_body_path: undefined,
@ -143,15 +147,15 @@ describe("util", () => {
); );
}); });
it("parses basic config with commitish", () => { it('parses basic config with commitish', () => {
assert.deepStrictEqual( assert.deepStrictEqual(
parseConfig({ parseConfig({
INPUT_TARGET_COMMITISH: "affa18ef97bc9db20076945705aba8c516139abd", INPUT_TARGET_COMMITISH: 'affa18ef97bc9db20076945705aba8c516139abd',
}), }),
{ {
github_ref: "", github_ref: '',
github_repository: "", github_repository: '',
github_token: "", github_token: '',
input_append_body: false, input_append_body: false,
input_body: undefined, input_body: undefined,
input_body_path: undefined, input_body_path: undefined,
@ -163,22 +167,22 @@ describe("util", () => {
input_name: undefined, input_name: undefined,
input_tag_name: undefined, input_tag_name: undefined,
input_fail_on_unmatched_files: false, input_fail_on_unmatched_files: false,
input_target_commitish: "affa18ef97bc9db20076945705aba8c516139abd", input_target_commitish: 'affa18ef97bc9db20076945705aba8c516139abd',
input_discussion_category_name: undefined, input_discussion_category_name: undefined,
input_generate_release_notes: false, input_generate_release_notes: false,
input_make_latest: undefined, input_make_latest: undefined,
}, },
); );
}); });
it("supports discussion category names", () => { it('supports discussion category names', () => {
assert.deepStrictEqual( assert.deepStrictEqual(
parseConfig({ parseConfig({
INPUT_DISCUSSION_CATEGORY_NAME: "releases", INPUT_DISCUSSION_CATEGORY_NAME: 'releases',
}), }),
{ {
github_ref: "", github_ref: '',
github_repository: "", github_repository: '',
github_token: "", github_token: '',
input_append_body: false, input_append_body: false,
input_body: undefined, input_body: undefined,
input_body_path: undefined, input_body_path: undefined,
@ -191,22 +195,22 @@ describe("util", () => {
input_tag_name: undefined, input_tag_name: undefined,
input_fail_on_unmatched_files: false, input_fail_on_unmatched_files: false,
input_target_commitish: undefined, input_target_commitish: undefined,
input_discussion_category_name: "releases", input_discussion_category_name: 'releases',
input_generate_release_notes: false, input_generate_release_notes: false,
input_make_latest: undefined, input_make_latest: undefined,
}, },
); );
}); });
it("supports generating release notes", () => { it('supports generating release notes', () => {
assert.deepStrictEqual( assert.deepStrictEqual(
parseConfig({ parseConfig({
INPUT_GENERATE_RELEASE_NOTES: "true", INPUT_GENERATE_RELEASE_NOTES: 'true',
}), }),
{ {
github_ref: "", github_ref: '',
github_repository: "", github_repository: '',
github_token: "", github_token: '',
input_append_body: false, input_append_body: false,
input_body: undefined, input_body: undefined,
input_body_path: undefined, input_body_path: undefined,
@ -226,19 +230,19 @@ describe("util", () => {
); );
}); });
it("prefers GITHUB_TOKEN over token input for backwards compatibility", () => { it('prefers GITHUB_TOKEN over token input for backwards compatibility', () => {
assert.deepStrictEqual( assert.deepStrictEqual(
parseConfig({ parseConfig({
INPUT_DRAFT: "false", INPUT_DRAFT: 'false',
INPUT_PRERELEASE: "true", INPUT_PRERELEASE: 'true',
INPUT_PRESERVE_ORDER: "true", INPUT_PRESERVE_ORDER: 'true',
GITHUB_TOKEN: "env-token", GITHUB_TOKEN: 'env-token',
INPUT_TOKEN: "input-token", INPUT_TOKEN: 'input-token',
}), }),
{ {
github_ref: "", github_ref: '',
github_repository: "", github_repository: '',
github_token: "env-token", github_token: 'env-token',
input_append_body: false, input_append_body: false,
input_body: undefined, input_body: undefined,
input_body_path: undefined, input_body_path: undefined,
@ -257,17 +261,17 @@ describe("util", () => {
}, },
); );
}); });
it("uses input token as the source of GITHUB_TOKEN by default", () => { it('uses input token as the source of GITHUB_TOKEN by default', () => {
assert.deepStrictEqual( assert.deepStrictEqual(
parseConfig({ parseConfig({
INPUT_DRAFT: "false", INPUT_DRAFT: 'false',
INPUT_PRERELEASE: "true", INPUT_PRERELEASE: 'true',
INPUT_TOKEN: "input-token", INPUT_TOKEN: 'input-token',
}), }),
{ {
github_ref: "", github_ref: '',
github_repository: "", github_repository: '',
github_token: "input-token", github_token: 'input-token',
input_append_body: false, input_append_body: false,
input_body: undefined, input_body: undefined,
input_body_path: undefined, input_body_path: undefined,
@ -286,16 +290,16 @@ describe("util", () => {
}, },
); );
}); });
it("parses basic config with draft and prerelease", () => { it('parses basic config with draft and prerelease', () => {
assert.deepStrictEqual( assert.deepStrictEqual(
parseConfig({ parseConfig({
INPUT_DRAFT: "false", INPUT_DRAFT: 'false',
INPUT_PRERELEASE: "true", INPUT_PRERELEASE: 'true',
}), }),
{ {
github_ref: "", github_ref: '',
github_repository: "", github_repository: '',
github_token: "", github_token: '',
input_append_body: false, input_append_body: false,
input_body: undefined, input_body: undefined,
input_body_path: undefined, input_body_path: undefined,
@ -314,15 +318,15 @@ describe("util", () => {
}, },
); );
}); });
it("parses basic config where make_latest is passed", () => { it('parses basic config where make_latest is passed', () => {
assert.deepStrictEqual( assert.deepStrictEqual(
parseConfig({ parseConfig({
INPUT_MAKE_LATEST: "false", INPUT_MAKE_LATEST: 'false',
}), }),
{ {
github_ref: "", github_ref: '',
github_repository: "", github_repository: '',
github_token: "", github_token: '',
input_append_body: false, input_append_body: false,
input_body: undefined, input_body: undefined,
input_body_path: undefined, input_body_path: undefined,
@ -337,19 +341,19 @@ describe("util", () => {
input_target_commitish: undefined, input_target_commitish: undefined,
input_discussion_category_name: undefined, input_discussion_category_name: undefined,
input_generate_release_notes: false, input_generate_release_notes: false,
input_make_latest: "false", input_make_latest: 'false',
}, },
); );
}); });
it("parses basic config with append_body", () => { it('parses basic config with append_body', () => {
assert.deepStrictEqual( assert.deepStrictEqual(
parseConfig({ parseConfig({
INPUT_APPEND_BODY: "true", INPUT_APPEND_BODY: 'true',
}), }),
{ {
github_ref: "", github_ref: '',
github_repository: "", github_repository: '',
github_token: "", github_token: '',
input_append_body: true, input_append_body: true,
input_body: undefined, input_body: undefined,
input_body_path: undefined, input_body_path: undefined,
@ -369,46 +373,43 @@ describe("util", () => {
); );
}); });
}); });
describe("isTag", () => { describe('isTag', () => {
it("returns true for tags", async () => { it('returns true for tags', async () => {
assert.equal(isTag("refs/tags/foo"), true); assert.equal(isTag('refs/tags/foo'), true);
}); });
it("returns false for other kinds of refs", async () => { it('returns false for other kinds of refs', async () => {
assert.equal(isTag("refs/heads/master"), false); assert.equal(isTag('refs/heads/master'), false);
}); });
}); });
describe("paths", () => { describe('paths', () => {
it("resolves files given a set of paths", async () => { it('resolves files given a set of paths', async () => {
assert.deepStrictEqual( assert.deepStrictEqual(paths(['tests/data/**/*', 'tests/data/does/not/exist/*']), [
paths(["tests/data/**/*", "tests/data/does/not/exist/*"]), 'tests/data/foo/bar.txt',
["tests/data/foo/bar.txt"], ]);
);
}); });
}); });
describe("unmatchedPatterns", () => { describe('unmatchedPatterns', () => {
it("returns the patterns that don't match any files", async () => { it("returns the patterns that don't match any files", async () => {
assert.deepStrictEqual( assert.deepStrictEqual(
unmatchedPatterns(["tests/data/**/*", "tests/data/does/not/exist/*"]), unmatchedPatterns(['tests/data/**/*', 'tests/data/does/not/exist/*']),
["tests/data/does/not/exist/*"], ['tests/data/does/not/exist/*'],
); );
}); });
}); });
describe("replaceSpacesWithDots", () => { describe('replaceSpacesWithDots', () => {
it("replaces all spaces with dots", () => { it('replaces all spaces with dots', () => {
expect(alignAssetName("John Doe.bla")).toBe("John.Doe.bla"); expect(alignAssetName('John Doe.bla')).toBe('John.Doe.bla');
}); });
it("handles names with multiple spaces", () => { it('handles names with multiple spaces', () => {
expect(alignAssetName("John William Doe.bla")).toBe( expect(alignAssetName('John William Doe.bla')).toBe('John.William.Doe.bla');
"John.William.Doe.bla",
);
}); });
it("returns the same string if there are no spaces", () => { it('returns the same string if there are no spaces', () => {
expect(alignAssetName("JohnDoe")).toBe("JohnDoe"); expect(alignAssetName('JohnDoe')).toBe('JohnDoe');
}); });
}); });
}); });

View file

@ -1,9 +1,9 @@
import { GitHub } from "@actions/github/lib/utils"; import { GitHub } from '@actions/github/lib/utils';
import { statSync } from "fs"; import { statSync } from 'fs';
import { open } from "fs/promises"; import { open } from 'fs/promises';
import { lookup } from "mime-types"; import { lookup } from 'mime-types';
import { basename } from "path"; import { basename } from 'path';
import { alignAssetName, Config, isTag, releaseBody } from "./util"; import { alignAssetName, Config, isTag, releaseBody } from './util';
type GitHub = InstanceType<typeof GitHub>; type GitHub = InstanceType<typeof GitHub>;
@ -27,11 +27,7 @@ export interface Release {
} }
export interface Releaser { export interface Releaser {
getReleaseByTag(params: { getReleaseByTag(params: { owner: string; repo: string; tag: string }): Promise<{ data: Release }>;
owner: string;
repo: string;
tag: string;
}): Promise<{ data: Release }>;
createRelease(params: { createRelease(params: {
owner: string; owner: string;
@ -44,7 +40,7 @@ export interface Releaser {
target_commitish: string | undefined; target_commitish: string | undefined;
discussion_category_name: string | undefined; discussion_category_name: string | undefined;
generate_release_notes: boolean | undefined; generate_release_notes: boolean | undefined;
make_latest: "true" | "false" | "legacy" | undefined; make_latest: 'true' | 'false' | 'legacy' | undefined;
}): Promise<{ data: Release }>; }): Promise<{ data: Release }>;
updateRelease(params: { updateRelease(params: {
@ -59,13 +55,10 @@ export interface Releaser {
prerelease: boolean | undefined; prerelease: boolean | undefined;
discussion_category_name: string | undefined; discussion_category_name: string | undefined;
generate_release_notes: boolean | undefined; generate_release_notes: boolean | undefined;
make_latest: "true" | "false" | "legacy" | undefined; make_latest: 'true' | 'false' | 'legacy' | undefined;
}): Promise<{ data: Release }>; }): Promise<{ data: Release }>;
allReleases(params: { allReleases(params: { owner: string; repo: string }): AsyncIterableIterator<{ data: Release[] }>;
owner: string;
repo: string;
}): AsyncIterableIterator<{ data: Release[] }>;
} }
export class GitHubReleaser implements Releaser { export class GitHubReleaser implements Releaser {
@ -93,11 +86,11 @@ export class GitHubReleaser implements Releaser {
target_commitish: string | undefined; target_commitish: string | undefined;
discussion_category_name: string | undefined; discussion_category_name: string | undefined;
generate_release_notes: boolean | undefined; generate_release_notes: boolean | undefined;
make_latest: "true" | "false" | "legacy" | undefined; make_latest: 'true' | 'false' | 'legacy' | undefined;
}): Promise<{ data: Release }> { }): Promise<{ data: Release }> {
if ( if (
typeof params.make_latest === "string" && typeof params.make_latest === 'string' &&
!["true", "false", "legacy"].includes(params.make_latest) !['true', 'false', 'legacy'].includes(params.make_latest)
) { ) {
params.make_latest = undefined; params.make_latest = undefined;
} }
@ -117,11 +110,11 @@ export class GitHubReleaser implements Releaser {
prerelease: boolean | undefined; prerelease: boolean | undefined;
discussion_category_name: string | undefined; discussion_category_name: string | undefined;
generate_release_notes: boolean | undefined; generate_release_notes: boolean | undefined;
make_latest: "true" | "false" | "legacy" | undefined; make_latest: 'true' | 'false' | 'legacy' | undefined;
}): Promise<{ data: Release }> { }): Promise<{ data: Release }> {
if ( if (
typeof params.make_latest === "string" && typeof params.make_latest === 'string' &&
!["true", "false", "legacy"].includes(params.make_latest) !['true', 'false', 'legacy'].includes(params.make_latest)
) { ) {
params.make_latest = undefined; params.make_latest = undefined;
} }
@ -129,10 +122,7 @@ export class GitHubReleaser implements Releaser {
return this.github.rest.repos.updateRelease(params); return this.github.rest.repos.updateRelease(params);
} }
allReleases(params: { allReleases(params: { owner: string; repo: string }): AsyncIterableIterator<{ data: Release[] }> {
owner: string;
repo: string;
}): AsyncIterableIterator<{ data: Release[] }> {
const updatedParams = { per_page: 100, ...params }; const updatedParams = { per_page: 100, ...params };
return this.github.paginate.iterator( return this.github.paginate.iterator(
this.github.rest.repos.listReleases.endpoint.merge(updatedParams), this.github.rest.repos.listReleases.endpoint.merge(updatedParams),
@ -149,7 +139,7 @@ export const asset = (path: string): ReleaseAsset => {
}; };
export const mimeOrDefault = (path: string): string => { export const mimeOrDefault = (path: string): string => {
return lookup(path) || "application/octet-stream"; return lookup(path) || 'application/octet-stream';
}; };
export const upload = async ( export const upload = async (
@ -159,7 +149,7 @@ export const upload = async (
path: string, path: string,
currentAssets: Array<{ id: number; name: string }>, currentAssets: Array<{ id: number; name: string }>,
): Promise<any> => { ): Promise<any> => {
const [owner, repo] = config.github_repository.split("/"); const [owner, repo] = config.github_repository.split('/');
const { name, mime, size } = asset(path); const { name, mime, size } = asset(path);
const currentAsset = currentAssets.find( const currentAsset = currentAssets.find(
// note: GitHub renames asset filenames that have special characters, non-alphanumeric characters, and leading or trailing periods. The "List release assets" endpoint lists the renamed filenames. // note: GitHub renames asset filenames that have special characters, non-alphanumeric characters, and leading or trailing periods. The "List release assets" endpoint lists the renamed filenames.
@ -169,9 +159,7 @@ export const upload = async (
); );
if (currentAsset) { if (currentAsset) {
if (config.input_overwrite_files === false) { if (config.input_overwrite_files === false) {
console.log( console.log(`Asset ${name} already exists and overwrite_files is false...`);
`Asset ${name} already exists and overwrite_files is false...`,
);
return null; return null;
} else { } else {
console.log(`♻️ Deleting previously uploaded asset ${name}...`); console.log(`♻️ Deleting previously uploaded asset ${name}...`);
@ -184,18 +172,18 @@ export const upload = async (
} }
console.log(`⬆️ Uploading ${name}...`); console.log(`⬆️ Uploading ${name}...`);
const endpoint = new URL(url); const endpoint = new URL(url);
endpoint.searchParams.append("name", name); endpoint.searchParams.append('name', name);
const fh = await open(path); const fh = await open(path);
try { try {
const resp = await github.request({ const resp = await github.request({
method: "POST", method: 'POST',
url: endpoint.toString(), url: endpoint.toString(),
headers: { headers: {
"content-length": `${size}`, 'content-length': `${size}`,
"content-type": mime, 'content-type': mime,
authorization: `token ${config.github_token}`, authorization: `token ${config.github_token}`,
}, },
data: fh.readableWebStream({ type: "bytes" }), data: fh.readableWebStream({ type: 'bytes' }),
}); });
const json = resp.data; const json = resp.data;
if (resp.status !== 201) { if (resp.status !== 201) {
@ -219,25 +207,18 @@ export const release = async (
): Promise<Release> => { ): Promise<Release> => {
if (maxRetries <= 0) { if (maxRetries <= 0) {
console.log(`❌ Too many retries. Aborting...`); console.log(`❌ Too many retries. Aborting...`);
throw new Error("Too many retries."); throw new Error('Too many retries.');
} }
const [owner, repo] = config.github_repository.split("/"); const [owner, repo] = config.github_repository.split('/');
const tag = const tag =
config.input_tag_name || config.input_tag_name ||
(isTag(config.github_ref) (isTag(config.github_ref) ? config.github_ref.replace('refs/tags/', '') : '');
? config.github_ref.replace("refs/tags/", "")
: "");
const discussion_category_name = config.input_discussion_category_name; const discussion_category_name = config.input_discussion_category_name;
const generate_release_notes = config.input_generate_release_notes; const generate_release_notes = config.input_generate_release_notes;
try { try {
const _release: Release | undefined = await findTagFromReleases( const _release: Release | undefined = await findTagFromReleases(releaser, owner, repo, tag);
releaser,
owner,
repo,
tag,
);
if (_release === undefined) { if (_release === undefined) {
return await createRelease( return await createRelease(
@ -253,9 +234,7 @@ export const release = async (
} }
let existingRelease: Release = _release!; let existingRelease: Release = _release!;
console.log( console.log(`Found release ${existingRelease.name} (with id=${existingRelease.id})`);
`Found release ${existingRelease.name} (with id=${existingRelease.id})`,
);
const release_id = existingRelease.id; const release_id = existingRelease.id;
let target_commitish: string; let target_commitish: string;
@ -277,23 +256,18 @@ export const release = async (
// body parts as a release gets updated. some users will likely want this while // body parts as a release gets updated. some users will likely want this while
// others won't previously this was duplicating content for most which // others won't previously this was duplicating content for most which
// no one wants // no one wants
const workflowBody = releaseBody(config) || ""; const workflowBody = releaseBody(config) || '';
const existingReleaseBody = existingRelease.body || ""; const existingReleaseBody = existingRelease.body || '';
let body: string; let body: string;
if (config.input_append_body && workflowBody && existingReleaseBody) { if (config.input_append_body && workflowBody && existingReleaseBody) {
body = existingReleaseBody + "\n" + workflowBody; body = existingReleaseBody + '\n' + workflowBody;
} else { } else {
body = workflowBody || existingReleaseBody; body = workflowBody || existingReleaseBody;
} }
const draft = const draft = config.input_draft !== undefined ? config.input_draft : existingRelease.draft;
config.input_draft !== undefined
? config.input_draft
: existingRelease.draft;
const prerelease = const prerelease =
config.input_prerelease !== undefined config.input_prerelease !== undefined ? config.input_prerelease : existingRelease.prerelease;
? config.input_prerelease
: existingRelease.prerelease;
const make_latest = config.input_make_latest; const make_latest = config.input_make_latest;
@ -377,13 +351,11 @@ async function createRelease(
const prerelease = config.input_prerelease; const prerelease = config.input_prerelease;
const target_commitish = config.input_target_commitish; const target_commitish = config.input_target_commitish;
const make_latest = config.input_make_latest; const make_latest = config.input_make_latest;
let commitMessage: string = ""; let commitMessage: string = '';
if (target_commitish) { if (target_commitish) {
commitMessage = ` using commit "${target_commitish}"`; commitMessage = ` using commit "${target_commitish}"`;
} }
console.log( console.log(`👩‍🏭 Creating new GitHub release for tag ${tag_name}${commitMessage}...`);
`👩‍🏭 Creating new GitHub release for tag ${tag_name}${commitMessage}...`,
);
try { try {
let release = await releaser.createRelease({ let release = await releaser.createRelease({
owner, owner,
@ -407,16 +379,16 @@ async function createRelease(
switch (error.status) { switch (error.status) {
case 403: case 403:
console.log( console.log(
"Skip retry — your GitHub token/PAT does not have the required permission to create a release", 'Skip retry — your GitHub token/PAT does not have the required permission to create a release',
); );
throw error; throw error;
case 404: case 404:
console.log("Skip retry - discussion category mismatch"); console.log('Skip retry - discussion category mismatch');
throw error; throw error;
case 422: case 422:
console.log("Skip retry - validation failed"); console.log('Skip retry - validation failed');
throw error; throw error;
} }

View file

@ -1,24 +1,14 @@
import { setFailed, setOutput } from "@actions/core"; import { setFailed, setOutput } from '@actions/core';
import { getOctokit } from "@actions/github"; import { getOctokit } from '@actions/github';
import { GitHubReleaser, release, upload } from "./github"; import { GitHubReleaser, release, upload } from './github';
import { import { isTag, parseConfig, paths, unmatchedPatterns, uploadUrl } from './util';
isTag,
parseConfig,
paths,
unmatchedPatterns,
uploadUrl,
} from "./util";
import { env } from "process"; import { env } from 'process';
async function run() { async function run() {
try { try {
const config = parseConfig(env); const config = parseConfig(env);
if ( if (!config.input_tag_name && !isTag(config.github_ref) && !config.input_draft) {
!config.input_tag_name &&
!isTag(config.github_ref) &&
!config.input_draft
) {
throw new Error(`⚠️ GitHub Releases requires a tag`); throw new Error(`⚠️ GitHub Releases requires a tag`);
} }
if (config.input_files) { if (config.input_files) {
@ -44,9 +34,7 @@ async function run() {
//new oktokit( //new oktokit(
throttle: { throttle: {
onRateLimit: (retryAfter, options) => { onRateLimit: (retryAfter, options) => {
console.warn( console.warn(`Request quota exhausted for request ${options.method} ${options.url}`);
`Request quota exhausted for request ${options.method} ${options.url}`,
);
if (options.request.retryCount === 0) { if (options.request.retryCount === 0) {
// only retries once // only retries once
console.log(`Retrying after ${retryAfter} seconds!`); console.log(`Retrying after ${retryAfter} seconds!`);
@ -55,9 +43,7 @@ async function run() {
}, },
onAbuseLimit: (retryAfter, options) => { onAbuseLimit: (retryAfter, options) => {
// does not retry, only logs a warning // does not retry, only logs a warning
console.warn( console.warn(`Abuse detected for request ${options.method} ${options.url}`);
`Abuse detected for request ${options.method} ${options.url}`,
);
}, },
}, },
}); });
@ -67,25 +53,15 @@ async function run() {
const files = paths(config.input_files); const files = paths(config.input_files);
if (files.length == 0) { if (files.length == 0) {
if (config.input_fail_on_unmatched_files) { if (config.input_fail_on_unmatched_files) {
throw new Error( throw new Error(`⚠️ ${config.input_files} does not include a valid file.`);
`⚠️ ${config.input_files} does not include a valid file.`,
);
} else { } else {
console.warn( console.warn(`🤔 ${config.input_files} does not include a valid file.`);
`🤔 ${config.input_files} does not include a valid file.`,
);
} }
} }
const currentAssets = rel.assets; const currentAssets = rel.assets;
const uploadFile = async (path) => { const uploadFile = async (path) => {
const json = await upload( const json = await upload(config, gh, uploadUrl(rel.upload_url), path, currentAssets);
config,
gh,
uploadUrl(rel.upload_url),
path,
currentAssets,
);
if (json) { if (json) {
delete json.uploader; delete json.uploader;
} }
@ -103,12 +79,12 @@ async function run() {
} }
const assets = results.filter(Boolean); const assets = results.filter(Boolean);
setOutput("assets", assets); setOutput('assets', assets);
} }
console.log(`🎉 Release ready at ${rel.html_url}`); console.log(`🎉 Release ready at ${rel.html_url}`);
setOutput("url", rel.html_url); setOutput('url', rel.html_url);
setOutput("id", rel.id.toString()); setOutput('id', rel.id.toString());
setOutput("upload_url", rel.upload_url); setOutput('upload_url', rel.upload_url);
} catch (error) { } catch (error) {
setFailed(error.message); setFailed(error.message);
} }

View file

@ -1,5 +1,5 @@
import * as glob from "glob"; import * as glob from 'glob';
import { statSync, readFileSync } from "fs"; import { statSync, readFileSync } from 'fs';
export interface Config { export interface Config {
github_token: string; github_token: string;
@ -21,11 +21,11 @@ export interface Config {
input_discussion_category_name?: string; input_discussion_category_name?: string;
input_generate_release_notes?: boolean; input_generate_release_notes?: boolean;
input_append_body?: boolean; input_append_body?: boolean;
input_make_latest: "true" | "false" | "legacy" | undefined; input_make_latest: 'true' | 'false' | 'legacy' | undefined;
} }
export const uploadUrl = (url: string): string => { export const uploadUrl = (url: string): string => {
const templateMarkerPos = url.indexOf("{"); const templateMarkerPos = url.indexOf('{');
if (templateMarkerPos > -1) { if (templateMarkerPos > -1) {
return url.substring(0, templateMarkerPos); return url.substring(0, templateMarkerPos);
} }
@ -34,8 +34,7 @@ export const uploadUrl = (url: string): string => {
export const releaseBody = (config: Config): string | undefined => { export const releaseBody = (config: Config): string | undefined => {
return ( return (
(config.input_body_path && (config.input_body_path && readFileSync(config.input_body_path).toString('utf8')) ||
readFileSync(config.input_body_path).toString("utf8")) ||
config.input_body config.input_body
); );
}; };
@ -46,7 +45,7 @@ export const parseInputFiles = (files: string): string[] => {
return files.split(/\r?\n/).reduce<string[]>( return files.split(/\r?\n/).reduce<string[]>(
(acc, line) => (acc, line) =>
acc acc
.concat(line.split(",")) .concat(line.split(','))
.filter((pat) => pat) .filter((pat) => pat)
.map((pat) => pat.trim()), .map((pat) => pat.trim()),
[], [],
@ -55,38 +54,31 @@ export const parseInputFiles = (files: string): string[] => {
export const parseConfig = (env: Env): Config => { export const parseConfig = (env: Env): Config => {
return { return {
github_token: env.GITHUB_TOKEN || env.INPUT_TOKEN || "", github_token: env.GITHUB_TOKEN || env.INPUT_TOKEN || '',
github_ref: env.GITHUB_REF || "", github_ref: env.GITHUB_REF || '',
github_repository: env.INPUT_REPOSITORY || env.GITHUB_REPOSITORY || "", github_repository: env.INPUT_REPOSITORY || env.GITHUB_REPOSITORY || '',
input_name: env.INPUT_NAME, input_name: env.INPUT_NAME,
input_tag_name: env.INPUT_TAG_NAME?.trim(), input_tag_name: env.INPUT_TAG_NAME?.trim(),
input_body: env.INPUT_BODY, input_body: env.INPUT_BODY,
input_body_path: env.INPUT_BODY_PATH, input_body_path: env.INPUT_BODY_PATH,
input_files: parseInputFiles(env.INPUT_FILES || ""), input_files: parseInputFiles(env.INPUT_FILES || ''),
input_overwrite_files: env.INPUT_OVERWRITE_FILES input_overwrite_files: env.INPUT_OVERWRITE_FILES
? env.INPUT_OVERWRITE_FILES == "true" ? env.INPUT_OVERWRITE_FILES == 'true'
: undefined, : undefined,
input_draft: env.INPUT_DRAFT ? env.INPUT_DRAFT === "true" : undefined, input_draft: env.INPUT_DRAFT ? env.INPUT_DRAFT === 'true' : undefined,
input_preserve_order: env.INPUT_PRESERVE_ORDER input_preserve_order: env.INPUT_PRESERVE_ORDER ? env.INPUT_PRESERVE_ORDER == 'true' : undefined,
? env.INPUT_PRESERVE_ORDER == "true" input_prerelease: env.INPUT_PRERELEASE ? env.INPUT_PRERELEASE == 'true' : undefined,
: undefined, input_fail_on_unmatched_files: env.INPUT_FAIL_ON_UNMATCHED_FILES == 'true',
input_prerelease: env.INPUT_PRERELEASE
? env.INPUT_PRERELEASE == "true"
: undefined,
input_fail_on_unmatched_files: env.INPUT_FAIL_ON_UNMATCHED_FILES == "true",
input_target_commitish: env.INPUT_TARGET_COMMITISH || undefined, input_target_commitish: env.INPUT_TARGET_COMMITISH || undefined,
input_discussion_category_name: input_discussion_category_name: env.INPUT_DISCUSSION_CATEGORY_NAME || undefined,
env.INPUT_DISCUSSION_CATEGORY_NAME || undefined, input_generate_release_notes: env.INPUT_GENERATE_RELEASE_NOTES == 'true',
input_generate_release_notes: env.INPUT_GENERATE_RELEASE_NOTES == "true", input_append_body: env.INPUT_APPEND_BODY == 'true',
input_append_body: env.INPUT_APPEND_BODY == "true",
input_make_latest: parseMakeLatest(env.INPUT_MAKE_LATEST), input_make_latest: parseMakeLatest(env.INPUT_MAKE_LATEST),
}; };
}; };
const parseMakeLatest = ( const parseMakeLatest = (value: string | undefined): 'true' | 'false' | 'legacy' | undefined => {
value: string | undefined, if (value === 'true' || value === 'false' || value === 'legacy') {
): "true" | "false" | "legacy" | undefined => {
if (value === "true" || value === "false" || value === "legacy") {
return value; return value;
} }
return undefined; return undefined;
@ -94,26 +86,22 @@ const parseMakeLatest = (
export const paths = (patterns: string[]): string[] => { export const paths = (patterns: string[]): string[] => {
return patterns.reduce((acc: string[], pattern: string): string[] => { return patterns.reduce((acc: string[], pattern: string): string[] => {
return acc.concat( return acc.concat(glob.sync(pattern).filter((path) => statSync(path).isFile()));
glob.sync(pattern).filter((path) => statSync(path).isFile()),
);
}, []); }, []);
}; };
export const unmatchedPatterns = (patterns: string[]): string[] => { export const unmatchedPatterns = (patterns: string[]): string[] => {
return patterns.reduce((acc: string[], pattern: string): string[] => { return patterns.reduce((acc: string[], pattern: string): string[] => {
return acc.concat( return acc.concat(
glob.sync(pattern).filter((path) => statSync(path).isFile()).length == 0 glob.sync(pattern).filter((path) => statSync(path).isFile()).length == 0 ? [pattern] : [],
? [pattern]
: [],
); );
}, []); }, []);
}; };
export const isTag = (ref: string): boolean => { export const isTag = (ref: string): boolean => {
return ref.startsWith("refs/tags/"); return ref.startsWith('refs/tags/');
}; };
export const alignAssetName = (assetName: string): string => { export const alignAssetName = (assetName: string): string => {
return assetName.replace(/ /g, "."); return assetName.replace(/ /g, '.');
}; };