From 0648ce9cbfc5719c9591db8d723cba3a7a11fbe9 Mon Sep 17 00:00:00 2001 From: Adela Homolova Date: Tue, 10 Dec 2019 21:12:50 +0100 Subject: [PATCH] fix: add linesAdded and linesRemoved to own interface of Commit. Implement it in BitbucketService and rewrite tests. --- .../bitbucket/BitbucketService.spec.ts | 3 + src/services/bitbucket/BitbucketService.ts | 168 +++++++++++------- src/services/git/model.ts | 2 + src/test/helpers/bitbucketNock.ts | 11 ++ 4 files changed, 121 insertions(+), 63 deletions(-) diff --git a/src/services/bitbucket/BitbucketService.spec.ts b/src/services/bitbucket/BitbucketService.spec.ts index 05bcdb32e..6a98ed02c 100644 --- a/src/services/bitbucket/BitbucketService.spec.ts +++ b/src/services/bitbucket/BitbucketService.spec.ts @@ -53,6 +53,7 @@ describe('Bitbucket Service', () => { it('returns pullrequest commits in own interface', async () => { bitbucketNock.getApiResponse('pullrequests', 622, 'commits'); + bitbucketNock.getAdditionsAndDeletions('622', true); const response = await service.getPullCommits('pypy', 'pypy', 622); expect(response).toMatchObject(getPullCommits); @@ -97,6 +98,7 @@ describe('Bitbucket Service', () => { it('returns repo commits in own interface', async () => { bitbucketNock.getApiResponse('commits'); + bitbucketNock.getAdditionsAndDeletions('f9c2cfcfaafa644dcc286ce2fc8b3386d46c11df'); const response = await service.getRepoCommits('pypy', 'pypy'); expect(response).toMatchObject(getRepoCommits); @@ -104,6 +106,7 @@ describe('Bitbucket Service', () => { it('return on commit in own interface', async () => { bitbucketNock.getApiResponse('commit', '961b3a27'); + bitbucketNock.getAdditionsAndDeletions('961b3a27'); const response = await service.getCommit('pypy', 'pypy', '961b3a27'); expect(response).toMatchObject(getRepoCommit); diff --git a/src/services/bitbucket/BitbucketService.ts b/src/services/bitbucket/BitbucketService.ts index 9d80b071c..482211a4e 100644 --- a/src/services/bitbucket/BitbucketService.ts +++ b/src/services/bitbucket/BitbucketService.ts @@ -99,41 +99,41 @@ export class BitbucketService implements IVCSService { const response: DeepRequired> = await axios.get(apiUrl); - const values = response.data.values.map(async (val) => { - return { - user: { - id: val.author.uuid, - login: val.author.nickname, - url: val.author.links.html.href, - }, - url: val.links.html.href, - body: val.description, - createdAt: val.created_on, - updatedAt: val.updated_on, - closedAt: - val.state === BitbucketPullRequestState.closed || val.state === BitbucketPullRequestState.declined ? val.updated_on : null, - mergedAt: val.state === BitbucketPullRequestState.closed ? val.updated_on : null, - state: val.state, - id: val.id, - base: { - repo: { - url: val.destination.repository.links.html.href, - name: val.destination.repository.name, - id: val.destination.repository.uuid, - owner: { - login: owner, - id: ownerId, - url: ownerUrl, + const items = await Promise.all( + response.data.values.map(async (val) => { + return { + user: { + id: val.author.uuid, + login: val.author.nickname, + url: val.author.links.html.href, + }, + url: val.links.html.href, + body: val.description, + createdAt: val.created_on, + updatedAt: val.updated_on, + closedAt: + val.state === BitbucketPullRequestState.closed || val.state === BitbucketPullRequestState.declined ? val.updated_on : null, + mergedAt: val.state === BitbucketPullRequestState.closed ? val.updated_on : null, + state: val.state, + id: val.id, + base: { + repo: { + url: val.destination.repository.links.html.href, + name: val.destination.repository.name, + id: val.destination.repository.uuid, + owner: { + login: owner, + id: ownerId, + url: ownerUrl, + }, }, }, - }, - }; - }); + }; + }), + ); const pagination = this.getPagination(response.data); - const items = await Promise.all(values); - return { items, ...pagination }; } @@ -195,24 +195,40 @@ export class BitbucketService implements IVCSService { const response = >>await this.client.pullrequests.listCommits(params); - const items = response.data.values.map((val) => ({ - sha: val.hash, - commit: { - url: val.links.html.href, - message: val.message, - author: { - name: val.author.raw, - email: 'undefined', - date: val.date, - }, - tree: { + const items = await Promise.all( + response.data.values.map(async (val) => { + const diffValues = ( + (await this.client.repositories.getPullRequestDiffStat({ repo_slug: repo, username: owner, pull_request_id: `${prNumber}` })).data + .values + ); + let linesAdded = 0; + let linesRemoved = 0; + diffValues.forEach((val) => { + linesAdded += val.lines_added; + linesRemoved += val.lines_removed; + }); + return { sha: val.hash, - url: val.links.html.href, - }, - //TODO - verified: false, - }, - })); + commit: { + url: val.links.html.href, + message: val.message, + author: { + name: val.author.raw, + email: 'undefined', + date: val.date, + }, + linesAdded: linesAdded, + linesRemoved: linesRemoved, + tree: { + sha: val.hash, + url: val.links.html.href, + }, + //TODO + verified: false, + }, + }; + }), + ); const pagination = this.getPagination(response.data); return { items, ...pagination }; @@ -309,22 +325,37 @@ export class BitbucketService implements IVCSService { username: owner, }; const response = >>await this.client.repositories.listCommits(params); - const items = response.data.values.map((val) => ({ - sha: val.hash, - url: val.links.html.href, - message: val.rendered.message.raw, - author: { - name: val.author.user.nickname, - email: this.extractEmailFromString(val.author.raw) || '', - date: val.date, - }, - tree: { - sha: val.parents[0].hash, - url: val.parents[0].links.html.href, - }, - // TODO - verified: false, - })); + const items = await Promise.all( + response.data.values.map(async (val) => { + const diffValues = ( + (await this.client.repositories.listDiffStats({ repo_slug: repo, username: owner, spec: val.hash })).data.values + ); + let linesAdded = 0; + let linesRemoved = 0; + diffValues.forEach((val) => { + linesAdded += val.lines_added; + linesRemoved += val.lines_removed; + }); + return { + sha: val.hash, + url: val.links.html.href, + message: val.rendered.message.raw, + author: { + name: val.author.user.nickname, + email: this.extractEmailFromString(val.author.raw) || '', + date: val.date, + }, + linesAdded: linesAdded, + linesRemoved: linesRemoved, + tree: { + sha: val.parents[0].hash, + url: val.parents[0].links.html.href, + }, + // TODO + verified: false, + }; + }), + ); const pagination = this.getPagination(response.data); return { items, ...pagination }; @@ -337,6 +368,15 @@ export class BitbucketService implements IVCSService { username: owner, }; const response = >>await this.client.commits.get(params); + const diffValues = ( + (await this.client.repositories.listDiffStats({ repo_slug: repo, username: owner, spec: commitSha })).data.values + ); + let linesAdded = 0; + let linesRemoved = 0; + diffValues.forEach((val) => { + linesAdded += val.lines_added; + linesRemoved += val.lines_removed; + }); return { sha: response.data.hash, url: response.data.links.html.href, @@ -346,6 +386,8 @@ export class BitbucketService implements IVCSService { email: this.extractEmailFromString(response.data.author.raw) || '', date: response.data.date, }, + linesAdded: linesAdded, + linesRemoved: linesRemoved, tree: { sha: response.data.parents[0].hash, url: response.data.parents[0].links.html.href, diff --git a/src/services/git/model.ts b/src/services/git/model.ts index 02de4e620..80b5bab69 100644 --- a/src/services/git/model.ts +++ b/src/services/git/model.ts @@ -36,6 +36,8 @@ export interface Commit { author: Author; message: string; tree: Tree; + linesAdded: number; + linesRemoved: number; verified: boolean; } diff --git a/src/test/helpers/bitbucketNock.ts b/src/test/helpers/bitbucketNock.ts index 3b42741b4..25c846320 100644 --- a/src/test/helpers/bitbucketNock.ts +++ b/src/test/helpers/bitbucketNock.ts @@ -106,6 +106,17 @@ 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`; + } + const params = {}; + const persist = true; + const response = { values: [{ lines_removed: 1, lines_added: 2 }] }; + return BitbucketNock.get(url, params, persist).reply(200, response); + } + private static get(url: string, params: nock.DataMatcherMap, persist = true): nock.Interceptor { const urlObj = new URL(url);