Forums

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

How to convert scripts Integer to Float

Venkatesh Bhureddy
Contributor
January 27, 2020

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){

}
}

1 answer

1 accepted

0 votes
Answer accepted
Muhammad Ramzan_Atlassian Certified Master_
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.
January 27, 2020

Please point out the line of code where you are facing issue so we can help you.

Venkatesh Bhureddy
Contributor
January 27, 2020

@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?

Muhammad Ramzan_Atlassian Certified Master_
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.
January 27, 2020

In groovy you can simply  convert integer to float using (as float)

 

example: 

float storyPointCF = customFieldManager.getCustomFieldObject("customfield_10106" as float)

Suggest an answer

Log in or Sign up to answer