-
-
Save matts-mpg/19d5c7e430c65cf4647d9e82f9d20771 to your computer and use it in GitHub Desktop.
//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)); |
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)
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.
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
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)
I guess we are back to the original question --
- how to get an "issue" in the ScriptRunner console w/o hard-coding the issue key?
- 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?
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.
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)```
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 ....
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!