Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

how to capture the value of updated custom field when specific value added or removed

J Manas Kumar
Contributor
March 14, 2020

HI My Basic requirement is , I have parent issue and 2 child issues.

Ex:- parent custom field= 1,2,3,4   . child field 1 = 1,2,3,4   .Child field 2 = 1,2,3

what i want is when ever i will remove any value from child issue it will check that if the removed value is present in other child issues or not , if not it can remove the value from parent issue, if the value is present in other child issues then no need to change values in parent issue.

i am able to do the first scenario(when removed value from one child issue is present in other child issue, so dont delete in parent level).

 

but for the second conition i am facing issues, as i am using removeAll() method its removing all unique one , not the specific element.

Can anyone help on this ? Any help will be appreciated.

 

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.customfields.option.Option

import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.issue.index.IssueIndexingService

import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.issue.search.SearchResults

import org.apache.log4j.Logger
import org.apache.log4j.Level ;

log.setLevel(Level.DEBUG) ;

Issue epicIssue = event.issue
String issuetype = epicIssue.getIssueType().getName()

if(issuetype == "Epic"){
if (event?.getChangeLog()?.getRelated("ChildChangeItem")?.find { it.field == "Impacted Application/s" }) {

def changeItem = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find { it.field == "Impacted Application/s" }
String[] newValue = changeItem.get("newstring").toString().split(';')
String[] oldValue = changeItem.get("oldstring").toString().split(';')

log.debug("New Value: "+newValue)
log.debug("Old Value: "+oldValue)

//Split and get array of entries per CSI

if( (oldValue[0] == 'null') || newValue.size() < oldValue.size()){

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def ImpactedAppField = customFieldManager.getCustomFieldObjectByName("Impacted Application/s")
String ImpactedAppValue = epicIssue.getCustomFieldValue(ImpactedAppField)
log.debug("Value in Impacted App field is: "+ImpactedAppValue)
log.debug("Value in Event is: "+ event?.getChangeLog()?.getRelated("ChildChangeItem")?.find { it.field == "Impacted Application/s" })

def change = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find { it.field == "Impacted Application/s" }
log.debug("Value In CHANGE is : ///////" + change)

def cf = customFieldManager.getCustomFieldObjectByName("Parent Link")
def parentValue = epicIssue.getCustomFieldValue(cf)

if(parentValue != null){
// log.debug("parent Value is: "+parentValue)

def issueManager = ComponentAccessor.getIssueManager() ;
Issue PlanviewIssue = issueManager.getIssueObject("" + parentValue);
log.debug("Planview Issue is: "+PlanviewIssue)
String planviewImpactedApp = PlanviewIssue.getCustomFieldValue(ImpactedAppField)
log.debug("Planview Impacted Apps are: "+planviewImpactedApp)

if(planviewImpactedApp != null){
def userManager = ComponentAccessor.getUserManager()
ApplicationUser admin = userManager.getUserByName("INT_JIRA")
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser.class)
def searchService = ComponentAccessor.getComponent(SearchService)
def searchProvider = ComponentAccessor.getComponent(SearchProvider.class)



String[] impacted = ImpactedAppValue.split("\\s*[;]\\s*");

List<String> fixedLenghtList1 = Arrays.asList(impacted);

ArrayList<String> listOfEpicValues = new ArrayList<String>(fixedLenghtList1);

log.info "Printing listOfEpicValues For Epic Issuee Value : --" + listOfEpicValues

String[] planViewImpact = planviewImpactedApp.split("\\s*[;]\\s*");

List<String> fixedLenghtList = Arrays.asList(planViewImpact);

ArrayList<String> listOfPlanViewValues = new ArrayList<String>(fixedLenghtList);

ArrayList<String> newList = new ArrayList<String>(fixedLenghtList);

log.debug("Printing New List Parent --- before Subtract ---->>>>" + newList)


if(planviewImpactedApp != ImpactedAppValue){

listOfPlanViewValues.removeAll(listOfEpicValues)
log.debug("Printing listOfPlanViewValues after Subtract --->>>>" + listOfPlanViewValues)




ArrayList<String> duplicatevalues = new ArrayList<String>();
ArrayList<String> uniquevalues = new ArrayList<String>();

for (String item : listOfEpicValues) {
if (listOfPlanViewValues.contains(item)) {
duplicatevalues.add(item);
log.debug("Printing duplicatevalues after --->>>>" + duplicatevalues)
} else {
uniquevalues.add(item);
log.debug("Printing uniquevalues after --->>>>" + uniquevalues)
}
}

def removedAppId = listOfPlanViewValues.toString().replace("[", "").replace("]", "");

log.debug("Printing removedAppId after --->>>>" + removedAppId)


def query = "\"Parent Link\" = ${PlanviewIssue.key} AND issuetype = Epic"

SearchService.ParseResult parseResult = searchService.parseQuery(admin, query)
def searchResult = searchService.search(admin, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
def issue1 = searchResult.getResults()
log.debug("Getting Issues1 values :--------"+issue1)
searchResult.getResults().each {documentIssue ->

def query2 = "\"Parent Link\" = ${PlanviewIssue.key} AND issuetype = Epic AND 'Impacted Application/s' ~ '"+removedAppId+"'"
SearchService.ParseResult parseResult1 = searchService.parseQuery(admin, query2)
def result = searchService.search(admin, parseResult1.getQuery(), PagerFilter.getUnlimitedFilter())

def issue2 = result.getResults()
log.debug("Getting Issues2 values :--------"+issue2)

/*Here i am checking the value which has been deleted from Epic & is it present in any other epic or not */

if(result.getTotal()>0){
log.debug("it is coming to If Block....-->>>")
// do nothing

}else{

newList.removeAll(Collections.singleton(removedAppId));
/*
for(String str: listOfPlanViewValues){
if(str == removedAppId){
newList.remove(removedAppId)
log.debug("Prinitng newList inside esle --> for --> if -->....-->>>" + newList)
}
} */

// newList.removeAll(listOfPlanViewValues)
log.debug("it is coming to Else Block....-->>>")




def planviewValues = newList as String

log.debug("Printing PlanView Valuessssssss :-->>>>>>>>" + planviewValues)



String SEPARATOR = ";";

StringBuilder csvBuilder = new StringBuilder();

for(String app : newList){
csvBuilder.append(app);
csvBuilder.append(SEPARATOR);
}

String csv = csvBuilder.toString();

//Remove last semi column
csv = csv.substring(0, csv.length() - SEPARATOR.length());
log.debug("Printing csv :-->>>>>>>>" + csv)

// def newPlanviewvalue = csv + ";" + planviewImpactedApp

// log.debug("Printing newPlanviewvalue :-->>>>>>>>" + newPlanviewvalue)

modifyValue(admin,PlanviewIssue,ImpactedAppField, csv)
}
}
}
}
else{
}
}
}

}
}

