Skip to content

Commit ae5174e

Browse files
committed
don't inadvertently pass empty parameters
1 parent 30a7721 commit ae5174e

File tree

1 file changed

+64
-18
lines changed

1 file changed

+64
-18
lines changed

src/main.ts

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@
88
import * as core from '@actions/core';
99
import * as github from '@actions/github';
1010

11+
// Function to turn empty string to null
12+
function emptyStringToUndefined(s: string): string | undefined {
13+
if (s === '') {
14+
return undefined;
15+
} else {
16+
return s;
17+
}
18+
}
19+
1120
async function run(): Promise<void> {
1221
// partially taken from https://github.com/actions/create-release
1322
try {
@@ -24,19 +33,33 @@ async function run(): Promise<void> {
2433

2534
// Get the inputs from the workflow file: https://github.com/actions/toolkit/tree/master/packages/core#inputsoutputs
2635
const id = Number(core.getInput('id', {required: true}));
27-
const new_name = core.getInput('new_name', {required: false});
28-
const new_body = core.getInput('new_body', {required: false});
29-
const new_tag = core.getInput('new_tag', {required: false});
36+
const new_name = emptyStringToUndefined(
37+
core.getInput('new_name', {required: false})
38+
);
39+
const new_body = emptyStringToUndefined(
40+
core.getInput('new_body', {required: false})
41+
);
42+
const new_tag = emptyStringToUndefined(
43+
core.getInput('new_tag', {required: false})
44+
);
3045
const commitish = github.context.sha;
3146
const delete_assets =
3247
Boolean(core.getInput('delete_assets', {required: false})) || false;
3348
const delete_tags_prefix: string | null = core.getInput(
3449
'delete_tags_prefix',
3550
{required: false}
3651
);
37-
const new_draft_status: boolean | null = Boolean(
52+
const new_draft_status_str: string | undefined = emptyStringToUndefined(
3853
core.getInput('new_draft_status', {required: false})
3954
);
55+
let new_draft_status: boolean | undefined = undefined;
56+
if (new_draft_status_str !== undefined) {
57+
if (new_draft_status_str !== 'true' && new_draft_status_str !== 'false') {
58+
throw new Error(`new_draft_status must be either 'true' or 'false'`);
59+
}
60+
new_draft_status = new_draft_status_str === 'true';
61+
}
62+
4063
core.info(
4164
`arguments: ${JSON.stringify({id, new_name, new_body, new_tag, commitish, delete_assets, delete_tags_prefix, new_draft_status})}`
4265
);
@@ -84,7 +107,7 @@ async function run(): Promise<void> {
84107
core.info(`${tagsToBeDeleted.length} release(s) have been deleted`);
85108
}
86109

87-
if (new_tag !== null) {
110+
if (new_tag !== undefined) {
88111
core.info(`Creating tag '${new_tag}' from commit '${commitish}'`);
89112
// Create a new tag
90113
// API Documentation: https://developer.github.com/v3/git/tags/#create-a-tag-object
@@ -112,24 +135,47 @@ async function run(): Promise<void> {
112135

113136
let getUpdateReleaseResponse;
114137
if (
115-
new_tag !== null ||
116-
new_name !== null ||
117-
new_body !== null ||
118-
commitish !== null ||
119-
new_draft_status !== null
138+
new_tag !== undefined ||
139+
new_name !== undefined ||
140+
new_body !== undefined ||
141+
commitish !== undefined ||
142+
new_draft_status !== undefined
120143
) {
121-
// API Documentation: https://developer.github.com/v3/repos/releases
122-
// Octokit Documentation: https://octokit.github.io/rest.js
123-
getUpdateReleaseResponse = await octokit.rest.repos.updateRelease({
144+
let updateReleaseParams: {
145+
owner: string;
146+
repo: string;
147+
release_id: number;
148+
target_commitish: string;
149+
tag_name: string | undefined;
150+
name: string | undefined;
151+
body: string | undefined;
152+
draft: boolean | undefined;
153+
} = {
124154
owner,
125155
repo,
126156
release_id: id,
127-
tag_name: new_tag,
128-
name: new_name,
129-
body: new_body,
130157
target_commitish: commitish,
131-
draft: new_draft_status
132-
});
158+
tag_name: undefined,
159+
name: undefined,
160+
body: undefined,
161+
draft: undefined
162+
};
163+
if (new_tag !== null) {
164+
updateReleaseParams = {...updateReleaseParams, tag_name: new_tag};
165+
}
166+
if (new_name !== null) {
167+
updateReleaseParams = {...updateReleaseParams, name: new_name};
168+
}
169+
if (new_body !== null) {
170+
updateReleaseParams = {...updateReleaseParams, body: new_body};
171+
}
172+
if (new_draft_status !== null) {
173+
updateReleaseParams = {...updateReleaseParams, draft: new_draft_status};
174+
}
175+
// API Documentation: https://developer.github.com/v3/repos/releases
176+
// Octokit Documentation: https://octokit.github.io/rest.js
177+
getUpdateReleaseResponse =
178+
await octokit.rest.repos.updateRelease(updateReleaseParams);
133179
core.info(
134180
`Release ${id} was successfully updated, with the following changes:\n` +
135181
`- tag_name: ${new_tag}\n - name: ${new_name}\n - body: ${new_body}\n - target_commitish: ${commitish}\n - draft: ${new_draft_status}`

0 commit comments

Comments
 (0)