How to flag an issue using Java API (scriptrunner Listener)

Adrien SITTER March 13, 2020

Hello everyone,

I am trying to propagate to parents the fact that I flag a subtask.

I have created a first version of a Listener.

I am on it since 3 days, trying every way I know to update an issue. Everywhere I can read that I just need to change custom field "Flagged" but I can not make it work, it is never saved.

I have an other version using ModifiedValue that does not work either.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.event.type.EventDispatchOption

def issue = event.issue
def change = event.changeLog.getRelated("ChildChangeItem").find {it.field == "Flagged"}

if (!change) {
return
}

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def comment = event.comment
def parent = (MutableIssue) issue.parentObject

def customFieldManager = ComponentAccessor.customFieldManager
def optionsManager = ComponentAccessor.optionsManager
def issueManager = ComponentAccessor.issueManager

def cf = customFieldManager.getCustomFieldObjects(issue).find {
it.name == 'Flagged'
}
def fieldConfig = cf.getRelevantConfig(parent)
def value = optionsManager.getOptions(fieldConfig)?.find {
it.toString() == 'Impediment'
}
def changeHolder = new DefaultIssueChangeHolder();

if (change.newstring == "Impediment") {
log.warn ("issue: " + issue.key + " has been Flagged with comment: " + comment)
parent.setCustomFieldValue(cf, [value])
issueManager.updateIssue(user, parent, EventDispatchOption.ISSUE_UPDATED, false)
log.warn ("Should have flagged parent issue: " + parent.key + " with value: " + value)
} else {
log.warn ("issue: " + issue.key + " has been Unflagged with comment: " + comment)
}

 

I also found this: https://docs.atlassian.com/jira-software/6.7.6/com/atlassian/greenhopper/service/issue/flagging/FlagService.html

 

But it seems that this API does not exist anymore since the compiler can not find "com.atlassian.greenhopper.service.issue.flagging.FlagService"

Does any one have any idea?

Thank you very much

1 answer

1 accepted

0 votes
Answer accepted
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 13, 2020

Hi @Adrien SITTER 

What happens with your script?

I see nothing wrong with it. What version of Jira are you on?

When I run this script (which uses all the same components your does) in my console, I see the flag added to the board on the specified issue

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.event.type.EventDispatchOption
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def im = ComponentAccessor.issueManager
def cfm = ComponentAccessor.customFieldManager
def om = ComponentAccessor.optionsManager

def flag = cfm.getCustomFieldObjectByName('Flagged')
def issue = im.getIssueObject('JSP-4395')
def fieldConfig = flag.getRelevantConfig(issue)
def value = om.getOptions(fieldConfig)?.find {
it.value == 'Impediment'
}

issue.setCustomFieldValue(flag, [value])
im.updateIssue(user,issue, EventDispatchOption.ISSUE_UPDATED, false)

issue.getCustomFieldValue(flag)

 Ah... wait a minute, when I tried to get the parent object as a MutableIssue, it didn't work for me either. I'm not sure why not.

You could try this instead:

def parent = issueManager.getIssueObject(issue.parentObject.id)
Adrien SITTER March 13, 2020

Hi @Peter-Dave Sheehan

Thank you very much finding that it works with Issue but not MutableIssue helped me to find the reason why it didn't worked.

I found out that it worked, but there were an error on the event ISSUE_UPDATED generated when doing the updateIssue. Which means the parent issue was not indexed!

I made a global Reindex, forced the index and it worked. Then I removed the forced index and try to fix my event listener.

This one works:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.event.type.EventDispatchOption


def issue = event.issue
def change = event.changeLog.getRelated("ChildChangeItem").find {it.field == "Flagged"}

// Was not the 'Flagged' field that changed, do nothing
if (!change) {
return
}

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def comment = event.comment

def customFieldManager = ComponentAccessor.customFieldManager
def optionsManager = ComponentAccessor.optionsManager
def issueManager = ComponentAccessor.issueManager

def parentIssue = (MutableIssue) issue.parentObject;
if (!parentIssue)
{
return
}


def cf = customFieldManager.getCustomFieldObjects(parentIssue).find {
it.name == 'Flagged'
}
def fieldConfig = cf.getRelevantConfig(parentIssue)
def value = optionsManager.getOptions(fieldConfig)?.find {
it.toString() == 'Impediment'
}

if (change.newstring == "Impediment") {
log.warn ("issue: " + issue.key + " has been Flagged with comment: " + comment)
parentIssue.setCustomFieldValue(cf, [value])
issueManager.updateIssue(user, parentIssue, EventDispatchOption.ISSUE_UPDATED, false)
log.warn ("Should have flagged parent issue: " + parentIssue.key + " with value: " + value)
} else {
log.warn ("issue: " + issue.key + " has been Unflagged with comment: " + comment)
parentIssue.setCustomFieldValue(cf, [])
issueManager.updateIssue(user, parentIssue, EventDispatchOption.ISSUE_UPDATED, false)
log.warn ("Should have unflagged parent issue: " + parentIssue.key + " with value: " + value)
}

 

Now I still have to figure out why event.comment is empty.
And make it if there is no "parent" look to "epic link" but that will be done next week.

Thank you again

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 13, 2020

If you want to have jira take care of indexing itself, use IssueService  instead of issueManager to do the update.

It requires a couple of extra steps, but it's generally safer and will ensure that permissions are respected.

Otherwise, you will have to get an IssueIndexingService object and reindex the parent issue after updating.

import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.issue.index.IssueIndexingService

boolean wasIndexing = ImportUtils.isIndexIssues()
ImportUtils.setIndexIssues(true)
ComponentAccessor.getComponent(IssueIndexingService.class).reIndex(parent)
ImportUtils.setIndexIssues(wasIndexing)

 

Suggest an answer

Log in or Sign up to answer