Skip to content

Commit 280e4cc

Browse files
committed
Merge pull request #54 from nagyv/master
OAuth support + new estimates on addWorklog
2 parents 991b15b + 7ed0950 commit 280e4cc

File tree

3 files changed

+85
-11
lines changed

3 files changed

+85
-11
lines changed

lib/jira.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
// * `Jira API Version<string>`: Known to work with `2` and `2.0.alpha1`
5050
// * `verbose<bool>`: Log some info to the console, usually for debugging
5151
// * `strictSSL<bool>`: Set to false if you have self-signed certs or something non-trustworthy
52-
//
52+
// * `oauth`: A disctionary of `consumer_key`, `consumer_secret`, `access_token` and `access_token_secret` to be used for OAuth authentication.
53+
//
5354
// ## Implemented APIs ##
5455
//
5556
// * Authentication
@@ -122,10 +123,11 @@
122123
// * _0.0.3 Added APIs and Docco documentation_
123124
// * _0.0.2 Initial version_
124125
var url = require('url'),
125-
logger = console;
126+
logger = console,
127+
OAuth = require("oauth");
126128

127129

128-
var JiraApi = exports.JiraApi = function(protocol, host, port, username, password, apiVersion, verbose, strictSSL) {
130+
var JiraApi = exports.JiraApi = function(protocol, host, port, username, password, apiVersion, verbose, strictSSL, oauth) {
129131
this.protocol = protocol;
130132
this.host = host;
131133
this.port = port;
@@ -165,11 +167,19 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
165167
};
166168

167169
this.doRequest = function(options, callback) {
168-
options.auth = {
170+
if(oauth && oauth.consumer_key && oauth.consumer_secret) {
171+
options.oauth = {
172+
consumer_key: oauth.consumer_key,
173+
consumer_secret: oauth.consumer_secret,
174+
token: oauth.access_token,
175+
token_secret: oauth.access_token_secret
176+
};
177+
} else {
178+
options.auth = {
169179
'user': this.username,
170180
'pass': this.password
171-
};
172-
181+
};
182+
}
173183
this.request(options, callback);
174184
};
175185

@@ -1355,10 +1365,14 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
13551365
* "id": "100028"
13561366
* }
13571367
*/
1358-
this.addWorklog = function(issueId, worklog, callback) {
1368+
this.addWorklog = function(issueId, worklog, newEstimate, callback) {
1369+
if(typeof callback == 'undefined') {
1370+
callback = newEstimate
1371+
newEstimate = false
1372+
}
13591373
var options = {
13601374
rejectUnauthorized: this.strictSSL,
1361-
uri: this.makeUri('/issue/' + issueId + '/worklog'),
1375+
uri: this.makeUri('/issue/' + issueId + '/worklog' + (newEstimate ? "?adjustEstimate=new&newEstimate=" + newEstimate : "")),
13621376
body: worklog,
13631377
method: 'POST',
13641378
followAllRedirects: true,

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
}
2323
],
2424
"dependencies": {
25-
"request": "<2.16.0"
25+
"request": "<2.16.0",
26+
"oauth": "^0.9.11"
2627
},
2728
"scripts": {
2829
"test": "grunt test"
@@ -33,6 +34,7 @@
3334
"grunt-docco": "^0.3.3",
3435
"grunt-jasmine-node": "^0.2.1",
3536
"coffee-script": "^1.7.1",
36-
"grunt-jslint": "^1.1.8"
37+
"grunt-jslint": "^1.1.8",
38+
"rewire": "^2.0.0"
3739
}
3840
}

spec/jira.spec.coffee

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
url = require 'url'
22

3-
nodeJira = require '../lib/jira'
3+
rewire = require 'rewire'
4+
nodeJira = rewire '../lib/jira'
45

56
describe "Node Jira Tests", ->
67
makeUrl = (path, altBase) ->
@@ -15,10 +16,38 @@ describe "Node Jira Tests", ->
1516

1617

1718
beforeEach ->
19+
OAuth = nodeJira.__get__ "OAuth"
20+
OAuth.OAuth.prototype = jasmine.createSpyObj 'OAuth', ['getOAuthRequestToken', '_encodeData']
21+
nodeJira.__set__ "OAuth", OAuth
22+
1823
@jira = new nodeJira.JiraApi 'http', 'localhost', 80, 'test', 'test', 2
1924
spyOn @jira, 'request'
2025
@cb = jasmine.createSpy 'callback'
2126

27+
it "Sets basic auth if oauth is not passed in", ->
28+
options =
29+
auth =
30+
user: 'test'
31+
pass: 'test'
32+
@jira.doRequest options, @cb
33+
expect(@jira.request)
34+
.toHaveBeenCalledWith(options, jasmine.any(Function))
35+
36+
it "Sets OAuth oauth for the requests if oauth is passed in", ->
37+
options =
38+
oauth =
39+
consumer_key: 'ck'
40+
consumer_secret: 'cs'
41+
access_token: 'ac'
42+
access_token_secret: 'acs'
43+
# oauth = new OAuth.OAuth(null, null, oauth.consumer_key, oauth.consumer_secret, null, null, "RSA-SHA1")
44+
@jira = new nodeJira.JiraApi 'http', 'localhost', 80, 'test', 'test', 2, false, false, options.oauth
45+
spyOn @jira, 'request'
46+
47+
@jira.doRequest options, @cb
48+
expect(@jira.request)
49+
.toHaveBeenCalledWith(options, jasmine.any(Function))
50+
2251
it "Sets strictSSL to false when passed in", ->
2352
expected = false
2453
jira = new nodeJira.JiraApi 'http', 'localhost', 80, 'test', 'test', 2, false, expected
@@ -529,6 +558,35 @@ describe "Node Jira Tests", ->
529558
@jira.request.mostRecentCall.args[1] null, statusCode:201
530559
expect(@cb).toHaveBeenCalledWith null, "Success"
531560

561+
it "Adds a worklog to a project with remaining time set", ->
562+
options =
563+
rejectUnauthorized: true
564+
uri: makeUrl "issue/1/worklog?adjustEstimate=new&newEstimate=1h"
565+
body: 'aWorklog'
566+
method: 'POST'
567+
followAllRedirects: true
568+
json: true
569+
auth:
570+
user: 'test'
571+
pass: 'test'
572+
573+
@jira.addWorklog 1, 'aWorklog', '1h', @cb
574+
expect(@jira.request).toHaveBeenCalledWith options, jasmine.any(Function)
575+
576+
@jira.request.mostRecentCall.args[1] null, statusCode:400,
577+
'{"body:"none"}'
578+
expect(@cb).toHaveBeenCalledWith 'Invalid Fields: "{\\"body:\\"none\\"}"'
579+
580+
@jira.request.mostRecentCall.args[1] null, statusCode:403
581+
expect(@cb).toHaveBeenCalledWith 'Insufficient Permissions'
582+
583+
@jira.request.mostRecentCall.args[1] null, statusCode:401
584+
expect(@cb).toHaveBeenCalledWith '401: Error while updating'
585+
586+
# Successful Request
587+
@jira.request.mostRecentCall.args[1] null, statusCode:201
588+
expect(@cb).toHaveBeenCalledWith null, "Success"
589+
532590
it "Lists Issue Types", ->
533591
options =
534592
rejectUnauthorized: true

0 commit comments

Comments
 (0)