1- import { Injectable } from '@ali/common-di' ;
2- import { Deferred } from '@ali/ide-core-common ' ;
1+ import { Injectable , Autowired } from '@ali/common-di' ;
2+ import { GitModelService } from './git-model.service ' ;
33import { request , API } from './request' ;
44import { IGitAPIService } from './types' ;
55
6- // TODO: 适配 gitlab 接口
7- // TODO: 放到集成侧去做?AntCode 上如果也用 IDE,那么也会造成重复
86@Injectable ( )
97export class GitAPIService implements IGitAPIService {
10- public platform = 'antcode' ;
11- private initialized = new Deferred ( ) ;
12- public project : string ;
13- public projectId : number ;
14- public commit : string ;
15- public branch : string ;
16-
17- async initProject ( config : {
18- project : string ;
19- projectId ?: number ;
20- commit ?: string ;
21- branch ?: string ;
22- } ) {
23- this . project = config . project ;
24- if ( config . projectId ) {
25- this . projectId = config . projectId ;
26- }
27- if ( config . commit ) {
28- this . commit = config . commit ;
29- }
30- if ( config . branch ) {
31- this . branch = config . branch ;
32- }
33- // 唯一确定一个项目
34- if ( this . projectId && this . commit ) {
35- return ;
36- }
37- if ( ! this . projectId || ! this . branch ) {
38- const projectInfo = await this . getProjectInfo ( ) ;
39- this . projectId = projectInfo . id ;
40- if ( ! this . branch ) {
41- this . branch = projectInfo . default_branch ;
42- }
43- }
44- if ( ! this . commit ) {
45- const commitInfo = await this . getCommit ( this . branch ) ;
46- this . commit = commitInfo . id ;
47- }
48- this . initialized . resolve ( ) ;
49- }
50-
51- get ready ( ) {
52- return this . initialized . promise ;
53- }
8+ @Autowired ( )
9+ gitModel : GitModelService ;
5410
5511 getProjectInfo ( ) {
5612 return request < API . ResponseGetProjectById > (
57- `/api/v3/projects/${ encodeURIComponent ( this . project ) } `
13+ `/api/v3/projects/${ encodeURIComponent ( this . gitModel . project ) } `
5814 ) ;
5915 }
6016
61- // 根据分支获取最新的 commit
6217 async getCommit ( ref : string ) {
6318 return request < API . ResponseGetCommit > (
64- `/api/v3/projects/${ this . projectId } /repository/commits/${ ref } `
19+ `/api/v3/projects/${ this . gitModel . projectId } /repository/commits/${ ref } `
6520 ) ;
6621 }
6722
68- // 获取文件节点
6923 async getTreeEntry ( path : string ) {
70- await this . ready ;
24+ await this . gitModel . ready ;
7125 return request < API . ResponseGetTreeEntry > (
72- `/api/v3/projects/${ this . projectId } /repository/tree_entry` ,
26+ `/api/v3/projects/${ this . gitModel . projectId } /repository/tree_entry` ,
7327 {
7428 params : {
75- ref_name : this . commit ,
29+ ref_name : this . gitModel . commit ,
7630 path,
7731 } ,
7832 }
7933 ) ;
8034 }
8135
82- // 根据 commit 和 path 获取 tree
36+ /**
37+ * 根据 commit 和 path 获取 tree
38+ */
8339 async getTree ( path : string = '' ) {
84- await this . ready ;
85- return request < API . ResponseGetTree > ( `/api/v3/projects/${ this . projectId } /repository/tree` , {
86- params : {
87- ref_name : this . commit ,
88- path,
89- } ,
90- } ) ;
40+ await this . gitModel . ready ;
41+ return request < API . ResponseGetTree > (
42+ `/api/v3/projects/${ this . gitModel . projectId } /repository/tree` ,
43+ {
44+ params : {
45+ ref_name : this . gitModel . commit ,
46+ path,
47+ } ,
48+ }
49+ ) ;
9150 }
9251
9352 async getBlobSize ( path : string ) {
94- await this . ready ;
53+ await this . gitModel . ready ;
9554 const res : Response = await request (
96- `/api/v3/projects/${ this . projectId } /repository/blobs/${ this . commit } ` ,
55+ `/api/v3/projects/${ this . gitModel . projectId } /repository/blobs/${ this . gitModel . commit } ` ,
9756 {
9857 params : {
9958 filepath : path ,
@@ -105,11 +64,10 @@ export class GitAPIService implements IGitAPIService {
10564 return parseInt ( res . headers . get ( 'Content-Length' ) || '-1' , 10 ) ;
10665 }
10766
108- // 根据 commit 和 path 获取 blob
10967 async getBlob ( path : string = '' ) {
110- await this . ready ;
68+ await this . gitModel . ready ;
11169 const buf = await request < ArrayBuffer > (
112- `/api/v3/projects/${ this . projectId } /repository/blobs/${ this . commit } ` ,
70+ `/api/v3/projects/${ this . gitModel . projectId } /repository/blobs/${ this . gitModel . commit } ` ,
11371 {
11472 params : {
11573 filepath : path ,
0 commit comments