Skip to content

Commit

Permalink
fix: add filtering and pagination in getIssues()
Browse files Browse the repository at this point in the history
  • Loading branch information
adelkahomolova committed Jan 10, 2020
1 parent 80c2ee9 commit e4b1cf5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
11 changes: 8 additions & 3 deletions src/inspectors/IssueTrackingInspector.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/* eslint-disable @typescript-eslint/camelcase */
import { injectable, inject } from 'inversify';
import { IIssueTrackingInspector } from './IIssueTrackingInspector';
import { IIssueTrackingInspector, IssueState } from './IIssueTrackingInspector';
import { Paginated } from './common/Paginated';
import { Issue, IssueComment } from '../services/git/model';
import { Types } from '../types';
import { VCSService } from '../model';
import { ListGetterOptions } from '.';

@injectable()
export class IssueTrackingInspector implements IIssueTrackingInspector {
Expand All @@ -14,8 +15,12 @@ export class IssueTrackingInspector implements IIssueTrackingInspector {
this.service = service;
}

async getIssues(owner: string, repo: string): Promise<Paginated<Issue>> {
return this.service.getIssues(owner, repo);
async getIssues(
owner: string,
repo: string,
options?: { withDiffStat?: boolean } & ListGetterOptions<{ state?: IssueState }>,
): Promise<Paginated<Issue>> {
return this.service.getIssues(owner, repo, options);
}

async getIssue(owner: string, repo: string, issueId: number): Promise<Issue> {
Expand Down
32 changes: 28 additions & 4 deletions src/services/bitbucket/BitbucketService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import { inject, injectable } from 'inversify';
import { inspect } from 'util';
import axios from 'axios';
import qs from 'qs';
import { IVCSService, VCSServiceType, BitbucketPullRequestState } from '..';
import { IVCSService, VCSServiceType } from '..';
import { ArgumentsProvider } from '../../scanner';
import { ICache } from '../../scanner/cache/ICache';
import { Types } from '../../types';
import { ListGetterOptions, PullRequestState, Paginated } from '../../inspectors';
import { BitbucketIssueState } from '../../inspectors/IIssueTrackingInspector';
import { IssueState } from '../../inspectors/IIssueTrackingInspector';
import {
PullRequest,
PullFiles,
Expand All @@ -32,6 +32,7 @@ import {
import { VCSServicesUtils } from '../git/VCSServicesUtils';
import { DeepRequired } from '../../lib/deepRequired';
import { InMemoryCache } from '../../scanner/cache';
import { BitbucketPullRequestState, BitbucketIssueState } from './IBitbucketService';

const debug = Debug('cli:services:git:bitbucket-service');

Expand Down Expand Up @@ -246,13 +247,36 @@ export class BitbucketService implements IVCSService {
return { items, ...pagination };
}

async getIssues(owner: string, repo: string): Promise<Paginated<Issue>> {
async getIssues(
owner: string,
repo: string,
options?: { withDiffStat?: boolean } & ListGetterOptions<{ state?: IssueState }>,
): Promise<Paginated<Issue>> {
this.authenticate();

const params: Bitbucket.Params.IssueTrackerList = {
let params: Bitbucket.Params.IssueTrackerList = {
repo_slug: repo,
username: owner,
};

if (options?.filter?.state) {
const state = VCSServicesUtils.getIssueState(options.filter.state, VCSServiceType.bitbucket);
const stringifiedState = qs.stringify(
{ state: state },
{ addQueryPrefix: false, indices: false, encode: false, arrayFormat: 'repeat' },
);
params = { ...params, q: stringifiedState };
}

if (options?.pagination) {
if (options.pagination.page) {
params = { ...params, page: `${options.pagination.page}` };
}
if (options.pagination.perPage) {
params = { ...params, pagelen: options.pagination.perPage };
}
}

const response = <DeepRequired<Bitbucket.Response<Bitbucket.Schema.PaginatedIssues>>>await this.client.issue_tracker.list(params);

const items = response.data.values.map((val) => ({
Expand Down
21 changes: 19 additions & 2 deletions src/services/git/GitHubService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
import { VCSServicesUtils } from './VCSServicesUtils';
import qs from 'qs';
import { ArgumentsProvider } from '../../scanner';
import { IssueState } from '../../inspectors/IIssueTrackingInspector';
const debug = Debug('cli:services:git:github-service');

@injectable()
Expand Down Expand Up @@ -380,8 +381,24 @@ export class GitHubService implements IVCSService {
/**
* List all issues in the repo.
*/
async getIssues(owner: string, repo: string): Promise<Paginated<Issue>> {
const response: IssuesListForRepoResponseItem[] = await this.paginate('GET /repos/:owner/:repo/issues', owner, repo);
async getIssues(
owner: string,
repo: string,
options?: { withDiffStat?: boolean } & ListGetterOptions<{ state?: IssueState }>,
): Promise<Paginated<Issue>> {
let url = 'GET /repos/:owner/:repo/issues';

const state = VCSServicesUtils.getIssueState(options?.filter?.state, VCSServiceType.github);

url = url.concat(
`${qs.stringify(
// eslint-disable-next-line @typescript-eslint/camelcase
{ state: state, page: options?.pagination?.page, per_page: options?.pagination?.perPage },
{ addQueryPrefix: true, indices: false, arrayFormat: 'repeat' },
)}`,
);

const response: IssuesListForRepoResponseItem[] = await this.paginate(url, owner, repo);

const items = response.map((val) => ({
user: {
Expand Down

0 comments on commit e4b1cf5

Please sign in to comment.