Skip to content

Commit

Permalink
Merge pull request #27 from victorzchan/issue-#26-allow-comment-on-pu…
Browse files Browse the repository at this point in the history
…ll-request

Issue #26 allow comment on pull request
  • Loading branch information
vctr-dev committed Aug 16, 2019
2 parents b209a8c + 97af864 commit cde7bc3
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 29 deletions.
32 changes: 32 additions & 0 deletions lib/actions/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

const { makePullRequest } = require('../factory/pull-request');
const { BitbucketClient } = require('../bitbucket');
const logger = require('../logger');

module.exports = {
executeCommentAction,
};

function executeCommentAction({ args, config, credentials }) {
const {
repoUser = config.bitbucket.repoUser,
repoSlug = config.bitbucket.repoSlug,
pullRequestID,
commentMessage: message,
} = args;

if (!message) {
return Promise.reject(new Error('No comment to post onto pull request'));
}

const client = new BitbucketClient({
username: credentials.bitbucket.username,
password: credentials.bitbucket.password,
});
const pullRequest = makePullRequest({ client, user: repoUser, slug: repoSlug, id: pullRequestID });

logger.step(1, 1, `posting message to pull request id ${pullRequestID}`);
logger.debug(`message: \n${message}`);
return pullRequest.addComment({ message });
}
70 changes: 70 additions & 0 deletions lib/actions/comment.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
'use strict';

const chai = require('chai');
const nock = require('nock');

const { executeCommentAction } = require('./comment');

const HTTP_CREATED = 201;

describe('Action Comment', () => {
describe('executeCommentAction', () => {
let config, credentials, args, postCommentNock;
beforeEach(() => {
config = {
bitbucket: {
repoUser: 'my-user',
repoSlug: 'my-repo',
},
};
credentials = {
bitbucket: {
username: 'user1',
password: 'pass1',
},
};
args = {
pullRequestID: 10,
commentMessage: 'foo bar',
};
postCommentNock = nock('https://api.bitbucket.org/2.0')
.post('/repositories/my-user/my-repo/pullrequests/10/comments')
.once()
.reply(HTTP_CREATED, {});
});

afterEach(() => {
nock.cleanAll();
});

it('returns error if there are no comments to post', done => {
delete args.commentMessage;
executeCommentAction({
args,
config,
credentials,
})
.then(() => done('expected error when there is no comment message to post'))
.catch(({ message }) => {
chai.expect(message).to.equal('No comment to post onto pull request');
chai.expect(postCommentNock.isDone(), 'Request should not be called').to.be.false;
done();
});
});

it('returns success when posting comment with all args', done => {
executeCommentAction({
args,
config,
credentials,
})
.then(() => {
chai.expect(postCommentNock.isDone(), 'Request should have been called once').to.be.true;
done();
})
.catch(({ message }) => {
done(message);
});
});
});
});
8 changes: 3 additions & 5 deletions lib/actions/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const Q = require('q');

const logger = require('../logger');
const { BitbucketClient } = require('../bitbucket');
const { makePullRequest } = require('../factory/pull-request');
const { loadAndroidLintResults, loadCheckstyleResults } = require('../loaders');
const TOTAL_STEPS = 3;

Expand All @@ -25,10 +26,6 @@ function executeCommentsAction({ args, config, credentials }) {
failOnSeverity = [],
} = args;

if (!repoSlug || !repoUser || !pullRequestID) {
throw new Error('Required repo slug, repo user and pull request id');
}

if (checkstyleFilePaths.length === 0 && androidLintFilePaths.length === 0) {
throw new Error('Please supply --checkstyle or --android-lint result files.');
}
Expand All @@ -41,7 +38,8 @@ function executeCommentsAction({ args, config, credentials }) {
username: credentials.bitbucket.username,
password: credentials.bitbucket.password,
});
const pullRequest = client.repository(repoUser, repoSlug).pullRequest(pullRequestID);

const pullRequest = makePullRequest({ client, user: repoUser, slug: repoSlug, id: pullRequestID });

