Listener sprintCreatedEvent only on certain project

Atlassian TMG April 7, 2023

I would my listener on the sprintCreatedEvent to only be triggered on a certain project. Currently the script below creates a version in a project when there is a sprint created in every project. 

 

{code}

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.Project

def sprintName = event.sprint.name


def version = ComponentAccessor.versionManager.createVersion(sprintName,
    null,
    null,
    sprintName,
    16400,
    null,false)

    return version.name

{code}

3 answers

Suggest an answer

Log in or Sign up to answer
1 vote
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.
April 7, 2023

The following makes no sense:

def project = ComponentAccessor.projectManager.getProjectByCurrentKey("INTB2BSSD")

 if (project.key == 'INTB2BSSD') {

You hardcode a project lookup then check that the project is the one you just hardcoded?

No.

You need to use the information from your event to find your project.

The best way I was able to find to do that is to use the rapidViewId. As far as I know, usually, each rapidVIew (board) should be associated with a single project.

You might need to hardcode that mapping.

For example:

import com.atlassian.greenhopper.api.events.sprint.SprintStartedEvent

event = event as SprintStartedEvent
def boardProjectMap = [
123L: 'PKEY1',
345L: 'PKEY2',
567L: 'PKEY3',
]
def boardId = event.sprint?.rapidViewId
if(!boardId) return //nothing to do if we don't know the board
log.debug "found boardId from the event: $boardId"
def projectKey = boardProjectMap[boardId]
if(!projectKey) return //nothing to do if the board id didn't map to a projectKey
log.debug "found project key $projectKey from boardProjectMap"

def project = ComponentAccessor.projectManager.getProjectObjByKey(projectKey)

the data in boardProjectMap will have to be updated to match your board and project keys

0 votes
Atlassian TMG April 7, 2023

image.png

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.
April 7, 2023

Hi @Atlassian TMG

Could you please share a screenshot of your listener configuration? I am requesting this to check if you have specified the projects you want the listener to work on in the configuration.

I am looking forward to your feedback.

Thank you and Kind regards,

Ram

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.
April 7, 2023

Hi @Atlassian TMG

If you want the Sprint Listener to trigger for specific Projects only, try adding this filter:-

import com.adaptavist.hapi.jira.projects.Projects

def project = Projects.getByKey('MOCK')

if (project.key == 'MOCK') {
// Your code
}

Please note that the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

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

Thank you and Kind regards,

Ram

Atlassian TMG April 7, 2023

Hi @Ram Kumar Aravindakshan _Adaptavist_ 

I am getting a error.

The script could not be compiled:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script660.groovy: 2: unable to resolve class com.adaptavist.hapi.jira.projects.Projects
 @ line 2, column 1.
   import com.adaptavist.hapi.jira.projects.Projects
   ^

1 error
.
Atlassian TMG April 7, 2023

@Ram Kumar Aravindakshan _Adaptavist_ we are currently still using script runner 6.57 and we cannot update intill we are migrated to jira cloud which will take a while.

Atlassian TMG April 7, 2023

@Ram Kumar Aravindakshan _Adaptavist_ , I changed the script to something similar but didn't work. It still keeps creating versions when a sprint is created in another project.

{code}

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.Project

def project = ComponentAccessor.projectManager.getProjectByCurrentKey("INTB2BSSD")

if (project.key == 'INTB2BSSD') {

 def sprintName = event.sprint.name  
 def version = ComponentAccessor.versionManager.createVersion(sprintName,
    null,
    null,
    sprintName,
    16400,
    null,false)


    return version.name
}
{code}

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.
April 7, 2023

Hi @Atlassian TMG

I forgot to mention that you need to upgrade your ScriptRunner plugin to the latest rease, i.e. 8.0.0 so you can use the HAPI feature that is shown in the sample code I provided.

Thank you and Kind regards,

Ram

Atlassian TMG April 7, 2023

@Ram Kumar Aravindakshan _Adaptavist_ is there another cmd I can use? Because it's not possible to upgrade at the moment.

TAGS
AUG Leaders

Atlassian Community Events