How to create a link between two issues programatically using a listener?

Hi,

What i have is a listener that creates a new issue when the actual issue is reopened. What i want is a rutine that creates a link to bind both issues.

I've find some documentation and even i don't understand the code completely. I try to do some test with this code:

***********

private final static Long EVENT_REOPEN = 7L;

public void workflowEvent(IssueEvent event){

if(EVENT_REOPEN.equals(event.getEventTypeId())){

boolean wasIndexing = ImportUtils.isIndexIssues();
ImportUtils.setIndexIssues(true);

IssueInputParameters issueInputParameters = new IssueInputParametersImpl()
.setProjectId(event.getIssue().getProjectObject().getId()).setIssueTypeId("1")
.setSummary("New issue")
.setReporterId(event.getIssue().getProjectObject().getLeadUserName())
.setAssigneeId(ComponentManager.getInstance().getUserUtil().getUser("gfi").getName())
.setDescription("Issue '" + event.getIssue().getSummary()
+ "' reopened.").setEnvironment("Tests").setStatusId("1")
.setPriorityId("1").setResolutionId("1");

IssueService issueService = ComponentManager.getInstance().getIssueService();

CreateValidationResult createValidationResult = issueService.validateCreate(event.getRemoteUser(), issueInputParameters);

IssueResult createResult = issueService.create(event.getRemoteUser(), createValidationResult);

ImportUtils.setIndexIssues(wasIndexing);

IssueLinkManager issueLinkManager = ComponentManager.getInstance().getIssueLinkManager();
try{
issueLinkManager.createIssueLink(event.getIssue().getId(), createResult.getIssue().getId(), 10040L, new Long(0), event.getRemoteUser());
}catch (CreateException e) { e.printStackTrace();}

}

}

***********

"10040L" and "new Long()" are just testing values to put on the required IssueLinkTypeId and sequence constructor-fileds. I really doesn't know to get the appropriated values. Well, this code makes my JIRA go crazy. I get a error on the console and i've problems to find my issues with the issue browser (with this error message: "An error occurred whilst rendering this message. Please contact the administrators, and inform them of this bug. Details: ------- ").

So, resuming. Any idea about how to make the link programmatically?

Thanks!!

Alberto.

2 answers

1 accepted

0 vote
Mizan Community Champion Jun 10, 2012

You can have a look at the source of Create and Link plugin for reference , it is very similar to what you are trying to achieve.

Hi Mizan,

Thanks for ur answer, but is not what i'm looking for. I mean, i need to do this with code, beacouse is what they want me to do. And there's not the plugin code anywhere :( it's really close to what i'm trying to do, but i can't use plugins.

Alberto.

0 vote

I think you're pretty close, it's just your hard-coding that's causing a problem.

The 10040L should be a long referring to the issue link type you want to add to the issue - go to "Admin -> issue linking" and find the link type you want to add, then hover over the edit or delete option - it'll show you a url with an "id" in it - use that number for now. You can then look at how to grab it using code later.

The sequence is a little more difficult because you need to look at the number of links on the issue already, and add one - I think you can simply use the issuelinkmanager again to get the number though.

Hi Nic,

Thanks for the explaining.

You are rigth. Using your method i can see my link's id. Now i understand a little better who JIRA manages the links. I need to create the link (and get the id from t) before bind the issues.

I'll keep on testing with this ideas you'd give me.

Regards.

Alberto.

hi again Nic,

There's no way to make this works :( I've tryed this:

***************

IssueLinkManager issueLinkManager = ComponentManager.getInstance().getIssueLinkManager();
IssueLink il = issueLinkManager.getIssueLink(event.getIssue().getId(), 10030L, 10001L);
System.out.println("Sequence value: "+il.getSequence());
try {

issueLinkManager.createIssueLink(event.getIssue().getId(), 10030L, 10001L, il.getSequence(), event.getRemoteUser());
} catch (Exception e) {e.printStackTrace(); }

***************

Where 10001L is the id of my link and 10030L the id of the issue i want to link. So, when i reopen my issue, this must be linked using the 10001L link to the 10030L issue. But the il.getSequence() brings me a nullPointerException :S.

I'm sure there must be any asyer way to do this. Even to create the link automatically (i mean, i had to create a link manually).

Any idea?

Thanks for reading!

Alberto.

Hmm. I'm a bit stuck. When I wrote code to create a link, it worked along the lines I gave earlier, but that was in version 3.something-quite-low and I haven't revisited it. I'd reach for an example - @Mizan already mentioned the create and link plugin and there's code in there to create the link after issue creation - I'd look to see how it's done in there next

Thanks both!

Mizan was right, in the plugin was the answer, but i haven't found the code the first time i visit the plugin's web. I thougth it was just a useless compiled code. The second time (by Nic's recommendation) i looked for on the wrong code, beacouse the compatible plugin with my JIRA version(4.1.2) was just the 4.2 version. Finally, after some tests, i reach what i wanted. That's my code:

*****************

boolean wasIndexing = ImportUtils.isIndexIssues();
ImportUtils.setIndexIssues(true);

IssueInputParameters issueInputParameters = new IssueInputParametersImpl()
.setProjectId(event.getIssue().getProjectObject().getId())
.setIssueTypeId("1")
.setSummary("Incidencia detectada en la petición")
.setReporterId(event.getIssue().getProjectObject().getLeadUserName())
.setAssigneeId(ComponentManager.getInstance().getUserUtil().getUser("gfi").getName())
.setDescription("Issue '"+event.getIssue().getSummary()+"' reopened.").setEnvironment("Tests").setStatusId("1")
.setPriorityId("1").setResolutionId("1");
IssueService issueService = ComponentManager.getInstance().getIssueService();
CreateValidationResult createValidationResult = issueService.validateCreate(event.getRemoteUser(), issueInputParameters);
IssueResult createResult = issueService.create(event.getRemoteUser(), createValidationResult);
ImportUtils.setIndexIssues(wasIndexing);
IssueLinkManager issueLinkManager = ComponentManager.getInstance().getIssueLinkManager();
IssueLinkTypeManager issueLinkTypeManager = (IssueLinkTypeManager)ComponentManager.getComponentInstanceOfType(IssueLinkTypeManager.class);
IssueLinkType linkType = issueLinkTypeManager.getIssueLinkType(new Long(10000L));
IssueLink il = issueLinkManager.getIssueLink(event.getIssue().getId(), createResult.getIssue().getId(), linkType.getId());
try{issueLinkManager.createIssueLink(event.getIssue().getId(), createResult.getIssue().getId(), linkType.getId(), null, event.getRemoteUser());}catch(Exception e){}

*****************

Thanks Again.

Alberto.

[CLOSED]

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Monday in Jira Software

How large do you think Jira Software can grow?

Hi Atlassian Community! My name is Shana, and I’m on the Jira Software team. One of the many reasons this Community exists is to connect you to others on similar product journeys or with comparabl...

597 views 6 12
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you