diff --git a/src/api.ts b/src/api.ts index e907378b..5debb68d 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018 StApps + * 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. @@ -19,15 +19,23 @@ import { Branch, Group, Issue, + IssueState, Label, Member, + MembershipScope, MergeRequest, + MergeRequestApproval, + MergeRequestState, Milestone, Project, + Scope, Tag, TreeFile, } from './types'; +/** + * Instance of logger + */ export const logger = new Logger(); /** @@ -59,8 +67,8 @@ export interface ApiRequestOptions { */ export interface ApiGetIssuesOptions { groupId?: number; - milestone?: 'Meeting' | 'Backlog' | 'No Milestone'; - state?: 'opened' | 'closed'; + milestone?: 'Backlog' | 'No Milestone'; + state?: IssueState; } /** @@ -102,17 +110,17 @@ export class Api { /** * Add member to 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 * @param userId ID of the user * @param accessLevel Access level for the new member in the scope */ - public addMember(scope: 'groups' | 'projects', + public addMember(scope: MembershipScope, id: number, userId: number, accessLevel: AccessLevel): Promise { return this.makeGitLabAPIRequest( - scope + '/' + id + '/members', + `${scope}/${id}/members`, { data: { access_level: accessLevel, @@ -132,7 +140,7 @@ export class Api { */ public createIssue(projectId: number, title: string, description: string): Promise { return this.makeGitLabAPIRequest( - 'projects/' + projectId + '/issues', + `projects/${projectId}/issues`, { data: { description: description, @@ -157,7 +165,7 @@ export class Api { } return this.makeGitLabAPIRequest( - 'projects/' + projectId + '/labels', + `projects/${projectId}/labels`, { data: { color: color, @@ -215,12 +223,12 @@ export class Api { /** * Delete a member from 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 * @param userId ID of the user */ - public deleteMember(scope: 'groups' | 'projects', id: number, userId: number): Promise { - return this.makeGitLabAPIRequest(scope + '/' + id + '/members/' + userId, {method: 'DELETE'}); + public deleteMember(scope: MembershipScope, id: number, userId: number): Promise { + return this.makeGitLabAPIRequest(`${scope}/${id}/members/${userId}`, {method: 'DELETE'}); } /** @@ -236,7 +244,7 @@ export class Api { } return this.makeGitLabAPIRequest( - 'projects/' + projectId + '/labels', + `projects/${projectId}/labels`, { data: { color: newValues.color, @@ -252,17 +260,17 @@ export class Api { /** * Edit member in 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 * @param userId ID of the user * @param accessLevel Access level for the member in the scope */ - public editMember(scope: 'groups' | 'projects', + public editMember(scope: MembershipScope, id: number, userId: number, accessLevel: AccessLevel): Promise { return this.makeGitLabAPIRequest( - scope + '/' + id + '/members', + `${scope}/${id}/members`, { data: { access_level: accessLevel, @@ -279,7 +287,7 @@ export class Api { * @param projectId Project ID to get branches for */ public getBranchesForProject(projectId: number): Promise { - return this.makeGitLabAPIRequest('projects/' + projectId + '/repository/branches'); + return this.makeGitLabAPIRequest(`projects/${projectId}/repository/branches`); } /** @@ -291,7 +299,7 @@ export class Api { */ public getFile(projectId: number, filePath: string, commitish: string): Promise { return this.makeGitLabAPIRequest( - 'projects/' + projectId + '/repository/files/' + + `projects/${projectId}/repository/files/` + encodeURIComponent(filePath).replace('.', '%2E') + '/raw?ref=' + encodeURIComponent(commitish), ); } @@ -302,7 +310,7 @@ export class Api { * @param projectId ID of the project */ public getFileList(projectId: number): Promise { - return this.makeGitLabAPIRequest('projects/' + projectId + '/repository/tree'); + return this.makeGitLabAPIRequest(`projects/${projectId}/repository/tree`); } /** @@ -375,7 +383,7 @@ export class Api { * @param projectId Project ID to get milestones for */ public getMilestonesForProject(projectId: number): Promise { - return this.makeGitLabAPIRequest('projects/' + projectId + '/milestones'); + return this.makeGitLabAPIRequest(`projects/${projectId}/milestones`); } /** @@ -384,7 +392,7 @@ export class Api { * @param groupId Group ID to get projects for */ public getProjectsForGroup(groupId: number): Promise { - return this.makeGitLabAPIRequest('groups/' + groupId + '/projects'); + return this.makeGitLabAPIRequest(`groups/${groupId}/projects`); } /** @@ -403,7 +411,7 @@ export class Api { */ public getTags(projectId: number): Promise { return this.makeGitLabAPIRequest( - 'projects/' + projectId + '/repository/tags', + `projects/${projectId}/repository/tags`, ); } @@ -502,8 +510,8 @@ export class Api { */ public protectBranch(projectId: number, branch: string): Promise { return this.makeGitLabAPIRequest( - 'projects/' + projectId + '/repository/branches/' + branch + '/protect' + - '?developers_can_push=false&developers_can_merge=false', + /* tslint:disable-next-line:max-line-length */ + `projects/${projectId}/repository/branches/${branch}/protect?developers_can_push=false&developers_can_merge=false`, { method: 'PUT', }, @@ -516,9 +524,9 @@ export class Api { * @param issue Issue to set milestone for * @param userId ID of the milestone to set for the issue */ - public setAssigneeForIssue(issue: any, userId: number): Promise { + public setAssigneeForIssue(issue: Issue, userId: number): Promise { return this.makeGitLabAPIRequest( - 'projects/' + issue.project_id + '/issues/' + issue.iid + '?assignee_ids=' + userId, + `projects/${issue.project_id}/issues/${issue.iid}?assignee_ids=${userId}`, { method: 'PUT', }, @@ -531,10 +539,10 @@ export class Api { * @param issue Issue to set milestone for * @param milestoneId ID of the milestone to set for the issue */ - public setMilestoneForIssue(issue: any, milestoneId: number): Promise { + public setMilestoneForIssue(issue: Issue, milestoneId: number): Promise { if (milestoneId === null) { return this.makeGitLabAPIRequest( - 'projects/' + issue.project_id + '/issues/' + issue.iid + '?milestone_id=', + `projects/${issue.project_id}/issues/${issue.iid}?milestone_id=`, { method: 'PUT', }, @@ -542,7 +550,7 @@ export class Api { } return this.makeGitLabAPIRequest( - 'projects/' + issue.project_id + '/issues/' + issue.iid + '?milestone_id=' + milestoneId, + `projects/${issue.project_id}/issues/${issue.iid}?milestone_id=${milestoneId}`, { method: 'PUT', },