@@ -19,7 +19,7 @@ import {
1919import { API as ConflictAPI } from '../antcode/types' ;
2020import { request , RequestOptions } from '@alipay/alex-shared' ;
2121import { CODE_PLATFORM_CONFIG , HelperService } from '../common' ;
22- import { URI , MessageType } from '@opensumi/ide-core-common' ;
22+ import { URI , MessageType , isObject } from '@opensumi/ide-core-common' ;
2323import { API } from './types' ;
2424
2525@Injectable ( )
@@ -32,14 +32,54 @@ export class AtomGitAPIService implements ICodeAPIService {
3232 private _PRIVATE_TOKEN : string | null ;
3333
3434 get PRIVATE_TOKEN ( ) {
35- return this . _PRIVATE_TOKEN ;
35+ return this . _PRIVATE_TOKEN || this . helper . ATOMGIT_TOKEN ;
3636 }
3737
3838 constructor ( ) {
39- this . _PRIVATE_TOKEN = this . config . token || 'atu_7d36d5a52af4156b76d6f61b240d72cc' ;
39+ this . _PRIVATE_TOKEN = this . config . token || this . helper . ATOMGIT_TOKEN || '' ;
40+ }
41+
42+ private async checkAccessToken ( ) : Promise < boolean > {
43+ const popupWindow = window . open ( `${ this . config . origin } /login/oauth/authorize?client_id=9d8b531661f441d1` , '_blank' , 'directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=800,height=520,top=150,left=150' ) ;
44+ return new Promise < boolean > ( ( resolve , reject ) => {
45+ const handleMessage = async ( event : MessageEvent ) => {
46+ try {
47+ const { data } = event ;
48+ if ( isObject ( data ) && data . type === 'atomgit' ) {
49+ const { data : { code } } = data ;
50+ popupWindow ?. close ( ) ;
51+ if ( ! code ) {
52+ resolve ( false ) ;
53+ return ;
54+ }
55+ const tokenResult : any = await this . request ( 'http://svc-ldh2u2i3brw4lg1e.cloudide.svc.et15-sqa.alipay.net:7001/openapi/atomgit-auth-callback' , {
56+ baseURL : '' ,
57+ method : 'post' ,
58+ credentials : 'omit' ,
59+ data : {
60+ code
61+ }
62+ } ) ;
63+ if ( tokenResult && tokenResult . access_token ) {
64+ this . helper . ATOMGIT_TOKEN = tokenResult . access_token ;
65+ resolve ( true ) ;
66+ return ;
67+ }
68+ resolve ( false ) ;
69+ }
70+ } catch ( error ) {
71+ reject ( error ) ;
72+ }
73+ } ;
74+ window . addEventListener ( 'message' , handleMessage ) ;
75+ } ) ;
4076 }
4177
4278 public async available ( ) : Promise < boolean > {
79+ const token = this . _PRIVATE_TOKEN ;
80+ if ( ! token ) {
81+ return await this . checkAccessToken ( ) ;
82+ }
4383 return true ;
4484 }
4585
@@ -57,7 +97,7 @@ export class AtomGitAPIService implements ICodeAPIService {
5797 const { headers, ...rest } = options || { } ;
5898 const privateToken = this . PRIVATE_TOKEN ;
5999 return await request ( path , {
60- baseURL : this . config . endpoint ,
100+ baseURL : options ?. baseURL ?? this . config . endpoint ,
61101 responseType : 'json' ,
62102 headers : {
63103 ...( privateToken
@@ -74,6 +114,8 @@ export class AtomGitAPIService implements ICodeAPIService {
74114 let messageKey = 'error.request' ;
75115 if ( status === 401 ) {
76116 messageKey = 'atomgit.unauthorized' ;
117+ // 401 的情况再登陆一次
118+ await this . checkAccessToken ( ) ;
77119 } else if ( status === 404 ) {
78120 messageKey = 'error.resource-not-found' ;
79121 }
@@ -136,6 +178,10 @@ export class AtomGitAPIService implements ICodeAPIService {
136178 throw new Error ( 'Method not implemented.' ) ;
137179 }
138180 async getBranches ( repo : IRepositoryModel ) : Promise < BranchOrTag [ ] > {
181+ if ( ! this . _PRIVATE_TOKEN ) {
182+ return [ ] ;
183+ }
184+
139185 const branches = await this . request < API . ResponseBranchesInfo [ ] > ( `/repos/${ this . getProjectPath ( repo ) } /branches` ) ;
140186 if ( ! Array . isArray ( branches ) ) {
141187 throw new Error ( '[can not find branch list]' ) ;
@@ -210,7 +256,6 @@ export class AtomGitAPIService implements ICodeAPIService {
210256 }
211257 } ) ;
212258
213- console . log ( 'blamePart:>>>>' , blamePart )
214259 return new TextEncoder ( ) . encode ( JSON . stringify ( blamePart ) ) ;
215260 }
216261 getCommits ( _repo : IRepositoryModel , _params : CommitParams ) : Promise < CommitRecord [ ] > {
0 commit comments