From e4b1cf57b11cd53bf674d68dd2cda09fe8bf7508 Mon Sep 17 00:00:00 2001 From: Adela Homolova Date: Fri, 10 Jan 2020 15:42:27 +0100 Subject: [PATCH] fix: add filtering and pagination in getIssues() --- src/inspectors/IssueTrackingInspector.ts | 11 ++++++-- src/services/bitbucket/BitbucketService.ts | 32 +++++++++++++++++++--- src/services/git/GitHubService.ts | 21 ++++++++++++-- 3 files changed, 55 insertions(+), 9 deletions(-) diff --git a/src/inspectors/IssueTrackingInspector.ts b/src/inspectors/IssueTrackingInspector.ts index 998832bd5..5c4350aec 100644 --- a/src/inspectors/IssueTrackingInspector.ts +++ b/src/inspectors/IssueTrackingInspector.ts @@ -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 { @@ -14,8 +15,12 @@ export class IssueTrackingInspector implements IIssueTrackingInspector { this.service = service; } - async getIssues(owner: string, repo: string): Promise> { - return this.service.getIssues(owner, repo); + async getIssues( + owner: string, + repo: string, + options?: { withDiffStat?: boolean } & ListGetterOptions<{ state?: IssueState }>, + ): Promise> { + return this.service.getIssues(owner, repo, options); } async getIssue(owner: string, repo: string, issueId: number): Promise { diff --git a/src/services/bitbucket/BitbucketService.ts b/src/services/bitbucket/BitbucketService.ts index 022c4b663..4905867a0 100644 --- a/src/services/bitbucket/BitbucketService.ts +++ b/src/services/bitbucket/BitbucketService.ts @@ -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, @@ -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'); @@ -246,13 +247,36 @@ export class BitbucketService implements IVCSService { return { items, ...pagination }; } - async getIssues(owner: string, repo: string): Promise> { + async getIssues( + owner: string, + repo: string, + options?: { withDiffStat?: boolean } & ListGetterOptions<{ state?: IssueState }>, + ): Promise> { 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 = >>await this.client.issue_tracker.list(params); const items = response.data.values.map((val) => ({ diff --git a/src/services/git/GitHubService.ts b/src/services/git/GitHubService.ts index e53a4a73f..084875672 100644 --- a/src/services/git/GitHubService.ts +++ b/src/services/git/GitHubService.ts @@ -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() @@ -380,8 +381,24 @@ export class GitHubService implements IVCSService { /** * List all issues in the repo. */ - async getIssues(owner: string, repo: string): Promise> { - 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> { + 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: {