8
8
import * as core from '@actions/core' ;
9
9
import * as github from '@actions/github' ;
10
10
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
+
11
20
async function run ( ) : Promise < void > {
12
21
// partially taken from https://github.com/actions/create-release
13
22
try {
@@ -24,19 +33,33 @@ async function run(): Promise<void> {
24
33
25
34
// Get the inputs from the workflow file: https://github.com/actions/toolkit/tree/master/packages/core#inputsoutputs
26
35
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
+ ) ;
30
45
const commitish = github . context . sha ;
31
46
const delete_assets =
32
47
Boolean ( core . getInput ( 'delete_assets' , { required : false } ) ) || false ;
33
48
const delete_tags_prefix : string | null = core . getInput (
34
49
'delete_tags_prefix' ,
35
50
{ required : false }
36
51
) ;
37
- const new_draft_status : boolean | null = Boolean (
52
+ const new_draft_status_str : string | undefined = emptyStringToUndefined (
38
53
core . getInput ( 'new_draft_status' , { required : false } )
39
54
) ;
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
+
40
63
core . info (
41
64
`arguments: ${ JSON . stringify ( { id, new_name, new_body, new_tag, commitish, delete_assets, delete_tags_prefix, new_draft_status} ) } `
42
65
) ;
@@ -84,7 +107,7 @@ async function run(): Promise<void> {
84
107
core . info ( `${ tagsToBeDeleted . length } release(s) have been deleted` ) ;
85
108
}
86
109
87
- if ( new_tag !== null ) {
110
+ if ( new_tag !== undefined ) {
88
111
core . info ( `Creating tag '${ new_tag } ' from commit '${ commitish } '` ) ;
89
112
// Create a new tag
90
113
// API Documentation: https://developer.github.com/v3/git/tags/#create-a-tag-object
@@ -112,24 +135,47 @@ async function run(): Promise<void> {
112
135
113
136
let getUpdateReleaseResponse ;
114
137
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
120
143
) {
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
+ } = {
124
154
owner,
125
155
repo,
126
156
release_id : id ,
127
- tag_name : new_tag ,
128
- name : new_name ,
129
- body : new_body ,
130
157
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 ) ;
133
179
core . info (
134
180
`Release ${ id } was successfully updated, with the following changes:\n` +
135
181
`- tag_name: ${ new_tag } \n - name: ${ new_name } \n - body: ${ new_body } \n - target_commitish: ${ commitish } \n - draft: ${ new_draft_status } `
0 commit comments