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

Add GitlabHelper

parent b3c524da
No related branches found
No related tags found
No related merge requests found
import axios from 'axios';
import Config from '../config/Config';
import GitlabRepository from '../shared/types/Gitlab/GitlabRepository';
import GitlabAccessLevel from '../shared/types/Gitlab/GitlabAccessLevel';
import GitlabMember from '../shared/types/Gitlab/GitlabMember';
enum GitlabRoutes {
REPOSITORY_GET = '/projects/{{id}}',
REPOSITORY_CREATE = '/projects',
REPOSITORY_MEMBER_ADD = '/projects/{{id}}/members',
REPOSITORY_MEMBERS_GET = '/projects/{{id}}/members/all'
}
class GitlabHelper {
private static _instance: GitlabHelper;
private constructor() { }
public static get instance(): GitlabHelper {
if ( !GitlabHelper._instance ) {
GitlabHelper._instance = new GitlabHelper();
}
return GitlabHelper._instance;
}
private getApiUrl(route: GitlabRoutes): string {
return `${ Config.gitlab.apiURL }${ route }`;
}
async createRepository(name: string, description: string, visibility: string, initializeWithReadme: boolean, namespace: number, sharedRunnersEnabled: boolean, wikiEnabled: boolean, import_url: string): Promise<GitlabRepository> {
const response = await axios.post<GitlabRepository>(this.getApiUrl(GitlabRoutes.REPOSITORY_CREATE), {
name : name,
description : description,
import_url : import_url,
initialize_with_readme: initializeWithReadme,
namespace_id : namespace,
shared_runners_enabled: sharedRunnersEnabled,
visibility : visibility,
wiki_enabled : wikiEnabled
});
return response.data;
}
async getRepository(idOrNamespace: string): Promise<GitlabRepository> {
const response = await axios.get<GitlabRepository>(this.getApiUrl(GitlabRoutes.REPOSITORY_GET).replace('{{id}}', encodeURIComponent(idOrNamespace)));
return response.data;
}
async getRepositoryMembers(idOrNamespace: string): Promise<Array<GitlabMember>> {
const response = await axios.get<Array<GitlabMember>>(this.getApiUrl(GitlabRoutes.REPOSITORY_MEMBERS_GET).replace('{{id}}', encodeURIComponent(idOrNamespace)));
return response.data;
}
async addRepositoryMember(repoId: number, userId: number, accessLevel: GitlabAccessLevel): Promise<GitlabMember> {
const response = await axios.post<GitlabMember>(this.getApiUrl(GitlabRoutes.REPOSITORY_MEMBER_ADD).replace('{{id}}', String(repoId)), {
user_id : userId,
access_level: accessLevel
});
return response.data;
}
}
export default GitlabHelper.instance;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment