Forums

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

Fire two events/listeners during the same transition

Sngy October 16, 2020

Hello,

is it possible to fire two events (each event has a seperate listener) one after another, during the same transition?

I see that I can only add one post function, which fires during an event. I hoped I could add another post function right after, but this does not seem to be possible.

Anyone know a way around it?

1 answer

1 accepted

0 votes
Answer accepted
Martin Bayer _MoroSystems_ s_r_o__
Community Champion
October 16, 2020

hi @Sngy can you describe the whole use case? What postfunctions should be executed? What should listeners do? Is usage of listeners a good way?

We can discuss it here :)

Sngy October 16, 2020

Hello @Martin Bayer _MoroSystems_ s_r_o__ 

so my first listener creates a copy of an existing project.

def projectKeyField = customFieldManager.getCustomFieldObject(12902)
def projectKey = issue.getCustomFieldValue(projectKeyField) as String

Thread executorThread = new Thread(new Runnable() { void run() {
def copyProject = new CopyProject()
def inputs = [
(CopyProject.FIELD_SOURCE_PROJECT) : "SWTEMP",
(CopyProject.FIELD_TARGET_PROJECT) : projectKey,
(CopyProject.FIELD_TARGET_PROJECT_NAME) : projectName,
(CopyProject.FIELD_COPY_VERSIONS) : false,
(CopyProject.FIELD_COPY_COMPONENTS) : false,
(CopyProject.FIELD_COPY_ISSUES) : false,
(CopyProject.FIELD_COPY_DASH_AND_FILTERS) : false,
]
def errorCollection = copyProject.doValidate(inputs, false)
if(errorCollection.hasAnyErrors()) {
log.warn("Couldn't create project: $errorCollection")
}
else {
def util = ComponentAccessor.getUserUtil()
def adminsGroup = util.getGroupObject("jira-administrators")
assert adminsGroup
def admins = util.getAllUsersInGroups([adminsGroup])
assert admins
ComponentAccessor.getJiraAuthenticationContext().setLoggedInUser(util.getUserByName("z003xnnn"))
copyProject.doScript(inputs)
ComponentAccessor.getJiraAuthenticationContext().setLoggedInUser(util.getUserByName(admins.first().name))
}
}
})
executorThread.start()

My second listener changes the project lead, description etc. with the method:

def projectKeyField = customFieldManager.getCustomFieldObject(12902)
def projectKey = issue.getCustomFieldValue(projectKeyField) as String
def projectNameField = customFieldManager.getCustomFieldObject(12901)
def projectName = issue.getCustomFieldValue(projectNameField) as String
def
originalproject = ComponentAccessor.getProjectManager().getProjectObjByKey(projectKey)
def name = projectName
def description = "Beschreibung"
def url = "https://anyurl/"
def String leadkey = ComponentAccessor.userManager.getUserByName("Oli.Boli").key
ComponentAccessor.getProjectManager().updateProject(originalproject,name,description,leadkey,url,AssigneeTypes.UNASSIGNED)

so the project Key is the same for both Listeners; I first wrote the two listeners into one. But I noticed in the updateProject() method I get a nullpointer error for the "originalproject" parameter. But when I used the updateProject() method after the project was created, I did not get a nullpointer error and everything worked fine. That's why I wanted to create two seperate Listeners, who fire one after another

Martin Bayer _MoroSystems_ s_r_o__
Community Champion
October 17, 2020

Hi @Sngy it wil be harder one. THere is a problem with new Thread. You can't be sure, that new project is created in some time.

It would be better to complete all the project configuration tasks in that thread. I don't know if you can be sure, that copyProject.doScript is atomic operation, it looks like creation of project is COMPLETE (I found some source code https://searchcode.com/codesearch/view/134706501/, but it can be outdated)

 

@Adaptavist Supp  could you help, please? Is there any documentation to CopyProject class

 

p.s.: @Sngy we experienced some problems earlier when using new Thread because of bug https://productsupport.adaptavist.com/browse/SRJIRA-3793. We got information from adaptavist support, we can use

import com.atlassian.jira.util.thread.JiraThreadLocalUtils 

Thread executorThread = new Thread(JiraThreadLocalUtils.wrap {
...your code here...
})
Sngy October 19, 2020

Hello @Martin Bayer _MoroSystems_ s_r_o__ ,

I wrote the second method into the Thread, and replaced the one I had with  

executorThread = new Thread(JiraThreadLocalUtils.wrap

as you suggested. It luckily works all fine now!

Thank you a lot for your work :) You're a big help.

Martin Bayer _MoroSystems_ s_r_o__
Community Champion
October 19, 2020

Hi @Sngy , it was my pleasure to help :)

Suggest an answer

Log in or Sign up to answer