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

Commit

Permalink
feat: list merge requests on a project
Browse files Browse the repository at this point in the history
Relates to #166
  • Loading branch information
KnisterPeter committed Sep 23, 2017
1 parent bce63f3 commit 7423018
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 18 deletions.
12 changes: 7 additions & 5 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// Place your settings in this file to overwrite default and user settings.
{
"files.exclude": {
"out": false // set this to true to hide the "out" folder with the compiled JS files
"out": false
},
"search.exclude": {
"out": true // set this to false to include "out" folder in search results
"out": true
},
"typescript.tsdk": "./node_modules/typescript/lib" // we want to use the TS server from our node_modules folder to control its version
}
"typescript.tsdk": "./node_modules/typescript/lib",
"editor.rulers": [
120
]
}
2 changes: 2 additions & 0 deletions src/provider/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export async function createClient(cwd: string, tokens: Tokens): Promise<Client>

export interface Client {

name: string;

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

}
Expand Down
2 changes: 2 additions & 0 deletions src/provider/github/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export class GithubClient implements Client {

private client: GitHub;

public name = 'GitHub Client';

constructor(protocol: string, hostname: string, token: string) {
this.client = getClient(this.getApiEndpoint(protocol, hostname), token);
}
Expand Down
24 changes: 23 additions & 1 deletion src/provider/gitlab/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {

export interface GitLab {
getProject(id: string): Promise<GitLabResponse<Project>>;
getMergeRequests(id: string, parameters?: GetMergeRequestParameters): Promise<GitLabResponse<MergeRequest[]>>;
}

export interface GitLabResponse<T> {
Expand All @@ -16,8 +17,27 @@ export interface GitLabResponse<T> {
body: T;
}

export interface GetMergeRequestParameters {
state?: 'opened' | 'closed' | 'merged';
order_by?: 'created_at' | 'updated_at';
sort?: 'asc' | 'desc';
}

export interface MergeRequest {
id: number;
iid: number;
state: 'opened' | 'closed' | 'merged';
title: string;
description: string;
web_url: string;
target_branch: string;
source_branch: string;
merge_status: 'can_be_merged';
}

export interface Project {
id: number;
path_with_namespace: string;
name: string;
default_branch: string;
web_url: string;
Expand Down Expand Up @@ -65,7 +85,7 @@ namespace impl {
return async response => {
if (response.status >= 400) {
const body = await response.json();
throw new GitLabError(`${body.message || response.statusText}`, response);
throw new GitLabError(`${body.error || response.statusText}`, response);
}
const headers = {};
response.headers.forEach((value, index) => {
Expand All @@ -82,6 +102,8 @@ namespace impl {
export class GitLabBlueprint implements GitLab {
@Get('/projects/:id')
public getProject(): any {/* */}
@Get('/projects/:id/merge_requests', true)
public getMergeRequests(): any {/* */}
}

}
2 changes: 2 additions & 0 deletions src/provider/gitlab/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export class GitLabClient implements Client {

private client: GitLab;

public name = 'GitLab Client';

constructor(protocol: string, hostname: string, token: string) {
this.client = getClient(this.getApiEndpoint(protocol, hostname), token);
}
Expand Down
65 changes: 56 additions & 9 deletions src/provider/gitlab/merge-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,66 @@ import {
CancelReviewBody,
Comment
} from '../pull-request';
import { GitLab, MergeRequest } from './api';
import { GitLabRepository } from './repository';
import { GitLabUser } from './user';

export class GitLabMergeRequest implements PullRequest {

public id: number;
public number: number;
public state: 'open' | 'closed';
public title: string;
public body: string;
public url: string;
public sourceBranch: string;
public targetBranch: string;
public mergeable?: boolean | null | undefined;
private client: GitLab;
private repository: GitLabRepository;
private mergeRequest: MergeRequest;

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

public get number(): number {
return this.mergeRequest.iid;
}

public get state(): 'open' | 'closed' {
switch (this.mergeRequest.state) {
case 'opened':
return 'open';
case 'closed':
case 'merged':
return 'closed';
}
}

public get title(): string {
return this.mergeRequest.title;
}

public get body(): string {
return this.mergeRequest.description;
}

public get url(): string {
return this.mergeRequest.web_url;
}

public get sourceBranch(): string {
return this.mergeRequest.source_branch;
}

public get targetBranch(): string {
return this.mergeRequest.target_branch;
}

public get mergeable(): boolean {
switch (this.mergeRequest.merge_status) {
case 'can_be_merged':
return true;
}
}

constructor(client: GitLab, repository: GitLabRepository, mergeRequest: MergeRequest) {
this.client = client;
this.repository = repository;
this.mergeRequest = mergeRequest;
}

public async getComments(): Promise<Response<Comment[]>> {
throw new Error('Method not implemented.');
Expand Down
44 changes: 41 additions & 3 deletions src/provider/gitlab/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
CreatePullRequestBody,
IssuesParameters
} from '../repository';
import { GitLab, Project } from './api';
import { GitLab, Project, GetMergeRequestParameters } from './api';
import { GitLabMergeRequest } from './merge-request';

export class GitLabRepository implements Repository {
Expand All @@ -18,6 +18,10 @@ export class GitLabRepository implements Repository {
return this.project.name;
}

public get pathWithNamespace(): string {
return this.project.path_with_namespace;
}

public get defaultBranch(): string {
return this.project.default_branch;
}
Expand Down Expand Up @@ -47,9 +51,43 @@ export class GitLabRepository implements Repository {
this.project = project;
}

public async getPullRequests(_parameters?: ListPullRequestsParameters | undefined):
public async getPullRequests(parameters: ListPullRequestsParameters = {}):
Promise<Response<GitLabMergeRequest[]>> {
throw new Error('Method not implemented.');
function getState(state: ListPullRequestsParameters['state']): GetMergeRequestParameters['state'] {
switch (state) {
case 'open':
return 'opened';
case 'close':
return 'closed';
default:
return undefined;
}
}
function getOrderBy(orderBy: ListPullRequestsParameters['sort']): GetMergeRequestParameters['order_by'] {
switch (orderBy) {
case 'created':
return 'created_at';
case 'updated':
return 'updated_at';
default:
return undefined;
}
}

const params: GetMergeRequestParameters = {};
if (parameters.state) {
params.state = getState(parameters.state);
}
if (parameters.sort) {
params.order_by = getOrderBy(parameters.sort);
}
if (parameters.direction) {
params.sort = parameters.direction;
}
const respose = await this.client.getMergeRequests(encodeURIComponent(this.project.path_with_namespace), params);
return {
body: respose.body.map(mergeRequest => new GitLabMergeRequest(this.client, this, mergeRequest))
};
}

public async getPullRequest(_id: number): Promise<Response<GitLabMergeRequest>> {
Expand Down
1 change: 1 addition & 0 deletions src/workflow-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class WorkflowManager {

public async connect(tokens: Tokens): Promise<void> {
this.provider = await createClient(this.cwd, tokens);
this.log(`Connected with provider ${this.provider.name}`);
}

public async getRepository(): Promise<Repository> {
Expand Down

0 comments on commit 7423018

Please sign in to comment.