Skip to content

Instantly share code, notes, and snippets.

@matts-mpg
Created July 27, 2016 04:37
Show Gist options
  • Save matts-mpg/19d5c7e430c65cf4647d9e82f9d20771 to your computer and use it in GitHub Desktop.
Save matts-mpg/19d5c7e430c65cf4647d9e82f9d20771 to your computer and use it in GitHub Desktop.
This script should work with scriptrunner in JIRA to a) get an issue; b) match some stuff in the description; and c) get values from custom fields.
//Import classes you need
import com.atlassian.jira.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.issue.fields.CustomField;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
// Get some additional information you typically need like current user
JiraAuthenticationContext userMgr = ComponentAccessor.getJiraAuthenticationContext();
ApplicationUser currentUserObj = userMgr.getLoggedInUser();
//Get an IssueManager. you always need one of those.
IssueManager issueMgr = ComponentAccessor.getIssueManager();
CustomFieldManager fieldmgr = ComponentAccessor.getCustomFieldManager();
MutableIssue issue = issueMgr.getIssueObject("JRA-132") // this is just an example key
//Create the regex matcher(s) you need
def matcher = issue.description =~ /INC\d\d\d\d\d/
try {
String res = matcher[0]
log.error "Component matcher found " + res;
} catch ( IndexOutOfBoundsException e ) {
log.error e;
}
//Print stuff. Just assuming you are playing around/learning in the Scriptrunner console.
log.error "";
log.error "The current user is " + currentUserObj.name;
log.error "The current issue ID is " + issue.id;
log.error "The current issue key is " + issue.key;
log.error "The current issue description is " + issue.description;
log.error "The current issue special field value is " + issue.getCustomFieldValue(fieldmgr.getCustomFieldObject(10603L));
@espositot
Copy link

I tried also MutableIssue issue = issueMgr.getIssueByCurrentKey(issue.key) and get error

image

@espositot
Copy link

A little better if I cast Object to String. Still not seeing variable "issue". ScriptRunner is verison 5.5.7

image

@espositot
Copy link

I guess we are back to the original question --

  1. how to get an "issue" in the ScriptRunner console w/o hard-coding the issue key?
  2. The subsequent code will be used as a post function so the followup to the above question is, after getting the code to work in ScriptRunner, how to modify the code (if modification needed) to run as a post-function?

Is the answer just to use a hard-coded key in the ScriptRunner Console for testing then replace the hard-coded key with "issue.key" when making it a post-function?

@espositot
Copy link

espositot commented Aug 20, 2019

Got code to work as post-function by removing entirely the code:

MutableIssue issue = issueMgr.getIssueObject((String) issue.key())

since the "issue" is already defined / exists when it is a post-function. I guess the only way to test is to hard-code the issue key in ScriptRunner.

@cabrizio
Copy link

no luck in my case

CustomFieldManager fieldmgr = ComponentAccessor.getCustomFieldManager()
MutableIssue issue = issueMgr.getIssueObject((String)issue.key())
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_11601")
def cFieldValue = issue.getCustomFieldValue(cField)```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment