-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from victorzchan/issue-#26-allow-comment-on-pu…
…ll-request Issue #26 allow comment on pull request
- Loading branch information
Showing
9 changed files
with
150 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,6 @@ | |
|
||
module.exports = { | ||
ACTION_COMMENTS: 'comments', | ||
ACTION_COMMENT: 'comment', | ||
ACTION_SCRIPT: 'run', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |