mirror of
https://gh-proxy.org/https://github.com/softprops/action-gh-release.git
synced 2026-07-16 23:43:00 +08:00
d015dc32db
* chore(deps): bump the npm group across 1 directory with 8 updates Bumps the npm group with 7 updates in the / directory: | Package | From | To | | --- | --- | --- | | [@actions/core](https://github.com/actions/toolkit/tree/HEAD/packages/core) | `2.0.2` | `3.0.0` | | [@actions/github](https://github.com/actions/toolkit/tree/HEAD/packages/github) | `7.0.0` | `9.0.0` | | [@octokit/plugin-retry](https://github.com/octokit/plugin-retry.js) | `8.0.3` | `8.1.0` | | [glob](https://github.com/isaacs/node-glob) | `13.0.0` | `13.0.6` | | [@types/node](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/node) | `20.19.30` | `20.19.33` | | [@vitest/coverage-v8](https://github.com/vitest-dev/vitest/tree/HEAD/packages/coverage-v8) | `4.0.17` | `4.0.18` | | [prettier](https://github.com/prettier/prettier) | `3.8.0` | `3.8.1` | Updates `@actions/core` from 2.0.2 to 3.0.0 - [Changelog](https://github.com/actions/toolkit/blob/main/packages/core/RELEASES.md) - [Commits](https://github.com/actions/toolkit/commits/HEAD/packages/core) Updates `@actions/github` from 7.0.0 to 9.0.0 - [Changelog](https://github.com/actions/toolkit/blob/main/packages/github/RELEASES.md) - [Commits](https://github.com/actions/toolkit/commits/HEAD/packages/github) Updates `@octokit/plugin-retry` from 8.0.3 to 8.1.0 - [Release notes](https://github.com/octokit/plugin-retry.js/releases) - [Commits](https://github.com/octokit/plugin-retry.js/compare/v8.0.3...v8.1.0) Updates `glob` from 13.0.0 to 13.0.6 - [Changelog](https://github.com/isaacs/node-glob/blob/main/changelog.md) - [Commits](https://github.com/isaacs/node-glob/compare/v13.0.0...v13.0.6) Updates `@types/node` from 20.19.30 to 20.19.33 - [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases) - [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/node) Updates `@vitest/coverage-v8` from 4.0.17 to 4.0.18 - [Release notes](https://github.com/vitest-dev/vitest/releases) - [Commits](https://github.com/vitest-dev/vitest/commits/v4.0.18/packages/coverage-v8) Updates `prettier` from 3.8.0 to 3.8.1 - [Release notes](https://github.com/prettier/prettier/releases) - [Changelog](https://github.com/prettier/prettier/blob/main/CHANGELOG.md) - [Commits](https://github.com/prettier/prettier/compare/3.8.0...3.8.1) Updates `vitest` from 4.0.17 to 4.0.18 - [Release notes](https://github.com/vitest-dev/vitest/releases) - [Commits](https://github.com/vitest-dev/vitest/commits/v4.0.18/packages/vitest) --- updated-dependencies: - dependency-name: "@actions/core" dependency-version: 3.0.0 dependency-type: direct:production update-type: version-update:semver-major dependency-group: npm - dependency-name: "@actions/github" dependency-version: 9.0.0 dependency-type: direct:production update-type: version-update:semver-major dependency-group: npm - dependency-name: "@octokit/plugin-retry" dependency-version: 8.1.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: npm - dependency-name: glob dependency-version: 13.0.6 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: npm - dependency-name: "@types/node" dependency-version: 20.19.33 dependency-type: direct:development update-type: version-update:semver-patch dependency-group: npm - dependency-name: "@vitest/coverage-v8" dependency-version: 4.0.18 dependency-type: direct:development update-type: version-update:semver-patch dependency-group: npm - dependency-name: prettier dependency-version: 3.8.1 dependency-type: direct:development update-type: version-update:semver-patch dependency-group: npm - dependency-name: vitest dependency-version: 4.0.18 dependency-type: direct:development update-type: version-update:semver-patch dependency-group: npm ... Signed-off-by: dependabot[bot] <support@github.com> * fix ESM dependency build on dependabot branch Signed-off-by: Rui Chen <rui@chenrui.dev> * remove unused ncc dependency Signed-off-by: Rui Chen <rui@chenrui.dev> --------- Signed-off-by: dependabot[bot] <support@github.com> Signed-off-by: Rui Chen <rui@chenrui.dev> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Rui Chen <rui@chenrui.dev>
500 lines
15 KiB
TypeScript
500 lines
15 KiB
TypeScript
import { GitHub } from '@actions/github/lib/utils';
|
|
import { statSync } from 'fs';
|
|
import { open } from 'fs/promises';
|
|
import { lookup } from 'mime-types';
|
|
import { basename } from 'path';
|
|
import { alignAssetName, Config, isTag, releaseBody } from './util';
|
|
|
|
type GitHub = InstanceType<typeof GitHub>;
|
|
|
|
export interface ReleaseAsset {
|
|
name: string;
|
|
mime: string;
|
|
size: number;
|
|
}
|
|
|
|
export interface Release {
|
|
id: number;
|
|
upload_url: string;
|
|
html_url: string;
|
|
tag_name: string;
|
|
name: string | null;
|
|
body?: string | null | undefined;
|
|
target_commitish: string;
|
|
draft: boolean;
|
|
prerelease: boolean;
|
|
assets: Array<{ id: number; name: string }>;
|
|
}
|
|
|
|
export interface Releaser {
|
|
getReleaseByTag(params: { owner: string; repo: string; 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: 'true' | 'false' | 'legacy' | undefined;
|
|
}): 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: 'true' | 'false' | 'legacy' | undefined;
|
|
}): Promise<{ data: Release }>;
|
|
|
|
finalizeRelease(params: {
|
|
owner: string;
|
|
repo: string;
|
|
release_id: number;
|
|
}): Promise<{ data: Release }>;
|
|
|
|
allReleases(params: { owner: string; repo: string }): AsyncIterable<{ data: Release[] }>;
|
|
}
|
|
|
|
export class GitHubReleaser implements Releaser {
|
|
github: GitHub;
|
|
constructor(github: GitHub) {
|
|
this.github = github;
|
|
}
|
|
|
|
getReleaseByTag(params: {
|
|
owner: string;
|
|
repo: string;
|
|
tag: string;
|
|
}): Promise<{ data: Release }> {
|
|
return this.github.rest.repos.getReleaseByTag(params);
|
|
}
|
|
|
|
async getReleaseNotes(params: {
|
|
owner: string;
|
|
repo: string;
|
|
tag_name: string;
|
|
target_commitish: string | undefined;
|
|
}): Promise<{
|
|
data: {
|
|
name: string;
|
|
body: string;
|
|
};
|
|
}> {
|
|
return await this.github.rest.repos.generateReleaseNotes(params);
|
|
}
|
|
|
|
truncateReleaseNotes(input: string): string {
|
|
// release notes can be a maximum of 125000 characters
|
|
const githubNotesMaxCharLength = 125000;
|
|
return input.substring(0, githubNotesMaxCharLength - 1);
|
|
}
|
|
|
|
async 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: 'true' | 'false' | 'legacy' | undefined;
|
|
}): Promise<{ data: Release }> {
|
|
if (
|
|
typeof params.make_latest === 'string' &&
|
|
!['true', 'false', 'legacy'].includes(params.make_latest)
|
|
) {
|
|
params.make_latest = undefined;
|
|
}
|
|
if (params.generate_release_notes) {
|
|
const releaseNotes = await this.getReleaseNotes(params);
|
|
params.generate_release_notes = false;
|
|
if (params.body) {
|
|
params.body = `${params.body}\n\n${releaseNotes.data.body}`;
|
|
} else {
|
|
params.body = releaseNotes.data.body;
|
|
}
|
|
}
|
|
params.body = params.body ? this.truncateReleaseNotes(params.body) : undefined;
|
|
return this.github.rest.repos.createRelease(params);
|
|
}
|
|
|
|
async 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: 'true' | 'false' | 'legacy' | undefined;
|
|
}): Promise<{ data: Release }> {
|
|
if (
|
|
typeof params.make_latest === 'string' &&
|
|
!['true', 'false', 'legacy'].includes(params.make_latest)
|
|
) {
|
|
params.make_latest = undefined;
|
|
}
|
|
if (params.generate_release_notes) {
|
|
const releaseNotes = await this.getReleaseNotes(params);
|
|
params.generate_release_notes = false;
|
|
if (params.body) {
|
|
params.body = `${params.body}\n\n${releaseNotes.data.body}`;
|
|
} else {
|
|
params.body = releaseNotes.data.body;
|
|
}
|
|
}
|
|
params.body = params.body ? this.truncateReleaseNotes(params.body) : undefined;
|
|
return this.github.rest.repos.updateRelease(params);
|
|
}
|
|
|
|
async finalizeRelease(params: { owner: string; repo: string; release_id: number }) {
|
|
return await this.github.rest.repos.updateRelease({
|
|
owner: params.owner,
|
|
repo: params.repo,
|
|
release_id: params.release_id,
|
|
draft: false,
|
|
});
|
|
}
|
|
|
|
allReleases(params: { owner: string; repo: string }): AsyncIterable<{ data: Release[] }> {
|
|
const updatedParams = { per_page: 100, ...params };
|
|
return this.github.paginate.iterator(
|
|
this.github.rest.repos.listReleases.endpoint.merge(updatedParams),
|
|
);
|
|
}
|
|
}
|
|
|
|
export const asset = (path: string): ReleaseAsset => {
|
|
return {
|
|
name: basename(path),
|
|
mime: mimeOrDefault(path),
|
|
size: statSync(path).size,
|
|
};
|
|
};
|
|
|
|
export const mimeOrDefault = (path: string): string => {
|
|
return lookup(path) || 'application/octet-stream';
|
|
};
|
|
|
|
export const upload = async (
|
|
config: Config,
|
|
github: GitHub,
|
|
url: string,
|
|
path: string,
|
|
currentAssets: Array<{ id: number; name: string }>,
|
|
): Promise<any> => {
|
|
const [owner, repo] = config.github_repository.split('/');
|
|
const { name, mime, size } = asset(path);
|
|
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.
|
|
// due to this renaming we need to be mindful when we compare the file name we're uploading with a name github may already have rewritten for logical comparison
|
|
// see https://docs.github.com/en/rest/releases/assets?apiVersion=2022-11-28#upload-a-release-asset
|
|
({ name: currentName }) => currentName == alignAssetName(name),
|
|
);
|
|
if (currentAsset) {
|
|
if (config.input_overwrite_files === false) {
|
|
console.log(`Asset ${name} already exists and overwrite_files is false...`);
|
|
return null;
|
|
} else {
|
|
console.log(`♻️ Deleting previously uploaded asset ${name}...`);
|
|
await github.rest.repos.deleteReleaseAsset({
|
|
asset_id: currentAsset.id || 1,
|
|
owner,
|
|
repo,
|
|
});
|
|
}
|
|
}
|
|
console.log(`⬆️ Uploading ${name}...`);
|
|
const endpoint = new URL(url);
|
|
endpoint.searchParams.append('name', name);
|
|
const fh = await open(path);
|
|
try {
|
|
const resp = await github.request({
|
|
method: 'POST',
|
|
url: endpoint.toString(),
|
|
headers: {
|
|
'content-length': `${size}`,
|
|
'content-type': mime,
|
|
authorization: `token ${config.github_token}`,
|
|
},
|
|
data: fh.readableWebStream({ type: 'bytes' }),
|
|
});
|
|
const json = resp.data;
|
|
if (resp.status !== 201) {
|
|
throw new Error(
|
|
`Failed to upload release asset ${name}. received status code ${
|
|
resp.status
|
|
}\n${json.message}\n${JSON.stringify(json.errors)}`,
|
|
);
|
|
}
|
|
console.log(`✅ Uploaded ${name}`);
|
|
return json;
|
|
} finally {
|
|
await fh.close();
|
|
}
|
|
};
|
|
|
|
export const release = async (
|
|
config: Config,
|
|
releaser: Releaser,
|
|
maxRetries: number = 3,
|
|
): Promise<Release> => {
|
|
if (maxRetries <= 0) {
|
|
console.log(`❌ Too many retries. Aborting...`);
|
|
throw new Error('Too many retries.');
|
|
}
|
|
|
|
const [owner, repo] = config.github_repository.split('/');
|
|
const tag =
|
|
config.input_tag_name ||
|
|
(isTag(config.github_ref) ? config.github_ref.replace('refs/tags/', '') : '');
|
|
|
|
const discussion_category_name = config.input_discussion_category_name;
|
|
const generate_release_notes = config.input_generate_release_notes;
|
|
try {
|
|
const _release: Release | undefined = await findTagFromReleases(releaser, owner, repo, tag);
|
|
|
|
if (_release === undefined) {
|
|
return await createRelease(
|
|
tag,
|
|
config,
|
|
releaser,
|
|
owner,
|
|
repo,
|
|
discussion_category_name,
|
|
generate_release_notes,
|
|
maxRetries,
|
|
);
|
|
}
|
|
|
|
let existingRelease: Release = _release!;
|
|
console.log(`Found release ${existingRelease.name} (with id=${existingRelease.id})`);
|
|
|
|
const release_id = existingRelease.id;
|
|
let target_commitish: string;
|
|
if (
|
|
config.input_target_commitish &&
|
|
config.input_target_commitish !== existingRelease.target_commitish
|
|
) {
|
|
console.log(
|
|
`Updating commit from "${existingRelease.target_commitish}" to "${config.input_target_commitish}"`,
|
|
);
|
|
target_commitish = config.input_target_commitish;
|
|
} else {
|
|
target_commitish = existingRelease.target_commitish;
|
|
}
|
|
|
|
const tag_name = tag;
|
|
const name = config.input_name || existingRelease.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
|
|
// others won't previously this was duplicating content for most which
|
|
// no one wants
|
|
const workflowBody = releaseBody(config) || '';
|
|
const existingReleaseBody = existingRelease.body || '';
|
|
let body: string;
|
|
if (config.input_append_body && workflowBody && existingReleaseBody) {
|
|
body = existingReleaseBody + '\n' + workflowBody;
|
|
} else {
|
|
body = workflowBody || existingReleaseBody;
|
|
}
|
|
|
|
const prerelease =
|
|
config.input_prerelease !== undefined ? config.input_prerelease : existingRelease.prerelease;
|
|
|
|
const make_latest = config.input_make_latest;
|
|
|
|
const release = await releaser.updateRelease({
|
|
owner,
|
|
repo,
|
|
release_id,
|
|
tag_name,
|
|
target_commitish,
|
|
name,
|
|
body,
|
|
draft: existingRelease.draft,
|
|
prerelease,
|
|
discussion_category_name,
|
|
generate_release_notes,
|
|
make_latest,
|
|
});
|
|
return release.data;
|
|
} catch (error) {
|
|
if (error.status !== 404) {
|
|
console.log(
|
|
`⚠️ Unexpected error fetching GitHub release for tag ${config.github_ref}: ${error}`,
|
|
);
|
|
throw error;
|
|
}
|
|
|
|
return await createRelease(
|
|
tag,
|
|
config,
|
|
releaser,
|
|
owner,
|
|
repo,
|
|
discussion_category_name,
|
|
generate_release_notes,
|
|
maxRetries,
|
|
);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Finalizes a release by unmarking it as "draft" (if relevant)
|
|
* after all artifacts have been uploaded.
|
|
*
|
|
* @param config - Release configuration as specified by user
|
|
* @param releaser - The GitHub API wrapper for release operations
|
|
* @param release - The existing release to be finalized
|
|
* @param maxRetries - The maximum number of attempts to finalize the release
|
|
*/
|
|
export const finalizeRelease = async (
|
|
config: Config,
|
|
releaser: Releaser,
|
|
release: Release,
|
|
maxRetries: number = 3,
|
|
): Promise<Release> => {
|
|
if (config.input_draft === true) {
|
|
return release;
|
|
}
|
|
|
|
if (maxRetries <= 0) {
|
|
console.log(`❌ Too many retries. Aborting...`);
|
|
throw new Error('Too many retries.');
|
|
}
|
|
|
|
const [owner, repo] = config.github_repository.split('/');
|
|
try {
|
|
const { data } = await releaser.finalizeRelease({
|
|
owner,
|
|
repo,
|
|
release_id: release.id,
|
|
});
|
|
|
|
return data;
|
|
} catch (error) {
|
|
console.warn(`error finalizing release: ${error}`);
|
|
console.log(`retrying... (${maxRetries - 1} retries remaining)`);
|
|
return finalizeRelease(config, releaser, release, maxRetries - 1);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Finds a release by tag name from all a repository's releases.
|
|
*
|
|
* @param releaser - The GitHub API wrapper for release operations
|
|
* @param owner - The owner of the repository
|
|
* @param repo - The name of the repository
|
|
* @param tag - The tag name to search for
|
|
* @returns The release with the given tag name, or undefined if no release with that tag name is found
|
|
*/
|
|
export async function findTagFromReleases(
|
|
releaser: Releaser,
|
|
owner: string,
|
|
repo: string,
|
|
tag: string,
|
|
): Promise<Release | undefined> {
|
|
for await (const { data: releases } of releaser.allReleases({
|
|
owner,
|
|
repo,
|
|
})) {
|
|
const release = releases.find((release) => release.tag_name === tag);
|
|
if (release) {
|
|
return release;
|
|
}
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
async function createRelease(
|
|
tag: string,
|
|
config: Config,
|
|
releaser: Releaser,
|
|
owner: string,
|
|
repo: string,
|
|
discussion_category_name: string | undefined,
|
|
generate_release_notes: boolean | undefined,
|
|
maxRetries: number,
|
|
) {
|
|
const tag_name = tag;
|
|
const name = config.input_name || tag;
|
|
const body = releaseBody(config);
|
|
const prerelease = config.input_prerelease;
|
|
const target_commitish = config.input_target_commitish;
|
|
const make_latest = config.input_make_latest;
|
|
let commitMessage: string = '';
|
|
if (target_commitish) {
|
|
commitMessage = ` using commit "${target_commitish}"`;
|
|
}
|
|
console.log(`👩🏭 Creating new GitHub release for tag ${tag_name}${commitMessage}...`);
|
|
try {
|
|
let release = await releaser.createRelease({
|
|
owner,
|
|
repo,
|
|
tag_name,
|
|
name,
|
|
body,
|
|
draft: true,
|
|
prerelease,
|
|
target_commitish,
|
|
discussion_category_name,
|
|
generate_release_notes,
|
|
make_latest,
|
|
});
|
|
return release.data;
|
|
} catch (error) {
|
|
// presume a race with competing matrix runs
|
|
console.log(`⚠️ GitHub release failed with status: ${error.status}`);
|
|
console.log(`${JSON.stringify(error.response.data)}`);
|
|
|
|
switch (error.status) {
|
|
case 403:
|
|
console.log(
|
|
'Skip retry — your GitHub token/PAT does not have the required permission to create a release',
|
|
);
|
|
throw error;
|
|
|
|
case 404:
|
|
console.log('Skip retry - discussion category mismatch');
|
|
throw error;
|
|
|
|
case 422:
|
|
// Check if this is a race condition with "already_exists" error
|
|
const errorData = error.response?.data;
|
|
if (errorData?.errors?.[0]?.code === 'already_exists') {
|
|
console.log(
|
|
'⚠️ Release already exists (race condition detected), retrying to find and update existing release...',
|
|
);
|
|
// Don't throw - allow retry to find existing release
|
|
} else {
|
|
console.log('Skip retry - validation failed');
|
|
throw error;
|
|
}
|
|
break;
|
|
}
|
|
|
|
console.log(`retrying... (${maxRetries - 1} retries remaining)`);
|
|
return release(config, releaser, maxRetries - 1);
|
|
}
|
|
}
|