Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

feat: add/remove assignees for gitlab #220

Merged
merged 1 commit into from
Oct 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/provider/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Git } from '../git';
import { Tokens } from '../workflow-manager';
import { Repository } from './repository';
import { User } from './user';

import { GithubClient } from './github/client';
import { GitLabClient } from './gitlab/client';
Expand All @@ -26,6 +27,8 @@ export interface Client {

getRepository(rid: string): Promise<Response<Repository>>;

getUserByUsername(username: string): Promise<Response<User>>;

}

export interface Response<T> {
Expand Down
7 changes: 7 additions & 0 deletions src/provider/github/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {

import { GitHub, getClient } from './index';
import { GithubRepository } from './repository';
import { GithubUser } from './user';

export class GithubClient implements Client {

Expand Down Expand Up @@ -34,4 +35,10 @@ export class GithubClient implements Client {
};
}

public async getUserByUsername(username: string): Promise<Response<GithubUser>> {
const response = await this.client.getUser(username);
return {
body: new GithubUser(this.client, response.body)
};
}
}
10 changes: 9 additions & 1 deletion src/provider/github/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export interface GitHub {
getPullRequestComments(owner: string, repo: string, number: number): Promise<GitHubResponse<PullRequestComment[]>>;

editIssue(owner: string, repo: string, number: number, body: EditIssueBody): Promise<GitHubResponse<EditIssueBody>>;

getUser(username: string): Promise<GitHubResponse<UserResponse>>;
}

export interface GitHubResponse<T> {
Expand All @@ -39,6 +41,11 @@ export interface GitHubResponse<T> {
body: T;
}

export interface UserResponse {
id: number;
login: string;
}

export interface EditIssueBody {
state?: 'open' | 'closed';
assignees?: string[];
Expand Down Expand Up @@ -231,7 +238,6 @@ namespace impl {
}

export class GitHubBlueprint implements GitHub {

@Headers('Accept: application/vnd.github.polaris-preview')
@Get('/repos/:owner/:repo')
public getRepository(): any {/* */}
Expand Down Expand Up @@ -273,6 +279,8 @@ namespace impl {
@Patch('/repos/:owner/:repo/issues/:number')
public editIssue(): any {/* */}

@Get('/users/:username')
public getUser(): any {/* */}
}

}
2 changes: 1 addition & 1 deletion src/provider/github/pull-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export class GithubPullRequest implements PullRequest {
this.repository.repository,
this.number,
{
assignees: assignees.map(assignee => assignee.id)
assignees: assignees.map(assignee => assignee.username)
}
);
}
Expand Down
20 changes: 18 additions & 2 deletions src/provider/github/user.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
import { User } from '../user';
import { GitHub, UserResponse } from './index';

export class GithubUser implements User<string> {
export class GithubUser implements User {

public id: string;
private client: GitHub;

private struct: UserResponse;

public get id(): number {
return this.struct.id;
}

public get username(): string {
return this.struct.login;
}

constructor(client: GitHub, struct: UserResponse) {
this.client = client;
this.struct = struct;
}

}
40 changes: 37 additions & 3 deletions src/provider/gitlab/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ import {
IPretendRequestInterceptor,
IPretendDecoder,
Get,
Post
Post,
Put
} from 'pretend';

export interface GitLab {
getProject(id: string): Promise<GitLabResponse<Project>>;
getMergeRequests(id: string, parameters?: GetMergeRequestParameters): Promise<GitLabResponse<MergeRequest[]>>;
getMergeRequest(id: string, mr_iid: number): Promise<GitLabResponse<MergeRequest>>;
createMergeRequest(id: string, body: CreateMergeRequestBody): Promise<GitLabResponse<MergeRequest>>;
updateMergeRequest(id: string, mr_iid: number, body: UpdateMergeRequestBody): Promise<GitLabResponse<MergeRequest>>;
getProjectIssues(id: string, body: ProjectIssuesBody): Promise<GitLabResponse<Issue[]>>;
searchUser(parameters?: SearchUsersParameters): Promise<GitLabResponse<UserResponse[]>>;
}

export interface GitLabResponse<T> {
Expand All @@ -21,6 +24,15 @@ export interface GitLabResponse<T> {
body: T;
}

export interface SearchUsersParameters {
username?: string;
}

export interface UserResponse {
id: number;
username: string;
}

export interface ProjectIssuesBody {
state?: 'opened' | 'closed';
order_by?: 'created_at' | 'updated_at';
Expand All @@ -41,6 +53,16 @@ export interface CreateMergeRequestBody {
remove_source_branch?: boolean;
}

export interface UpdateMergeRequestBody {
target_branch?: string;
title?: string;
description?: string;
state_event?: 'close' | 'reopen';
assignee_id?: number;
remove_source_branch?: boolean;
squash?: boolean;
}

export interface GetMergeRequestParameters {
state?: 'opened' | 'closed' | 'merged';
order_by?: 'created_at' | 'updated_at';
Expand Down Expand Up @@ -108,8 +130,8 @@ namespace impl {

export function formEncoding(): IPretendRequestInterceptor {
return request => {
if (request.options.method === 'POST') {
request.options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
if (request.options.method !== 'GET') {
request.options.headers.set('Content-Type', 'application/x-www-form-urlencoded');
if (request.options.body) {
const body = JSON.parse(request.options.body);
const encodedBody = Object.keys(body)
Expand Down Expand Up @@ -143,16 +165,28 @@ namespace impl {
}

export class GitLabBlueprint implements GitLab {

@Get('/users', true)
public searchUser(): any {/* */}

@Get('/projects/:id')
public getProject(): any {/* */}

@Get('/projects/:id/merge_requests', true)
public getMergeRequests(): any {/* */}

@Get('/projects/:id/merge_requests/:merge_request_iid')
public getMergeRequest(): any {/* */}

@Post('/projects/:id/merge_requests')
public createMergeRequest(): any {/* */}

@Put('/projects/:id/merge_requests/:merge_request_iid')
public updateMergeRequest(): any {/* */}

@Get('/projects/:id/issues')
public getProjectIssues(): any {/* */}

}

}
10 changes: 10 additions & 0 deletions src/provider/gitlab/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Client, Response } from '../client';
import { getClient, GitLab } from './api';
import { GitLabRepository } from './repository';
import { GitLabUser } from './user';

export class GitLabClient implements Client {

Expand All @@ -23,4 +24,13 @@ export class GitLabClient implements Client {
};
}

public async getUserByUsername(username: string): Promise<Response<GitLabUser>> {
const response = await this.client.searchUser({
username
});
return {
body: new GitLabUser(this.client, response.body[0])
};
}

}
19 changes: 16 additions & 3 deletions src/provider/gitlab/merge-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,25 @@ export class GitLabMergeRequest implements PullRequest {
throw new Error('Method not implemented.');
}

public async assign(_assignees: GitLabUser[]): Promise<void> {
throw new Error('Method not implemented.');
public async assign(assignees: GitLabUser[]): Promise<void> {
await this.client.updateMergeRequest(
encodeURIComponent(this.repository.pathWithNamespace),
this.mergeRequest.iid,
{
assignee_id: assignees[0].id
}
);
}

public async unassign(): Promise<void> {
throw new Error('Method not implemented.');
// note: assign to '0'
await this.client.updateMergeRequest(
encodeURIComponent(this.repository.pathWithNamespace),
this.mergeRequest.iid,
{
assignee_id: 0
}
);
}

public async requestReview(_body: RequestReviewBody): Promise<void> {
Expand Down
22 changes: 20 additions & 2 deletions src/provider/gitlab/user.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import { User } from '../user';
import { GitLab, UserResponse } from './api';

export class GitLabUser implements User {

private client: GitLab;

private struct: UserResponse;

public get id(): number {
return this.struct.id;
}

public get username(): string {
return this.struct.username;
}

constructor(client: GitLab, struct: UserResponse) {
this.client = client;
this.struct = struct;
}

export class GitLabUser implements User<number> {
public id: number;
}
2 changes: 1 addition & 1 deletion src/provider/pull-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface PullRequest {

getComments(): Promise<Response<Comment[]>>;
merge(body: MergeBody): Promise<Response<MergeResult>>;
assign(assignees: User<any>[]): Promise<void>;
assign(assignees: User[]): Promise<void>;
unassign(): Promise<void>;
requestReview(body: RequestReviewBody): Promise<void>;
cancelReview(body: CancelReviewBody): Promise<void>;
Expand Down
5 changes: 3 additions & 2 deletions src/provider/user.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export interface User<ID> {
id: ID;
export interface User {
id: number;
username: string;
}
3 changes: 2 additions & 1 deletion src/workflow-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ export class WorkflowManager {
}

public async addAssignee(pullRequest: PullRequest, name: string): Promise<void> {
await pullRequest.assign([{id: name}]);
const user = await this.provider.getUserByUsername(name);
await pullRequest.assign([user.body]);
}

public async removeAssignee(pullRequest: PullRequest): Promise<void> {
Expand Down