Import timecard from Jira Tempo integration
//runIntegration();
runIntegrationTest();
function runIntegration() {
var dateFrom = '2017-11-01';
var dateTo = '2017-11-30';
new JiraIntegration().execute(dateFrom, dateTo);
}
function runIntegrationTest() {
var dateFrom = '2017-11-01';
var dateTo = '2017-11-30';
var jiraKey = 'ASP';
new JiraIntegration().executeTest(dateFrom, dateTo, jiraKey);
}
var JiraIntegration = Class.create();
JiraIntegration.prototype = {
initialize: function() {
},
execute: function(dateFrom, dateTo) {
var responseBody, number;
var jiraKeyArray = this.getJiraKey();
for (var i = 0; i < jiraKeyArray.length; i++) {
responseBody = this.tempoRESTCall(dateFrom, dateTo, jiraKeyArray[i]);
if (responseBody != undefined) {
number = this.getProjectNumber(jiraKeyArray[i]);
this.insertTimeCard(responseBody, jiraKeyArray[i], number);
} else {
gs.log("No response from " + jiraKeyArray[i], "Script Include - JiraIntegration");
}
}
},
executeTest: function(dateFrom, dateTo, jiraKey) {
var responseBody, number;
responseBody = this.tempoRESTCall(dateFrom, dateTo, jiraKey);
if (responseBody != undefined) {
number = this.getProjectNumber(jiraKey);
this.insertTimeCard(responseBody, jiraKey, number);
} else {
gs.log("No response from " + jiraKey + " dateFrom: " + dateFrom + " dateTo: " + dateTo, "Script Include - JiraIntegration");
}
},
//Return unique JiraKey in an array
getJiraKey: function() {
var jiraKey = [];
var arrayUtil = new ArrayUtil();
var gr = new GlideRecord('pm_project');
gr.addNotNullQuery('u_ekb');
//gr.setLimit(5);
//gr.addQuery('u_ekb', 'ASP');
gr.query();
while (gr.next()) {
jiraKey.push(gr.u_ekb + '');
}
return arrayUtil.unique(jiraKey);
},
//Return project number
getProjectNumber : function(jiraKey) {
var gr = new GlideRecord('pm_project');
gr.addQuery('u_ekb', jiraKey);
gr.query();
if (gr.next()) {
return gr.number;
} else {
gs.log("Project number not found", "Script Include - JiraIntegration");
}
},
//Make REST API call
tempoRESTCall: function(dateFrom, dateTo, projectKey) {
var responseBody, httpStatus, response;
try {
var r = new sn_ws.RESTMessageV2('Jira Integration (Tempo)', 'get');
r.setStringParameterNoEscape('dateFrom', dateFrom);
r.setStringParameterNoEscape('dateTo', dateTo);
r.setStringParameterNoEscape('projectKey', projectKey);
response = r.execute();
responseBody = response.getBody();
httpStatus = response.getStatusCode();
}
catch(ex) {
var message = ex.getMessage();
}
if (response.haveError()) {
gs.log("Jira API Response: " + response.getErrorMessage() + " HTTP Status: " + httpStatus, "Script Include - JiraIntegration");
gs.log(message);
} else {
return responseBody;
}
},
//Insert a timecard record to the import table
insertTimeCard: function(responseBody, jiraKey, number) {
responseBody = JSON.parse(responseBody);
var gr = new GlideRecord('u_jira_integration_time_card_import');
responseBody.forEach(function(object) {
gr.initialize();
gr.u_timespentseconds = object.timeSpentSeconds;
gr.u_datestarted = object.dateStarted;
gr.u_comment = object.comment;
gr.u_jiraworklogid = object.jiraWorklogId;
gr.u_author = object.author.displayName;
gr.u_issuetype = object.worklogAttributes[0].value;
gr.u_task = number;
gr.u_jirakey = jiraKey;
gr.insert();
});
},
type: 'JiraIntegration'
};