Skip to content

Commit

Permalink
fix: add linesAdded and linesRemoved to own interface of Commit. Impl…
Browse files Browse the repository at this point in the history
…ement it in BitbucketService and rewrite tests.
  • Loading branch information
adelkahomolova committed Dec 10, 2019
1 parent 4d2fb16 commit 0648ce9
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 63 deletions.
3 changes: 3 additions & 0 deletions src/services/bitbucket/BitbucketService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -97,13 +98,15 @@ 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);
});

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);
Expand Down
168 changes: 105 additions & 63 deletions src/services/bitbucket/BitbucketService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,41 +99,41 @@ export class BitbucketService implements IVCSService {

const response: DeepRequired<Bitbucket.Response<Bitbucket.Schema.PaginatedPullrequests>> = 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 };
}

Expand Down Expand Up @@ -195,24 +195,40 @@ export class BitbucketService implements IVCSService {

const response = <DeepRequired<Bitbucket.Response<BitbucketCommit>>>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 = <Bitbucket.Schema.Diffstat[]>(
(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 += <number>val.lines_added;
linesRemoved += <number>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 };
Expand Down Expand Up @@ -309,22 +325,37 @@ export class BitbucketService implements IVCSService {
username: owner,
};
const response = <DeepRequired<Bitbucket.Response<BitbucketCommit>>>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 = <Bitbucket.Schema.Diffstat[]>(
(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 += <number>val.lines_added;
linesRemoved += <number>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 };
Expand All @@ -337,6 +368,15 @@ export class BitbucketService implements IVCSService {
username: owner,
};
const response = <DeepRequired<Bitbucket.Response<Bitbucket.Schema.Commit>>>await this.client.commits.get(params);
const diffValues = <Bitbucket.Schema.Diffstat[]>(
(await this.client.repositories.listDiffStats({ repo_slug: repo, username: owner, spec: commitSha })).data.values
);
let linesAdded = 0;
let linesRemoved = 0;
diffValues.forEach((val) => {
linesAdded += <number>val.lines_added;
linesRemoved += <number>val.lines_removed;
});
return {
sha: response.data.hash,
url: response.data.links.html.href,
Expand All @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/services/git/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export interface Commit {
author: Author;
message: string;
tree: Tree;
linesAdded: number;
linesRemoved: number;
verified: boolean;
}

Expand Down
11 changes: 11 additions & 0 deletions src/test/helpers/bitbucketNock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 0648ce9

Please sign in to comment.