From 80fd3aa42ec5922dc6f668b87448cbdc1daf5510 Mon Sep 17 00:00:00 2001 From: Karl-Philipp Wulfert Date: Tue, 5 Feb 2019 15:45:16 +0100 Subject: [PATCH] refactor: adjust existing methods for greater scopes --- src/api.ts | 61 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/src/api.ts b/src/api.ts index b86cb0d0..91dbffd1 100644 --- a/src/api.ts +++ b/src/api.ts @@ -173,25 +173,29 @@ export class Api { * @param title Title of the milestone to create */ public createMilestone(projectId: number, title: string): Promise { - return this.makeGitLabAPIRequest('projects/' + projectId + '/milestones?title=' + title, { + return this.makeGitLabAPIRequest(`projects/${projectId}/milestones?title=${title}`, { method: 'POST', }); } /** - * Create a note/comment in an issue + * Create a note (comment) in an issue/merge request * * @param projectId Project ID, the issue belongs to - * @param issueIid IID of the issue to create the note in + * @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 createNote(projectId: number, issueIid: number, body: string): Promise { - return this.makeGitLabAPIRequest(`projects/${projectId}/issues/${issueIid}/notes`, { - data: { - body, + public createNote(projectId: number, scope: Scope, iid: number, body: string): Promise { + return this.makeGitLabAPIRequest( + `projects/${projectId}/${scope}/${iid}/notes`, + { + data: { + body, + }, + method: 'POST', }, - method: 'POST', - }); + ); } /** @@ -340,30 +344,45 @@ export class Api { * @param projectId ID of the project to get the labels for */ public getLabels(projectId: number): Promise { - return this.makeGitLabAPIRequest( - 'projects/' + projectId + '/labels', - ); + return this.makeGitLabAPIRequest(`projects/${projectId}/labels`); } /** * Get members of a group or a project * - * @param scope Scope of the ID + * @param scope MembershipScope of the ID * @param id ID of the group or project */ - public getMembers(scope: 'groups' | 'projects', id: number): Promise { - return this.makeGitLabAPIRequest( - scope + '/' + id + '/members', - ); + public getMembers(scope: MembershipScope, id: number): Promise { + return this.makeGitLabAPIRequest(`${scope}/${id}/members`); } /** - * Get merge requests for a project + * Get a merge request approval * - * @param projectId Project ID to get merge requests for + * @param projectId ID of the project the merge request belongs to + * @param mergeRequestIid IID of the merge request */ - public getMergeRequestsForProject(projectId: number): Promise { - return this.makeGitLabAPIRequest('projects/' + projectId + '/merge_requests?state=opened'); + public getMergeRequestApproval(projectId: number, mergeRequestIid: number): Promise { + return this.makeGitLabAPIRequest(`/projects/${projectId}/merge_requests/${mergeRequestIid}/approvals`); + } + + /** + * 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 getMergeRequests(scope: MembershipScope, + id: number, + state: MergeRequestState | MergeRequestState[]): Promise { + // 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}`); } /**