boolean modifyValue(ApplicationUser user, Issue issue, CustomField customField, String value){

IssueService issueService = ComponentAccessor.getComponent(IssueService);
IssueInputParameters issueInputParameters = issueService.newIssueInputParameters()
issueInputParameters.addCustomFieldValue(customField.id, value)
def update = issueService.validateUpdate(user, issue.getId(), issueInputParameters)

/* validate update info and execute update */

if (update.isValid()) {

IssueService.IssueResult status = issueService.update(user, update)
if (!status.isValid())
{
for (def error : status) {

}
} else {

}
} else {


IssueService.IssueResult status = issueService.update(user, update)

for (def error : status) {

}
}

return true
}

def String[] separateCSI(String apps){

def log = Logger.getLogger("com.onresolve.scriptrunner.runner.ScriptRunnerImpl")

//need to take out the [ ] that show up under my CSI's
apps = apps.trim()
log.warn(apps)
//if(apps.contains("\\[")) apps = apps.split("\\[")
def counter = 0
String[] result = apps.split(';')
result.each { String csi ->
if(csi.contains('[')){
String[] s = csi.split('\\[')
result[counter] = s[0].trim()
} else if(csi.contains('(')){
String[] s = csi.split('\\(')
result[counter] = s[0].trim()
} else if(csi.contains('-')){
String[] s = csi.split('\\-')
result[counter] = s[0].trim()
} else result[counter] = csi.trim()

counter++

}

//Change this line 10/15 from result to result.toUnique() to remove duplicates
return result.toUnique();

}
log.debug(""+epicIssue.key)

 

 

Regards

Manas

1 answer

0 votes
Ashkan Malekly
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 15, 2020

Hi @J Manas Kumar 

Well i didn't read the whole of your content, but i suggest you setup automation for Jira plugin. And it is very flexible to write condition and validator and post-function. If you have it, i can guide you to accomplish your goal.

Suggest an answer

Log in or Sign up to answer