Add input_id

This commit is contained in:
patrick brisbin 2023-05-26 11:52:33 -04:00
parent c9b46fe7aa
commit 4dac02b400
No known key found for this signature in database
GPG key ID: 07BF97A312D7F34C
3 changed files with 77 additions and 24 deletions

View file

@ -3,6 +3,9 @@ name: "GH Release"
description: "Github Action for creating Github Releases" description: "Github Action for creating Github Releases"
author: "softprops" author: "softprops"
inputs: inputs:
id:
description: "An existing release id, useful if updating"
required: false
body: body:
description: "Note-worthy description of changes in release" description: "Note-worthy description of changes in release"
required: false required: false

View file

@ -28,6 +28,12 @@ export interface Release {
} }
export interface Releaser { export interface Releaser {
getReleaseById(params: {
owner: string;
repo: string;
release_id: number;
}): Promise<{ data: Release }>;
getReleaseByTag(params: { getReleaseByTag(params: {
owner: string; owner: string;
repo: string; repo: string;
@ -73,6 +79,14 @@ export class GitHubReleaser implements Releaser {
this.github = github; this.github = github;
} }
getReleaseById(params: {
owner: string;
repo: string;
release_id: number;
}): Promise<{ data: Release }> {
return this.github.rest.repos.getRelease(params);
}
getReleaseByTag(params: { getReleaseByTag(params: {
owner: string; owner: string;
repo: string; repo: string;
@ -199,47 +213,36 @@ export const release = async (
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 {
// you can't get a an existing draft by tag const existingRelease = await findExistingRelease(
// so we must find one in the list of all releases config,
if (config.input_draft) { releaser,
for await (const response of releaser.allReleases({
owner, owner,
repo, repo,
})) { tag
let release = response.data.find((release) => release.tag_name === tag); );
if (release) {
return release;
}
}
}
let existingRelease = await releaser.getReleaseByTag({
owner,
repo,
tag,
});
const release_id = existingRelease.data.id; const release_id = existingRelease.id;
let target_commitish: string; let target_commitish: string;
if ( if (
config.input_target_commitish && config.input_target_commitish &&
config.input_target_commitish !== existingRelease.data.target_commitish config.input_target_commitish !== existingRelease.target_commitish
) { ) {
console.log( console.log(
`Updating commit from "${existingRelease.data.target_commitish}" to "${config.input_target_commitish}"` `Updating commit from "${existingRelease.target_commitish}" to "${config.input_target_commitish}"`
); );
target_commitish = config.input_target_commitish; target_commitish = config.input_target_commitish;
} else { } else {
target_commitish = existingRelease.data.target_commitish; target_commitish = existingRelease.target_commitish;
} }
const tag_name = tag; const tag_name = tag;
const name = config.input_name || existingRelease.data.name || tag; const name = config.input_name || existingRelease.name || tag;
// revisit: support a new body-concat-strategy input for accumulating // revisit: support a new body-concat-strategy input for accumulating
// 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.data.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;
@ -250,11 +253,11 @@ export const release = async (
const draft = const draft =
config.input_draft !== undefined config.input_draft !== undefined
? config.input_draft ? config.input_draft
: existingRelease.data.draft; : existingRelease.draft;
const prerelease = const prerelease =
config.input_prerelease !== undefined config.input_prerelease !== undefined
? config.input_prerelease ? config.input_prerelease
: existingRelease.data.prerelease; : existingRelease.prerelease;
const release = await releaser.updateRelease({ const release = await releaser.updateRelease({
owner, owner,
@ -318,3 +321,48 @@ export const release = async (
} }
} }
}; };
async function findExistingRelease(
config: Config,
releaser: Releaser,
owner: string,
repo: string,
tag: string
): Promise<Release> {
const release_id = config.input_id;
if (release_id !== undefined) {
const { data } = await releaser.getReleaseById({
owner,
repo,
release_id,
});
console.log(`Found Release by id: ${release_id}`);
return data;
}
// you can't get a an existing draft by tag
// so we must find one in the list of all releases
if (config.input_draft) {
for await (const response of releaser.allReleases({
owner,
repo,
})) {
let release = response.data.find((release) => release.tag_name === tag);
if (release) {
console.log(`Found draft Release by tag: ${tag}`);
return release;
}
}
}
const { data } = await releaser.getReleaseByTag({
owner,
repo,
tag,
});
console.log(`Found release by tag: ${tag}`);
return data;
}

View file

@ -6,6 +6,7 @@ export interface Config {
github_ref: string; github_ref: string;
github_repository: string; github_repository: string;
// user provided // user provided
input_id?: number;
input_name?: string; input_name?: string;
input_tag_name?: string; input_tag_name?: string;
input_repository?: string; input_repository?: string;
@ -55,6 +56,7 @@ export const parseConfig = (env: Env): Config => {
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_id: env.INPUT_ID ? parseInt(env.INPUT_ID.trim(), 10) : undefined,
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,