Hi,
I am using the built-in scriptrunner listener to clone an issue. I need to add the value of a field from the source issue as a comment on the destination issue but I dont get that to work. In the additional issue actions section I have this piece of code:
def cfRP = customFieldManager.getCustomFieldObjects(sourceIssue).find {it.name == 'Remediation Plan'}
def comment = sourceIssue.getCustomFieldValue(cfRP)
CommentManager commentManager = ComponentAccessor.getCommentManager()
def CurrentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
UserManager userManager = ComponentAccessor.getUserManager()
ApplicationUser user = userManager.getUserByName(CurrentUser.name)
def issue = event.issue as Issue
commentManager.create(issue, user, comment, false)
The problem is that the comment is created in the source issue instead of the destination issue. I have also put the last two lines of code above inside the doAfterCreate function but it errors out.
Any help is greatly appreciated.
Thank you
Hi Tomas,
By looking at your code snippet, it seems that you are missing the "createIssue" operation.
When working with the event listener, the issue captured in the event is the "current" issue that triggers that event.
When you write:
def issue = event.issue as Issue
What you are doing is defining the source issue as the destination issue, based on your code.
And when you write :
commentManager.create(issue, user, comment, false)
What you are doing is creating a new comment, not a new issue. The comment happens in the same issue because this is what is defined by these two actions.
More info on the CommentManager class create() method here: Interface CommentManager
In order to capture the value of the field and set it as a comment onto a brand new issue, you would need to call the methods that are required for that, and then pass the value of your field as a comment within the parameters of that brand new issue. It looks like the interface you need (which is IssueInputParameters) contains a setComment() method that takes a text string that can be set on issue creation.
See a reference here: IssueInputParameters
The following snippet below is a suggestion and you would need to modify it in order to make it work, but it has the elements you need to get started. Please note that this only addresses creating a new issue, so you would still need to merge this code with yours to collect the value of your field from the event-triggering issue:
// -if you are missing some of those, add them to your code
import com.atlassian.jira.issue.context.IssueContextImpl
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.user.ApplicationUser
// -if fetching the user code already works for you, ignore this single line below
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
// -call the service to create the issue and prepare the parameter package
IssueService issueService = ComponentAccessor.issueService
IssueInputParameters issueInputParameters = issueService.newIssueInputParameters()
// -set the values of these params: summary, issuetype and project are mandatory
issueInputParameters.setIssueTypeId(THE_ID_OF_THE_DESTINATION_ISSUETYPE)
.setProjectId(THE_ID_OF_THE_DESTINATION_PROJECT)
.setSummary(THE_MANDATORY_SUMMARY_STRING)
.setComment(THE_STRING_OR_VARIABLE_NAME_WITH_THE_FIELD_VALUE)
// -validate that the user can perform this operation and the parameters are valid
IssueService.CreateValidationResult createValidationResult = issueService.validateCreate(user, issueInputParameters)
def paramsValidation = createValidationResult.isValid()
// -finally, proceed to create the new issue if the parameters passed
if (paramsValidation == true)
{
// -commit the operation and return the result
def createResult = issueService.create(user, createValidationResult)
return createResult // -this will return the ID of the new issue, or an error
}
// -if your destination issuetype has other mandatory parameters, you will need to set those too
// -you can always try testing with hardcoded string values until you get it to work
Again, this will not work out of the box. Please read the comment lines for each action.
You can remove these 2 lines below from your code snippet. They won't be necessary, as you would set the comment upon creation.
def issue = event.issue as Issue
commentManager.create(issue, user, comment, false)
I don't have much time to create and test the whole case right now so I can give you a fully working example. But hope my answer was helpful.
Update:
In case that I missed your original intention and you were just using the point and click function for cloning issues that is available on the console, your solution could be simpler. I usually like to code the whole thing so I overlooked this possibility.
Have you tried removing this line from your current code?
def issue = event.issue as Issue
As I explained above, this is assigning the value of the issue that triggered the event (in this case it would be the source issue) to the issue that will receive the comment, which explains why the comment goes to the source issue.
It's probably all you need.
If I am not mistaken the context for the "issue" already exists and you would not need to define the variable again.
I could be wrong, but try it out.
Good luck.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @AF
Thank you for getting back to me.
I am putting my code in the additional issue action sections of the clone issues built-in script that comes with scriptrunner. What I need is a way to refer to the destination issue.
I had this:
def issue = event.issue as Issue
commentManager.create(issue, user, comment, false)
that creates the comment but it creates the comment in the source issue and not in the destination issue.
Using:
def issue = event.issue as MutableIssue
commentManager.create(issue, user, comment, false)
That also created the comment in the source issue and not in the destination issue.
Removing the line where I define the issue variable causes an error and the comment is not even created. It seems I need to define the issue variable; the question is what is the line of code to define the destination issue?
Thank you for your assistance. Really appreciate it
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @AF
Your idea of removing a line of code gave me the hint I needed to finally got this! Than you
def issue = event.issue as Issue
I put the creation of the comment inside the doAfterCreate function and it worked. Like this:
doAfterCreate = {
commentManager.create(issue, user, comment, false)
}
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.