diff --git a/managers/SharedGitlabManager.ts b/managers/SharedGitlabManager.ts index 6984792ee2f3ce7755c3331f5160d131ef3c66a4..42039c2b98381f26619b02706a7b8084aee85c5a 100644 --- a/managers/SharedGitlabManager.ts +++ b/managers/SharedGitlabManager.ts @@ -1,24 +1,42 @@ import axios from 'axios'; import SharedConfig from '../config/SharedConfig'; import * as GitlabCore from '@gitbeaker/core'; +import { GitbeakerRequestError } from '@gitbeaker/requester-utils'; import { Gitlab, PipelineSchema } from '@gitbeaker/rest'; import GitlabToken from '../types/Gitlab/GitlabToken'; +import { StatusCodes } from 'http-status-codes'; class SharedGitlabManager { private api!: GitlabCore.Gitlab<false>; + private readonly refreshTokenFunction?: () => Promise<string>; setToken(token: string) { - this.api = new Gitlab({ - host : SharedConfig.gitlab.URL, - token: token - }); + this.api = new Gitlab(Object.assign({ + host : SharedConfig.gitlab.URL, + token: token + })); } - constructor(token: string) { + constructor(token: string, refreshTokenFunction?: () => Promise<string>) { + this.refreshTokenFunction = refreshTokenFunction; this.setToken(token); } + private async executeGitlabRequest<T>(request: () => Promise<T>): Promise<T> { + try { + return await request(); + } catch ( error ) { + if ( this.refreshTokenFunction && error instanceof GitbeakerRequestError && error.cause?.response.status === StatusCodes.UNAUTHORIZED ) { + this.setToken(await this.refreshTokenFunction()); + + return await request(); + } else { + throw error; + } + } + } + async getTokens(codeOrRefresh: string, isRefresh: boolean = false, clientSecret: string = ''): Promise<GitlabToken> { const response = await axios.post<GitlabToken>(SharedConfig.login.gitlab.url.token, { client_id : SharedConfig.login.gitlab.client.id, @@ -33,8 +51,10 @@ class SharedGitlabManager { } async getRepositoryPipelines(repoId: number, branch: string = 'main'): Promise<Array<PipelineSchema>> { - return await this.api.Pipelines.all(repoId, { - ref: branch + return await this.executeGitlabRequest(async () => { + return await this.api.Pipelines.all(repoId, { + ref: branch + }); }); } }