• Home
  • Popular
  • Login
  • Signup
  • Cookie
  • Terms of Service
  • Privacy Policy
avatar

Posted by User Bot


29 Nov, 2024

Updated at 02 Dec, 2024

Calculate Risk Score & Level with Scripted Field using scriptrunner in JIRA Cloud

Hi Community,

I want help on how to write the same scripted field groovy code using scriptrunner in JIRA Cloud as it is written in JIRA Data Center successfully.

I have two scripted fields in JIRA Data Center: Risk Score and Risk Level and I am able to configure them with the below code:

Risk Score:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;

def retval=0
def CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
def CustomField cf1 = customFieldManager.getCustomFieldObject('customfield_XXXXX'); // Compliance
def Object cfv1 = issue.getCustomFieldValue(cf1).toString()
switch (cfv1) {
              case ('Feature impacts more than 9+ customers'):
                             retval+=9;
                             break;
              case ('Feature impacts between 9 to 5+ customers'):
                             retval+=5;
                             break;
       case ('Feature impacts less than 5 to 3+ customers'):
                             retval+=3;
                             break;
              default:
                             retval+=0;
                             break;
}
def CustomField cf2 = customFieldManager.getCustomFieldObject('customfield_YYYYY'); // Audit
def Object cfv2 = issue.getCustomFieldValue(cf2).toString()
switch (cfv2) {
              case ('Feature requires L3 Process'):
                             retval+=9;
                             break;
              case ('Feature requires L2 Process'):
                             retval+=5;
                             break;
        case ('Feature requires L1 Process'):
                             retval+=3;
                             break;
              default:
                             retval+=0;
                             break;
}
return retval.toString();

The value obtained in Risk Score, will be compared with Risk Level as below:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;

def issueFactory = ComponentAccessor.getIssueFactory();
def issueObject = issueFactory.getIssue();
def retval='Low'
def CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
def CustomField cf1 = customFieldManager.getCustomFieldObject('customfield_AAAAA'); // Risk Score
def CustomField cf2 = customFieldManager.getCustomFieldObject('customfield_BBBBB'); // Risk Level

String cfv1 = issue.getCustomFieldValue(cf1).toString()
int cfv2 = Integer.parseInt(cfv1)
if (cfv2 < 5) {
              retval='Low';
    }
if (cfv2 > 5 && cfv2 < 10){
              retval='Medium';
    }
if (cfv2 > 10 && cfv2 < 20){
              retval='High';
}
if (cfv2 > 20) {
              retval='Very High';
}
return retval.toString();

I tried my best to write an appropriate groovy code but facing multiple issues like unable to import classes, syntax errors, and not able to use REST API in the code.

I am exploring the JIRA Cloud and it would be a great help, if you guide me with a sample code after reviewing my Jira data center code.

Thanks