Skip to content

Commit

Permalink
fix: use own definition of PR -> implement getPRState() to get the st…
Browse files Browse the repository at this point in the history
…ate specific for services
  • Loading branch information
adelkahomolova committed Nov 22, 2019
1 parent fa2d21d commit 2b67091
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 53 deletions.
16 changes: 5 additions & 11 deletions src/inspectors/CollaborationInspector.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
import { inject, injectable } from 'inversify';
import { ProjectIssueBrowserService as ContentRepositoryBrowserService } from '../model';
import { GitHubPullRequestState } from '../services/git/IGitHubService';
import { Types } from '../types';
import { ListGetterOptions } from './common/ListGetterOptions';
import { ICollaborationInspector } from './ICollaborationInspector';
import { BitbucketPullRequestState } from '../services/git/ICVSService';
import { ICollaborationInspector, PullRequestState } from './ICollaborationInspector';
import { CSVService } from '../model';

@injectable()
export class CollaborationInspector implements ICollaborationInspector {
private service: ContentRepositoryBrowserService;
private service: CSVService;

constructor(@inject(Types.IContentRepositoryBrowser) service: ContentRepositoryBrowserService) {
constructor(@inject(Types.IContentRepositoryBrowser) service: CSVService) {
this.service = service;
}

async getPullRequests(
owner: string,
repo: string,
options?: ListGetterOptions<{ state?: GitHubPullRequestState | BitbucketPullRequestState }>,
) {
async getPullRequests(owner: string, repo: string, options?: ListGetterOptions<{ state?: PullRequestState }>) {
return this.service.getPullRequests(owner, repo, options);
}

Expand Down
8 changes: 1 addition & 7 deletions src/inspectors/ICollaborationInspector.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import { PullCommits, PullFiles, PullRequest } from '../services/git/model';
import { ListGetterOptions } from './common/ListGetterOptions';
import { Paginated } from './common/Paginated';
import { GitHubPullRequestState } from '../services/git/IGitHubService';
import { BitbucketPullRequestState } from '../services/git/ICVSService';

export interface ICollaborationInspector {
getPullRequests(
owner: string,
repo: string,
options?: ListGetterOptions<{ state?: GitHubPullRequestState | BitbucketPullRequestState }>,
): Promise<Paginated<PullRequest>>;
getPullRequests(owner: string, repo: string, options?: ListGetterOptions<{ state?: PullRequestState }>): Promise<Paginated<PullRequest>>;
getPullRequest(owner: string, repo: string, prNumber: number): Promise<PullRequest>;
getPullCommits(owner: string, repo: string, prNumber: number): Promise<Paginated<PullCommits>>;
getPullRequestFiles(owner: string, repo: string, prNumber: number): Promise<Paginated<PullFiles>>;
Expand Down
6 changes: 3 additions & 3 deletions src/inspectors/IssueTrackingInspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { IIssueTrackingInspector } from './IIssueTrackingInspector';
import { Paginated } from './common/Paginated';
import { Issue, IssueComment } from '../services/git/model';
import { Types } from '../types';
import { ProjectIssueBrowserService } from '../model';
import { CSVService } from '../model';

@injectable()
export class IssueTrackingInspector implements IIssueTrackingInspector {
private service: ProjectIssueBrowserService;
private service: CSVService;

constructor(@inject(Types.IContentRepositoryBrowser) service: ProjectIssueBrowserService) {
constructor(@inject(Types.IContentRepositoryBrowser) service: CSVService) {
this.service = service;
}

Expand Down
4 changes: 3 additions & 1 deletion src/model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { GitHubService } from './services/git/GitHubService';
import { BitbucketService } from './services/bitbucket/BitbucketService';
import { CSVServicesUtils } from './services/git/CSVServicesUtils';

// New model starts here

Expand Down Expand Up @@ -203,4 +205,4 @@ export enum GitFLow {
Git = 'Git',
}

export type ProjectIssueBrowserService = GitHubService;
export type CSVService = GitHubService | BitbucketService;
10 changes: 5 additions & 5 deletions src/services/bitbucket/BitbucketService.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import nock from 'nock';
import { BitbucketNock } from '../../../test/helpers/bitbucketNock';
import { BitbucketNock, PullRequest } from '../../../test/helpers/bitbucketNock';
import { BitbucketService } from './BitbucketService';
import { getPullRequestsResponse } from '../git/__MOCKS__/bitbucketServiceMockFolder/getPullRequestsResponse';
import { getPullRequestResponse } from '../git/__MOCKS__/bitbucketServiceMockFolder/getPullRequestResponse';
Expand All @@ -10,6 +10,7 @@ import { getIssueCommentsResponse } from '../git/__MOCKS__/bitbucketServiceMockF
import { GitHubPullRequestState } from '../git/IGitHubService';
import { BitbucketPullRequestState } from '../git/ICVSService';
import { ListGetterOptions } from '../../inspectors/common/ListGetterOptions';
import { PullRequestState } from '../../inspectors/ICollaborationInspector';

describe('Bitbucket Service', () => {
let service: BitbucketService;
Expand Down Expand Up @@ -67,17 +68,16 @@ describe('Bitbucket Service', () => {
});

it('returns declined pull requests in own interface', async () => {
const state: ListGetterOptions<{ state?: BitbucketPullRequestState }> = {
const state: ListGetterOptions<{ state?: PullRequestState }> = {
filter: {
state: BitbucketPullRequestState.declined,
state: PullRequestState.closed,
},
};

nock(bitbucketNock.url)
.get('/users/pypy')
.reply(200);
bitbucketNock.getApiResponse('pullrequests', undefined, undefined, state.filter!.state);

bitbucketNock.getApiResponse('pullrequests', undefined, undefined, 'MERGED');
const response = await service.getPullRequests('pypy', 'pypy', state);
expect(response).toMatchObject(getPullRequestsResponse);
});
Expand Down
8 changes: 5 additions & 3 deletions src/services/bitbucket/BitbucketService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ import {
Directory,
File,
} from '../git/model';
import { ICVSService, BitbucketPullRequestState } from '../git/ICVSService';
import { ICVSService, BitbucketPullRequestState, CSVService } from '../git/ICVSService';
import { ListGetterOptions } from '../../inspectors/common/ListGetterOptions';
import { PullRequestState } from '../../inspectors/ICollaborationInspector';
import { CSVServicesUtils } from '../git/CSVServicesUtils';
const debug = Debug('cli:services:git:bitbucket-service');

@injectable()
Expand Down Expand Up @@ -78,7 +79,7 @@ export class BitbucketService implements ICVSService {
async getPullRequests(
owner: string,
repo: string,
options?: ListGetterOptions<{ state?: BitbucketPullRequestState }>,
options?: ListGetterOptions<{ state?: PullRequestState }>,
): Promise<Paginated<PullRequest>> {
const params: Bitbucket.Params.PullrequestsList = {
repo_slug: repo,
Expand All @@ -87,7 +88,8 @@ export class BitbucketService implements ICVSService {

let state;
if (options !== undefined && options.filter !== undefined && options.filter.state !== undefined) {
state = options.filter.state;
state = CSVServicesUtils.getPRState(options.filter.state, CSVService.bitbucket);
console.log(state);
Object.assign(params, { state: state });
}

Expand Down
36 changes: 36 additions & 0 deletions src/services/git/CSVServicesUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { PullRequestState } from '../../inspectors/ICollaborationInspector';
import { BitbucketPullRequestState, CSVService } from './ICVSService';
import { GitHubPullRequestState } from './IGitHubService';

export class CSVServicesUtils {
static getPRState = (
state: PullRequestState | undefined,
service: CSVService,
): GitHubPullRequestState | BitbucketPullRequestState | undefined => {
if (service === CSVService.github) {
switch (state) {
case PullRequestState.open:
return GitHubPullRequestState.open;
case PullRequestState.closed:
return GitHubPullRequestState.closed;
case PullRequestState.all:
return GitHubPullRequestState.all;
case undefined:
return undefined;
}
}

if (service === CSVService.bitbucket) {
switch (state) {
case PullRequestState.open:
return BitbucketPullRequestState.open;
case PullRequestState.closed:
return BitbucketPullRequestState.closed;
case PullRequestState.all:
return BitbucketPullRequestState.open && BitbucketPullRequestState.closed && BitbucketPullRequestState.declined;
case undefined:
return undefined;
}
}
};
}
19 changes: 9 additions & 10 deletions src/services/git/Git.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import { Repository } from '../../model';
import { isArray } from 'util';
import { inject, injectable } from 'inversify';
import { ErrorFactory } from '../../lib/errors/ErrorFactory';
import * as nodePath from 'path';
import { Metadata, MetadataType, IProjectFilesBrowserService } from '../model';
import { isArray } from 'util';
import { PullRequestState } from '../../inspectors/ICollaborationInspector';
import { ErrorFactory } from '../../lib/errors/ErrorFactory';
import { CSVService, Repository } from '../../model';
import { Types } from '../../types';
import { ProjectIssueBrowserService as ContentRepositoryBrowserService } from '../../model';
import { Directory, File, Symlink } from './model';
import { IProjectFilesBrowserService, Metadata, MetadataType } from '../model';
import { GitServiceUtils } from './GitServiceUtils';
import { GitHubPullRequestState } from './IGitHubService';
import { Directory, File, Symlink } from './model';

@injectable()
export class Git implements IProjectFilesBrowserService {
private repository: Repository;
private service: ContentRepositoryBrowserService;
private service: CSVService;

constructor(repository: Repository, @inject(Types.IContentRepositoryBrowser) service: ContentRepositoryBrowserService) {
constructor(repository: Repository, @inject(Types.IContentRepositoryBrowser) service: CSVService) {
this.repository = repository;
this.service = service;
}
Expand Down Expand Up @@ -116,7 +115,7 @@ export class Git implements IProjectFilesBrowserService {

async getPullRequestCount(): Promise<number> {
const params = GitServiceUtils.getOwnerAndRepoName(this.repository.url);
return this.service.getPullRequests(params.owner, params.repoName, { filter: { state: GitHubPullRequestState.all } }).then((r) => {
return this.service.getPullRequests(params.owner, params.repoName, { filter: { state: PullRequestState.all } }).then((r) => {
if (!r) {
throw ErrorFactory.newInternalError('Could not get pull requests');
}
Expand Down
9 changes: 5 additions & 4 deletions src/services/git/GitHubService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ import { getPullsFilesResponse } from './__MOCKS__/gitHubServiceMockFolder/getPu
import { getPullsFilesServiceResponse } from './__MOCKS__/gitHubServiceMockFolder/getPullFilesServiceResponse.mock';
import { getPullCommitsResponse } from './__MOCKS__/gitHubServiceMockFolder/getPullsCommitsResponse.mock';
import { getPullCommitsServiceResponse } from './__MOCKS__/gitHubServiceMockFolder/getPullCommitsServiceResponse.mock';
import { GitHubNock } from '../../../test/helpers/gitHubNock';
import { GitHubNock, PullRequest } from '../../../test/helpers/gitHubNock';
import { getRepoCommitsResponse } from './__MOCKS__/gitHubServiceMockFolder/getRepoCommitsResponse.mock';
import { File } from './model';
import { GitHubPullRequestState } from './IGitHubService';
import { PullRequestState } from '../../inspectors/ICollaborationInspector';

describe('GitHub Service', () => {
let service: GitHubService;
Expand Down Expand Up @@ -70,7 +71,7 @@ describe('GitHub Service', () => {
'open',
);

const response = await service.getPullRequests('octocat', 'Hello-World', { filter: { state: GitHubPullRequestState.open } });
const response = await service.getPullRequests('octocat', 'Hello-World', { filter: { state: PullRequestState.open } });
expect(response.items.map((item) => item.state)).toMatchObject(['open']);
});

Expand All @@ -80,7 +81,7 @@ describe('GitHub Service', () => {
'closed',
);

const response = await service.getPullRequests('octocat', 'Hello-World', { filter: { state: GitHubPullRequestState.closed } });
const response = await service.getPullRequests('octocat', 'Hello-World', { filter: { state: PullRequestState.closed } });
expect(response.items.map((item) => item.state)).toMatchObject(['closed']);
});

Expand All @@ -93,7 +94,7 @@ describe('GitHub Service', () => {
'all',
);

const response = await service.getPullRequests('octocat', 'Hello-World', { filter: { state: GitHubPullRequestState.all } });
const response = await service.getPullRequests('octocat', 'Hello-World', { filter: { state: PullRequestState.all } });
expect(response.items.map((item) => item.state)).toMatchObject(['open', 'closed']);
});
});
Expand Down
8 changes: 5 additions & 3 deletions src/services/git/GitHubService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { ErrorFactory } from '../../lib/errors';
import { ICache } from '../../scanner/cache/ICache';
import { InMemoryCache } from '../../scanner/cache/InMemoryCache';
import { Types } from '../../types';
import { ICVSService, BitbucketPullRequestState } from './ICVSService';
import { ICVSService, BitbucketPullRequestState, CSVService } from './ICVSService';
import {
Commit,
Contributor,
Expand All @@ -34,6 +34,7 @@ import {
Symlink,
} from './model';
import { GitHubPullRequestState } from './IGitHubService';
import { CSVServicesUtils } from './CSVServicesUtils';
const debug = Debug('cli:services:git:github-service');

@injectable()
Expand Down Expand Up @@ -70,11 +71,12 @@ export class GitHubService implements ICVSService {
async getPullRequests(
owner: string,
repo: string,
options?: ListGetterOptions<{ state?: GitHubPullRequestState | BitbucketPullRequestState }>,
options?: ListGetterOptions<{ state?: PullRequestState }>,
): Promise<Paginated<PullRequest>> {
let url = 'GET /repos/:owner/:repo/pulls';
if (options !== undefined && options.filter !== undefined && options.filter.state !== undefined) {
url = `${url}?state=${options.filter.state}`;
const state = CSVServicesUtils.getPRState(options.filter.state, CSVService.github);
url = `${url}?state=${state}`;
}
const response: PullsListResponseItem[] = await this.paginate(url, owner, repo);

Expand Down
13 changes: 7 additions & 6 deletions src/services/git/ICVSService.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { ListGetterOptions } from '../../inspectors/common/ListGetterOptions';
import { Paginated } from '../../inspectors/common/Paginated';
import { GitHubPullRequestState } from './IGitHubService';
import { PullRequestState } from '../../inspectors/ICollaborationInspector';
import { Commit, Contributor, ContributorStats, Directory, File, Issue, PullFiles, PullRequest, PullRequestReview, Symlink } from './model';

export interface ICVSService {
getPullRequests(
owner: string,
repo: string,
options?: ListGetterOptions<{ state?: GitHubPullRequestState | BitbucketPullRequestState }>,
): Promise<Paginated<PullRequest>>;
getPullRequests(owner: string, repo: string, options?: ListGetterOptions<{ state?: PullRequestState }>): Promise<Paginated<PullRequest>>;
getPullRequestReviews(owner: string, repo: string, prNumber: number): Promise<Paginated<PullRequestReview>>;
getPullRequestFiles(owner: string, repo: string, prNumber: number): Promise<Paginated<PullFiles>>;
getPullRequest(owner: string, repo: string, prNumber: number): Promise<PullRequest>;
Expand All @@ -20,6 +16,11 @@ export interface ICVSService {
getRepoContent(owner: string, repo: string, path: string): Promise<File | Symlink | Directory | null>;
}

export enum CSVService {
github = 'GitHub',
bitbucket = 'Bitbucket',
}

export enum BitbucketPullRequestState {
open = 'OPEN',
closed = 'MERGED',
Expand Down

0 comments on commit 2b67091

Please sign in to comment.