Skip to content

Commit

Permalink
feat: implement getPullsDiffStat() in BitbucketService and add test f…
Browse files Browse the repository at this point in the history
…or that
  • Loading branch information
adelkahomolova committed Dec 11, 2019
1 parent d1232ed commit 3c78fe3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
9 changes: 8 additions & 1 deletion src/services/bitbucket/BitbucketService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,17 @@ describe('Bitbucket Service', () => {
expect(response).toMatchObject(getRepoCommits);
});

it('return on commit in own interface', async () => {
it('returns one commit in own interface', async () => {
bitbucketNock.getApiResponse('commit', '961b3a27');

const response = await service.getCommit('pypy', 'pypy', '961b3a27');
expect(response).toMatchObject(getRepoCommit);
});

it('returns pulls diff stat in own interface', async () => {
bitbucketNock.getAdditionsAndDeletions('622');

const response = await service.getPullsDiffStat('pypy', 'pypy', '622');
expect(response).toMatchObject({ additions: 2, deletions: 1, changes: 3 });
});
});
18 changes: 17 additions & 1 deletion src/services/bitbucket/BitbucketService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,23 @@ export class BitbucketService implements IVCSService {
throw new Error('Method not implemented yet.');
}

// async getDiffStat(owner: string, repo: string, sha: string) {}
async getPullsDiffStat(owner: string, repo: string, sha: string) {
const diffStatData = (await this.client.pullrequests.getDiffStat({ repo_slug: repo, username: owner, pull_request_id: sha })).data;

let linesRemoved = 0,
linesAdded = 0;

diffStatData.values.forEach((val: { lines_removed: number; lines_added: number }) => {
linesRemoved += val.lines_removed;
linesAdded += val.lines_added;
});

return {
additions: linesAdded,
deletions: linesRemoved,
changes: linesAdded + linesRemoved,
};
}

private unwrap<T>(clientPromise: Promise<Bitbucket.Response<T>>) {
return clientPromise
Expand Down
8 changes: 3 additions & 5 deletions src/test/helpers/bitbucketNock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,9 @@ export class BitbucketNock {
return BitbucketNock.get(url, params, persist).reply(200, response);
}

getAdditionsAndDeletions(sha: string, pullRequestCommit?: boolean) {
let url = `${this.url}/repositories/${this.user}/${this.repoName}/diffstat/${sha}`;
if (pullRequestCommit) {
url = `${this.url}/repositories/${this.user}/${this.repoName}/pullrequests/${sha}/diffstat`;
}
getAdditionsAndDeletions(sha: string) {
const url = `${this.url}/repositories/${this.user}/${this.repoName}/pullrequests/${sha}/diffstat`;

const params = {};
const persist = true;
const response = { values: [{ lines_removed: 1, lines_added: 2 }] };
Expand Down

0 comments on commit 3c78fe3

Please sign in to comment.