@@ -35,12 +35,23 @@ const GIT_CREDENTIALS_LABEL_SELECTOR: string = createLabelsSelector(GIT_CREDENTI
3535@injectable ( )
3636export class GithubServiceImpl implements GithubService {
3737 private token : string | undefined ;
38+ private tokenInitializationPromise : Promise < void > | undefined ;
3839
3940 constructor (
4041 @inject ( Logger ) private logger : Logger ,
4142 @inject ( K8SServiceImpl ) private readonly k8sService : K8SServiceImpl
42- ) {
43- this . initializeToken ( ) ;
43+ ) { }
44+
45+ private async ensureTokenInitialized ( ) : Promise < void > {
46+ if ( this . token !== undefined ) {
47+ return ;
48+ }
49+ if ( ! this . tokenInitializationPromise ) {
50+ this . tokenInitializationPromise = this . initializeToken ( ) . finally ( ( ) => {
51+ this . tokenInitializationPromise = undefined ;
52+ } ) ;
53+ }
54+ await this . tokenInitializationPromise ;
4455 }
4556
4657 private checkToken ( ) : void {
@@ -50,17 +61,20 @@ export class GithubServiceImpl implements GithubService {
5061 }
5162
5263 async getToken ( ) : Promise < string > {
64+ await this . ensureTokenInitialized ( ) ;
5365 this . checkToken ( ) ;
5466 return this . token ! ;
5567 }
5668
5769 async getUser ( ) : Promise < GithubUser > {
70+ await this . ensureTokenInitialized ( ) ;
5871 this . checkToken ( ) ;
5972 const result = await this . fetchGithubUser ( this . token ! ) ;
6073 return result . user ;
6174 }
6275
6376 async getTokenScopes ( token : string ) : Promise < string [ ] > {
77+ await this . ensureTokenInitialized ( ) ;
6478 this . checkToken ( ) ;
6579 const result = await this . fetchGithubUser ( token ) ;
6680 return result . scopes ;
@@ -128,7 +142,8 @@ export class GithubServiceImpl implements GithubService {
128142 }
129143
130144 // another token should be used by the Github Service after removing the Device Authentication token
131- this . initializeToken ( ) ;
145+ this . token = undefined ;
146+ await this . ensureTokenInitialized ( ) ;
132147 }
133148
134149 private async initializeToken ( ) : Promise < void > {
0 commit comments