Forums

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

post function with sql query does not work when automatic transition is performed.

Dante Labate
Contributor
September 12, 2018

Could someone explain to me why this does not work?
I have a code that performs a query in the jira database and with result I execute other activities in the same script.
for example, I query in the database the issue id related to the current issue and remove the link between them. (this is just an example)

 

import com.atlassian.jira.component.ComponentAccessor
import groovy.sql.Sql
import org.ofbiz.core.entity.ConnectionFactory
import org.ofbiz.core.entity.DelegatorInterface
import com.atlassian.jira.config.properties.PropertiesManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.IssueLink

import java.sql.Connection

def delegator = (DelegatorInterface) ComponentAccessor.getComponent(DelegatorInterface)
String helperName = delegator.getGroupHelperName("default");

Connection conn = ConnectionFactory.getConnection(helperName);
Sql sql = new Sql(conn)
StringBuffer sb = new StringBuffer()
try {
sql.eachRow("""
select il.source from jiraissue c
left join issuelink il on il.DESTINATION = c.id
where c.project = 12100
and c.id = ${issue.id}
""") {
sb << "${it}"
}
sb.toString()

} finally {
sql.close()
}

def id = sb.replaceAll("\\[source:","").replaceAll("\\]","")
def uu = ComponentAccessor.getUserUtil()
Object user = uu.getUserByName("automacao")
def linkMgr = ComponentAccessor.getIssueLinkManager()
def sourceId = id as long
def linkType =10400 as long
def issueLink = linkMgr.getIssueLink(sourceId, issue.id, linkType);
linkMgr.removeIssueLink(issueLink, user);

 

This post function script works perfectly if it is executed by clicking the transition button.
But if another problem that has a script that transitions into another problem, calls the problem that has this script, several errors occur.
after a lot of head breaking I found that if a issue (issue A) performs the automatic transition in issue (issue B) where it has the SQL post function, the SQL query does not work (several errors occur)


Why is this occurring?
What is the difference of the post function script being executed manually (by clicking the button on the screen) of what is automatically executed by another issue?

and the most important ...
How can I solve this?  :S

3 answers

1 vote
Dante Labate
Contributor
September 13, 2018

@Orkun Gedik ... @MoroSystems Support

maybe this will help understand what I'm trying to do ...

order of activities

Issue A (active in screen)
Clicking on the transition button happens:
automatic transition in issue B

Issue B (inactive in screen)
postfuncion: a current issue is cloned (issue B)

Issue C (clone of issue B)
create postfuncion:
Code above containing SQL query (several errors occur)

 

I guess the error is in the parameter entered in SQL query.

I inform the issue.id, but since it is not the active issue on the screen, the issue.id informed in SQL is not correct (and the various errors occur).

Now the question is ...
how to pass the issue.id of an issue that was automatically cloned and not active on the screen?

MoroSystems Support
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.
September 13, 2018

Hi Dante, I think that it is what @Orkun Gedik says. If you're trying to use 'issue' property in postfunction on Create transition, than you should set it to the bottom of the list of the postfunctions. Can you post a printscreen of your "Create" transition's postfunctions configuration?

I remember there can be still some problems with scripts on create transition and workaround is to:

  • create new transition from Any to Any status (or in your case it should be enough to make it from your initiali status to initiali status) and set its name to the action you're trying to achieve (e.g. "Update Links")
  • put your postfunction (with SQL) as postfunction of "Update Links" transition
  • set postfunction "Fast-track transition" (https://scriptrunner.adaptavist.com/latest/jira/builtin-scripts.html#_fast_track_transition_an_issue) on "Create" transition and set it up to invoke your "Update Links" transition

Martin

Dante Labate
Contributor
September 17, 2018

@MoroSystems Support

Martin

good idea
put a Fast-track after creating the issue for a transition from the first status to the first status (loop) and in this transition put the SQL script.

As soon as I carry out the suggested test I inform the result.

very grateful for the idea!

1 vote
Orkun Gedik
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.
September 12, 2018

Hello Mr.Dante,

I think your problem is getting current issue. As you said, script console works fine because your are giving an issue correctly to your script yet if you have placed your script with incorrect order into postfunctions then your script cannot to work on correct issue.

What is the position of this script in postfunctions?  Can you please move your script to end, if it's not, and try again.

Regards

Orkun Gedik

Dante Labate
Contributor
September 13, 2018

@Orkun Gedik

 

I'll take the test and let you know when I'm done.

 

0 votes
MoroSystems Support
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.
September 12, 2018

Hi, can you post the errors?

 

Martin

Dante Labate
Contributor
September 13, 2018

@MoroSystems Support

 

I noticed that the errors are in the lines that has the variable that SQL should bring ...
I did a test and manually entered in the script the id, issuekey (all values that SQL should bring) and everything worked as it should.

this proved to me that the postfunction is correct, the problem is in the SQL query (I think the issue.id used in the query is not the issue.id of the correct issue but the active issue)

Suggest an answer

Log in or Sign up to answer