Skip to content

Commit

Permalink
fix: Merge Request Approvals API did not match official API
Browse files Browse the repository at this point in the history
Updated the function headers to match the official API.

BREAKING CHANGE: Updated Approvals API support to match https://docs.gitlab.com/ee/api/merge_request_approvals.html
  • Loading branch information
jdalrymple committed May 25, 2019
1 parent 97dd060 commit e4ba731
Showing 1 changed file with 168 additions and 72 deletions.
240 changes: 168 additions & 72 deletions src/services/MergeRequests.ts
Original file line number Diff line number Diff line change
@@ -1,80 +1,142 @@
import { BaseService, RequestHelper } from '../infrastructure';
import { RequestOptions } from '../infrastructure/RequestHelper';

export type MergeRequestId = string | number;

class MergeRequests extends BaseService {
accept(projectId: ProjectId, mergerequestId: MergeRequestId, options: RequestOptions) {
const [pId, mId] = [projectId, mergerequestId].map(encodeURIComponent);
accept(
projectId: ProjectId,
mergerequestIId: MergeRequestId,
options?: AcceptMergeRequestOptions & BaseRequestOptions,
) {
const [pId, mIId] = [projectId, mergerequestIId].map(encodeURIComponent);

return RequestHelper.put(this, `projects/${pId}/merge_requests/${mId}/merge`, options);
return RequestHelper.put(this, `projects/${pId}/merge_requests/${mIId}/merge`, options);
}

addSpentTime(projectId: ProjectId, mergerequestId: MergeRequestId, duration: Duration) {
const [pId, mId] = [projectId, mergerequestId].map(encodeURIComponent);
addSpentTime(
projectId: ProjectId,
mergerequestIId: MergeRequestId,
duration: string,
options?: Sudo,
) {
const [pId, mIId] = [projectId, mergerequestIId].map(encodeURIComponent);

return RequestHelper.post(this, `projects/${pId}/issues/${mId}/add_spent_time`, {
return RequestHelper.post(this, `projects/${pId}/merge_requests/${mIId}/add_spent_time`, {
duration,
...options,
});
}

addTimeEstimate(projectId: ProjectId, mergerequestId: MergeRequestId, duration: Duration) {
const [pId, mId] = [projectId, mergerequestId].map(encodeURIComponent);
addTimeEstimate(
projectId: ProjectId,
mergerequestIId: MergeRequestId,
duration: string,
options?: Sudo,
) {
const [pId, mIId] = [projectId, mergerequestIId].map(encodeURIComponent);

return RequestHelper.post(this, `projects/${pId}/issues/${mId}/time_estimate`, {
return RequestHelper.post(this, `projects/${pId}/merge_requests/${mIId}/time_estimate`, {
duration,
...options,
});
}

approve(projectId: ProjectId, mergerequestId: MergeRequestId, { sha }: { sha: string }) {
const [pId, mId] = [projectId, mergerequestId].map(encodeURIComponent);
all({
projectId,
groupId,
...options
}: ({ projectId: ProjectId } | { groupId: GroupId } | {}) & PaginatedRequestOptions) {
let url;

if (projectId) {
url = `projects/${encodeURIComponent(projectId)}/merge_requests`;
} else if (groupId) {
url = `groups/${encodeURIComponent(groupId)}/merge_requests`;
} else {
url = 'merge_requests';
}

return RequestHelper.post(this, `projects/${pId}/merge_requests/${mId}/approve`, { sha });
return RequestHelper.get(this, url, options);
}

approvals(projectId: ProjectId, { mergerequestId }: { mergerequestId?: MergeRequestId } = {}) {
const pId = encodeURIComponent(projectId);
const mergeRequest = mergerequestId ? `merge_requests/${encodeURIComponent(mergerequestId)}` : '';
approve(
projectId: ProjectId,
mergerequestIId: MergeRequestId,
options: { sha?: string } & BaseRequestOptions,
) {
const [pId, mIId] = [projectId, mergerequestIId].map(encodeURIComponent);

return RequestHelper.get(this, `projects/${pId}/${mergeRequest}/approvals`);
return RequestHelper.post(this, `projects/${pId}/merge_requests/${mIId}/approve`, options);
}

all({ projectId, ...options }: { projectId?: ProjectId } = {}) {
const url = projectId ? `projects/${encodeURIComponent(projectId)}/merge_requests` : 'merge_requests';
approvals(
projectId: ProjectId,
{ mergerequestIId, ...options }: { mergerequestIId?: MergeRequestId } & BaseRequestOptions,
) {
const [pId, mIId] = [projectId, mergerequestIId].map(encodeURIComponent);

let url;

if (mergerequestIId) {
url = `projects/${pId}/merge_requests/${mIId}/approvals`;
} else {
url = `projects/${pId}/approvals`;
}

return RequestHelper.get(this, url, options);
}

cancelOnPipelineSucess(projectId: ProjectId, mergerequestId: MergeRequestId) {
const [pId, mId] = [projectId, mergerequestId].map(encodeURIComponent);
approvers(
projectId: ProjectId,
approverIds: UserId[],
approverGroupIds: GroupId[],
{ mergerequestIId, ...options }: { mergerequestIId?: MergeRequestId } & BaseRequestOptions,
) {
const [pId, mIId] = [projectId, mergerequestIId].map(encodeURIComponent);

let url;

if (mergerequestIId) {
url = `projects/${pId}/merge_requests/${mIId}/approvers`;
} else {
url = `projects/${pId}/approvers`;
}

return RequestHelper.put(this, `projects/${pId}/merge_requests/${mId}/cancel_merge_when_pipeline_succeeds`);
return RequestHelper.put(this, url, { approverIds, approverGroupIds, ...options });
}

changes(projectId: ProjectId, mergerequestId: MergeRequestId) {
const [pId, mId] = [projectId, mergerequestId].map(encodeURIComponent);
cancelOnPipelineSucess(projectId: ProjectId, mergerequestIId: MergeRequestId, options?: Sudo) {
const [pId, mIId] = [projectId, mergerequestIId].map(encodeURIComponent);

return RequestHelper.get(this, `projects/${pId}/merge_requests/${mId}/changes`);
return RequestHelper.put(
this,
`projects/${pId}/merge_requests/${mIId}/cancel_merge_when_pipeline_succeeds`,
options,
);
}

closesIssues(projectId: ProjectId, mergerequestId: MergeRequestId) {
const [pId, mId] = [projectId, mergerequestId].map(encodeURIComponent);
changes(projectId: ProjectId, mergerequestIId: MergeRequestId, options?: Sudo) {
const [pId, mIId] = [projectId, mergerequestIId].map(encodeURIComponent);

return RequestHelper.get(this, `projects/${pId}/merge_requests/${mId}/closes_issues`);
return RequestHelper.get(this, `projects/${pId}/merge_requests/${mIId}/changes`, options);
}

commits(projectId: ProjectId, mergerequestId: MergeRequestId) {
const [pId, mId] = [projectId, mergerequestId].map(encodeURIComponent);
closesIssues(projectId: ProjectId, mergerequestIId: MergeRequestId, options?: Sudo) {
const [pId, mIId] = [projectId, mergerequestIId].map(encodeURIComponent);

return RequestHelper.get(this, `projects/${pId}/merge_requests/${mId}/commits`);
return RequestHelper.get(this, `projects/${pId}/merge_requests/${mIId}/closes_issues`, options);
}

commits(projectId: ProjectId, mergerequestIId: MergeRequestId, options?: Sudo) {
const [pId, mIId] = [projectId, mergerequestIId].map(encodeURIComponent);

return RequestHelper.get(this, `projects/${pId}/merge_requests/${mIId}/commits`, options);
}

create(
projectId: ProjectId,
sourceBranch: string,
targetBranch: string,
title: string,
options: RequestOptions,
options?: CreateMergeRequestOptions & BaseRequestOptions,
) {
const pId = encodeURIComponent(projectId);

Expand All @@ -87,24 +149,37 @@ class MergeRequests extends BaseService {
});
}

edit(projectId: ProjectId, mergerequestId: MergeRequestId, options: RequestOptions) {
const [pId, mId] = [projectId, mergerequestId].map(encodeURIComponent);
edit(
projectId: ProjectId,
mergerequestIId: MergeRequestId,
options?: UpdateMergeRequestOptions & BaseRequestOptions,
) {
const [pId, mIId] = [projectId, mergerequestIId].map(encodeURIComponent);

return RequestHelper.put(this, `projects/${pId}/merge_requests/${mId}`, options);
return RequestHelper.put(this, `projects/${pId}/merge_requests/${mIId}`, options);
}

editApprovals(projectId: ProjectId, { mergerequestId, ...options }: temporaryAny) {
const pId = encodeURIComponent(projectId);
const mergeRequest = mergerequestId ? `merge_requests/${encodeURIComponent(mergerequestId)}/` : '';
editApprovals(
projectId: ProjectId,
{ mergerequestIId, ...options }: { mergerequestIId?: MergeRequestId } & BaseRequestOptions,
) {
const [pId, mIId] = [projectId, mergerequestIId].map(encodeURIComponent);

let url;

return RequestHelper.post(this, `projects/${pId}/${mergeRequest}approvals`, options);
if (mergerequestIId) {
url = `projects/${pId}/merge_requests/${mIId}/approvals`;
} else {
url = `projects/${pId}/approvals`;
}

return RequestHelper.post(this, url, options);
}

editApprovers(projectId: ProjectId, { mergerequestId, ...options }: temporaryAny) {
const pId = encodeURIComponent(projectId);
const mergeRequest = mergerequestId ? `merge_requests/${encodeURIComponent(mergerequestId)}/` : '';
participants(projectId: ProjectId, mergerequestIId: MergeRequestId, options?: Sudo) {
const [pId, mIId] = [projectId, mergerequestIId].map(encodeURIComponent);

return RequestHelper.put(this, `projects/${pId}/${mergeRequest}approvers`, options);
return RequestHelper.get(this, `projects/${pId}/merge_requests/${mIId}/participants`, options);
}

pipelines(projectId: ProjectId, { mergerequestId }: { mergerequestId?: string } = {}) {
Expand All @@ -114,58 +189,79 @@ class MergeRequests extends BaseService {
return RequestHelper.get(this, `projects/${pId}/${mergeRequest}/pipelines`);
}

remove(projectId: ProjectId, mergerequestId: MergeRequestId) {
const [pId, mId] = [projectId, mergerequestId].map(encodeURIComponent);
remove(projectId: ProjectId, mergerequestIId: MergeRequestId, options?: Sudo) {
const [pId, mIId] = [projectId, mergerequestIId].map(encodeURIComponent);

return RequestHelper.delete(this, `projects/${pId}/merge_requests/${mId}`);
return RequestHelper.del(this, `projects/${pId}/merge_requests/${mIId}`, options);
}

resetSpentTime(projectId: ProjectId, mergerequestId: MergeRequestId) {
const [pId, mId] = [projectId, mergerequestId].map(encodeURIComponent);
resetSpentTime(projectId: ProjectId, mergerequestIId: MergeRequestId, options?: Sudo) {
const [pId, mIId] = [projectId, mergerequestIId].map(encodeURIComponent);

return RequestHelper.post(this, `projects/${pId}/merge_requests/${mId}/reset_spent_time`);
return RequestHelper.post(
this,
`projects/${pId}/merge_requests/${mIId}/reset_spent_time`,
options,
);
}

resetTimeEstimate(projectId: ProjectId, mergerequestId: MergeRequestId) {
const [pId, mId] = [projectId, mergerequestId].map(encodeURIComponent);
resetTimeEstimate(projectId: ProjectId, mergerequestIId: MergeRequestId, options?: Sudo) {
const [pId, mIId] = [projectId, mergerequestIId].map(encodeURIComponent);

return RequestHelper.post(this, `projects/${pId}/merge_requests/${mId}/reset_time_estimate`);
return RequestHelper.post(
this,
`projects/${pId}/merge_requests/${mIId}/reset_time_estimate`,
options,
);
}

show(projectId: ProjectId, mergerequestId: MergeRequestId) {
const [pId, mId] = [projectId, mergerequestId].map(encodeURIComponent);
show(
projectId: ProjectId,
mergerequestIId: MergeRequestId,
options?: ShowMergeRequestOptions & BaseRequestOptions,
) {
const [pId, mIId] = [projectId, mergerequestIId].map(encodeURIComponent);

return RequestHelper.get(this, `projects/${pId}/merge_requests/${mId}`);
return RequestHelper.get(this, `projects/${pId}/merge_requests/${mIId}`, options);
}

timeStats(projectId: ProjectId, mergerequestId: MergeRequestId) {
const [pId, mId] = [projectId, mergerequestId].map(encodeURIComponent);
timeStats(projectId: ProjectId, mergerequestIId: MergeRequestId, options?: Sudo) {
const [pId, mIId] = [projectId, mergerequestIId].map(encodeURIComponent);

return RequestHelper.get(this, `projects/${pId}/merge_requests/${mId}/time_stats`);
return RequestHelper.get(this, `projects/${pId}/merge_requests/${mIId}/time_stats`, options);
}

version(projectId: ProjectId, mergerequestId: MergeRequestId, versionId: string | number) {
const [pId, mId, vId] = [projectId, mergerequestId, versionId].map(encodeURIComponent);
version(
projectId: ProjectId,
mergerequestIId: MergeRequestId,
versionId: number,
options?: Sudo,
) {
const [pId, mIId, vId] = [projectId, mergerequestIId, versionId].map(encodeURIComponent);

return RequestHelper.get(this, `projects/${pId}/merge_requests/${mId}/versions/${vId}`);
return RequestHelper.get(
this,
`projects/${pId}/merge_requests/${mIId}/versions/${vId}`,
options,
);
}

versions(projectId: ProjectId, mergerequestId: MergeRequestId) {
const [pId, mId] = [projectId, mergerequestId].map(encodeURIComponent);
versions(projectId: ProjectId, mergerequestIId: MergeRequestId, options?: Sudo) {
const [pId, mIId] = [projectId, mergerequestIId].map(encodeURIComponent);

return RequestHelper.get(this, `projects/${pId}/merge_requests/${mId}/versions`);
return RequestHelper.get(this, `projects/${pId}/merge_requests/${mIId}/versions`, options);
}

unapprove(projectId: ProjectId, mergerequestId: MergeRequestId) {
const [pId, mId] = [projectId, mergerequestId].map(encodeURIComponent);
unapprove(projectId: ProjectId, mergerequestIId: MergeRequestId, options?: Sudo) {
const [pId, mIId] = [projectId, mergerequestIId].map(encodeURIComponent);

return RequestHelper.post(this, `projects/${pId}/merge_requests/${mId}/approve`);
return RequestHelper.post(this, `projects/${pId}/merge_requests/${mIId}/approve`, options);
}

unsubscribe(projectId: ProjectId, mergerequestId: MergeRequestId) {
const [pId, mId] = [projectId, mergerequestId].map(encodeURIComponent);
unsubscribe(projectId: ProjectId, mergerequestIId: MergeRequestId, options?: Sudo) {
const [pId, mIId] = [projectId, mergerequestIId].map(encodeURIComponent);

return RequestHelper.delete(this, `projects/${pId}/merge_requests/${mId}/unsubscribe`);
return RequestHelper.del(this, `projects/${pId}/merge_requests/${mIId}/unsubscribe`, options);
}
}

Expand Down

0 comments on commit e4ba731

Please sign in to comment.