1
1
import fs from 'fs' ;
2
2
import path from 'path' ;
3
3
import { Context } from '@actions/github/lib/context' ;
4
- import { Octokit } from '@octokit/rest' ;
5
- import { Octokit as OctokitCore } from '@octokit/core' ;
4
+ import { GitHub } from '@actions/github/lib/utils' ;
5
+ import { PaginateInterface } from '@octokit/plugin-paginate-rest' ;
6
+ import { RestEndpointMethods } from '@octokit/plugin-rest-endpoint-methods/dist-types/generated/method-types' ;
7
+ import {
8
+ OctokitResponse ,
9
+ GitGetCommitResponseData ,
10
+ PullsGetResponseData ,
11
+ GitCreateTreeResponseData ,
12
+ GitCreateCommitResponseData ,
13
+ GitGetRefResponseData ,
14
+ PullsListResponseData ,
15
+ PullsCreateResponseData ,
16
+ PullsUpdateResponseData ,
17
+ GitListMatchingRefsResponseData ,
18
+ } from '@octokit/types' ;
6
19
import { exportVariable } from '@actions/core' ;
7
20
import { Logger } from './index' ;
8
21
import { getRefForUpdate , isPrRef , getBranch , trimRef , versionCompare , generateNewPatchVersion , generateNewMajorVersion , generateNewMinorVersion } from './utils' ;
@@ -42,14 +55,14 @@ type PullsListParams = {
42
55
*/
43
56
export default class ApiHelper {
44
57
45
- private readonly branch ?: string | undefined = undefined ;
46
- private readonly sender ?: string | undefined = undefined ;
47
- private readonly suppressBPError ?: boolean | undefined = undefined ;
48
- private readonly refForUpdate ?: string | undefined = undefined ;
49
- private prCache : { [ key : number ] : Octokit . Response < Octokit . PullsGetResponse > } = { } ;
58
+ private readonly branch ?: string | undefined = undefined ;
59
+ private readonly sender ?: string | undefined = undefined ;
60
+ private readonly suppressBPError ?: boolean | undefined = undefined ;
61
+ private readonly refForUpdate ?: string | undefined = undefined ;
62
+ private prCache : { [ key : number ] : PullsGetResponseData } = { } ;
50
63
51
64
/**
52
- * @param {Octokit } octokit octokit
65
+ * @param {RestEndpointMethods } octokit octokit
53
66
* @param {Context } context context
54
67
* @param {Logger } logger logger
55
68
* @param {object } options options
@@ -59,7 +72,7 @@ export default class ApiHelper {
59
72
* @param {boolean|undefined } options.suppressBPError suppress branch protection error?
60
73
*/
61
74
constructor (
62
- private readonly octokit : OctokitCore ,
75
+ private readonly octokit : InstanceType < typeof GitHub > ,
63
76
private readonly context : Context ,
64
77
private readonly logger ?: Logger ,
65
78
options ?: { branch ?: string ; sender ?: string ; refForUpdate ?: string ; suppressBPError ?: boolean } ,
@@ -70,6 +83,12 @@ export default class ApiHelper {
70
83
this . suppressBPError = options ?. suppressBPError ;
71
84
}
72
85
86
+ /**
87
+ * @param {OctokitResponse } response response
88
+ * @return {any } data
89
+ */
90
+ private getResponseData = async < T > ( response : Promise < OctokitResponse < T > > ) : Promise < T > => ( await response ) . data ;
91
+
73
92
/**
74
93
* @param {function } caller caller
75
94
*/
@@ -90,7 +109,7 @@ export default class ApiHelper {
90
109
*/
91
110
public getRefForUpdate = async ( encode : boolean ) : Promise < string > => {
92
111
const ref = this . refForUpdate ? this . refForUpdate : (
93
- isPrRef ( this . context ) ? ( 'heads/' + ( await this . getPR ( ) ) . data . head . ref ) : getRefForUpdate ( this . context )
112
+ isPrRef ( this . context ) ? ( 'heads/' + ( await this . getPR ( ) ) . head . ref ) : getRefForUpdate ( this . context )
94
113
) ;
95
114
return encode ? encodeURIComponent ( ref ) : ref ;
96
115
} ;
@@ -101,7 +120,7 @@ export default class ApiHelper {
101
120
* @return {Promise<{ path: string, sha: string }> } blob
102
121
*/
103
122
private createBlob = async ( rootDir : string , filepath : string ) : Promise < { path : string ; sha : string } > => {
104
- const blob = await this . octokit . git . createBlob ( {
123
+ const blob = await ( this . octokit as RestEndpointMethods ) . git . createBlob ( {
105
124
...this . context . repo ,
106
125
content : Buffer . from ( fs . readFileSync ( path . resolve ( rootDir , filepath ) , 'utf8' ) ) . toString ( 'base64' ) ,
107
126
encoding : 'base64' ,
@@ -119,23 +138,23 @@ export default class ApiHelper {
119
138
private getCommitSha = ( ) : string => isPrRef ( this . context ) && this . context . payload . pull_request ? this . context . payload . pull_request . head . sha : this . context . sha ;
120
139
121
140
/**
122
- * @return {Promise<Octokit.Response<Octokit.GitGetCommitResponse> > } commit
141
+ * @return {Promise<GitGetCommitResponseData > } commit
123
142
*/
124
- private getCommit = async ( ) : Promise < Octokit . Response < Octokit . GitGetCommitResponse > > => this . octokit . git . getCommit ( {
143
+ private getCommit = async ( ) : Promise < GitGetCommitResponseData > => this . getResponseData ( ( this . octokit as RestEndpointMethods ) . git . getCommit ( {
125
144
...this . context . repo ,
126
145
'commit_sha' : this . getCommitSha ( ) ,
127
- } ) ;
146
+ } ) ) ;
128
147
129
148
/**
130
- * @return {Promise<Octokit.Response<Octokit.PullsGetResponse> > } commit
149
+ * @return {Promise<PullsGetResponseData > } commit
131
150
*/
132
- private getPR = async ( ) : Promise < Octokit . Response < Octokit . PullsGetResponse > > => {
151
+ private getPR = async ( ) : Promise < PullsGetResponseData > => {
133
152
const key = parseInt ( this . context . payload . number , 10 ) ;
134
153
if ( ! ( key in this . prCache ) ) {
135
- this . prCache [ key ] = await this . octokit . pulls . get ( {
154
+ this . prCache [ key ] = await this . getResponseData ( ( this . octokit as RestEndpointMethods ) . pulls . get ( {
136
155
...this . context . repo ,
137
156
'pull_number' : this . context . payload . number ,
138
- } ) ;
157
+ } ) ) ;
139
158
}
140
159
141
160
return this . prCache [ key ] ;
@@ -150,58 +169,58 @@ export default class ApiHelper {
150
169
151
170
/**
152
171
* @param {{ path: string, sha: string }[] } blobs blobs
153
- * @return {Promise<Octokit.Response<Octokit.GitCreateTreeResponse> > } tree
172
+ * @return {Promise<GitCreateTreeResponseData > } tree
154
173
*/
155
- public createTree = async ( blobs : { path : string ; sha : string } [ ] ) : Promise < Octokit . Response < Octokit . GitCreateTreeResponse > > => this . octokit . git . createTree ( {
174
+ public createTree = async ( blobs : { path : string ; sha : string } [ ] ) : Promise < GitCreateTreeResponseData > => this . getResponseData ( ( this . octokit as RestEndpointMethods ) . git . createTree ( {
156
175
...this . context . repo ,
157
- 'base_tree' : ( await this . getCommit ( ) ) . data . tree . sha ,
176
+ 'base_tree' : ( await this . getCommit ( ) ) . tree . sha ,
158
177
tree : Object . values ( blobs ) . map ( blob => ( {
159
178
path : blob . path ,
160
179
type : 'blob' ,
161
180
mode : '100644' ,
162
181
sha : blob . sha ,
163
182
} ) ) ,
164
- } ) ;
183
+ } ) ) ;
165
184
166
185
/**
167
186
* @param {string } commitMessage commit message
168
- * @param {Octokit.Response<Octokit.GitCreateTreeResponse> } tree tree
169
- * @return {Promise<Octokit.Response<Octokit.GitCreateCommitResponse> > } commit
187
+ * @param {GitCreateTreeResponseData } tree tree
188
+ * @return {Promise<GitCreateCommitResponseData > } commit
170
189
*/
171
- public createCommit = async ( commitMessage : string , tree : Octokit . Response < Octokit . GitCreateTreeResponse > ) : Promise < Octokit . Response < Octokit . GitCreateCommitResponse > > => this . octokit . git . createCommit ( {
190
+ public createCommit = async ( commitMessage : string , tree : GitCreateTreeResponseData ) : Promise < GitCreateCommitResponseData > => this . getResponseData ( ( this . octokit as RestEndpointMethods ) . git . createCommit ( {
172
191
...this . context . repo ,
173
- tree : tree . data . sha ,
192
+ tree : tree . sha ,
174
193
parents : [ this . getCommitSha ( ) ] ,
175
194
message : commitMessage ,
176
- } ) ;
195
+ } ) ) ;
177
196
178
197
/**
179
198
* @param {string } refName refName
180
- * @return {Promise<Octokit.AnyResponse |null> } refName
199
+ * @return {Promise<GitGetRefResponseData |null> } refName
181
200
*/
182
- private getRef = async ( refName : string ) : Promise < Octokit . AnyResponse | null > => {
201
+ private getRef = async ( refName : string ) : Promise < GitGetRefResponseData | null > => {
183
202
try {
184
- return await this . octokit . git . getRef ( {
203
+ return await this . getResponseData ( ( this . octokit as RestEndpointMethods ) . git . getRef ( {
185
204
...this . context . repo ,
186
205
ref : refName ,
187
- } ) ;
206
+ } ) ) ;
188
207
} catch ( error ) {
189
208
return null ;
190
209
}
191
210
} ;
192
211
193
212
/**
194
- * @param {Octokit.Response<Octokit.GitCreateCommitResponse> } commit commit
213
+ * @param {GitCreateCommitResponseData } commit commit
195
214
* @param {string } refName refName
196
215
* @param {boolean } force force
197
- * @return {Promise<void > } void
216
+ * @return {Promise<boolean > } updated?
198
217
*/
199
- public updateRef = async ( commit : Octokit . Response < Octokit . GitCreateCommitResponse > , refName : string , force : boolean ) : Promise < boolean > => {
218
+ public updateRef = async ( commit : GitCreateCommitResponseData , refName : string , force : boolean ) : Promise < boolean > => {
200
219
try {
201
- await this . octokit . git . updateRef ( {
220
+ await ( this . octokit as RestEndpointMethods ) . git . updateRef ( {
202
221
...this . context . repo ,
203
222
ref : refName ,
204
- sha : commit . data . sha ,
223
+ sha : commit . sha ,
205
224
force,
206
225
} ) ;
207
226
@@ -218,15 +237,15 @@ export default class ApiHelper {
218
237
} ;
219
238
220
239
/**
221
- * @param {Octokit.Response<Octokit.GitCreateCommitResponse> } commit commit
240
+ * @param {GitCreateCommitResponseData } commit commit
222
241
* @param {string } refName refName
223
242
* @return {Promise<void> } void
224
243
*/
225
- public createRef = async ( commit : Octokit . Response < Octokit . GitCreateCommitResponse > , refName : string ) : Promise < void > => {
226
- await this . octokit . git . createRef ( {
244
+ public createRef = async ( commit : GitCreateCommitResponseData , refName : string ) : Promise < void > => {
245
+ await ( this . octokit as RestEndpointMethods ) . git . createRef ( {
227
246
...this . context . repo ,
228
247
ref : refName ,
229
- sha : commit . data . sha ,
248
+ sha : commit . sha ,
230
249
} ) ;
231
250
} ;
232
251
@@ -235,18 +254,18 @@ export default class ApiHelper {
235
254
* @return {Promise<void> } void
236
255
*/
237
256
public deleteRef = async ( refName : string ) : Promise < void > => {
238
- await this . octokit . git . deleteRef ( {
257
+ await ( this . octokit as RestEndpointMethods ) . git . deleteRef ( {
239
258
...this . context . repo ,
240
259
ref : refName ,
241
260
} ) ;
242
261
} ;
243
262
244
263
/**
245
264
* @param {string } branchName branch name
246
- * @return {Promise<Octokit.PullsListResponseItem > } pull request
265
+ * @return {Promise<PullsListResponseData | null > } pull request
247
266
*/
248
- public findPullRequest = async ( branchName : string ) : Promise < Octokit . PullsListResponseItem | null > => {
249
- const response = await this . octokit . pulls . list ( {
267
+ public findPullRequest = async ( branchName : string ) : Promise < PullsListResponseData [ number ] | null > => {
268
+ const response = await ( this . octokit as RestEndpointMethods ) . pulls . list ( {
250
269
...this . context . repo ,
251
270
head : `${ this . context . repo . owner } :${ getBranch ( branchName , false ) } ` ,
252
271
} ) ;
@@ -259,10 +278,10 @@ export default class ApiHelper {
259
278
260
279
/**
261
280
* @param {PullsListParams } params params
262
- * @return {AsyncIterable<Octokit.PullsListResponseItem > } pull request list
281
+ * @return {AsyncIterable<PullsListResponseData > } pull request list
263
282
*/
264
- public pullsList = ( params : PullsListParams ) : Promise < Octokit . PullsListResponse > => this . octokit . paginate (
265
- this . octokit . pulls . list . endpoint . merge ( Object . assign ( {
283
+ public pullsList = ( params : PullsListParams ) : Promise < PullsListResponseData > => ( this . octokit . paginate as PaginateInterface ) (
284
+ ( this . octokit as RestEndpointMethods ) . pulls . list . endpoint . merge ( Object . assign ( {
266
285
sort : 'created' ,
267
286
direction : 'asc' ,
268
287
} , params , {
@@ -273,27 +292,27 @@ export default class ApiHelper {
273
292
/**
274
293
* @param {string } branchName branch name
275
294
* @param {PullsCreateParams } detail detail
276
- * @return {Promise<Octokit.Response<Octokit.PullsCreateResponse> > } pull
295
+ * @return {Promise<PullsCreateResponseData > } pull
277
296
*/
278
- public pullsCreate = async ( branchName : string , detail : PullsCreateParams ) : Promise < Octokit . Response < Octokit . PullsCreateResponse > > => this . octokit . pulls . create ( {
297
+ public pullsCreate = async ( branchName : string , detail : PullsCreateParams ) : Promise < PullsCreateResponseData > => this . getResponseData ( ( this . octokit as RestEndpointMethods ) . pulls . create ( {
279
298
...this . context . repo ,
280
299
head : `${ this . context . repo . owner } :${ getBranch ( branchName , false ) } ` ,
281
300
base : ( await this . getRefForUpdate ( false ) ) . replace ( / ^ h e a d s \/ / , '' ) ,
282
301
...detail ,
283
- } ) ;
302
+ } ) ) ;
284
303
285
304
/**
286
305
* @param {number } number pull number
287
306
* @param {PullsUpdateParams } detail detail
288
- * @return {Promise<Octokit.Response<Octokit.PullsUpdateResponse> > } pull
307
+ * @return {Promise<PullsUpdateResponseData > } pull
289
308
*/
290
- public pullsUpdate = async ( number : number , detail : PullsUpdateParams ) : Promise < Octokit . Response < Octokit . PullsUpdateResponse > > => this . octokit . pulls . update ( {
309
+ public pullsUpdate = async ( number : number , detail : PullsUpdateParams ) : Promise < PullsUpdateResponseData > => this . getResponseData ( ( this . octokit as RestEndpointMethods ) . pulls . update ( {
291
310
...this . context . repo ,
292
311
'pull_number' : number ,
293
312
base : ( await this . getRefForUpdate ( false ) ) . replace ( / ^ h e a d s \/ / , '' ) ,
294
313
state : 'open' ,
295
314
...detail ,
296
- } ) ;
315
+ } ) ) ;
297
316
298
317
/**
299
318
* @param {string } branch branch
@@ -315,7 +334,7 @@ export default class ApiHelper {
315
334
this . callLogger ( async logger => logger . startProcess ( 'Creating PullRequest... [%s] -> [%s]' , getBranch ( createBranchName , false ) , await this . getRefForUpdate ( false ) ) ) ;
316
335
const created = await this . pullsCreate ( createBranchName , detail ) ;
317
336
this . callLogger ( logger => logger . endProcess ( ) ) ;
318
- return Object . assign ( { isPrCreated : true } , created . data ) ;
337
+ return Object . assign ( { isPrCreated : true } , created ) ;
319
338
} ;
320
339
321
340
/**
@@ -329,7 +348,7 @@ export default class ApiHelper {
329
348
this . callLogger ( async logger => logger . startProcess ( 'Updating PullRequest... [%s] -> [%s]' , getBranch ( createBranchName , false ) , await this . getRefForUpdate ( false ) ) ) ;
330
349
const updated = await this . pullsUpdate ( pullRequest . number , detail ) ;
331
350
this . callLogger ( logger => logger . endProcess ( ) ) ;
332
- return Object . assign ( { isPrCreated : false } , updated . data ) ;
351
+ return Object . assign ( { isPrCreated : false } , updated ) ;
333
352
}
334
353
335
354
return this . createPulls ( createBranchName , detail ) ;
@@ -367,7 +386,7 @@ export default class ApiHelper {
367
386
return false ;
368
387
}
369
388
370
- await this . octokit . issues . createComment ( {
389
+ await ( this . octokit as RestEndpointMethods ) . issues . createComment ( {
371
390
...this . context . repo ,
372
391
'issue_number' : pullRequest . number ,
373
392
body,
@@ -399,16 +418,16 @@ export default class ApiHelper {
399
418
* @param {string } rootDir root dir
400
419
* @param {string } commitMessage commit message
401
420
* @param {string[] } files files
402
- * @return {Promise<Octokit.Response<Octokit.GitCreateCommitResponse> > } commit
421
+ * @return {Promise<GitCreateCommitResponseData > } commit
403
422
*/
404
- private prepareCommit = async ( rootDir : string , commitMessage : string , files : string [ ] ) : Promise < Octokit . Response < Octokit . GitCreateCommitResponse > > => {
423
+ private prepareCommit = async ( rootDir : string , commitMessage : string , files : string [ ] ) : Promise < GitCreateCommitResponseData > => {
405
424
this . callLogger ( logger => logger . startProcess ( 'Creating blobs...' ) ) ;
406
425
const blobs = await this . filesToBlobs ( rootDir , files ) ;
407
426
408
427
this . callLogger ( logger => logger . startProcess ( 'Creating tree...' ) ) ;
409
428
const tree = await this . createTree ( blobs ) ;
410
429
411
- this . callLogger ( logger => logger . startProcess ( 'Creating commit... [%s]' , tree . data . sha ) ) ;
430
+ this . callLogger ( logger => logger . startProcess ( 'Creating commit... [%s]' , tree . sha ) ) ;
412
431
return this . createCommit ( commitMessage , tree ) ;
413
432
} ;
414
433
@@ -426,10 +445,10 @@ export default class ApiHelper {
426
445
const commit = await this . prepareCommit ( rootDir , commitMessage , files ) ;
427
446
const ref = await this . getRefForUpdate ( true ) ;
428
447
429
- this . callLogger ( logger => logger . startProcess ( 'Updating ref... [%s] [%s]' , ref , commit . data . sha ) ) ;
448
+ this . callLogger ( logger => logger . startProcess ( 'Updating ref... [%s] [%s]' , ref , commit . sha ) ) ;
430
449
if ( await this . updateRef ( commit , ref , false ) ) {
431
- process . env . GITHUB_SHA = commit . data . sha ;
432
- exportVariable ( 'GITHUB_SHA' , commit . data . sha ) ;
450
+ process . env . GITHUB_SHA = commit . sha ;
451
+ exportVariable ( 'GITHUB_SHA' , commit . sha ) ;
433
452
}
434
453
435
454
this . callLogger ( logger => logger . endProcess ( ) ) ;
@@ -453,10 +472,10 @@ export default class ApiHelper {
453
472
const commit = await this . prepareCommit ( rootDir , commitMessage , files ) ;
454
473
const ref = await this . getRef ( headName ) ;
455
474
if ( null === ref ) {
456
- this . callLogger ( logger => logger . startProcess ( 'Creating reference... [%s] [%s]' , refName , commit . data . sha ) ) ;
475
+ this . callLogger ( logger => logger . startProcess ( 'Creating reference... [%s] [%s]' , refName , commit . sha ) ) ;
457
476
await this . createRef ( commit , refName ) ;
458
477
} else {
459
- this . callLogger ( logger => logger . startProcess ( 'Updating reference... [%s] [%s]' , refName , commit . data . sha ) ) ;
478
+ this . callLogger ( logger => logger . startProcess ( 'Updating reference... [%s] [%s]' , refName , commit . sha ) ) ;
460
479
await this . updateRef ( commit , headName , true ) ;
461
480
}
462
481
@@ -504,7 +523,7 @@ export default class ApiHelper {
504
523
throw new Error ( 'Sender is not valid.' ) ;
505
524
}
506
525
507
- const { data : user } = await this . octokit . users . getByUsername ( {
526
+ const { data : user } = await ( this . octokit as RestEndpointMethods ) . users . getByUsername ( {
508
527
username : sender ,
509
528
} ) ;
510
529
@@ -519,19 +538,19 @@ export default class ApiHelper {
519
538
/**
520
539
* @return {Promise<string> } default branch
521
540
*/
522
- public getDefaultBranch = async ( ) : Promise < string > => this . context . payload . repository ?. default_branch ?? ( await this . octokit . repos . get ( { // eslint-disable-line camelcase
541
+ public getDefaultBranch = async ( ) : Promise < string > => this . context . payload . repository ?. default_branch ?? ( await ( this . octokit as RestEndpointMethods ) . repos . get ( { // eslint-disable-line camelcase
523
542
...this . context . repo ,
524
543
} ) ) . data . default_branch ;
525
544
526
545
/**
527
546
* @return {Promise<Array<string>> } tags
528
547
*/
529
- public getTags = async ( ) : Promise < Array < string > > => ( await this . octokit . paginate (
530
- this . octokit . git . listMatchingRefs . endpoint . merge ( {
548
+ public getTags = async ( ) : Promise < Array < string > > => ( await ( this . octokit . paginate as PaginateInterface ) (
549
+ ( this . octokit as RestEndpointMethods ) . git . listMatchingRefs . endpoint . merge ( {
531
550
...this . context . repo ,
532
551
ref : 'tags/' ,
533
552
} ) ,
534
- ) ) . map ( ( item : Octokit . GitListMatchingRefsResponseItem ) : string => trimRef ( item . ref ) ) ;
553
+ ) ) . map ( ( item ) : string => trimRef ( ( item as GitListMatchingRefsResponseData [ number ] ) . ref ) ) ;
535
554
536
555
/**
537
556
* @return {Promise<string> } tag
0 commit comments