@@ -4,7 +4,7 @@ import { Context } from '@actions/github/lib/context';
4
4
import { Octokit } from '@octokit/rest' ;
5
5
import { exportVariable } from '@actions/core' ;
6
6
import { Logger } from './index' ;
7
- import { getRefForUpdate , isPrRef , getBranch } from './utils' ;
7
+ import { getRefForUpdate , isPrRef , getBranch , trimRef , versionCompare , generateNewPatchVersion } from './utils' ;
8
8
import { getSender } from './context-helper' ;
9
9
10
10
type PullsUpdateParams = {
@@ -100,8 +100,7 @@ export default class ApiHelper {
100
100
*/
101
101
private createBlob = async ( rootDir : string , filepath : string ) : Promise < { path : string ; sha : string } > => {
102
102
const blob = await this . octokit . git . createBlob ( {
103
- owner : this . context . repo . owner ,
104
- repo : this . context . repo . repo ,
103
+ ...this . context . repo ,
105
104
content : Buffer . from ( fs . readFileSync ( path . resolve ( rootDir , filepath ) , 'utf8' ) ) . toString ( 'base64' ) ,
106
105
encoding : 'base64' ,
107
106
} ) ;
@@ -121,8 +120,7 @@ export default class ApiHelper {
121
120
* @return {Promise<Octokit.Response<Octokit.GitGetCommitResponse>> } commit
122
121
*/
123
122
private getCommit = async ( ) : Promise < Octokit . Response < Octokit . GitGetCommitResponse > > => this . octokit . git . getCommit ( {
124
- owner : this . context . repo . owner ,
125
- repo : this . context . repo . repo ,
123
+ ...this . context . repo ,
126
124
'commit_sha' : this . getCommitSha ( ) ,
127
125
} ) ;
128
126
@@ -133,8 +131,7 @@ export default class ApiHelper {
133
131
const key = parseInt ( this . context . payload . number , 10 ) ;
134
132
if ( ! ( key in this . prCache ) ) {
135
133
this . prCache [ key ] = await this . octokit . pulls . get ( {
136
- owner : this . context . repo . owner ,
137
- repo : this . context . repo . repo ,
134
+ ...this . context . repo ,
138
135
'pull_number' : this . context . payload . number ,
139
136
} ) ;
140
137
}
@@ -154,8 +151,7 @@ export default class ApiHelper {
154
151
* @return {Promise<Octokit.Response<Octokit.GitCreateTreeResponse>> } tree
155
152
*/
156
153
public createTree = async ( blobs : { path : string ; sha : string } [ ] ) : Promise < Octokit . Response < Octokit . GitCreateTreeResponse > > => this . octokit . git . createTree ( {
157
- owner : this . context . repo . owner ,
158
- repo : this . context . repo . repo ,
154
+ ...this . context . repo ,
159
155
'base_tree' : ( await this . getCommit ( ) ) . data . tree . sha ,
160
156
tree : Object . values ( blobs ) . map ( blob => ( {
161
157
path : blob . path ,
@@ -171,8 +167,7 @@ export default class ApiHelper {
171
167
* @return {Promise<Octokit.Response<Octokit.GitCreateCommitResponse>> } commit
172
168
*/
173
169
public createCommit = async ( commitMessage : string , tree : Octokit . Response < Octokit . GitCreateTreeResponse > ) : Promise < Octokit . Response < Octokit . GitCreateCommitResponse > > => this . octokit . git . createCommit ( {
174
- owner : this . context . repo . owner ,
175
- repo : this . context . repo . repo ,
170
+ ...this . context . repo ,
176
171
tree : tree . data . sha ,
177
172
parents : [ this . getCommitSha ( ) ] ,
178
173
message : commitMessage ,
@@ -185,8 +180,7 @@ export default class ApiHelper {
185
180
private getRef = async ( refName : string ) : Promise < Octokit . AnyResponse | null > => {
186
181
try {
187
182
return await this . octokit . git . getRef ( {
188
- owner : this . context . repo . owner ,
189
- repo : this . context . repo . repo ,
183
+ ...this . context . repo ,
190
184
ref : refName ,
191
185
} ) ;
192
186
} catch ( error ) {
@@ -203,8 +197,7 @@ export default class ApiHelper {
203
197
public updateRef = async ( commit : Octokit . Response < Octokit . GitCreateCommitResponse > , refName : string , force : boolean ) : Promise < boolean > => {
204
198
try {
205
199
await this . octokit . git . updateRef ( {
206
- owner : this . context . repo . owner ,
207
- repo : this . context . repo . repo ,
200
+ ...this . context . repo ,
208
201
ref : refName ,
209
202
sha : commit . data . sha ,
210
203
force,
@@ -229,8 +222,7 @@ export default class ApiHelper {
229
222
*/
230
223
public createRef = async ( commit : Octokit . Response < Octokit . GitCreateCommitResponse > , refName : string ) : Promise < void > => {
231
224
await this . octokit . git . createRef ( {
232
- owner : this . context . repo . owner ,
233
- repo : this . context . repo . repo ,
225
+ ...this . context . repo ,
234
226
ref : refName ,
235
227
sha : commit . data . sha ,
236
228
} ) ;
@@ -242,8 +234,7 @@ export default class ApiHelper {
242
234
*/
243
235
public deleteRef = async ( refName : string ) : Promise < void > => {
244
236
await this . octokit . git . deleteRef ( {
245
- owner : this . context . repo . owner ,
246
- repo : this . context . repo . repo ,
237
+ ...this . context . repo ,
247
238
ref : refName ,
248
239
} ) ;
249
240
} ;
@@ -254,8 +245,7 @@ export default class ApiHelper {
254
245
*/
255
246
public findPullRequest = async ( branchName : string ) : Promise < Octokit . PullsListResponseItem | null > => {
256
247
const response = await this . octokit . pulls . list ( {
257
- owner : this . context . repo . owner ,
258
- repo : this . context . repo . repo ,
248
+ ...this . context . repo ,
259
249
head : `${ this . context . repo . owner } :${ getBranch ( branchName , false ) } ` ,
260
250
} ) ;
261
251
if ( response . data . length ) {
@@ -274,8 +264,7 @@ export default class ApiHelper {
274
264
sort : 'created' ,
275
265
direction : 'asc' ,
276
266
} , params , {
277
- owner : this . context . repo . owner ,
278
- repo : this . context . repo . repo ,
267
+ ...this . context . repo ,
279
268
} ) ) ,
280
269
) ;
281
270
@@ -285,8 +274,7 @@ export default class ApiHelper {
285
274
* @return {Promise<Octokit.Response<Octokit.PullsCreateResponse>> } pull
286
275
*/
287
276
public pullsCreate = async ( branchName : string , detail : PullsCreateParams ) : Promise < Octokit . Response < Octokit . PullsCreateResponse > > => this . octokit . pulls . create ( {
288
- owner : this . context . repo . owner ,
289
- repo : this . context . repo . repo ,
277
+ ...this . context . repo ,
290
278
head : `${ this . context . repo . owner } :${ getBranch ( branchName , false ) } ` ,
291
279
base : ( await this . getRefForUpdate ( false ) ) . replace ( / ^ h e a d s \/ / , '' ) ,
292
280
...detail ,
@@ -298,8 +286,7 @@ export default class ApiHelper {
298
286
* @return {Promise<Octokit.Response<Octokit.PullsUpdateResponse>> } pull
299
287
*/
300
288
public pullsUpdate = async ( number : number , detail : PullsUpdateParams ) : Promise < Octokit . Response < Octokit . PullsUpdateResponse > > => this . octokit . pulls . update ( {
301
- owner : this . context . repo . owner ,
302
- repo : this . context . repo . repo ,
289
+ ...this . context . repo ,
303
290
'pull_number' : number ,
304
291
base : ( await this . getRefForUpdate ( false ) ) . replace ( / ^ h e a d s \/ / , '' ) ,
305
292
state : 'open' ,
@@ -379,8 +366,7 @@ export default class ApiHelper {
379
366
}
380
367
381
368
await this . octokit . issues . createComment ( {
382
- owner : this . context . repo . owner ,
383
- repo : this . context . repo . repo ,
369
+ ...this . context . repo ,
384
370
'issue_number' : pullRequest . number ,
385
371
body,
386
372
} ) ;
@@ -528,9 +514,30 @@ export default class ApiHelper {
528
514
} ;
529
515
} ;
530
516
531
- // eslint-disable-next-line camelcase
532
- public getDefaultBranch = async ( ) : Promise < string > => this . context . payload . repository ?. default_branch ?? ( await this . octokit . repos . get ( {
533
- owner : this . context . repo . owner ,
534
- repo : this . context . repo . repo ,
517
+ /**
518
+ * @return {Promise<string> } default branch
519
+ */
520
+ public getDefaultBranch = async ( ) : Promise < string > => this . context . payload . repository ?. default_branch ?? ( await this . octokit . repos . get ( { // eslint-disable-line camelcase
521
+ ...this . context . repo ,
535
522
} ) ) . data . default_branch ;
523
+
524
+ /**
525
+ * @return {Promise<Array<string>> } tags
526
+ */
527
+ public getTags = async ( ) : Promise < Array < string > > => ( await this . octokit . paginate (
528
+ this . octokit . git . listMatchingRefs . endpoint . merge ( {
529
+ ...this . context . repo ,
530
+ ref : 'tags/' ,
531
+ } ) ,
532
+ ) ) . map ( ( item : Octokit . GitListMatchingRefsResponseItem ) : string => trimRef ( item . ref ) ) ;
533
+
534
+ /**
535
+ * @return {Promise<string> } tag
536
+ */
537
+ public getLastTag = async ( ) : Promise < string > => 'v' + ( ( await this . getTags ( ) ) . filter ( tag => / ^ v ? \d + ( \. \d + ) * $ / . test ( tag ) ) . sort ( versionCompare ) . reverse ( ) [ 0 ] ?. replace ( / ^ v / , '' ) ?? '0.0.0' ) ;
538
+
539
+ /**
540
+ * @return {Promise<string> } tag
541
+ */
542
+ public getNewPatchVersion = async ( ) : Promise < string > => generateNewPatchVersion ( await this . getLastTag ( ) ) ;
536
543
}
0 commit comments