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));
@approximatenumber
Copy link

this stuff helped me to understand that scriptrunner. thnks

@espositot
Copy link

espositot commented Aug 16, 2019

How do you get the issueKey of the current issue?
For example, if this were a post processing function,

MutableIssue issue = issueMgr.getIssueObject("?????") // how to populate this with current key that is being post-processed

@matts-mpg
Copy link
Author

Yeah, this is something that is maybe not documented well, and you sort of learn by exposure more than anything. Anytime you are working to build something into Jira via Scriptrunner you have to consider the context of the thing you are working with. And without having it documented clearly, it's some trial and error, but ....

  • If you are writing an Event Listener then the "thing" you are going to "get" in the listener is an Event. This is sort of by default. It's just there and you can refer to event.whatever or back out from the event to the issue or whatever you want to do. But you start with an Event. That's what you caught after all.
  • If you work any any sort of workflow stuff (postfunction, condition, etc) then you are "getting" an issue. More specifically, you get the issue that is currently passing through the workflow and hitting the postfunction you're writing. It just there. You can refer to it by just referencing issue so to answer your Q you would say ....
    MutableIssue issue = issueMgr.getIssueObject(issue.key)

No setup is required. This seems to be contradicted by the code above because I do have setup happening on lines 15-21. However, this was code I was running in the Script Console, NOT implementing in a post-function. So that is sort of a caveat to keep in mind as you develop something in the console vs. implementing it in a workflow. You need to go get an issue in the console. You already HAVE an issue when you're in the workflow.

HTH!

@espositot
Copy link

That's great! But I get an error in ScriptRunner

MutableIssue issue = issueMgr.getIssueObject(issue.key)

"Unable to resolve class MutableIssue"

Here my code up to the MutableIssue issue = issueMgr.getIssueObject(issue.key) :

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.worklog.WorklogImpl
import com.atlassian.jira.event.type.EventDispatchOption

IssueManager issueMgr = ComponentAccessor.getIssueManager()
CustomFieldManager fieldmgr = ComponentAccessor.getCustomFieldManager()
MutableIssue issue = issueMgr.getIssueObject(issue.key)

@matts-mpg
Copy link
Author

matts-mpg commented Aug 16, 2019

Anytime you see Unable to resolve class <class> it generally means you have not imported that class. I do not see an import for it. com.atlassian.jira.issue.Issue is not the same as that class cannot be edited .... it is Immutable. Look at my import:
import com.atlassian.jira.issue.MutableIssue

That is what you're missing.

@cabrizio
Copy link

cabrizio commented Aug 20, 2019

Hi, I tried this

import com.atlassian.jira.issue.MutableIssue
def issueManager = ComponentAccessor.getIssueManager()
MutableIssue issue = issueManager.getIssueObject(issue.key)
//def issueObject = issueManager.getIssueObject("ISSUE_KEY")
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_11601")
def cFieldValue = issue.getCustomFieldValue(cField)

but I'm getting null value, am I doing something wrong?

Thanks

@espositot
Copy link

image

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.worklog.WorklogImpl
import com.atlassian.jira.event.type.EventDispatchOption

// get issue, worklog hours values and multiply to convert to hours
//def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("GHD-14597")
IssueManager issueMgr = ComponentAccessor.getIssueManager()
CustomFieldManager fieldmgr = ComponentAccessor.getCustomFieldManager()
MutableIssue issue = issueMgr.getIssueObject(issue.key)

@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