JD2022-TU1/data/EngineData/SubmitAssistant/model/changelist.js

466 lines
15 KiB
JavaScript

define([
"storage_utils",
"regex",
"constants",
"ajax_conf",
"underscore",
"backbone",
],
function(storage_utils, regex, constants, ajax_conf){
/**
* Model to store the changelist.
*/
var ChangelistModel = Backbone.Model.extend({
defaults:{
//P4JsApi fields
"Change": "",
"Client": "",
"Description": "",
/* Files0, Files1, Files2, for each file of the CL */
"Status": "",
"User": "",
"type_SA": undefined,
//html fields
"scope_tag": "",
"domain_tag": [],
"type_tag": "",
"map_tag": [],
"jira_tag": [],
"description_field": "",
"changelog_field": "",
"reviewer_field": "",
"validator": "",
"scope_tag_content": [],
"reviewer_field_content": [],
// Adding details that will be required for the JIRA identification
"jira_user": "",
"failedToReachTeabuddy": "false",
"jira_id_description": [],
"isDefaultChangelist": "false",
//preflight details
"preflight_tag": "",
"use_preflight_tag":false,
//status
"is_valid": false,
},
/**
* See Backbone documentation.
*
* @param type_SA - the type of the SA.
*/
initialize: function(){
},
/**
* Override Backbone fetch to use storage utils command instead of ajax call.
*/
fetch: function () {
var currentCL = storage_utils.getCurrentCL();
// fetching the details of the JIRA issues that are assigned to the user
if (this.get("type_SA") === constants.type_code) {
var scope_tag_content = storage_utils.localLoad("scope_tags_list");
var reviewer_field_content = storage_utils.localLoad("reviewer_list");
// Make sure we have valid string before we move ahead with parsing
scope_tag_content = scope_tag_content.length > 0 ? JSON.parse(scope_tag_content) : [];
reviewer_field_content = reviewer_field_content.length > 0 ? JSON.parse(reviewer_field_content) : [];
// Now that we have parsed data assign it to the scope_tag_content model
this.set("scope_tag_content", scope_tag_content);
// Now that we have parsed data assign it to the reviewer_field_content model
this.set("reviewer_field_content", reviewer_field_content);
}
this.fetchJiraIdsOfUser(currentCL);
_.extend(this.attributes, this.parse(currentCL));
},
saveScopeTags: function () {
storage_utils.localSave("scope_tags_list", JSON.stringify(this.get("scope_tag_content")));
},
saveReviewerFields: function () {
storage_utils.localSave("reviewer_list", JSON.stringify(this.get("reviewer_field_content")));
},
//this function will fetch the details of the jira IDs of the user from the JIRA server
fetchJiraIdsOfUser: function(currentCL) {
var email = storage_utils.getEmail();
this.requestJIRAData(email);
this.set("jira_user", email);
},
// Request TeaBox for the details of the user Jira
requestJIRAData: function(email) {
var request = jQuery.ajax({
type: "GET",
url: "http://127.0.0.1:4242?name=GetJiraIssuesForUser",
});
var self = this;
request.done(function(response){
self.onSuccessOfJiraFetch(response);
});
request.fail(function(response) {
self.onFailureOfJiraFetch(response);
});
},
// The data was received from Jira
onSuccessOfJiraFetch: function(response) {
var obj = JSON.parse(response);
var arrayOfJiraIssues = [];
for (index = 0; index < obj.issues.length; index++) {
var _key = obj.issues[index].key;
var _description = obj.issues[index].fields.summary;
var temp = {id:_key, desc:_description};
arrayOfJiraIssues[index] = temp;
}
this.set("jira_id_description", arrayOfJiraIssues.reverse());
},
// If there is any issue in reaching TeaBox
onFailureOfJiraFetch: function(response) {
this.set("failedToReachTeabuddy", true);
},
/**
* Override Backbone parse method to fill in HTML fields.
*/
parse: function(currentCL){
if(currentCL.Status != "new"){
//saved CL
currentCL = this.parseDesc(currentCL);
}
else{
//default CL
currentCL = this.loadTag(currentCL);
}
return currentCL;
},
/**
* Add Preflight tag and status at direct submit.
*Preflight tag will be added before the description.
*/
checkAndAddPreflightTag: function () {
if (this.get("use_preflight_tag") && !this.get("preflight_tag")) {
var new_description = "[Preflight-000000,NOT_DONE]" + this.get("description_field");
this.set("description_field", new_description);
}
},
/**
* Parse the description from the original CL returned by the server.
*
* @param currentCL - the original CL returned by the server.
*
* return - an object containing original CL + all the parsed HTML fields.
*/
parseDesc: function(currentCL){
var type_SA = this.get("type_SA");
var description = currentCL.Description;
var jira_tag = description.match(regex.jira_tag);
if(jira_tag){
var i = 1;
var length = jira_tag.length;
while(! jira_tag[i] && i < length){
i++;
}
var jira_tab = jira_tag[i].split(",");
currentCL.jira_tag = jira_tab;
}
if(type_SA === constants.type_code) {
currentCL = this.parseDescCode(currentCL);
}
else if(type_SA === constants.type_data){
currentCL = this.parseDescData(currentCL);
}
return currentCL;
},
/**
* Parse the description for "code" SA.
*
* @param currentCL - the original CL.
*
* return - an object containing original CL + all the parsed HTML fields for "code" SA.
*/
parseDescCode: function (currentCL) {
var description = currentCL.Description;
var scope_tag = description.match(regex.first_tag);
var type_tag = description.match(regex.second_tag);
var desc_code = description.match(regex.desc_code);
var changelog = description.match(regex.changelog);
var reviewer = description.match(regex.reviewer);
var preflight_tag = description.match(regex.preflight_tag);
//single cases RegEx, parentheses match is always at index 1
if(scope_tag){
currentCL.scope_tag = $.trim(scope_tag[1]);
// will add the scope tag that we picked from the description to the list of scope tags incase its not present
var elements = this.get("scope_tag_content");
var _tempModel = this;
_.each(currentCL.scope_tag.split(','), function(searchItem) {
var foundElement = _.find(elements, function (item) {
return item.text.toLowerCase() == searchItem.toLowerCase();
});
if (foundElement == undefined) {
elements.push({ id: searchItem, text: searchItem });
_tempModel.saveScopeTags();
}
});
}
if(type_tag){
currentCL.type_tag = type_tag[1];
}
if (preflight_tag) {
this.set("preflight_tag", preflight_tag[1]);
}
if (reviewer) {
currentCL.reviewer_field = $.trim(reviewer[1]);
// will add the reviewr data that we picked from the description to the list of scope tags incase its not present
var elements = this.get("reviewer_field_content");
var _tempModel = this;
_.each(currentCL.reviewer_field.split(','), function (searchItem) {
var foundElement = _.find(elements, function (item) {
return item.text.toLowerCase() == searchItem.toLowerCase();
});
if (foundElement == undefined) {
elements.push({ id: searchItem, text: searchItem });
_tempModel.saveReviewerFields();
}
});
}
//multiple cases RegEx, we have to find the parentheses match between all the cases
if(desc_code){
var i = 1;
var length = desc_code.length;
while(! desc_code[i] && i < length){
i++;
}
currentCL.description_field = $.trim(desc_code[i]);
}
if(changelog){
var i = 1;
var length = changelog.length;
while(! changelog[i] && i < length){
i++;
}
currentCL.changelog_field = $.trim(changelog[i]);
}
//treat unformated description
if(! currentCL.scope_tag && ! currentCL.type_tag && ! currentCL.description_field &&
! currentCL.changelog_field){
currentCL = this.loadTag(currentCL);
currentCL.description_field = $.trim(description);
}
return currentCL;
},
/**
* Parse the description for "data" SA.
*
* @param currentCL - the original CL.
*
* return - an object containing original CL + all the parsed HTML fields for "data" SA.
*/
parseDescData: function(currentCL){
var description = currentCL.Description;
var domain_tag = description.match(regex.first_tag);
var map_list = description.match(regex.second_tag);
var desc_data = description.match(regex.desc_data);
var preflight_tag = description.match(regex.preflight_tag);
if(domain_tag){
var domain_tab = domain_tag[1].split(",");
currentCL.domain_tag = domain_tab;
}
if(map_list){
var map_tab = map_list[1].split(",");
currentCL.map_tag = map_tab;
}
if (preflight_tag) {
this.set("preflight_tag", preflight_tag[1]);
}
if(desc_data){
var i = 1;
var length = desc_data.length;
while(! desc_data[i] && i < length){
i++;
}
currentCL.description_field = $.trim(desc_data[i]);
}
//treat unformated description
if(! currentCL.domain_tag && ! currentCL.map_tag && ! currentCL.description_field){
currentCL = this.loadTag(currentCL);
currentCL.description_field = $.trim(description);
}
return currentCL;
},
/**
* Load the first tag of the description from the localstorage.
*
* @param currentCL - the original CL.
*
* @return - an object containing original CL + saved tag
*/
loadTag: function(currentCL){
var type_SA = this.get("type_SA");
if(type_SA === constants.type_code){
//currentCL.scope_tag = storage_utils.localLoad("scope_tag");
}
else if(type_SA === constants.type_data){
var domain_tab = storage_utils.localLoad("domain_tag").split(",");
currentCL.domain_tag = domain_tab;
}
return currentCL;
},
/**
* Return the formated changelist with all the description fields.
*
* @return - the formated description.return - the formated description
*/
getFormatedDescription: function(){
var formated_description = "";
var type_SA = this.get("type_SA");
var validator = this.get("validator");
if(type_SA === constants.type_code){
formated_description = this.getFormatedDescriptionCode(formated_description);
}
else if(type_SA === constants.type_data){
formated_description = this.getFormatedDescriptionData(formated_description);
}
if(validator){
formated_description += "\n" + constants.validator + " " + validator;
}
//add tabs to the beginning of each line to make it
//a legal changelist description:
formated_description = "\t" + formated_description.replace(/\n/gm, '\n\t');
return formated_description;
},
/**
* Return the formated changelist with all the fields of "code" SA.
*
* return - the formated description for "code" SA.
*/
getFormatedDescriptionCode: function(formated_description){
var scope_tag = this.get("scope_tag");
var type_tag = this.get("type_tag");
var jira_tag = this.get("jira_tag");
var description_field = this.get("description_field");
var changelog_field = this.get("changelog_field");
var reviewer_field = this.get("reviewer_field");
formated_description += "[" + scope_tag + "]"
+ "[" + type_tag + "]";
if(jira_tag.length){
formated_description += "[" + jira_tag + "]";
}
if(description_field){
formated_description += " " + description_field + " ";
}
if(changelog_field){
formated_description += "[" + constants.changelog + "] " + changelog_field;
}
if (reviewer_field) {
formated_description += "\n" + constants.reviewer + " " + reviewer_field;
}
return formated_description;
},
/**
* Return the formated changelist with all the fields of "data" SA.
*
* return - the formated description for "data" SA.
*/
getFormatedDescriptionData: function(formated_description){
var domain_tag = this.get("domain_tag");
var map_tag = this.get("map_tag");
var jira_tag = this.get("jira_tag");
var description_field = this.get("description_field");
formated_description += "[" + domain_tag + "]";
if (map_tag.length) {
formated_description += "[" + map_tag + "]";
} else {
formated_description += "[NoMap]";
}
if(jira_tag.length){
formated_description += "[" + jira_tag + "]";
}
formated_description += " " + description_field;
return formated_description;
},
});
return ChangelistModel;
});