Skip to content

Commit

Permalink
fix: remove unnecessary Promise.all(); remove request for getting dif…
Browse files Browse the repository at this point in the history
…fStat
  • Loading branch information
adelkahomolova committed Dec 11, 2019
1 parent ce8b4dd commit 07e88e5
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 109 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
"@octokit/rest": "16.35.0",
"@types/eslint": "6.1.3",
"@types/js-yaml": "3.12.1",
"@types/table": "^4.0.7",
"@types/qs": "^6.9.0",
"@types/table": "^4.0.7",
"@types/yaml": "^1.2.0",
"axios": "0.19.0",
"bitbucket": "1.15.2",
Expand All @@ -47,8 +47,8 @@
"inversify": "5.0.1",
"js-yaml": "3.13.1",
"lodash": "4.17.15",
"moment": "^2.24.0",
"memfs": "3.0.1",
"moment": "^2.24.0",
"node-filter-async": "1.1.3",
"npm-check-updates": "3.2.2",
"oclif": "1.15.1",
Expand Down
3 changes: 0 additions & 3 deletions src/services/bitbucket/BitbucketService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ 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 @@ -98,15 +97,13 @@ 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
173 changes: 69 additions & 104 deletions src/services/bitbucket/BitbucketService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,38 +99,36 @@ export class BitbucketService implements IVCSService {

const response: DeepRequired<Bitbucket.Response<Bitbucket.Schema.PaginatedPullrequests>> = await axios.get(apiUrl);

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 items = response.data.values.map((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);

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

const response = <DeepRequired<Bitbucket.Response<BitbucketCommit>>>await this.client.pullrequests.listCommits(params);

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,
commit: {
const items = response.data.values.map((val) => {
return {
sha: val.hash,
commit: {
url: val.links.html.href,
message: val.message,
author: {
name: val.author.raw,
email: 'undefined',
date: val.date,
},
tree: {
sha: val.hash,
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,
},
};
}),
);
//TODO
verified: false,
},
};
});

const pagination = this.getPagination(response.data);

return { items, ...pagination };
Expand Down Expand Up @@ -325,37 +310,25 @@ export class BitbucketService implements IVCSService {
username: owner,
};
const response = <DeepRequired<Bitbucket.Response<BitbucketCommit>>>await this.client.repositories.listCommits(params);
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 items = response.data.values.map((val) => {
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,
},
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 @@ -368,15 +341,7 @@ 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 @@ -386,8 +351,6 @@ 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 All @@ -409,6 +372,8 @@ export class BitbucketService implements IVCSService {
throw new Error('Method not implemented yet.');
}

// async getDiffStat(owner: string, repo: string, sha: string) {}

private unwrap<T>(clientPromise: Promise<Bitbucket.Response<T>>) {
return clientPromise
.then((response) => {
Expand Down

0 comments on commit 07e88e5

Please sign in to comment.