My Script:
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.user.ApplicationUsers
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.scriptrunner.runner.util.UserMessageUtil
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.JiraException
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.changehistory.ChangeHistoryManager
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.search.SearchException
import com.atlassian.jira.issue.search.SearchResults
import com.atlassian.jira.security.JiraAuthenticationContext
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.project.ProjectCategory
import com.atlassian.jira.project.Project
def userManager = ComponentAccessor.getUserManager()
def groupManager = ComponentAccessor.getGroupManager()
// logged user
ApplicationUser user = event.user
log.warn(user)
// current issue
Issue issue = event.issue
log.error("Epic issue updated event: update curent issue key**** "+ issue.getKey())
def issueType = issue.getIssueType().getName()
ProjectCategory pCategory=ComponentAccessor.getProjectManager().getProjectCategoryForProject(issue.getProjectObject());
Project proj = issue.getProjectObject();
if( pCategory.getName().equals("Value Stream") && issueType.equals("Epic")){
def customFieldManager = ComponentAccessor.getCustomFieldManager()
String jqlQuery = "'Epic Link' ="+issue.getKey();
def storyPointCF = customFieldManager.getCustomFieldObject("customfield_10106") //Storypoint customfield ID
Integer issueLinkSPCFValue = (int)issue.getCustomFieldValue(storyPointCF);
def hiddenStoryPointCF = customFieldManager.getCustomFieldObject("customfield_10200") // hidden story points
Integer hiddenSPCFValue = issue.getCustomFieldValue(hiddenStoryPointCF) != null?(int)issue.getCustomFieldValue(hiddenStoryPointCF) : 0;
log.error("Epic issue updated event: hiddenStoryPointCFValue "+hiddenSPCFValue );
// issue story point value
def remainingStoryPointCF = customFieldManager.getCustomFieldObject("customfield_10114") //my remaining storypoint Customfield ID
def allocatedStoryPointCF = customFieldManager.getCustomFieldObject("customfield_10112") // allocated story points id
Integer currentIssueSPCFValue = (int)(issue.getCustomFieldValue(storyPointCF))
Integer remainingStoryPointCFValue = issue.getCustomFieldValue(remainingStoryPointCF) != null ? (int)(issue.getCustomFieldValue(remainingStoryPointCF)) : 0;
Integer allocatedStoryPointCFValue = issue.getCustomFieldValue(allocatedStoryPointCF) != null ? (int)(issue.getCustomFieldValue(allocatedStoryPointCF)) : 0;
//calculate remaining and allocated on update
if((issueLinkSPCFValue >= 0) && (hiddenSPCFValue != issueLinkSPCFValue)){
log.error("Epic issue updated event: before update log remaining "+remainingStoryPointCFValue );
log.error("Epic issue updated event: before update currentIssueSPCFValue "+currentIssueSPCFValue );
log.error("Epic issue updated event: before update allocatedStoryPointCFValue "+allocatedStoryPointCFValue );
def remainingSPValue = 0;
def allocatedSPValue = 0;
int updatedSPValue = 0 ; // (issueLinkSPCFValue > hiddenSPCFValue) ? (issueLinkSPCFValue - hiddenSPCFValue) : (hiddenSPCFValue - issueLinkSPCFValue )
if(issueLinkSPCFValue < hiddenSPCFValue){
updatedSPValue = hiddenSPCFValue - issueLinkSPCFValue
remainingSPValue = remainingStoryPointCFValue - updatedSPValue // this case add the updated story point value
//allocatedSPValue = allocatedStoryPointCFValue - updatedSPValue
} else if (issueLinkSPCFValue > hiddenSPCFValue) {
updatedSPValue = issueLinkSPCFValue - hiddenSPCFValue
remainingSPValue = remainingStoryPointCFValue + updatedSPValue
//allocatedSPValue = allocatedStoryPointCFValue + updatedSPValue
} else {
}
// Updated story point value - It should not decreased upto remaining value otherwise shows the error message
// and updated the previous value from the hidden
if(issueLinkSPCFValue < allocatedStoryPointCFValue){
updateHiddenField(storyPointCF, (MutableIssue)issue, hiddenSPCFValue);
UserMessageUtil.error("The updated story point value should not be dicreased by Remaining Story Points value. You can decrese the story points value upto " + remainingStoryPointCFValue)
return;
} else if (issueLinkSPCFValue > hiddenSPCFValue) {
// Updated story point value - It can be increased. Yes, calculate the remaining value according to that.
updatedSPValue = issueLinkSPCFValue - hiddenSPCFValue
remainingSPValue = remainingStoryPointCFValue + updatedSPValue
}
if(hasIssues(jqlQuery) && remainingSPValue>=0) {
updateHiddenField(remainingStoryPointCF, (MutableIssue)issue, remainingSPValue);
log.error("Epic issue updated event: successfully updated remaining "+remainingSPValue)
}
/*if(allocatedSPValue>=0) {
updateHiddenField(allocatedStoryPointCF, ()parentIssue, allocatedSPValue);
log.error("successfully updated allocated "+allocatedSPValue)
}*/
}
// update hidden story point value with new story point value
Integer valueSPCFValue = issue.getCustomFieldValue(hiddenStoryPointCF) != null ? (int)(issue.getCustomFieldValue(hiddenStoryPointCF)) : 0;
log.error("Epic issue updated event: hiddenStoryPointCFValue "+valueSPCFValue );
updateHiddenField(hiddenStoryPointCF,(MutableIssue)issue,issueLinkSPCFValue);
}
def void updateHiddenField(CustomField customField,MutableIssue currentIssue, int newValue ){
def changeHolder = new DefaultIssueChangeHolder()
customField.updateValue(null, currentIssue, new ModifiedValue(currentIssue.getCustomFieldValue(customField), Double.valueOf(newValue)), changeHolder)
}
def boolean hasIssues(String jql){
try{
log.error("has issues "+jql);
final SearchService searchServ = ComponentAccessor.getComponent(SearchService.class);
ApplicationUser user = ComponentAccessor.getUserManager().getUserByName("admin")
SearchResults results = null;;
final SearchService.ParseResult parseResult= searchServ.parseQuery(user, (jql.toString()));
if (parseResult.isValid()){
results = searchServ.search(user,parseResult.getQuery(), PagerFilter.getUnlimitedFilter());
List<Issue> issues = results.getResults();
log.error(" issues "+issues);
if(issues.isEmpty() ){
return false;
}
}
return true;
}catch(Exception e){
}
}
Please point out the line of code where you are facing issue so we can help you.
@Muhammad Ramzan(Atlassian Certified Master), The above Script code we write completely Integer data type, now this code completely we are convert in float datatype. how to convert?
def storyPointCF = customFieldManager.getCustomFieldObject("customfield_10106") //Storypoint customfield ID
Integer issueLinkSPCFValue = (int)issue.getCustomFieldValue(storyPointCF);
def hiddenStoryPointCF = customFieldManager.getCustomFieldObject("customfield_10200") // hidden story points
Integer hiddenSPCFValue = issue.getCustomFieldValue(hiddenStoryPointCF) != null?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
In groovy you can simply convert integer to float using (as float)
example:
float storyPointCF = customFieldManager.getCustomFieldObject("customfield_10106" as float)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.