Skip to content

Commit 5079b56

Browse files
committed
Merge pull request #117 from keithmorris/feature/implement_add_watcher_to_issue
Implement `addWatcher` method to add a watcher to an issue
2 parents 37626b8 + ba54db7 commit 5079b56

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ JiraApi options:
107107
* Issues
108108
* Add a new issue
109109
* Update an issue
110+
* Add watcher to an issue
110111
* Transition an issue
111112
* Pulling an issue
112113
* Issue linking

lib/jira.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,53 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
10301030
});
10311031
};
10321032

1033+
// ## Add a user as a watcher on an issue ##
1034+
// ### Takes ###
1035+
//
1036+
// * issueKey: the key of the existing issue
1037+
// * username: the jira username to add as a watcher to the issue
1038+
// * callback: for when it's done
1039+
//
1040+
// ### Returns ###
1041+
//
1042+
// * error: string of the error
1043+
//
1044+
//
1045+
// Empty callback on success
1046+
/**
1047+
* Adds a given user as a watcher to the given issue
1048+
*
1049+
* @param issueKey
1050+
* @param username
1051+
* @param callback
1052+
*/
1053+
this.addWatcher = function (issueKey, username, callback) {
1054+
1055+
var options = {
1056+
rejectUnauthorized: this.strictSSL,
1057+
uri: this.makeUri('/issue/' + issueKey + '/watchers'),
1058+
method: 'POST',
1059+
followAllRedirects: true,
1060+
json: true,
1061+
body: JSON.stringify(username)
1062+
};
1063+
1064+
this.doRequest(options, function (error, response) {
1065+
if (error) {
1066+
return callback(error, null);
1067+
}
1068+
1069+
if (response.statusCode === 404) {
1070+
return callback('Invalid URL');
1071+
}
1072+
1073+
if (response.statusCode !== 204) {
1074+
return callback(response.statusCode + ': Unable to connect to JIRA to add user as watcher.');
1075+
}
1076+
callback();
1077+
});
1078+
};
1079+
10331080
// ## Delete issue to Jira ##
10341081
// ### Takes ###
10351082
//

spec/jira.spec.coffee

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,28 @@ describe "Node Jira Tests", ->
538538
@jira.request.mostRecentCall.args[1] null, statusCode:201
539539
expect(@cb).toHaveBeenCalledWith null, "Success"
540540

541+
it "Adds a watcher to an issue", ->
542+
options =
543+
rejectUnauthorized: true
544+
uri: makeUrl "issue/1/watchers"
545+
body: JSON.stringify "testuser"
546+
method: 'POST'
547+
followAllRedirects: true
548+
json: true
549+
auth:
550+
user: 'test'
551+
pass: 'test'
552+
553+
@jira.addWatcher 1, "testuser", @cb
554+
expect(@jira.request).toHaveBeenCalledWith options, jasmine.any(Function)
555+
556+
@jira.request.mostRecentCall.args[1] null, statusCode: 400
557+
expect(@cb).toHaveBeenCalledWith '400: Unable to connect to JIRA to add user as watcher.'
558+
559+
# Successful Request
560+
@jira.request.mostRecentCall.args[1] null, statusCode: 204
561+
expect(@cb).toHaveBeenCalledWith
562+
541563
it "Adds a worklog to a project", ->
542564
options =
543565
rejectUnauthorized: true

0 commit comments

Comments
 (0)