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));
@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