Skip to content

Commit

Permalink
Merge pull request #21 from victorzchan/#15-update-bitbucket-api
Browse files Browse the repository at this point in the history
#15 update bitbucket api
  • Loading branch information
vctr-dev committed Jun 27, 2019
2 parents 1ca747c + a6cff02 commit b5a0735
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 39 deletions.
5 changes: 3 additions & 2 deletions lib/actions/comments.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { executeCommentsAction, _test } = require('./comments');
const { getPreviousCommentIds } = _test;

const HTTP_OK = 200;
const HTTP_CREATED = 201;

describe('Action Comments', () => {
describe('executeCommentsAction', () => {
Expand All @@ -20,10 +21,10 @@ describe('Action Comments', () => {
nock('https://api.bitbucket.org/2.0')
.get('/repositories/my-user/my-repo/pullrequests/10/comments')
.reply(HTTP_OK, {});
nock('https://api.bitbucket.org/1.0')
nock('https://api.bitbucket.org/2.0')
.persist()
.post('/repositories/my-user/my-repo/pullrequests/10/comments')
.reply(HTTP_OK, {});
.reply(HTTP_CREATED, {});
nock('https://api.bitbucket.org/2.0')
.get('/user')
.reply(HTTP_OK, {
Expand Down
14 changes: 9 additions & 5 deletions lib/bitbucket/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ class BitbucketBackend {
this._tryAgainInMilliseconds = DEFAULT_TRY_AGAIN_MS;
}

request(method, url, formData) {
request(method, url, payload) {
const backend = this;
const deferred = Q.defer();

const executeRequest = () => {
const requestConfig = backend._requestConfig({ url, formData });
const requestConfig = backend._requestConfig({ url, payload });
request[method](requestConfig, handleResponse);
};

Expand Down Expand Up @@ -98,16 +98,20 @@ class BitbucketBackend {
}
}

_requestConfig({ url, formData = {} }) {
return {
formData,
_requestConfig({ url, payload }) {
const config = {
url,
auth: {
user: this._username,
pass: this._password,
sendImmediately: true,
},
};
if (payload) {
config.json = true;
config.body = payload;
}
return config;
}
}

Expand Down
3 changes: 1 addition & 2 deletions lib/bitbucket/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@

module.exports = Object.freeze({
API_2_BASE: 'https://api.bitbucket.org/2.0',
API_1_BASE: 'https://api.bitbucket.org/1.0',
});
});
30 changes: 14 additions & 16 deletions lib/bitbucket/pull-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { ChangedChunks } = require('../git');

const HTTP_OK = 200;
const isStatusCodeSuccess = code => code >= 200 && code < 300;
const constants = require('./constants');

class BitbucketPullRequest {
Expand Down Expand Up @@ -32,7 +32,7 @@ class BitbucketPullRequest {
.then(parsePullRequestResponse);

function parsePullRequestResponse({ statusCode, body }) {
if (statusCode !== HTTP_OK) {
if (!isStatusCodeSuccess(statusCode)) {
return null;
}
return JSON.parse(body);
Expand All @@ -51,7 +51,7 @@ class BitbucketPullRequest {
.then(parts => {
const errors = [];
parts.forEach(({ statusCode, body }) => {
if (statusCode !== HTTP_OK) {
if (!isStatusCodeSuccess(statusCode)) {
errors.push(body);
}
});
Expand All @@ -63,7 +63,7 @@ class BitbucketPullRequest {
function deleteComment(id) {
return pullRequest._backend.request(
'delete',
`${constants.API_1_BASE}/repositories/${pullRequest.repository.repoUser}/${pullRequest.repository
`${constants.API_2_BASE}/repositories/${pullRequest.repository.repoUser}/${pullRequest.repository
.repoSlug}/pullrequests/${pullRequest.pullRequestId}/comments/${id}`
);
}
Expand All @@ -79,7 +79,7 @@ class BitbucketPullRequest {
return pullRequest._backend.request('get', url).then(gotComments);

function gotComments({ statusCode, body }) {
if (statusCode !== HTTP_OK) {
if (!isStatusCodeSuccess(statusCode)) {
return null;
}

Expand All @@ -100,25 +100,23 @@ class BitbucketPullRequest {
}

addComment(comment) {
const url = `${constants.API_1_BASE}/repositories/${this.repository.repoUser}/${this.repository
const url = `${constants.API_2_BASE}/repositories/${this.repository.repoUser}/${this.repository
.repoSlug}/pullrequests/${this.pullRequestId}/comments`;

// Line from is for unchanged and line to for changed
// see: https://bitbucket.org/site/master/issues/13110/post-comment-on-a-commit-pull-request-api
const lineParamKey = comment.changed ? 'line_to' : 'line_from';
const lineParamKey = comment.changed ? 'to' : 'from';
const line = comment.changed ? comment.newLine : comment.previousLine;

// From https://answers.atlassian.com/questions/32977327/are-you-planning-on-offering-an-update-pull-request-comment-api
// You can create inline comments with 1.0. Use the filename and line_from / line_to elements in the JSON request body, as per

return this._backend
.request('post', url, {
[lineParamKey]: line,
filename: comment.fileName,
content: comment.message,
inline: {
[lineParamKey]: line,
path: comment.fileName,
},
content: { raw: comment.message },
})
.then(({ statusCode, body }) => {
if (statusCode !== HTTP_OK) {
if (!isStatusCodeSuccess(statusCode)) {
console.error(body);
return false;
}
Expand All @@ -128,7 +126,7 @@ class BitbucketPullRequest {
}

function parseChangedChunksResponse({ statusCode, body: diff = '' }) {
if (statusCode != HTTP_OK) {
if (!isStatusCodeSuccess(statusCode)) {
return null;
}
return new ChangedChunks({ diff });
Expand Down
32 changes: 18 additions & 14 deletions lib/bitbucket/pull-request.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
const sinon = require('sinon');
const chai = require('chai');

const {BitbucketBackend} = require('./backend');
const {BitbucketRepository} = require('./repository');
const { BitbucketBackend } = require('./backend');
const { BitbucketRepository } = require('./repository');

const constants = require('./constants');
const request = require('request');
Expand Down Expand Up @@ -41,13 +41,15 @@ describe('GIVEN a pull request', () => {
});
chai.assert(request.post.calledOnce);

const expectedUrl = `${constants.API_1_BASE}/repositories/my-repo-user/my-repo/pullrequests/1011/comments`;
const [{ formData, url, auth }] = request.post.getCall(0).args;
const expectedUrl = `${constants.API_2_BASE}/repositories/my-repo-user/my-repo/pullrequests/1011/comments`;
const [{ body, url, auth }] = request.post.getCall(0).args;
chai.expect(url).to.equal(expectedUrl);
chai.expect(formData).to.deep.equal({
line_from: 100,
filename: 'package.json',
content: 'Test message',
chai.expect(body).to.deep.equal({
inline: {
from: 100,
path: 'package.json',
},
content: { raw: 'Test message' },
});
chai.expect(auth).to.deep.equal({
user: 'my-username',
Expand All @@ -67,13 +69,15 @@ describe('GIVEN a pull request', () => {
});
chai.assert(request.post.calledOnce);

const expectedUrl = `${constants.API_1_BASE}/repositories/my-repo-user/my-repo/pullrequests/1011/comments`;
const [{ formData, url, auth }] = request.post.getCall(0).args;
const expectedUrl = `${constants.API_2_BASE}/repositories/my-repo-user/my-repo/pullrequests/1011/comments`;
const [{ body, url, auth }] = request.post.getCall(0).args;
chai.expect(url).to.equal(expectedUrl);
chai.expect(formData).to.deep.equal({
line_to: 101,
filename: 'package.json',
content: 'Test message',
chai.expect(body).to.deep.equal({
inline: {
to: 101,
path: 'package.json',
},
content: { raw: 'Test message' },
});
chai.expect(auth).to.deep.equal({
user: 'my-username',
Expand Down

0 comments on commit b5a0735

Please sign in to comment.