mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-19 08:02:55 +00:00
refactor: move gitlab-api to monorepo
This commit is contained in:
626
packages/gitlab-api/src/api.ts
Normal file
626
packages/gitlab-api/src/api.ts
Normal file
@@ -0,0 +1,626 @@
|
||||
/*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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,
|
||||
Note,
|
||||
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<void> {
|
||||
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<Member> {
|
||||
return this.makeGitLabAPIRequest(`${scope}/${id}/members`, {
|
||||
data: {
|
||||
access_level: accessLevel,
|
||||
user_id: userId,
|
||||
},
|
||||
method: 'POST',
|
||||
}) as Promise<Member>;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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<Issue> {
|
||||
return this.makeGitLabAPIRequest(`projects/${projectId}/issues`, {
|
||||
data: {
|
||||
description: description,
|
||||
title: title,
|
||||
},
|
||||
method: 'POST',
|
||||
}) as Promise<Issue>;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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<Label> {
|
||||
let _color = '#000000';
|
||||
if (typeof color !== 'string' || !/^#[0-9a-fA-F]{3,6}$/.test(color)) {
|
||||
_color = '#000000';
|
||||
}
|
||||
|
||||
return this.makeGitLabAPIRequest(`projects/${projectId}/labels`, {
|
||||
data: {
|
||||
color: _color,
|
||||
description,
|
||||
name,
|
||||
},
|
||||
method: 'POST',
|
||||
}) as Promise<Label>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a milestone in a project
|
||||
*
|
||||
* @param projectId Project ID to create milestone in
|
||||
* @param title Title of the milestone to create
|
||||
*/
|
||||
public async createMilestone(projectId: number, title: string): Promise<void> {
|
||||
return this.makeGitLabAPIRequest(`projects/${projectId}/milestones?title=${title}`, {
|
||||
method: 'POST',
|
||||
}) as Promise<void>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a note (comment) in an issue/merge request
|
||||
*
|
||||
* @param projectId Project ID, the issue belongs to
|
||||
* @param scope Scope of the note
|
||||
* @param iid IID of the issue/merge request to create the note in
|
||||
* @param body Body of the note to create
|
||||
*/
|
||||
public async createNote(projectId: number, scope: Scope, iid: number, body: string): Promise<void> {
|
||||
return this.makeGitLabAPIRequest(`projects/${projectId}/${scope}/${iid}/notes`, {
|
||||
data: {
|
||||
body,
|
||||
},
|
||||
method: 'POST',
|
||||
}) as Promise<void>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a label
|
||||
*
|
||||
* @param projectId ID of the project to delete the label from
|
||||
* @param name Name of the label to delete
|
||||
*/
|
||||
public async deleteLabel(projectId: number, name: string): Promise<void> {
|
||||
return this.makeGitLabAPIRequest(`projects/${projectId}/labels?name=${name}`, {
|
||||
method: 'DELETE',
|
||||
}) as Promise<void>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a member from 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
|
||||
*/
|
||||
public async deleteMember(scope: MembershipScope, id: number, userId: number): Promise<void> {
|
||||
return this.makeGitLabAPIRequest(`${scope}/${id}/members/${userId}`, {method: 'DELETE'}) as Promise<void>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit an existing label
|
||||
*
|
||||
* @param projectId ID of the project to edit the label in
|
||||
* @param name Name of the label to edit
|
||||
* @param newValues New values for the label
|
||||
*/
|
||||
public async editLabel(projectId: number, name: string, newValues: Partial<Label>): Promise<Label> {
|
||||
if (typeof newValues.color === 'string' && !/^#[0-9a-fA-F]{3,6}$/.test(newValues.color)) {
|
||||
newValues.color = undefined;
|
||||
}
|
||||
|
||||
return this.makeGitLabAPIRequest(`projects/${projectId}/labels`, {
|
||||
data: {
|
||||
color: newValues.color,
|
||||
description: newValues.description,
|
||||
name: name,
|
||||
new_name: newValues.name,
|
||||
},
|
||||
method: 'POST',
|
||||
}) as Promise<Label>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit member in 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 member in the scope
|
||||
*/
|
||||
public async editMember(
|
||||
scope: MembershipScope,
|
||||
id: number,
|
||||
userId: number,
|
||||
accessLevel: AccessLevel,
|
||||
): Promise<Member> {
|
||||
return this.makeGitLabAPIRequest(`${scope}/${id}/members`, {
|
||||
data: {
|
||||
access_level: accessLevel,
|
||||
user_id: userId,
|
||||
},
|
||||
method: 'PUT',
|
||||
}) as Promise<Member>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get branches for a project
|
||||
*
|
||||
* @param projectId Project ID to get branches for
|
||||
*/
|
||||
public async getBranchesForProject(projectId: number): Promise<Branch[]> {
|
||||
return this.makeGitLabAPIRequest(`projects/${projectId}/repository/branches`) as Promise<Branch[]>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a file from GitLab
|
||||
*
|
||||
* @param projectId ID of the project the file belongs to
|
||||
* @param filePath Path to the file - url encoded
|
||||
* @param commitish Commitish of the file
|
||||
*/
|
||||
public async getFile(projectId: number, filePath: string, commitish: string): Promise<unknown> {
|
||||
const fileIdentifier = `${encodeURIComponent(filePath).replace('.', '%2E')}/raw?ref=${encodeURIComponent(
|
||||
commitish,
|
||||
)}`;
|
||||
|
||||
return this.makeGitLabAPIRequest(
|
||||
`projects/${projectId}/repository/files/${fileIdentifier}`,
|
||||
) as Promise<unknown>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of files
|
||||
*
|
||||
* @param projectId ID of the project
|
||||
*/
|
||||
public async getFileList(projectId: number): Promise<TreeFile[]> {
|
||||
return this.makeGitLabAPIRequest(`projects/${projectId}/repository/tree`) as Promise<TreeFile[]>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get issues
|
||||
*
|
||||
* @param options Options to get issues
|
||||
*/
|
||||
public async getIssues(options: ApiGetIssuesOptions = {}): Promise<Issue[]> {
|
||||
// start to build request url
|
||||
let requestUrl = 'issues';
|
||||
|
||||
// set initial divider for filter params
|
||||
let divider = '?';
|
||||
|
||||
// request issues only for specific group, if group ID is set
|
||||
if (typeof options.groupId === 'number') {
|
||||
requestUrl = `groups/${options.groupId}/${requestUrl}`;
|
||||
}
|
||||
|
||||
if (typeof options.milestone === 'string') {
|
||||
// add milestone to request url
|
||||
requestUrl += `${divider}milestone=${options.milestone}`;
|
||||
divider = '&';
|
||||
}
|
||||
|
||||
// request issues only for specific state, if state is set
|
||||
if (typeof options.state === 'string') {
|
||||
requestUrl += `${divider}state=${options.state}`;
|
||||
// divider = '&';
|
||||
}
|
||||
|
||||
return this.makeGitLabAPIRequest(requestUrl) as Promise<Issue[]>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get labels of a project
|
||||
*
|
||||
* @param projectId ID of the project to get the labels for
|
||||
*/
|
||||
public async getLabels(projectId: number): Promise<Label[]> {
|
||||
return this.makeGitLabAPIRequest(`projects/${projectId}/labels`) as Promise<Label[]>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get members of a group or a project
|
||||
*
|
||||
* @param scope MembershipScope of the ID
|
||||
* @param id ID of the group or project
|
||||
*/
|
||||
public async getMembers(scope: MembershipScope, id: number): Promise<Member[]> {
|
||||
return this.makeGitLabAPIRequest(`${scope}/${id}/members`) as Promise<Member[]>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a merge request approval
|
||||
*
|
||||
* @param projectId ID of the project the merge request belongs to
|
||||
* @param mergeRequestIid IID of the merge request
|
||||
*/
|
||||
public async getMergeRequestApproval(
|
||||
projectId: number,
|
||||
mergeRequestIid: number,
|
||||
): Promise<MergeRequestApproval> {
|
||||
return this.makeGitLabAPIRequest(
|
||||
`/projects/${projectId}/merge_requests/${mergeRequestIid}/approvals`,
|
||||
) as Promise<MergeRequestApproval>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get discussions of a merge request
|
||||
*
|
||||
* @param projectId ID of the project the merge request belongs to
|
||||
* @param mergeRequestIid IID of the merge request
|
||||
*/
|
||||
public async getMergeRequestDiscussions(projectId: number, mergeRequestIid: number): Promise<Discussion[]> {
|
||||
return this.makeGitLabAPIRequest(
|
||||
`projects/${projectId}/merge_requests/${mergeRequestIid}/discussions`,
|
||||
) as Promise<Discussion[]>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get merge requests of a group or a project
|
||||
*
|
||||
* @param scope MembershipScope of the ID
|
||||
* @param id ID of the group or project
|
||||
* @param state State to filter the merge requests by
|
||||
*/
|
||||
public async getMergeRequests(
|
||||
scope: MembershipScope,
|
||||
id: number,
|
||||
state: MergeRequestState | MergeRequestState[],
|
||||
): Promise<MergeRequest[]> {
|
||||
let _state = state;
|
||||
|
||||
// join a list of states with commas
|
||||
if (Array.isArray(state)) {
|
||||
_state = state.join(',') as MergeRequestState;
|
||||
}
|
||||
|
||||
return this.makeGitLabAPIRequest(`${scope}/${id}/merge_requests?state=${_state}`) as Promise<
|
||||
MergeRequest[]
|
||||
>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get milestones for a project
|
||||
*
|
||||
* @param projectId Project ID to get milestones for
|
||||
*/
|
||||
public async getMilestonesForProject(projectId: number): Promise<Milestone[]> {
|
||||
return this.makeGitLabAPIRequest(`projects/${projectId}/milestones`) as Promise<Milestone[]>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get notes for issue
|
||||
*
|
||||
* @param projectId Project ID of issue to get notes for
|
||||
* @param issue Issue to get notes for
|
||||
*/
|
||||
public async getNotes(projectId: number, issue: Issue) {
|
||||
return this.makeGitLabAPIRequest(`/projects/${projectId}/issues/${issue.iid}/notes?sort=asc`) as Promise<
|
||||
Note[]
|
||||
>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get projects for a group
|
||||
*
|
||||
* @param groupId Group ID to get projects for
|
||||
*/
|
||||
public async getProjectsForGroup(groupId: number): Promise<Project[]> {
|
||||
return this.makeGitLabAPIRequest(`groups/${groupId}/projects`) as Promise<Project[]>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get sub groups of a group
|
||||
*
|
||||
* @param groupId Group ID to get subgroups for
|
||||
*/
|
||||
public async getSubGroupsForGroup(groupId: number): Promise<Group[]> {
|
||||
return this.makeGitLabAPIRequest(`/groups/${groupId}/subgroups`) as Promise<Group[]>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get tags of a project
|
||||
*
|
||||
* @param projectId ID of the project to get the tags for
|
||||
*/
|
||||
public async getTags(projectId: number): Promise<Tag[]> {
|
||||
return this.makeGitLabAPIRequest(`projects/${projectId}/repository/tags`) as Promise<Tag[]>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Query a GitLab API URL
|
||||
*
|
||||
* @param url GitLab API URL to query
|
||||
* @param options HTTP method/verb
|
||||
*/
|
||||
public async makeGitLabAPIRequest(url: string, options?: ApiRequestOptions): Promise<unknown> {
|
||||
// remove leading slash
|
||||
const _url = url.replace(/^\/+/g, '');
|
||||
|
||||
const _options: Required<ApiRequestOptions> = {
|
||||
// eslint-disable-next-line unicorn/no-null
|
||||
data: null,
|
||||
method: 'GET',
|
||||
retryOnAnyError: false,
|
||||
tries: 5,
|
||||
...options,
|
||||
};
|
||||
|
||||
if (!['DELETE', 'GET', 'POST', 'PUT'].includes(_options.method)) {
|
||||
_options.method = 'GET';
|
||||
}
|
||||
|
||||
let concatenator = '&';
|
||||
if (!_url.includes('?')) {
|
||||
concatenator = '?';
|
||||
}
|
||||
|
||||
let apiResult: unknown;
|
||||
let totalPages = 1;
|
||||
let currentPage = 0;
|
||||
|
||||
while (++currentPage <= totalPages) {
|
||||
if (currentPage > 1) {
|
||||
Logger.info(
|
||||
`Automatically paging call to '${_url}'... Getting page ${currentPage} of ${totalPages}.`,
|
||||
);
|
||||
}
|
||||
|
||||
let body;
|
||||
let tries = 0;
|
||||
|
||||
while (body === undefined && tries++ < _options.tries) {
|
||||
try {
|
||||
const requestUrl = `${_url}${concatenator}page=${currentPage}&per_page=100`;
|
||||
|
||||
body = await request(`${this.rootUrl}${requestUrl}`, {
|
||||
form: _options.data === null ? undefined : _options.data,
|
||||
headers: {'PRIVATE-TOKEN': this.privateToken},
|
||||
json: true,
|
||||
method: _options.method,
|
||||
timeout: 60_000,
|
||||
followAllRedirects: true,
|
||||
transform: (bodyToTransform, response) => {
|
||||
const xTotalPages = response.headers['x-total-pages'];
|
||||
|
||||
if (typeof xTotalPages === 'string') {
|
||||
totalPages = Number.parseInt(xTotalPages, 10);
|
||||
}
|
||||
|
||||
return bodyToTransform;
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
if ((error as Error).message.includes('not responding') || _options.retryOnAnyError) {
|
||||
const seconds = 5;
|
||||
|
||||
Logger.warn(`GitLab was not responding. Waiting ${seconds}s and retrying...`);
|
||||
|
||||
await sleep(seconds * 1000);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
Logger.log(url);
|
||||
Logger.log(JSON.stringify(options));
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
if (_options.method === 'DELETE') {
|
||||
return;
|
||||
}
|
||||
|
||||
apiResult =
|
||||
apiResult !== undefined && Array.isArray(apiResult) && currentPage > 1
|
||||
? (apiResult = [...apiResult, ...body])
|
||||
: (apiResult = body);
|
||||
}
|
||||
|
||||
return apiResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* Protect a branch
|
||||
*
|
||||
* @param projectId ID of the project the branch belongs to
|
||||
* @param branch Branch to protect
|
||||
*/
|
||||
public async protectBranch(projectId: number, branch: string): Promise<Branch> {
|
||||
return this.makeGitLabAPIRequest(
|
||||
`projects/${projectId}/repository/branches/${branch}/protect?developers_can_push=false&developers_can_merge=false`,
|
||||
{
|
||||
method: 'PUT',
|
||||
},
|
||||
) as Promise<Branch>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set assignee for an issue
|
||||
*
|
||||
* @param issue Issue to set assignee for
|
||||
* @param userId ID of the assignee to set for the issue
|
||||
*/
|
||||
public async setAssigneeForIssue(issue: Issue, userId: number): Promise<Issue> {
|
||||
return this.makeGitLabAPIRequest(
|
||||
`projects/${issue.project_id}/issues/${issue.iid}?assignee_ids=${userId}`,
|
||||
{
|
||||
method: 'PUT',
|
||||
},
|
||||
) as Promise<Issue>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set assignee for an merge request
|
||||
*
|
||||
* @param mergeRequest Merge request to set assignee for
|
||||
* @param userId ID of the assignee to set for the merge request
|
||||
*/
|
||||
public async setAssigneeForMergeRequest(mergeRequest: MergeRequest, userId: number): Promise<MergeRequest> {
|
||||
return this.makeGitLabAPIRequest(
|
||||
`projects/${mergeRequest.project_id}/merge_requests/${mergeRequest.iid}?assignee_ids=${userId}`,
|
||||
{
|
||||
method: 'PUT',
|
||||
},
|
||||
) as Promise<MergeRequest>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set milestone for an issue
|
||||
*
|
||||
* @param issue Issue to set milestone for
|
||||
* @param milestoneId ID of the milestone to set for the issue
|
||||
*/
|
||||
public async setMilestoneForIssue(issue: Issue, milestoneId: number): Promise<Issue> {
|
||||
if (milestoneId === null) {
|
||||
return this.makeGitLabAPIRequest(`projects/${issue.project_id}/issues/${issue.iid}?milestone_id=`, {
|
||||
method: 'PUT',
|
||||
}) as Promise<Issue>;
|
||||
}
|
||||
|
||||
return this.makeGitLabAPIRequest(
|
||||
`projects/${issue.project_id}/issues/${issue.iid}?milestone_id=${milestoneId}`,
|
||||
{
|
||||
method: 'PUT',
|
||||
},
|
||||
) as Promise<Issue>;
|
||||
}
|
||||
}
|
||||
180
packages/gitlab-api/src/cli.ts
Normal file
180
packages/gitlab-api/src/cli.ts
Normal file
@@ -0,0 +1,180 @@
|
||||
/*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {asyncPool} from '@krlwlfrt/async-pool';
|
||||
import {Logger} from '@openstapps/logger';
|
||||
import {AddLogLevel} from '@openstapps/logger/lib/transformations/add-log-level';
|
||||
import {Colorize} from '@openstapps/logger/lib/transformations/colorize';
|
||||
import {Command} from 'commander';
|
||||
import {readFileSync} from 'fs';
|
||||
import path from 'path';
|
||||
import {Api, ApiRequestOptions} from './api';
|
||||
import {Issue, IssueState, MembershipScope, Scope} from './types';
|
||||
|
||||
Logger.setTransformations([new AddLogLevel(), new Colorize()]);
|
||||
|
||||
// eslint-disable-next-line unicorn/prefer-module
|
||||
const packageJson = JSON.parse(readFileSync(path.join(__dirname, '..', 'package.json')).toString());
|
||||
|
||||
const commander = new Command('openstapps-gitlab-api');
|
||||
|
||||
commander.version(packageJson.version);
|
||||
|
||||
commander
|
||||
.option('-t, --token [token]', 'GitLab API token', process.env.GITLAB_PRIVATE_TOKEN)
|
||||
.option('-u, --url [url]', 'GitLab API URL', 'https://gitlab.com/api/v4/');
|
||||
|
||||
commander.command('request <call> [method] [data]').action(async (call, method, data) => {
|
||||
const options: ApiRequestOptions = {};
|
||||
|
||||
if (method !== 'GET') {
|
||||
options.method = method;
|
||||
}
|
||||
|
||||
if (data !== undefined) {
|
||||
options.data = JSON.parse(data);
|
||||
}
|
||||
|
||||
const api = new Api(commander.url, commander.token);
|
||||
|
||||
const result = await api.makeGitLabAPIRequest(call, options);
|
||||
|
||||
// eslint-disable-next-line no-console
|
||||
console.log(result);
|
||||
});
|
||||
|
||||
commander.command('batch-process <projectId> <action>').action(async (projectId, action) => {
|
||||
if (!['close'].includes(action)) {
|
||||
await Logger.error('Only "close" is supported as action.');
|
||||
}
|
||||
|
||||
const api = new Api(commander.url, commander.token);
|
||||
|
||||
const issues = (await api.makeGitLabAPIRequest(`/projects/${projectId}/issues?state=opened`, {
|
||||
retryOnAnyError: true,
|
||||
tries: 10,
|
||||
})) as Issue[];
|
||||
|
||||
Logger.log(`Fetched ${issues.length} issue(s).`);
|
||||
|
||||
await asyncPool(5, issues, async issue => {
|
||||
if (action === 'close') {
|
||||
Logger.info(`Closing issue #${issue.iid} of project '${projectId}': ${issue.title}.`);
|
||||
|
||||
await api.makeGitLabAPIRequest(`/projects/${projectId}/issues/${issue.iid}`, {
|
||||
data: {
|
||||
state_event: 'close',
|
||||
},
|
||||
method: 'PUT',
|
||||
retryOnAnyError: true,
|
||||
tries: 10,
|
||||
});
|
||||
}
|
||||
Logger.info(`Processed issue #${issue.iid} of project '${projectId}': ${issue.title}`);
|
||||
});
|
||||
|
||||
Logger.ok('Processed all issues.');
|
||||
});
|
||||
|
||||
commander
|
||||
.command('copy <projectId> <targetUrl> <targetToken> <targetProjectId>')
|
||||
.action(async (projectId, targetUrl, targetToken, targetProjectId) => {
|
||||
const api = new Api(commander.url, commander.token);
|
||||
const targetApi = new Api(targetUrl, targetToken);
|
||||
|
||||
// get all issues from project
|
||||
const issues = (await api.makeGitLabAPIRequest(`/projects/${projectId}/issues`, {
|
||||
retryOnAnyError: true,
|
||||
tries: 10,
|
||||
})) as Issue[];
|
||||
|
||||
// sort issues by their project specific ids
|
||||
issues.sort((a, b) => {
|
||||
return a.iid - b.iid;
|
||||
});
|
||||
|
||||
// get members of target project
|
||||
const members = await targetApi.getMembers(MembershipScope.PROJECTS, targetProjectId);
|
||||
|
||||
let index = 0;
|
||||
|
||||
await asyncPool(2, issues, async issue => {
|
||||
// get notes of old issue
|
||||
const notes = await api.getNotes(projectId, issue);
|
||||
|
||||
// create new issue
|
||||
const newIssue = await targetApi.createIssue(
|
||||
targetProjectId,
|
||||
issue.title,
|
||||
issue.description === null
|
||||
? '---'
|
||||
: `${issue.web_url}
|
||||
|
||||
${issue.description}`,
|
||||
);
|
||||
|
||||
for (const note of notes) {
|
||||
// skip system notes
|
||||
if (note.system) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// create new note in new issue for every note in issue
|
||||
await targetApi.createNote(
|
||||
targetProjectId,
|
||||
Scope.ISSUES,
|
||||
newIssue.iid,
|
||||
`**${note.author.name} (@${note.author.username}):**
|
||||
|
||||
${note.body}`,
|
||||
);
|
||||
}
|
||||
|
||||
// close newly created issue if original is closed to
|
||||
if (issue.state === IssueState.CLOSED) {
|
||||
await targetApi.makeGitLabAPIRequest(`/projects/${targetProjectId}/issues/${newIssue.iid}`, {
|
||||
data: {
|
||||
state_event: 'close',
|
||||
},
|
||||
method: 'PUT',
|
||||
retryOnAnyError: true,
|
||||
tries: 10,
|
||||
});
|
||||
}
|
||||
|
||||
// search for member in target group with same username
|
||||
const assignee = members.find(member => {
|
||||
if (issue.assignee === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return member.username === issue.assignee.username;
|
||||
});
|
||||
|
||||
// set assignee if usernames match
|
||||
if (assignee !== undefined) {
|
||||
await targetApi.setAssigneeForIssue(newIssue, assignee.id);
|
||||
}
|
||||
|
||||
Logger.log(`Finished issue ${++index} of ${issues.length}.`);
|
||||
});
|
||||
});
|
||||
|
||||
commander.parse(process.argv);
|
||||
|
||||
if (typeof commander.token !== 'string' || commander.token.length === 0) {
|
||||
Logger.warn(
|
||||
'You probably want to supply a GitLab token either via option or environment variable (GITLAB_PRIVATE_TOKEN).',
|
||||
);
|
||||
}
|
||||
393
packages/gitlab-api/src/types.ts
Normal file
393
packages/gitlab-api/src/types.ts
Normal file
@@ -0,0 +1,393 @@
|
||||
/*
|
||||
* Copyright (C) 2018, 2019 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Scope of membership
|
||||
*/
|
||||
export enum MembershipScope {
|
||||
GROUPS = 'groups',
|
||||
PROJECTS = 'projects',
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope
|
||||
*/
|
||||
export enum Scope {
|
||||
ISSUES = 'issues',
|
||||
MERGE_REQUESTS = 'merge_requests',
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge request state
|
||||
*/
|
||||
export enum MergeRequestState {
|
||||
CLOSED = 'closed',
|
||||
LOCKED = 'locked',
|
||||
MERGED = 'merged',
|
||||
OPENED = 'opened',
|
||||
}
|
||||
|
||||
/**
|
||||
* Issue state
|
||||
*/
|
||||
export enum IssueState {
|
||||
CLOSED = 'closed',
|
||||
OPENED = 'opened',
|
||||
REOPENED = 'reopened',
|
||||
}
|
||||
|
||||
/**
|
||||
* Milestone state
|
||||
*/
|
||||
export enum MilestoneState {
|
||||
ACTIVE = 'active',
|
||||
CLOSED = 'closed',
|
||||
}
|
||||
|
||||
/**
|
||||
* User state
|
||||
*/
|
||||
export enum UserState {
|
||||
ACTIVE = 'active',
|
||||
BLOCKED = 'blocked',
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge request merge status
|
||||
*/
|
||||
export enum MergeRequestMergeStatus {
|
||||
CAN_BE_MERGED = 'can_be_merged',
|
||||
}
|
||||
|
||||
/**
|
||||
* A note type
|
||||
*/
|
||||
export enum NoteType {
|
||||
DIFF_NOTE = 'DiffNote',
|
||||
}
|
||||
|
||||
/**
|
||||
* A type of a noteable thing
|
||||
*/
|
||||
export enum NoteableType {
|
||||
MERGE_REQUST = 'MergeRequest',
|
||||
}
|
||||
|
||||
/**
|
||||
* A label
|
||||
*/
|
||||
export interface Label {
|
||||
color: string;
|
||||
description?: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* A commit
|
||||
*/
|
||||
export interface Commit {
|
||||
author_email: string;
|
||||
author_name: string;
|
||||
authored_date: string;
|
||||
committed_date: string;
|
||||
committer_email: string;
|
||||
committer_name: string;
|
||||
id: string;
|
||||
message: string;
|
||||
parent_ids: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* A tag
|
||||
*/
|
||||
export interface Tag {
|
||||
commit: Commit;
|
||||
message: string | null;
|
||||
name: string;
|
||||
release: null;
|
||||
}
|
||||
|
||||
/**
|
||||
* A namespace
|
||||
*/
|
||||
export interface Namespace {
|
||||
full_path: string;
|
||||
id: number;
|
||||
kind: string;
|
||||
name: string;
|
||||
path: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* A project
|
||||
*/
|
||||
export interface Project {
|
||||
archived: boolean;
|
||||
avatar_url: string;
|
||||
container_registry_enabled: boolean;
|
||||
created_at: string;
|
||||
creator_id: number;
|
||||
default_branch: string;
|
||||
description: string;
|
||||
forks_count: number;
|
||||
http_url_to_repo: string;
|
||||
id: number;
|
||||
issues_enabled: boolean;
|
||||
jobs_enabled: boolean;
|
||||
last_activity_at: string;
|
||||
lfs_enabled: boolean;
|
||||
merge_requests_enabled: boolean;
|
||||
name: string;
|
||||
name_with_namespace: string;
|
||||
namespace: Namespace;
|
||||
only_allow_merge_if_all_discussions_are_resolved: boolean;
|
||||
only_allow_merge_if_pipeline_succeeds: boolean;
|
||||
open_issues_count: number;
|
||||
path: string;
|
||||
path_with_namespace: string;
|
||||
public_jobs: boolean;
|
||||
request_access_enabled: boolean;
|
||||
shared_runners_enabled: boolean;
|
||||
shared_with_groups: unknown[];
|
||||
snippets_enabled: boolean;
|
||||
ssh_url_to_repo: string;
|
||||
star_count: number;
|
||||
tag_list: string[];
|
||||
visibility: string;
|
||||
web_url: string;
|
||||
wiki_enabled: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* A tree file
|
||||
*/
|
||||
export interface TreeFile {
|
||||
id: string;
|
||||
mode: string;
|
||||
name: string;
|
||||
path: string;
|
||||
type: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* A member of a group or a project
|
||||
*/
|
||||
export interface Member extends User {
|
||||
access_level: AccessLevel;
|
||||
expires_at: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Available access levels
|
||||
*/
|
||||
export enum AccessLevel {
|
||||
Guest = 10,
|
||||
Reporter = 20,
|
||||
Developer = 30,
|
||||
Maintainer = 40,
|
||||
Owner = 50,
|
||||
}
|
||||
|
||||
/**
|
||||
* A milestone
|
||||
*/
|
||||
export interface Milestone {
|
||||
created_at: string;
|
||||
description: string | null;
|
||||
due_date: string;
|
||||
id: number;
|
||||
iid: number;
|
||||
project_id: number;
|
||||
start_date: string;
|
||||
state: MilestoneState;
|
||||
title: string;
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* A branch
|
||||
*/
|
||||
export interface Branch {
|
||||
commit: Commit;
|
||||
developers_can_merge: boolean;
|
||||
developers_can_push: boolean;
|
||||
merged: boolean;
|
||||
name: string;
|
||||
protected: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* A user
|
||||
*/
|
||||
export interface User {
|
||||
avatar_url: string;
|
||||
id: number;
|
||||
name: string;
|
||||
state: UserState;
|
||||
username: string;
|
||||
web_url: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* A issue
|
||||
*/
|
||||
export interface Issue extends ThingWithTimeStats {
|
||||
assignee: User;
|
||||
assignees: User[];
|
||||
author: User;
|
||||
closed_at: string | null;
|
||||
confidential: boolean;
|
||||
created_at: string;
|
||||
description: string | null;
|
||||
discussion_locked: boolean | null;
|
||||
downvotes: number;
|
||||
due_date: string | null;
|
||||
id: number;
|
||||
iid: number;
|
||||
labels: string[];
|
||||
milestone: Milestone | null;
|
||||
project_id: number;
|
||||
state: IssueState;
|
||||
title: string;
|
||||
updated_at: string;
|
||||
upvotes: number;
|
||||
user_notes_count: number;
|
||||
web_url: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* A merge request
|
||||
*/
|
||||
export interface MergeRequest extends ThingWithTimeStats {
|
||||
assignee: User;
|
||||
author: User;
|
||||
created_at: string;
|
||||
description: string | null;
|
||||
discussion_locked: boolean | null;
|
||||
downvotes: number;
|
||||
force_remove_source_branch: boolean;
|
||||
id: number;
|
||||
iid: number;
|
||||
labels: Label[];
|
||||
merge_commit_sha: string;
|
||||
merge_status: MergeRequestMergeStatus;
|
||||
merge_when_pipeline_succeeds: boolean;
|
||||
milestone: Milestone;
|
||||
project_id: number;
|
||||
sha: string;
|
||||
should_remove_source_branch: boolean;
|
||||
source_branch: string;
|
||||
source_project_id: number;
|
||||
state: MergeRequestState;
|
||||
target_branch: string;
|
||||
target_project_id: number;
|
||||
title: string;
|
||||
updated_at: string;
|
||||
upvotes: number;
|
||||
user_notes_count: number;
|
||||
web_url: string;
|
||||
work_in_progress: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* A merge request approval
|
||||
*/
|
||||
export interface MergeRequestApproval {
|
||||
approvals_left: number;
|
||||
approvals_required: number;
|
||||
approved_by: Array<{user: User}>;
|
||||
approver_groups: Group[];
|
||||
approvers: User[];
|
||||
created_at: string;
|
||||
description: string;
|
||||
id: number;
|
||||
iid: number;
|
||||
merge_status: MergeRequestMergeStatus;
|
||||
project_id: number;
|
||||
state: MergeRequestState;
|
||||
title: string;
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* A thing with time stats
|
||||
*/
|
||||
export interface ThingWithTimeStats {
|
||||
time_stats: {
|
||||
human_time_estimate: number | null;
|
||||
human_total_time_spent: number | null;
|
||||
time_estimate: number;
|
||||
total_time_spent: number;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* A group
|
||||
*/
|
||||
export interface Group {
|
||||
avatar_url: string;
|
||||
description: string;
|
||||
full_name: string;
|
||||
full_path: string;
|
||||
id: number;
|
||||
ldap_access: string;
|
||||
ldap_cn: string;
|
||||
lfs_enabled: boolean;
|
||||
name: string;
|
||||
parent_id: number;
|
||||
path: string;
|
||||
request_access_enabled: boolean;
|
||||
visibility: string;
|
||||
web_url: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* A discussion
|
||||
*/
|
||||
export interface Discussion {
|
||||
id: string;
|
||||
individual_note: boolean;
|
||||
notes: Note[];
|
||||
}
|
||||
|
||||
/**
|
||||
* A note
|
||||
*/
|
||||
export interface Note {
|
||||
attachment: null;
|
||||
author: User;
|
||||
body: string;
|
||||
created_at: string;
|
||||
id: number;
|
||||
noteable_id: number;
|
||||
noteable_iid: number;
|
||||
noteable_type: NoteableType;
|
||||
position: {
|
||||
base_sha: string;
|
||||
head_sha: string;
|
||||
new_line: number | null;
|
||||
new_path: string;
|
||||
old_line: number | null;
|
||||
old_path: string;
|
||||
position_type: string;
|
||||
start_sha: string;
|
||||
};
|
||||
resolvable: boolean;
|
||||
resolved: boolean | undefined;
|
||||
resolved_by: User | undefined;
|
||||
system: boolean;
|
||||
type: NoteType;
|
||||
updated_at: string;
|
||||
}
|
||||
Reference in New Issue
Block a user