Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added .getRapidBoards and fixed crash issue with undefined response.body #113

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
249 changes: 146 additions & 103 deletions lib/jira.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor

var apiVersion = this.apiVersion;
if (altApiVersion != null) {
apiVersion = altApiVersion;
apiVersion = altApiVersion;
}

var uri = url.format({
Expand All @@ -173,17 +173,17 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor

this.doRequest = function(options, callback) {
if(oauth && oauth.consumer_key && oauth.consumer_secret) {
options.oauth = {
consumer_key: oauth.consumer_key,
consumer_secret: oauth.consumer_secret,
token: oauth.access_token,
token_secret: oauth.access_token_secret
};
options.oauth = {
consumer_key: oauth.consumer_key,
consumer_secret: oauth.consumer_secret,
token: oauth.access_token,
token_secret: oauth.access_token_secret
};
} else {
options.auth = {
'user': this.username,
'pass': this.password
};
options.auth = {
'user': this.username,
'pass': this.password
};
}
this.request(options, callback);
};
Expand Down Expand Up @@ -340,42 +340,85 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
this.findRapidView = function(projectName, callback) {

var options = {
rejectUnauthorized: this.strictSSL,
uri: this.makeUri('/rapidviews/list', 'rest/greenhopper/'),
method: 'GET',
json: true
rejectUnauthorized: this.strictSSL,
uri: this.makeUri('/rapidviews/list', 'rest/greenhopper/'),
method: 'GET',
json: true
};

this.doRequest(options, function(error, response) {

if (error) {
callback(error, null);
return;
}

if (response.statusCode === 404) {
callback('Invalid URL');
return;
}
if (error) {
callback(error, null);
return;
}

if (response.statusCode !== 200) {
callback(response.statusCode + ': Unable to connect to JIRA during rapidView search.');
return;
}
if (response.statusCode === 404) {
callback('Invalid URL');
return;
}

if (response.body !== null) {
var rapidViews = response.body.views;
for (var i = 0; i < rapidViews.length; i++) {
if(rapidViews[i].name.toLowerCase() === projectName.toLowerCase()) {
callback(null, rapidViews[i]);
if (response.statusCode !== 200) {
callback(response.statusCode + ': Unable to connect to JIRA during rapidView search.');
return;
}
}
}

});
if (response.body !== null) {
var rapidViews = response.body.views;
for (var i = 0; i < rapidViews.length; i++) {
if(rapidViews[i].name.toLowerCase() === projectName.toLowerCase()) {
callback(null, rapidViews[i]);
return;
}
}
}

});
};



// ## Get an array containing every rapid board within JIRA ##
// ### Takes ###
// * callback: for when it's done
//
// ### Returns ###
// * error: string of the error
// * rapidViews: array with all the rapid boards
this.getRapidBoards = function(callback) {

var options = {
rejectUnauthorized: this.strictSSL,
uri: this.makeUri('/rapidviews/list', 'rest/greenhopper/'),
method: 'GET',
json: true
};

this.doRequest(options, function (error, response) {

if (error) {
callback(error, null);
return;
}

if (response.statusCode === 404) {
callback('Invalid URL');
return;
}

if (response.statusCode !== 200) {
callback(response.statusCode + ': Unable to connect to JIRA during rapidView search.');
return;
}

if (response.body !== null) {
var rapidViews = response.body.views;
callback(null, rapidViews);
return;
}
});

};
// ## Get a list of Sprints belonging to a Rapid View ##
// ### Takes ###
//
Expand All @@ -395,35 +438,35 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
this.getLastSprintForRapidView = function(rapidViewId, callback) {

var options = {
rejectUnauthorized: this.strictSSL,
uri: this.makeUri('/sprintquery/' + rapidViewId, 'rest/greenhopper/'),
method: 'GET',
json:true
rejectUnauthorized: this.strictSSL,
uri: this.makeUri('/sprintquery/' + rapidViewId, 'rest/greenhopper/'),
method: 'GET',
json:true
};

this.doRequest(options, function(error, response) {

if (error) {
callback(error, null);
return;
}

if (response.statusCode === 404) {
callback('Invalid URL');
return;
}
if (error) {
callback(error, null);
return;
}

if (response.statusCode !== 200) {
callback(response.statusCode + ': Unable to connect to JIRA during sprints search.');
return;
}
if (response.statusCode === 404) {
callback('Invalid URL');
return;
}

if (response.body !== null) {
if (response.statusCode !== 200) {
callback(response.statusCode + ': Unable to connect to JIRA during sprints search.');
return;
}
if(response.body === null || response.body === undefined) {
callback("Response from Sprint Request ist either null or undefined");
return;
}
var sprints = response.body.sprints;
callback(null, sprints.pop());
return;
}

});
};

Expand All @@ -447,37 +490,37 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
*/
this.getSprintIssues = function getSprintIssues(rapidViewId, sprintId, callback) {

var options = {
rejectUnauthorized: this.strictSSL,
uri: this.makeUri('/rapid/charts/sprintreport?rapidViewId=' + rapidViewId + '&sprintId=' + sprintId, 'rest/greenhopper/'),
method: 'GET',
json: true
};
var options = {
rejectUnauthorized: this.strictSSL,
uri: this.makeUri('/rapid/charts/sprintreport?rapidViewId=' + rapidViewId + '&sprintId=' + sprintId, 'rest/greenhopper/'),
method: 'GET',
json: true
};

this.doRequest(options, function(error, response) {
this.doRequest(options, function(error, response) {

if (error) {
callback(error, null);
return;
}
if (error) {
callback(error, null);
return;
}

if( response.statusCode === 404 ) {
callback('Invalid URL');
return;
}
if( response.statusCode === 404 ) {
callback('Invalid URL');
return;
}

if( response.statusCode !== 200 ) {
callback(response.statusCode + ': Unable to connect to JIRA during sprints search');
return;
}
if( response.statusCode !== 200 ) {
callback(response.statusCode + ': Unable to connect to JIRA during sprints search');
return;
}

if(response.body !== null) {
callback(null, response.body);
} else {
callback('No body');
}
if(response.body !== null) {
callback(null, response.body);
} else {
callback('No body');
}

});
});

};

Expand All @@ -504,34 +547,34 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
this.addIssueToSprint = function(issueId, sprintId, callback) {

var options = {
rejectUnauthorized: this.strictSSL,
uri: this.makeUri('/sprint/' + sprintId + '/issues/add', 'rest/greenhopper/'),
method: 'PUT',
followAllRedirects: true,
json:true,
body: {
issueKeys: [issueId]
}
rejectUnauthorized: this.strictSSL,
uri: this.makeUri('/sprint/' + sprintId + '/issues/add', 'rest/greenhopper/'),
method: 'PUT',
followAllRedirects: true,
json:true,
body: {
issueKeys: [issueId]
}
};

logger.log(options.uri);

this.doRequest(options, function(error, response) {

if (error) {
callback(error, null);
return;
}
if (error) {
callback(error, null);
return;
}

if (response.statusCode === 404) {
callback('Invalid URL');
return;
}
if (response.statusCode === 404) {
callback('Invalid URL');
return;
}

if (response.statusCode !== 204) {
callback(response.statusCode + ': Unable to connect to JIRA to add to sprint.');
return;
}
if (response.statusCode !== 204) {
callback(response.statusCode + ': Unable to connect to JIRA to add to sprint.');
return;
}

});
};
Expand Down Expand Up @@ -1453,7 +1496,7 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
rejectUnauthorized: this.strictSSL,
uri: this.makeUri('/issue/' + issueId + '/comment'),
body: {
"body": comment
"body": comment
},
method: 'POST',
followAllRedirects: true,
Expand All @@ -1475,7 +1518,7 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
callback("Invalid Fields: " + JSON.stringify(body));
return;
};

callback(response.statusCode + ': Error while adding comment');
});
};
Expand Down