Control sprintReport listener

Hi, I have a listener that every time a sprint is finished in the Jira whiteboard it automatically creates the item sprint Report.

This code works:

import com.atlassian.greenhopper.service.rapid.view.RapidViewService
import com.atlassian.greenhopper.service.sprint.Sprint
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.customisers.PluginModuleCompilationCustomiser
import com.onresolve.scriptrunner.runner.customisers.WithPlugin

@WithPlugin("com.pyxis.greenhopper.jira")

def sprint = event.sprint as Sprint

if (sprint.state == Sprint.State.CLOSED && sprint.name.toString().contains("-SP")) {

def rapidViewService = PluginModuleCompilationCustomiser.getGreenHopperBean(RapidViewService)
def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()

def equipos = ["ONLINESTORE-19610", "PRMOTIONSV-22013", "RETAILDIR-43513", "RETAILDIR-44112"
]

    def view = rapidViewService.getRapidView(user, sprint.rapidViewId).value
    def encontrado = equipos.find { it.split('-')[1].toInteger() == view.id }

    if (encontrado) {
        def proyecto = encontrado.split('-')[0]
        def boardId = encontrado.split('-')[1]

        // ----------- CREAR Y TRANSICIONAR PETICIÓN -----------
        Issues.create(proyecto, 'Sprint Report') {
            setSummary('Pendiente informar Sprint Id')
            setCustomFieldValue('Sprint Id', sprint.id.toString())
        }.transition('Refresh Metrics')
    }
}

How can I do in groovy to control that if that Sprint Report already exists just update it with (transition(Refresh Metrics)) but not create it again?

Could you help me?

Thank you very much
Michael

Hey @MichaelSordo,

Is it right to assume there should only be one sprint report in the project, with a specific value for Sprint Id?

What I would do is first run a JQL search to see if any such issue already exists, if it does you can transition that one, or create a new issue if not.

You’ll probably know what the correct JQL would be, I’d then wrap the code something like this:

def existingIssue = Issues.search("project = FOO and 'Sprint Id' = 'BAR'")[0]

if (existingIssue) {
    existingIssue.transition('Refresh Metrics')
} else {
    Issues.create(proyecto, 'Sprint Report') {
        setSummary('Pendiente informar Sprint Id')
        setCustomFieldValue('Sprint Id', sprint.id.toString())
    }.transition('Refresh Metrics')
}

Does that make sense? Check for an existing issue via JQL first, transition if it exists, if not then create a new issue.

Cheers!

Hi @rlander, sorry for the delay. It was indeed as you said. Thank you very much for your help.

Kind regards,

Michael Sordo