Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scriptrunner Post-Function: Add a comment to linked issues after transition not working

Diana
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 12, 2023

I'm following the guide here so that a comment can be added to linked issues when parent issue is transitioned, but my linked issues are not updating.

I have Project A with issue type "Test". The status "Done" has a transition screen that requires the user to add a linked issue (works fine in Behaviors), now after transition, the post-function doesn't add a comment to any linked issues via "relates to". I have scriptrunner version 7.9.0.

Steps to Reproduce:

  1. Add Post-Function "Adds a comment to linked issues when this issue is transitioned"
  2. Leave Conditions blank, set Issue Link Type to "relates to", create comment "This bug was tested in $issue"
  3. Set Post-Function to run first
  4. Under Project A, create issue type "Test" and transition to Done
  5. A transition screen pops up requiring user to add a linked issue. Must add via "relates to". The linked issue must be under Project B as issue type "Bug".
  6. Complete "Test" transition to status "Done"

Expected Results:

  • Issue "Bug" has an added comment and is linked to issue "Test"

Actual Results:

  • Issue "Bug" has no comments, but is linked to issue "Test"

4 answers

1 accepted

1 vote
Answer accepted
Diana
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 14, 2023

I have found a workaround. Since it was not working in the workflow's Post Function, I opt for creating a Custom Listener and set Event as Generic Event

Custom listener script here:

 

import com.atlassian.jira.issue.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.Issue

def commentManager = ComponentAccessor.getCommentManager()
def currentUserObj = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def issue = event.issue

//start at parent issue
if (issue.issueType.name == 'Test'){

    //loop through links
    List<IssueLink> allOutIssueLink = ComponentAccessor.getIssueLinkManager().getInwardLinks(issue.getId())
    for (Iterator<IssueLink> outIterator = allOutIssueLink.iterator(); outIterator.hasNext();) {
        IssueLink issueLink = (IssueLink) outIterator.next()

        //define all desired fields on parent issue
        def statusField = issue.getStatus().getName()

        //only update if the link is Relates to
        if (issueLink.getIssueLinkType().getName() == "Relates"){

            MutableIssue linkedIssue = issuelink.getSourceObject() as MutableIssue
            log.warn("List of linked issues: " + linkedIssue.key)

            //only update specific issue type
            if (linkedIssue.issueType.name == "Bug"){
                log.warn("Type of linked issues: " + linkedIssue.key + linkedIssue.issueType)

                //update if status changed to Done
                if(statusField == 'Done'){

                    //add a comment to linked Bug
                    def testComment = "This bug was tested in $issue.key"
                    commentManager.create(linkedIssue, currentUserObj, testComment, true)
                }
            }
        }
    }
}
0 votes
Fabio Vintorin June 18, 2024

Hi everyone! 

How could I pass the URL of the current issue via comment using this Post Function? Example, if I want to pass the person responsible, I use currentUser.name 

 

0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 12, 2023

Hi @Diana Gorv

From your description, this doesn't seem to be a problem with ScriptRunner but an error in your configuration.

To proceed, could you please clarify which relates to option did you select?

As shown in the screenshot below, there are two options you can pick from:-

image1.png

The objective is for the relates to work one way in any one Post-Function configuration.

If you select the first, it may work if the Test issue type transitions. Accordingly, if you are to select the second, it may work when the Bug issue type transitions.

However, if you want it to work when either the Test issue type transitions or the Bug issue type transitions, you will need to configure 2 separate Post-Functions for them; each Post-Function will need to be set to either one of the issue link types accordingly.

I hope this helps to answer your question. :-)

I am looking forward to your feedback.

Thank you and Kind regards,

Ram

Diana
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 13, 2023

@Ram Kumar Aravindakshan _Adaptavist_ Thank you for the quick response.

I selected the first "relates to", since this post-function is on the "Test" workflow and the intent is for when "Test" issue type transitions to Done only. So a "Bug" issue type can remain in whatever workflow status is on. The idea is, when "Test" is Done, a comment will be added to linked "Bug".

I actually tested using other link names, not just "relates to", and nothing seems to trigger to add a comment to the linked "Bug".

I have also tested by adding the linked "Bug" before transitioning the "Test" issue to Done, because I thought perhaps order of post-functions was incorrect, but still nothing.

Is it possible because they are under 2 different projects, it will not trigger?

0 votes
Evgenii
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 12, 2023

Hi, @Diana Gorv 

Can you show us screenshot of list of postfunctions on transition?

At first thought, looks like, that the order of posfunctions is not correct.

Diana
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 13, 2023

@Evgenii Thank you for the quick response.

Unfortunately, I cannot share screenshots due to security, but I can type it out. I have it as:

  1. custom Add a comment to linked issues when this issue is transitioned
  2. custom update field Resolution to Done
  3. Set issue status to the linked status of the destination workflow step
  4. Add a comment to an issue if one is entered during a transition
  5. Update change history for an issue and store the issue in the database
  6. Re-index an issue to keep indexes in sync with the database
  7. Fire a Generic Event that can be processed by the listeners

I have tried also having 1 at the bottom, but still no luck.

This "Test" issue type is under Project A that doesn't have any custom listeners mapped to it, and same for "Bug" issue type under Project B.

I do have a transition screen for "Test" when moving to Done that makes the users input a linked issue. However, I have also tried having the "Bug" linked to "Test" before I transition as well, and still the "Bug" does not get an added comment after "Test" moved to Done.

Is it possible that a custom listener might solve this instead of using post-function?

Diana
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 14, 2023

@Evgenii @Ram Kumar Aravindakshan _Adaptavist_ 

I have tried cycling through my custom Post Function through all the order of postfunctions 1-7, and nothing is setting the comment in my linked issue.

I have also tried testing with the same project key, and tried using all the different link types. It is not working.

Evgenii
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 14, 2023

Hi, @Diana Gorv 

Maybe, as a workaround, you can use Jira Automation?

You can set trigger to "Issue Transitioned", select Statuses "From", "To", then select conditions, to check, that issue type = Bug.

And then select all linked issues and add there comments "This bug was tested in {{triggerIssue.key}}

Suggest an answer

Log in or Sign up to answer