Skip to content
Snippets Groups Projects
Commit cb2f81e5 authored by michael.minelli's avatar michael.minelli Committed by michael.minelli
Browse files

SharedGitlabManager => Add possibility to update token

parent 5ae90635
No related branches found
No related tags found
No related merge requests found
import axios from 'axios'; import axios from 'axios';
import SharedConfig from '../config/SharedConfig'; import SharedConfig from '../config/SharedConfig';
import * as GitlabCore from '@gitbeaker/core'; import * as GitlabCore from '@gitbeaker/core';
import { GitbeakerRequestError } from '@gitbeaker/requester-utils';
import { Gitlab, PipelineSchema } from '@gitbeaker/rest'; import { Gitlab, PipelineSchema } from '@gitbeaker/rest';
import GitlabToken from '../types/Gitlab/GitlabToken'; import GitlabToken from '../types/Gitlab/GitlabToken';
import { StatusCodes } from 'http-status-codes';
class SharedGitlabManager { class SharedGitlabManager {
private api!: GitlabCore.Gitlab<false>; private api!: GitlabCore.Gitlab<false>;
private readonly refreshTokenFunction?: () => Promise<string>;
setToken(token: string) { setToken(token: string) {
this.api = new Gitlab({ this.api = new Gitlab(Object.assign({
host : SharedConfig.gitlab.URL, host : SharedConfig.gitlab.URL,
token: token token: token
}); }));
} }
constructor(token: string) { constructor(token: string, refreshTokenFunction?: () => Promise<string>) {
this.refreshTokenFunction = refreshTokenFunction;
this.setToken(token); 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> { async getTokens(codeOrRefresh: string, isRefresh: boolean = false, clientSecret: string = ''): Promise<GitlabToken> {
const response = await axios.post<GitlabToken>(SharedConfig.login.gitlab.url.token, { const response = await axios.post<GitlabToken>(SharedConfig.login.gitlab.url.token, {
client_id : SharedConfig.login.gitlab.client.id, client_id : SharedConfig.login.gitlab.client.id,
...@@ -33,8 +51,10 @@ class SharedGitlabManager { ...@@ -33,8 +51,10 @@ class SharedGitlabManager {
} }
async getRepositoryPipelines(repoId: number, branch: string = 'main'): Promise<Array<PipelineSchema>> { async getRepositoryPipelines(repoId: number, branch: string = 'main'): Promise<Array<PipelineSchema>> {
return await this.api.Pipelines.all(repoId, { return await this.executeGitlabRequest(async () => {
ref: branch return await this.api.Pipelines.all(repoId, {
ref: branch
});
}); });
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment