I'm currently trying to update a custom field during a transition that is triggered by a pull request merge. I tried doing this by accessing the info found on the "development" panel when JIRA and bitbucket are linked.
I tested this script on a scripted field type and was successful in getting the value I wanted
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.plugin.ComponentClassManager
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
ApplicationUser currentUser = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
IssueManager issueManager = ComponentAccessor.getIssueManager()
def ccm = ComponentAccessor.getComponentClassManager()
def devStatusSummaryService = ccm.newInstance("com.atlassian.jira.plugin.devstatus.impl.DefaultDevStatusSummaryService")
def issue = issueManager.getIssueObject(issue.getKey())
def pullRequestData = devStatusSummaryService.getDetailData(issue.id, "stash", "pullrequest", currentUser).right().get().getDetail()
def PRCollection = pullRequestData.pullRequests[0]
def i = 0
while (i < PRCollection.size()) {
def parsedRepoName = (PRCollection[i].destination.repository.name as String)?.replaceAll(/"/,'')
if (parsedRepoName[-1]== "C"){
if (PRCollection[i].status == "MERGED"){
return "U-RES"
}
}
i++
}
return "U-RES2"
however when I changed the script to set a custom field to that value during a transition as a post function it does not execute at all. I know that there aren't any sort of conditions blocking it because I have a separate unrelated custom scriptrunner post function on the same transition that does execute with no problem.(I see the number of successful runs go up while the other stays at "Has not run yet")
This is the script that doesn't execute
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.plugin.ComponentClassManager
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
ApplicationUser currentUser = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
IssueManager issueManager = ComponentAccessor.getIssueManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
def ccm = ComponentAccessor.getComponentClassManager()
def devStatusSummaryService = ccm.newInstance("com.atlassian.jira.plugin.devstatus.impl.DefaultDevStatusSummaryService")
def issue = issueManager.getIssueObject(issue.getKey())
def pullRequestData = devStatusSummaryService.getDetailData(issue.id, "stash", "pullrequest", currentUser).right().get().getDetail()
def PRCollection = pullRequestData.pullRequests[0]
def targetField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "State"}
def changeHolder = new DefaultIssueChangeHolder();
def i = 0
while (i < PRCollection.size()) {
def parsedRepoName = (PRCollection[i].destination.repository.name as String)?.replaceAll(/"/,'')
if (parsedRepoName[-1]== "C"){
if (PRCollection[i].status == "MERGED"){
targetField.updateValue(null, issue, new ModifiedValue("U-RES"),changeHolder);
}
}
i++
}
targetField.updateValue(null, issue, new ModifiedValue("U-RES2"),changeHolder);
I tried researching the problem myself and saw that listeners could be the way to go but when I try using the same script in a listener I get a lot of errors regarding static type checking and stuff not existing. Any and all help would be appreciated
Hi Omar,
There is no need to use updateValue here. To set a custom field on the issue being transitioned, you only need to call setCustomFieldValue on the issue object that's passed in the script bindings:
issue.setCustomFieldValue(targetField, "U-RES")
You can see more examples in our documentation. Note that, for this to work, your post function must come before the Update change history for an issue and store the issue in the database function: this is the function that Jira uses to persist any new field values to the database. Also, the issue variable must still be pointing to the Issue object that was passed in the bindings. That means you'll need to get rid of the line where you redefine issue:
def issue = issueManager.getIssueObject(issue.getKey()) //<-- remove this
As far as I can tell from your code, there is no need for this redefinition anyway.
I hope the above helps - please let me know if you need any of it elaborating on.
Joanna Choules, Adaptavist Product Support
Hello!
Thanks for the information! the Issue object redefinition makes sense, and I have made the changes you recommended. However after removing the line that redefines the issue I get a STC error message at this line.
def pullRequestData = devStatusSummaryService.getDetailData(issue.id, "stash", "pullrequest", currentUser).right().get().getDetail()
where it complains about not finding matching method getDetailData. I found this page but I'm afraid I couldn't quite make out what the root cause of my STC error is.
Any advice?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I figured it out.
my situation fell under the case
There are limitations to the type checker. It is possible to write code that shows errors, but it is valid and executes fine.
that the page I linked above talks about. My post function script is execute and works properly.
Thank you so much
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.