@@ -3,24 +3,34 @@ import path from 'path';
3
3
import { Context } from '@actions/github/lib/context' ;
4
4
import { PaginateInterface } from '@octokit/plugin-paginate-rest' ;
5
5
import { RestEndpointMethods } from '@octokit/plugin-rest-endpoint-methods/dist-types/generated/method-types' ;
6
- import {
7
- OctokitResponse ,
8
- GitGetCommitResponseData ,
9
- PullsGetResponseData ,
10
- GitCreateTreeResponseData ,
11
- GitCreateCommitResponseData ,
12
- GitGetRefResponseData ,
13
- PullsListResponseData ,
14
- PullsCreateResponseData ,
15
- PullsUpdateResponseData ,
16
- GitListMatchingRefsResponseData ,
17
- } from '@octokit/types' ;
6
+ import { OctokitResponse } from '@octokit/types' ;
7
+ import { components } from '@octokit/openapi-types' ;
18
8
import { exportVariable } from '@actions/core' ;
19
9
import { Logger } from '@technote-space/github-action-log-helper' ;
20
- import { getRefForUpdate , isPrRef , getBranch , trimRef , versionCompare , generateNewPatchVersion , generateNewMajorVersion , generateNewMinorVersion } from './utils' ;
10
+ import {
11
+ getRefForUpdate ,
12
+ isPrRef ,
13
+ getBranch ,
14
+ trimRef ,
15
+ versionCompare ,
16
+ generateNewPatchVersion ,
17
+ generateNewMajorVersion ,
18
+ generateNewMinorVersion ,
19
+ ensureNotNull ,
20
+ objectGet ,
21
+ } from './utils' ;
21
22
import { getSender } from './context-helper' ;
22
23
import { Octokit } from './types' ;
23
24
25
+ type GitGetCommitResponseData = components [ 'schemas' ] [ 'git-commit' ] ;
26
+ type PullsGetResponseData = components [ 'schemas' ] [ 'pull-request' ] ;
27
+ type GitCreateTreeResponseData = components [ 'schemas' ] [ 'git-tree' ] ;
28
+ type GitCreateCommitResponseData = components [ 'schemas' ] [ 'git-commit' ] ;
29
+ type GitGetRefResponseData = components [ 'schemas' ] [ 'git-ref' ] ;
30
+ type PullsListResponseData = components [ 'schemas' ] [ 'pull-request-simple' ] ;
31
+ type PullsCreateResponseData = components [ 'schemas' ] [ 'pull-request' ] ;
32
+ type PullsUpdateResponseData = components [ 'schemas' ] [ 'pull-request' ] ;
33
+
24
34
type PullsUpdateParams = {
25
35
body ?: string ;
26
36
draft ?: boolean ;
@@ -163,18 +173,18 @@ export default class ApiHelper {
163
173
/**
164
174
* @param {string } rootDir root dir
165
175
* @param {object } files files
166
- * @return {Promise<{ path: string, sha: string }[] > } blobs
176
+ * @return {Promise<Array< { path: string, sha: string }> > } blobs
167
177
*/
168
- public filesToBlobs = async ( rootDir : string , files : Array < string > ) : Promise < { path : string ; sha : string } [ ] > => await Promise . all ( Object . values ( files ) . map ( file => this . createBlob ( rootDir , file ) ) ) ;
178
+ public filesToBlobs = async ( rootDir : string , files : Array < string > ) : Promise < Array < { path : string ; sha : string } > > => await Promise . all ( files . map ( file => this . createBlob ( rootDir , file ) ) ) ;
169
179
170
180
/**
171
- * @param {{ path: string, sha: string }[] } blobs blobs
181
+ * @param {Array< { path: string, sha: string }> } blobs blobs
172
182
* @return {Promise<GitCreateTreeResponseData> } tree
173
183
*/
174
- public createTree = async ( blobs : { path : string ; sha : string } [ ] ) : Promise < GitCreateTreeResponseData > => this . getResponseData ( ( this . octokit as RestEndpointMethods ) . git . createTree ( {
184
+ public createTree = async ( blobs : Array < { path : string ; sha : string } > ) : Promise < GitCreateTreeResponseData > => this . getResponseData ( ( this . octokit as RestEndpointMethods ) . git . createTree ( {
175
185
...this . context . repo ,
176
- 'base_tree' : ( await this . getCommit ( ) ) . tree . sha ,
177
- tree : Object . values ( blobs ) . map ( blob => ( {
186
+ 'base_tree' : ensureNotNull ( objectGet ( ( await this . getCommit ( ) ) , ' tree.sha' ) ) ,
187
+ tree : blobs . map ( blob => ( {
178
188
path : blob . path ,
179
189
type : 'blob' ,
180
190
mode : '100644' ,
@@ -220,7 +230,7 @@ export default class ApiHelper {
220
230
await ( this . octokit as RestEndpointMethods ) . git . updateRef ( {
221
231
...this . context . repo ,
222
232
ref : refName ,
223
- sha : commit . sha ,
233
+ sha : ensureNotNull ( commit . sha ) ,
224
234
force,
225
235
} ) ;
226
236
@@ -245,7 +255,7 @@ export default class ApiHelper {
245
255
await ( this . octokit as RestEndpointMethods ) . git . createRef ( {
246
256
...this . context . repo ,
247
257
ref : refName ,
248
- sha : commit . sha ,
258
+ sha : ensureNotNull ( commit . sha ) ,
249
259
} ) ;
250
260
} ;
251
261
@@ -264,7 +274,7 @@ export default class ApiHelper {
264
274
* @param {string } branchName branch name
265
275
* @return {Promise<PullsListResponseData | null> } pull request
266
276
*/
267
- public findPullRequest = async ( branchName : string ) : Promise < PullsListResponseData [ number ] | null > => {
277
+ public findPullRequest = async ( branchName : string ) : Promise < PullsListResponseData | null > => {
268
278
const response = await ( this . octokit as RestEndpointMethods ) . pulls . list ( {
269
279
...this . context . repo ,
270
280
head : `${ this . context . repo . owner } :${ getBranch ( branchName , false ) } ` ,
@@ -278,9 +288,9 @@ export default class ApiHelper {
278
288
279
289
/**
280
290
* @param {PullsListParams } params params
281
- * @return {AsyncIterable<PullsListResponseData> } pull request list
291
+ * @return {AsyncIterable<Array< PullsListResponseData> > } pull request list
282
292
*/
283
- public pullsList = ( params : PullsListParams ) : Promise < PullsListResponseData > => ( this . octokit . paginate as PaginateInterface ) (
293
+ public pullsList = ( params : PullsListParams ) : Promise < Array < PullsListResponseData > > => ( this . octokit . paginate as PaginateInterface ) (
284
294
( this . octokit as RestEndpointMethods ) . pulls . list ,
285
295
Object . assign ( {
286
296
sort : 'created' ,
@@ -403,10 +413,10 @@ export default class ApiHelper {
403
413
private isProtectedBranchError = ( error : Error ) : boolean => / r e q u i r e d s t a t u s c h e c k s ? .* ( i s | a r e ) e x p e c t e d / i. test ( error . message ) ;
404
414
405
415
/**
406
- * @param {string[] } files files
416
+ * @param {Array< string> } files files
407
417
* @return {boolean } diff?
408
418
*/
409
- private checkDiff = ( files : string [ ] ) : boolean => {
419
+ private checkDiff = ( files : Array < string > ) : boolean => {
410
420
if ( ! files . length ) {
411
421
this . callLogger ( logger => logger . info ( 'There is no diff.' ) ) ;
412
422
return false ;
@@ -418,10 +428,10 @@ export default class ApiHelper {
418
428
/**
419
429
* @param {string } rootDir root dir
420
430
* @param {string } commitMessage commit message
421
- * @param {string[] } files files
431
+ * @param {Array< string> } files files
422
432
* @return {Promise<GitCreateCommitResponseData> } commit
423
433
*/
424
- private prepareCommit = async ( rootDir : string , commitMessage : string , files : string [ ] ) : Promise < GitCreateCommitResponseData > => {
434
+ private prepareCommit = async ( rootDir : string , commitMessage : string , files : Array < string > ) : Promise < GitCreateCommitResponseData > => {
425
435
this . callLogger ( logger => logger . startProcess ( 'Creating blobs...' ) ) ;
426
436
const blobs = await this . filesToBlobs ( rootDir , files ) ;
427
437
@@ -435,10 +445,10 @@ export default class ApiHelper {
435
445
/**
436
446
* @param {string } rootDir root dir
437
447
* @param {string } commitMessage commit message
438
- * @param {string[] } files files
448
+ * @param {Array< string> } files files
439
449
* @return {Promise<boolean> } result
440
450
*/
441
- public commit = async ( rootDir : string , commitMessage : string , files : string [ ] ) : Promise < boolean > => {
451
+ public commit = async ( rootDir : string , commitMessage : string , files : Array < string > ) : Promise < boolean > => {
442
452
if ( ! this . checkDiff ( files ) ) {
443
453
return false ;
444
454
}
@@ -459,12 +469,12 @@ export default class ApiHelper {
459
469
/**
460
470
* @param {string } rootDir root dir
461
471
* @param {string } commitMessage commit message
462
- * @param {string[] } files files
472
+ * @param {Array< string> } files files
463
473
* @param {string } createBranchName branch name
464
474
* @param {PullsCreateParams } detail detail
465
475
* @return {Promise<boolean|PullsInfo> } result
466
476
*/
467
- public createPR = async ( rootDir : string , commitMessage : string , files : string [ ] , createBranchName : string , detail : PullsCreateParams ) : Promise < boolean | PullsInfo > => {
477
+ public createPR = async ( rootDir : string , commitMessage : string , files : Array < string > , createBranchName : string , detail : PullsCreateParams ) : Promise < boolean | PullsInfo > => {
468
478
if ( ! this . checkDiff ( files ) ) {
469
479
return false ;
470
480
}
@@ -530,8 +540,8 @@ export default class ApiHelper {
530
540
531
541
return {
532
542
login : user . login ,
533
- email : user . email ,
534
- name : user . name ,
543
+ email : ensureNotNull ( user . email ) ,
544
+ name : ensureNotNull ( user . name ) ,
535
545
id : user . id ,
536
546
} ;
537
547
} ;
@@ -552,7 +562,7 @@ export default class ApiHelper {
552
562
...this . context . repo ,
553
563
ref : 'tags/' ,
554
564
} ,
555
- ) ) . map ( ( item ) : string => trimRef ( ( item as GitListMatchingRefsResponseData [ number ] ) . ref ) ) ;
565
+ ) ) . map ( ( item ) : string => trimRef ( item . ref ) ) ;
556
566
557
567
/**
558
568
* @return {Promise<string> } tag
0 commit comments