const loader = checkstyleFilePaths.length > 0 ? loadCheckstyleResults : loadAndroidLintResults;
const loaderFilePaths = checkstyleFilePaths.length > 0 ? checkstyleFilePaths : androidLintFilePaths;
Expand Down
1 change: 1 addition & 0 deletions lib/actions/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

module.exports = {
ACTION_COMMENTS: 'comments',
ACTION_COMMENT: 'comment',
ACTION_SCRIPT: 'run',
};
4 changes: 3 additions & 1 deletion lib/actions/dispatcher.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
'use strict';

const { ACTION_COMMENTS, ACTION_SCRIPT } = require('./constants');
const { ACTION_COMMENTS, ACTION_COMMENT, ACTION_SCRIPT } = require('./constants');

const { executeCommentsAction } = require('./comments');
const { executeCommentAction } = require('./comment');
const { executeScriptAction } = require('./script');

const { highlightText } = require('../colors');

const actions = {
[ACTION_COMMENTS]: executeCommentsAction,
[ACTION_COMMENT]: executeCommentAction,
[ACTION_SCRIPT]: executeScriptAction,
};

Expand Down
8 changes: 5 additions & 3 deletions lib/actions/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use strict';

module.exports = Object.assign({},
module.exports = Object.assign(
{},
require('./comments'),
require('./constants'),
require('./dispatcher')
);
require('./dispatcher'),
require('./comment')
);
4 changes: 3 additions & 1 deletion lib/args.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const path = require('path');
const {Command} = require('commander');
const { Command } = require('commander');

module.exports = {
getArgs,
Expand Down Expand Up @@ -33,6 +33,7 @@ function getArgs(argv) {
.option('--username <bitbucket username>', 'Bitbucket username. Needed if credentials path not provided.')
.option('--password <bitbucket password>', 'Bitbucket password. Needed if credentials path not provided.')
.option('--fail-on-severity <severity>', 'e.g, critical, error, fatal, warning etc', collectSeverities, [])
.option('--comment-message <comment message>', 'comment message to send. Only used for comment action.')
.parse(argv);

const args = {
Expand All @@ -46,6 +47,7 @@ function getArgs(argv) {
username: program.username,
password: program.password,
failOnSeverity: program.failOnSeverity.map(severity => severity.toLowerCase()),
commentMessage: program.commentMessage,
};
return {
args,
Expand Down
40 changes: 21 additions & 19 deletions lib/bitbucket/pull-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,26 +102,28 @@ class BitbucketPullRequest {
addComment(comment) {
const url = `${constants.API_2_BASE}/repositories/${this.repository.repoUser}/${this.repository
.repoSlug}/pullrequests/${this.pullRequestId}/comments`;
const requestParams = {
content: { raw: comment.message },
};

if (comment.fileName) {
// Line from is for unchanged and line to for changed
const lineParamKey = comment.changed ? 'to' : 'from';
const line = comment.changed ? comment.newLine : comment.previousLine;

requestParams.inline = {
[lineParamKey]: line,
path: comment.fileName,
};
}

// Line from is for unchanged and line to for changed
const lineParamKey = comment.changed ? 'to' : 'from';
const line = comment.changed ? comment.newLine : comment.previousLine;

return this._backend
.request('post', url, {
inline: {
[lineParamKey]: line,
path: comment.fileName,
},
content: { raw: comment.message },
})
.then(({ statusCode, body }) => {
if (!isStatusCodeSuccess(statusCode)) {
console.error(body);
return false;
}
return true;
});
return this._backend.request('post', url, requestParams).then(({ statusCode, body }) => {
if (!isStatusCodeSuccess(statusCode)) {
console.error(body);
return false;
}
return true;
});
}
}

Expand Down
12 changes: 12 additions & 0 deletions lib/factory/pull-request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

module.exports = {
makePullRequest,
};

function makePullRequest({ client, user, slug, id }) {
if (!slug || !user || !id) {
throw new Error('Required repo slug, repo user and pull request id');
}
return client.repository(user, slug).pullRequest(id);
}

0 comments on commit cde7bc3

Please sign in to comment.