diff --git a/.project b/.project new file mode 100644 index 00000000..323963b5 --- /dev/null +++ b/.project @@ -0,0 +1,11 @@ + + + node-jira + + + + + + + + diff --git a/lib/jira.js b/lib/jira.js index e919d9da..a5dfb6e0 100644 --- a/lib/jira.js +++ b/lib/jira.js @@ -132,13 +132,20 @@ var url = require('url'), OAuth = require("oauth"); -var JiraApi = exports.JiraApi = function(protocol, host, port, username, password, apiVersion, verbose, strictSSL, oauth) { +var JiraApi = exports.JiraApi = function(protocol, host, port, username, password, apiVersion, verbose, strictSSL, oauth,isLocal) { this.protocol = protocol; this.host = host; this.port = port; this.username = username; this.password = password; this.apiVersion = apiVersion; + + this.isLocal=false; + if(isLocal!=null) + { + this.isLocal=isLocal; + } + // Default strictSSL to true (previous behavior) but now allow it to be // modified if (strictSSL == null) { @@ -152,7 +159,12 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor // This is the same almost every time, refactored to make changing it // later, easier this.makeUri = function(pathname, altBase, altApiVersion) { - var basePath = 'rest/api/'; + + var basePath = 'rest/api/'; + if(this.isLocal){ + var basePath = 'jira/rest/api/'; + } + if (altBase != null) { basePath = altBase; } @@ -161,7 +173,7 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor if (altApiVersion != null) { apiVersion = altApiVersion; } - + var uri = url.format({ protocol: this.protocol, hostname: this.host, @@ -1916,5 +1928,264 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor callback(response.statusCode + ': Error while retrieving backlog'); }); }; + + /** + * Create a new filter for this user. + * var filterParams = { + * "name": "All Open Bugs", + * "description": "Lists all open bugs", + * "jql": "type = Bug and resolution is empty", + * "favourite": true + * } + */ + this.createFilter = function(filterParams,callback){ + + var options = { + rejectUnauthorized: this.strictSSL, + uri: this.makeUri('/filter'), + method: 'POST', + json: true, + body: filterParams + }; + console.log(options); + this.doRequest(options, function(error, response) { + //console.log(response.body); + if (error) { + callback(error, null); + + return; + } + + if (response.statusCode === 200) { + if (typeof response.body.errors === "undefined"){ + callback(null, response.body); + } + else { + callback(response.body.errors); + } + return; + } + callback(response.statusCode +' : ' +response.body.errors[0]); + }); + + }; + + + + /** + * Change the default scope for filters that the user makes + * } + */ + this.changeFilterShareScope = function(bGlobal,callback){ + + var options = { + rejectUnauthorized: this.strictSSL, + uri: this.makeUri('/filter/defaultShareScope'), + method: 'PUT', + json: true, + body: {"scope": (bGlobal)?"GLOBAL":"LOCAL"}, + }; + this.doRequest(options, function(error, response) { + //console.log(response.body); + if (error) { + callback(error, null); + return; + } + + if (response.statusCode === 200) { + if (typeof response.body.errors === "undefined"){ + callback(null, response.body); + } + else { + callback(response.body.errors); + } + return; + } + callback(response.statusCode +' : ' +response.body.errors[0]); + }); + + }; + + + /** + * Update an existing filter for this user. + * var filterParams = { + * "name": "All Open Bugs", + * "description": "Lists all open bugs", + * "jql": "type = Bug and resolution is empty", + * "favourite": true + * } + */ + this.updateFilter = function(filterId,filterParams,callback){ + + var options = { + rejectUnauthorized: this.strictSSL, + uri: this.makeUri('/filter/'+filterId), + method: 'PUT', + json: true, + body: filterParams + }; + this.doRequest(options, function(error, response) { + //console.log(response.body); + if (error) { + callback(error, null); + return; + } + + if (response.statusCode === 200) { + if (typeof response.body.errors === "undefined"){ + callback(null, response.body); + } + else { + callback(response.body.errors); + } + return; + } + callback(response.statusCode +' : ' +response.body.errors[0]); + }); + + }; + + /** + * deletes filter with id filterid + */ + this.deleteFilter = function(filterId, callback) { + + var options = { + rejectUnauthorized : this.strictSSL, + uri : this.makeUri('/filter/' + filterId), + method : 'DELETE', + json : false + }; + this.doRequest(options, function(error, response) { + //console.log(response.body); + if (error) { + callback(error, null); + return; + } + callback(null, response); + }); + }; + + /** + * get filter with name from fav filters of this user + */ + this.findFilter = function(name, callback) { + var options = { + rejectUnauthorized : this.strictSSL, + uri : this.makeUri('/filter/favourite'), + method : 'GET', + json : true + }; + this.doRequest(options, function(error, response) { + //console.log(response.body); + if (error) { + callback(error, 0); + return; + } + var id = 0; + for (var i = 0; i < response.body.length; i++) { + var item=response.body[i]; + if (item.name == name) { + id = item.id; + break; + } + } + if(id==0) console.log("could not find a filter with name "+name); + callback(null, id); + }); + }; + + /** + * get filters with names from fav filters of this user + */ + this.findFilters = function(names, callback) { + var options = { + rejectUnauthorized : this.strictSSL, + uri : this.makeUri('/filter/favourite'), + method : 'GET', + json : true + }; + this.doRequest(options, function(error, response) { + //console.log(response.body); + if (error) { + callback(error,null); + return; + } + names.errors=false; + for(var j =0; j < names.length; j++){ + names[j].id=0; + for (var i = 0; i < response.body.length; i++) { + var item=response.body[i]; + if (item.name == names[j].name) { + names[j].id=item.id; + break; + } + } + if(names[j].id==0){ + console.log("could not find a filter with name "+names[j].name); + names.errors=true; //at least one filter was not found + delete names[j].id; + } + } + callback(null, names); + }); + }; + + /** + * get details of a version + */ + this.getVersion = function(id, callback) { + var options = { + rejectUnauthorized : this.strictSSL, + uri : this.makeUri('/version/'+id+'/relatedIssueCounts'), + method : 'GET', + json : true + }; + var self=this; + this.doRequest(options, function(error, response) { + //console.log(response.body); + if (response){ + options.uri=self.makeUri('/version/'+id+'/unresolvedIssueCount'); + + self.doRequest(options, function(error, resp2) { + if(resp2){ + //merge the resp2 + response.body.issuesUnresolvedCount=resp2.body.issuesUnresolvedCount; + callback(null, response.body); + } + else if (error) { + callback(error, 0); + } + else{ + callback("Unknown error", 0); + } + }); + } + else if (error) { + callback(error, 0); + } + else{ + callback("Unknown error", 0); + } + }); + }; + + this.deleteVersion = function(id, callback) { + var options = { + rejectUnauthorized : this.strictSSL, + uri : this.makeUri('/version/'+id), + method : 'DELETE', + json : true + }; + this.doRequest(options, function(error, response) { + if(error){ + console.log("Error deleting version "+id+" : "+error); + } + if(callback){ + callback(error, response); + } + }); + }; }).call(JiraApi.prototype);