fix: build

This commit is contained in:
2023-03-14 18:04:29 +01:00
parent 3792a14e90
commit fd740b3091
185 changed files with 21932 additions and 71486 deletions

View File

@@ -13,7 +13,6 @@
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {Logger} from '@openstapps/logger';
import request from 'request-promise-native';
import {
AccessLevel,
Branch,
@@ -34,6 +33,7 @@ import {
Tag,
TreeFile,
} from './types';
import got from "got";
/**
* Sleep for a number of milliseconds
@@ -126,13 +126,13 @@ export class Api {
userId: number,
accessLevel: AccessLevel,
): Promise<Member> {
return this.makeGitLabAPIRequest(`${scope}/${id}/members`, {
return this.makeGitLabAPIRequest<Member>(`${scope}/${id}/members`, {
data: {
access_level: accessLevel,
user_id: userId,
},
method: 'POST',
}) as Promise<Member>;
});
}
/**
@@ -143,13 +143,13 @@ export class Api {
* @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`, {
return this.makeGitLabAPIRequest<Issue>(`projects/${projectId}/issues`, {
data: {
description: description,
title: title,
},
method: 'POST',
}) as Promise<Issue>;
});
}
/**
@@ -171,14 +171,14 @@ export class Api {
_color = '#000000';
}
return this.makeGitLabAPIRequest(`projects/${projectId}/labels`, {
return this.makeGitLabAPIRequest<Label>(`projects/${projectId}/labels`, {
data: {
color: _color,
description,
name,
},
method: 'POST',
}) as Promise<Label>;
});
}
/**
@@ -188,9 +188,9 @@ export class Api {
* @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}`, {
return this.makeGitLabAPIRequest<void>(`projects/${projectId}/milestones?title=${title}`, {
method: 'POST',
}) as Promise<void>;
});
}
/**
@@ -202,12 +202,12 @@ export class Api {
* @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`, {
return this.makeGitLabAPIRequest<void>(`projects/${projectId}/${scope}/${iid}/notes`, {
data: {
body,
},
method: 'POST',
}) as Promise<void>;
});
}
/**
@@ -217,9 +217,9 @@ export class Api {
* @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}`, {
return this.makeGitLabAPIRequest<void>(`projects/${projectId}/labels?name=${name}`, {
method: 'DELETE',
}) as Promise<void>;
});
}
/**
@@ -230,7 +230,7 @@ export class Api {
* @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>;
return this.makeGitLabAPIRequest<void>(`${scope}/${id}/members/${userId}`, {method: 'DELETE'});
}
/**
@@ -245,7 +245,7 @@ export class Api {
newValues.color = undefined;
}
return this.makeGitLabAPIRequest(`projects/${projectId}/labels`, {
return this.makeGitLabAPIRequest<Label>(`projects/${projectId}/labels`, {
data: {
color: newValues.color,
description: newValues.description,
@@ -253,7 +253,7 @@ export class Api {
new_name: newValues.name,
},
method: 'POST',
}) as Promise<Label>;
});
}
/**
@@ -270,13 +270,13 @@ export class Api {
userId: number,
accessLevel: AccessLevel,
): Promise<Member> {
return this.makeGitLabAPIRequest(`${scope}/${id}/members`, {
return this.makeGitLabAPIRequest<Member>(`${scope}/${id}/members`, {
data: {
access_level: accessLevel,
user_id: userId,
},
method: 'PUT',
}) as Promise<Member>;
});
}
/**
@@ -285,7 +285,7 @@ export class Api {
* @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[]>;
return this.makeGitLabAPIRequest<Branch[]>(`projects/${projectId}/repository/branches`);
}
/**
@@ -302,7 +302,7 @@ export class Api {
return this.makeGitLabAPIRequest(
`projects/${projectId}/repository/files/${fileIdentifier}`,
) as Promise<unknown>;
);
}
/**
@@ -311,7 +311,7 @@ export class Api {
* @param projectId ID of the project
*/
public async getFileList(projectId: number): Promise<TreeFile[]> {
return this.makeGitLabAPIRequest(`projects/${projectId}/repository/tree`) as Promise<TreeFile[]>;
return this.makeGitLabAPIRequest<TreeFile[]>(`projects/${projectId}/repository/tree`);
}
/**
@@ -343,7 +343,7 @@ export class Api {
// divider = '&';
}
return this.makeGitLabAPIRequest(requestUrl) as Promise<Issue[]>;
return this.makeGitLabAPIRequest<Issue[]>(requestUrl);
}
/**
@@ -352,7 +352,7 @@ export class Api {
* @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[]>;
return this.makeGitLabAPIRequest<Label[]>(`projects/${projectId}/labels`);
}
/**
@@ -362,7 +362,7 @@ export class Api {
* @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[]>;
return this.makeGitLabAPIRequest<Member[]>(`${scope}/${id}/members`);
}
/**
@@ -375,9 +375,9 @@ export class Api {
projectId: number,
mergeRequestIid: number,
): Promise<MergeRequestApproval> {
return this.makeGitLabAPIRequest(
return this.makeGitLabAPIRequest<MergeRequestApproval>(
`/projects/${projectId}/merge_requests/${mergeRequestIid}/approvals`,
) as Promise<MergeRequestApproval>;
);
}
/**
@@ -387,9 +387,9 @@ export class Api {
* @param mergeRequestIid IID of the merge request
*/
public async getMergeRequestDiscussions(projectId: number, mergeRequestIid: number): Promise<Discussion[]> {
return this.makeGitLabAPIRequest(
return this.makeGitLabAPIRequest<Discussion[]>(
`projects/${projectId}/merge_requests/${mergeRequestIid}/discussions`,
) as Promise<Discussion[]>;
);
}
/**
@@ -411,9 +411,7 @@ export class Api {
_state = state.join(',') as MergeRequestState;
}
return this.makeGitLabAPIRequest(`${scope}/${id}/merge_requests?state=${_state}`) as Promise<
MergeRequest[]
>;
return this.makeGitLabAPIRequest<MergeRequest[]>(`${scope}/${id}/merge_requests?state=${_state}`);
}
/**
@@ -422,7 +420,7 @@ export class Api {
* @param projectId Project ID to get milestones for
*/
public async getMilestonesForProject(projectId: number): Promise<Milestone[]> {
return this.makeGitLabAPIRequest(`projects/${projectId}/milestones`) as Promise<Milestone[]>;
return this.makeGitLabAPIRequest<Milestone[]>(`projects/${projectId}/milestones`);
}
/**
@@ -432,9 +430,7 @@ export class Api {
* @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[]
>;
return this.makeGitLabAPIRequest<Note[]>(`/projects/${projectId}/issues/${issue.iid}/notes?sort=asc`);
}
/**
@@ -443,7 +439,7 @@ export class Api {
* @param groupId Group ID to get projects for
*/
public async getProjectsForGroup(groupId: number): Promise<Project[]> {
return this.makeGitLabAPIRequest(`groups/${groupId}/projects`) as Promise<Project[]>;
return this.makeGitLabAPIRequest<Project[]>(`groups/${groupId}/projects`);
}
/**
@@ -452,7 +448,7 @@ export class Api {
* @param groupId Group ID to get subgroups for
*/
public async getSubGroupsForGroup(groupId: number): Promise<Group[]> {
return this.makeGitLabAPIRequest(`/groups/${groupId}/subgroups`) as Promise<Group[]>;
return this.makeGitLabAPIRequest<Group[]>(`/groups/${groupId}/subgroups`);
}
/**
@@ -461,7 +457,7 @@ export class Api {
* @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[]>;
return this.makeGitLabAPIRequest<Tag[]>(`projects/${projectId}/repository/tags`);
}
/**
@@ -470,7 +466,7 @@ export class Api {
* @param url GitLab API URL to query
* @param options HTTP method/verb
*/
public async makeGitLabAPIRequest(url: string, options?: ApiRequestOptions): Promise<unknown> {
public async makeGitLabAPIRequest<T = unknown>(url: string, options?: ApiRequestOptions): Promise<T> {
// remove leading slash
const _url = url.replace(/^\/+/g, '');
@@ -503,50 +499,26 @@ export class Api {
);
}
let body;
let tries = 0;
const requestUrl = `${_url}${concatenator}page=${currentPage}&per_page=100`;
while (body === undefined && tries++ < _options.tries) {
try {
const requestUrl = `${_url}${concatenator}page=${currentPage}&per_page=100`;
const body = await got(`${this.rootUrl}${requestUrl}`, {
form: _options.data === null ? undefined : _options.data,
headers: {'PRIVATE-TOKEN': this.privateToken},
method: _options.method,
retry: {
limit: _options.tries,
},
}).on('response', response => {
const xTotalPages = response.headers['x-total-pages'];
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 (typeof xTotalPages === 'string') {
totalPages = Number.parseInt(xTotalPages, 10);
}
}
}).json<unknown[]>();
if (_options.method === 'DELETE') {
return;
return (void 0) as unknown as T;
}
apiResult =
@@ -555,7 +527,7 @@ export class Api {
: (apiResult = body);
}
return apiResult;
return apiResult as T;
}
/**
@@ -565,12 +537,12 @@ export class Api {
* @param branch Branch to protect
*/
public async protectBranch(projectId: number, branch: string): Promise<Branch> {
return this.makeGitLabAPIRequest(
return this.makeGitLabAPIRequest<Branch>(
`projects/${projectId}/repository/branches/${branch}/protect?developers_can_push=false&developers_can_merge=false`,
{
method: 'PUT',
},
) as Promise<Branch>;
);
}
/**
@@ -580,12 +552,12 @@ export class Api {
* @param userId ID of the assignee to set for the issue
*/
public async setAssigneeForIssue(issue: Issue, userId: number): Promise<Issue> {
return this.makeGitLabAPIRequest(
return this.makeGitLabAPIRequest<Issue>(
`projects/${issue.project_id}/issues/${issue.iid}?assignee_ids=${userId}`,
{
method: 'PUT',
},
) as Promise<Issue>;
);
}
/**
@@ -595,12 +567,12 @@ export class Api {
* @param userId ID of the assignee to set for the merge request
*/
public async setAssigneeForMergeRequest(mergeRequest: MergeRequest, userId: number): Promise<MergeRequest> {
return this.makeGitLabAPIRequest(
return this.makeGitLabAPIRequest<MergeRequest>(
`projects/${mergeRequest.project_id}/merge_requests/${mergeRequest.iid}?assignee_ids=${userId}`,
{
method: 'PUT',
},
) as Promise<MergeRequest>;
);
}
/**
@@ -611,16 +583,16 @@ export class Api {
*/
public async setMilestoneForIssue(issue: Issue, milestoneId: number): Promise<Issue> {
if (milestoneId === null) {
return this.makeGitLabAPIRequest(`projects/${issue.project_id}/issues/${issue.iid}?milestone_id=`, {
return this.makeGitLabAPIRequest<Issue>(`projects/${issue.project_id}/issues/${issue.iid}?milestone_id=`, {
method: 'PUT',
}) as Promise<Issue>;
});
}
return this.makeGitLabAPIRequest(
return this.makeGitLabAPIRequest<Issue>(
`projects/${issue.project_id}/issues/${issue.iid}?milestone_id=${milestoneId}`,
{
method: 'PUT',
},
) as Promise<Issue>;
);
}
}

View File

@@ -12,7 +12,6 @@
* 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';
@@ -46,7 +45,7 @@ commander.command('request <call> [method] [data]').action(async (call, method,
options.data = JSON.parse(data);
}
const api = new Api(commander.url, commander.token);
const api = new Api(commander.getOptionValue('url'), commander.getOptionValue('token'));
const result = await api.makeGitLabAPIRequest(call, options);
@@ -59,7 +58,7 @@ commander.command('batch-process <projectId> <action>').action(async (projectId,
await Logger.error('Only "close" is supported as action.');
}
const api = new Api(commander.url, commander.token);
const api = new Api(commander.getOptionValue('url'), commander.getOptionValue('token'));
const issues = (await api.makeGitLabAPIRequest(`/projects/${projectId}/issues?state=opened`, {
retryOnAnyError: true,
@@ -68,7 +67,7 @@ commander.command('batch-process <projectId> <action>').action(async (projectId,
Logger.log(`Fetched ${issues.length} issue(s).`);
await asyncPool(5, issues, async issue => {
for (const issue of issues) {
if (action === 'close') {
Logger.info(`Closing issue #${issue.iid} of project '${projectId}': ${issue.title}.`);
@@ -82,7 +81,7 @@ commander.command('batch-process <projectId> <action>').action(async (projectId,
});
}
Logger.info(`Processed issue #${issue.iid} of project '${projectId}': ${issue.title}`);
});
}
Logger.ok('Processed all issues.');
});
@@ -90,7 +89,7 @@ commander.command('batch-process <projectId> <action>').action(async (projectId,
commander
.command('copy <projectId> <targetUrl> <targetToken> <targetProjectId>')
.action(async (projectId, targetUrl, targetToken, targetProjectId) => {
const api = new Api(commander.url, commander.token);
const api = new Api(commander.getOptionValue('url'), commander.getOptionValue('token'));
const targetApi = new Api(targetUrl, targetToken);
// get all issues from project
@@ -109,7 +108,7 @@ commander
let index = 0;
await asyncPool(2, issues, async issue => {
for (const issue of issues) {
// get notes of old issue
const notes = await api.getNotes(projectId, issue);
@@ -168,12 +167,13 @@ ${note.body}`,
}
Logger.log(`Finished issue ${++index} of ${issues.length}.`);
});
}
});
commander.parse(process.argv);
if (typeof commander.token !== 'string' || commander.token.length === 0) {
const token = commander.getOptionValue('token')
if (typeof token !== 'string' || token.length === 0) {
Logger.warn(
'You probably want to supply a GitLab token either via option or environment variable (GITLAB_PRIVATE_TOKEN).',
);