/* * Copyright (C) 2018-2020 StApps * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation, version 3. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ import {Logger} from '@openstapps/logger'; import request from 'request-promise-native'; import { AccessLevel, Branch, Discussion, Group, Issue, IssueState, Label, Member, MembershipScope, MergeRequest, MergeRequestApproval, MergeRequestState, Milestone, Project, Scope, Tag, TreeFile, } from './types'; /** * Sleep for a number of milliseconds * * @param ms Number of milliseconds to wait */ export async function sleep(ms: number): Promise { return new Promise((resolve) => setTimeout(resolve, ms)); } /** * API request options */ export interface ApiRequestOptions { /** * Data to be sent with the request */ data?: object | null; /** * HTTP verb to use for the request */ method?: 'DELETE' | 'GET' | 'POST' | 'PUT'; /** * Whether or not to retry on any error */ retryOnAnyError?: boolean; /** * Amount of tries */ tries?: number; } /** * GitLab API get issues options */ export interface ApiGetIssuesOptions { /** * Filter issues by group ID */ groupId?: number; /** * Filter issues by milestone */ milestone?: 'Backlog' | 'No Milestone'; /** * Filter issues by state */ state?: IssueState; } /** * GitLab API */ export class Api { /** * Private token */ private readonly privateToken: string; /** * Root url */ private readonly rootUrl: string; /** * Instantiate new GitLab API * * @param rootUrl Root URL of the GitLab API * @param privateToken Private token for the GitLab API */ constructor(rootUrl: string, privateToken: string) { this.rootUrl = rootUrl; this.privateToken = privateToken; } /** * Add member to a group or a project * * @param scope MembershipScope of the ID * @param id ID of the group or project * @param userId ID of the user * @param accessLevel Access level for the new member in the scope */ public async addMember(scope: MembershipScope, id: number, userId: number, accessLevel: AccessLevel): Promise { return this.makeGitLabAPIRequest( `${scope}/${id}/members`, { data: { access_level: accessLevel, user_id: userId, }, method: 'POST', }, ) as Promise; } /** * Create an issue in GitLab * * @param projectId ID of the project to create the issue in * @param title Title of the issue * @param description Description of the issue (can contain slash commands) */ public async createIssue(projectId: number, title: string, description: string): Promise { return this.makeGitLabAPIRequest( `projects/${projectId}/issues`, { data: { description: description, title: title, }, method: 'POST', }, ) as Promise; } /** * Create a new label * * @param projectId ID of the project to create the label in * @param name Name of the label to create * @param description Description of the label to create * @param color Color of the label to create */ public async createLabel(projectId: number, name: string, description?: string, color?: string): Promise