@@ -10,8 +10,6 @@ import { retry, RetryError } from '../common/utils';
1010import {
1111 ICodeAPIService ,
1212 TreeEntry ,
13- EntryParam ,
14- IRepositoryModel ,
1513 BranchOrTag ,
1614 CommitParams ,
1715 CommitFileChange ,
@@ -24,6 +22,7 @@ import {
2422 FileOperateResult ,
2523 FileActionType ,
2624} from '../common/types' ;
25+ import type { IRepositoryModel , EntryParam } from '../common/types' ;
2726import { CodePlatform , CommitFileStatus , CodeAPI as ConflictAPI } from '../common/types' ;
2827import { CODE_PLATFORM_CONFIG } from '../common/config' ;
2928
@@ -77,7 +76,13 @@ export class GitHubAPIService implements ICodeAPIService {
7776 constructor ( ) {
7877 this . _OAUTH_TOKEN = this . config . token || this . helper . GITHUB_TOKEN ;
7978 }
80- createPullRequest ( repo : IRepositoryModel , sourceBranch : string , targetBranch : string , title : string , autoMerge ?: boolean | undefined ) : Promise < ConflictAPI . ResponseCreatePR > {
79+ createPullRequest (
80+ repo : IRepositoryModel ,
81+ sourceBranch : string ,
82+ targetBranch : string ,
83+ title : string ,
84+ autoMerge ?: boolean | undefined
85+ ) : Promise < ConflictAPI . ResponseCreatePR > {
8186 throw new Error ( 'Method not implemented.' ) ;
8287 }
8388 mergeBase (
@@ -127,15 +132,15 @@ export class GitHubAPIService implements ICodeAPIService {
127132 } ;
128133 }
129134 async createBranch ( repo : IRepositoryModel , newBranch : string , ref : string ) : Promise < Branch > {
130- const data = await this . rest . createReference ( repo , newBranch , ref )
135+ const data = await this . rest . createReference ( repo , newBranch , ref ) ;
131136
132137 return {
133138 name : newBranch ,
134139 ref : data . ref ,
135140 commit : {
136- id : data . object . sha
141+ id : data . object . sha ,
137142 } ,
138- }
143+ } ;
139144 }
140145 async available ( ) {
141146 const token = this . _OAUTH_TOKEN ;
@@ -341,7 +346,11 @@ export class GitHubAPIService implements ICodeAPIService {
341346 } ) ;
342347 } ,
343348
344- createTree : async ( repo : IRepositoryModel , trees : API . RequestCreateTree [ ] , base_tree ?: string ) => {
349+ createTree : async (
350+ repo : IRepositoryModel ,
351+ trees : API . RequestCreateTree [ ] ,
352+ base_tree ?: string
353+ ) => {
345354 const data = await this . requestByREST < API . ResponseGetTree > (
346355 `/repos/${ this . getProjectPath ( repo ) } /git/trees` ,
347356 {
@@ -353,7 +362,7 @@ export class GitHubAPIService implements ICodeAPIService {
353362 data : {
354363 base_tree,
355364 tree : trees ,
356- }
365+ } ,
357366 }
358367 ) ;
359368
@@ -391,7 +400,10 @@ export class GitHubAPIService implements ICodeAPIService {
391400 return data . tree . filter ( ( item ) => item . type === 'blob' ) . map ( ( item ) => item . path ) ;
392401 } ,
393402
394- createBlob : async ( repo : IRepositoryModel , blob : { content : string , encoding ?: 'utf-8' | 'base64' } ) => {
403+ createBlob : async (
404+ repo : IRepositoryModel ,
405+ blob : { content : string ; encoding ?: 'utf-8' | 'base64' }
406+ ) => {
395407 const data = await this . requestByREST < API . ResponseCreateBlob > (
396408 `/repos/${ this . getProjectPath ( repo ) } /git/blobs` ,
397409 {
@@ -403,7 +415,7 @@ export class GitHubAPIService implements ICodeAPIService {
403415 data : {
404416 content : blob . content ,
405417 encoding : blob . encoding || 'utf-8' ,
406- }
418+ } ,
407419 }
408420 ) ;
409421
@@ -442,46 +454,60 @@ export class GitHubAPIService implements ICodeAPIService {
442454 } ,
443455
444456 createReference : async ( repo : IRepositoryModel , ref : string , sha : string ) => {
445- const data = await this . requestByREST < API . ResponseReference > ( `/repos/${ this . getProjectPath ( repo ) } /git/refs` , {
446- responseType : 'json' ,
447- method : 'post' ,
448- data : {
449- ref : `refs/heads/${ ref } ` ,
450- sha
457+ const data = await this . requestByREST < API . ResponseReference > (
458+ `/repos/${ this . getProjectPath ( repo ) } /git/refs` ,
459+ {
460+ responseType : 'json' ,
461+ method : 'post' ,
462+ data : {
463+ ref : `refs/heads/${ ref } ` ,
464+ sha,
465+ } ,
451466 }
452- } ) ;
467+ ) ;
453468
454- return data
469+ return data ;
455470 } ,
456471
457- updateReference : async ( repo : IRepositoryModel , ref : string , sha : string , force : boolean = false ) => {
472+ updateReference : async (
473+ repo : IRepositoryModel ,
474+ ref : string ,
475+ sha : string ,
476+ force : boolean = false
477+ ) => {
458478 // 去掉开头的 refs/
459479 ref = ref . replace ( / ^ r e f s \/ / , '' ) ;
460480 ref = ref . startsWith ( 'heads' ) ? ref : `heads/${ ref } ` ;
461481
462- const data = await this . requestByREST < API . ResponseReference > ( `/repos/${ this . getProjectPath ( repo ) } /git/refs/${ ref } ` , {
463- responseType : 'json' ,
464- method : 'post' ,
465- data : {
466- sha,
467- force
482+ const data = await this . requestByREST < API . ResponseReference > (
483+ `/repos/${ this . getProjectPath ( repo ) } /git/refs/${ ref } ` ,
484+ {
485+ responseType : 'json' ,
486+ method : 'post' ,
487+ data : {
488+ sha,
489+ force,
490+ } ,
468491 }
469- } ) ;
492+ ) ;
470493
471- return data
494+ return data ;
472495 } ,
473496
474497 getReference : async ( repo : IRepositoryModel , ref : string ) => {
475- const data = await this . requestByREST < API . ResponseReference > ( `/repos/${ this . getProjectPath ( repo ) } /git/ref/${ ref } ` , {
476- headers : {
477- Accept : 'application/vnd.github+json' ,
478- 'X-GitHub-Api-Version' : '2022-11-28'
479- } ,
480- responseType : 'json' ,
481- method : 'get' ,
482- } ) ;
498+ const data = await this . requestByREST < API . ResponseReference > (
499+ `/repos/${ this . getProjectPath ( repo ) } /git/ref/${ ref } ` ,
500+ {
501+ headers : {
502+ Accept : 'application/vnd.github+json' ,
503+ 'X-GitHub-Api-Version' : '2022-11-28' ,
504+ } ,
505+ responseType : 'json' ,
506+ method : 'get' ,
507+ }
508+ ) ;
483509
484- return data
510+ return data ;
485511 } ,
486512
487513 getBranches : async ( repo : IRepositoryModel ) : Promise < BranchOrTag [ ] > => {
@@ -829,26 +855,28 @@ export class GitHubAPIService implements ICodeAPIService {
829855
830856 // 先获取分支名
831857 // by CODE_SERVICE_COMMANDS
832- const repositoryInfo : IRepositoryModel | undefined = await this . commandService . executeCommand ( 'code-service.repository' ) ;
858+ const repositoryInfo : IRepositoryModel | undefined = await this . commandService . executeCommand (
859+ 'code-service.repository'
860+ ) ;
833861 if ( ! repositoryInfo || ! repositoryInfo . ref ) {
834862 return [ ] ;
835863 }
836864
837865 const { ref : branchName } = repositoryInfo ;
838866
839- const paramTree : API . RequestCreateTree [ ] = [ ]
867+ const paramTree : API . RequestCreateTree [ ] = [ ] ;
840868
841869 for await ( const action of actions ) {
842870 // 先创建 blob 对象
843- const blob = await this . rest . createBlob ( repo , { content : action . content } )
871+ const blob = await this . rest . createBlob ( repo , { content : action . content } ) ;
844872
845873 // 然后将 sha 传递给 tree 数据源
846874 paramTree . push ( {
847875 path : action . file_path ,
848876 mode : '100644' ,
849877 type : 'blob' ,
850- sha : action . action_type === FileActionType . delete ? null : blob . sha
851- } )
878+ sha : action . action_type === FileActionType . delete ? null : blob . sha ,
879+ } ) ;
852880 }
853881
854882 // 创建树
@@ -858,23 +886,26 @@ export class GitHubAPIService implements ICodeAPIService {
858886 const latestCommit = await this . rest . getCommit ( repo , branchName ) ;
859887
860888 // 创建 commit
861- const commitData = await this . requestByREST < API . ResponseCreateCommit > ( `/repos/${ this . getProjectPath ( repo ) } /git/commits` , {
862- responseType : 'json' ,
863- method : 'post' ,
864- data : {
865- message : commit_message ,
866- tree : treeData . sha ,
867- parents : [ latestCommit ]
889+ const commitData = await this . requestByREST < API . ResponseCreateCommit > (
890+ `/repos/${ this . getProjectPath ( repo ) } /git/commits` ,
891+ {
892+ responseType : 'json' ,
893+ method : 'post' ,
894+ data : {
895+ message : commit_message ,
896+ tree : treeData . sha ,
897+ parents : [ latestCommit ] ,
898+ } ,
868899 }
869- } ) ;
900+ ) ;
870901
871902 // 将其关联分支的引用,此时才成功 push
872- const referenceData = await this . rest . updateReference ( repo , branchName , commitData . sha )
903+ const referenceData = await this . rest . updateReference ( repo , branchName , commitData . sha ) ;
873904
874905 return [
875906 {
876- commit_id : referenceData . object . sha
877- }
907+ commit_id : referenceData . object . sha ,
908+ } ,
878909 ] as FileOperateResult [ ] ;
879910 }
880911}
0 commit comments