Plugin to create / update issues (Jira 7.1.8)

fla_91 July 14, 2016

How can i create or update issues within a plugin? 

i can get all the issues with JQL and i want to update (or also create a new one) some of them, if the data i get from outside are not the same

 

1 answer

1 accepted

1 vote
Answer accepted
Petar Petrov (Appfire)
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.
July 14, 2016

What kind of plug-in are you developing - Server or Cloud?

For Server you can use the Java APIs - take a look at IssueService. For Cloud you need to use the REST APIs. Let me know if you have a more concrete question.

fla_91 July 15, 2016

i am developing for server

i had a look on IssueService and build this code. Am i on the right track?

IssueInputParameters parameters =  issueService.newIssueInputParameters();
parameters.setDescription("description");	//example
 
// item.getId() gets the issueId that have to be updated
UpdateValidationResult updateResult = issueService.validateUpdate(currUser, new Long(item.getId()), parameters);	//updates the issue
Petar Petrov (Appfire)
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.
July 15, 2016

Yes, that's the way to do it, you just need to add the actual update invocation:

issueService.update(currUser, updateResult);

The pattern for creating an issue is the same - validateCreate() then create().

fla_91 July 17, 2016

thanks a lot, updating is working smile

but i have some problem creating a new issue.. I add the IssueInputParameters like that, but it do not work:

private IssueInputParameters addParameters(IssueInputParameters par, Long projId) {
		
		par.setProjectId(projId); 
		par.setDescription(getDescriptionString(item));
		
		return par;
	}

have i to do anything else?

Petar Petrov (Appfire)
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.
July 18, 2016

You need to call issueService.validateCreate() and then create(). What do you mean by "it does not work"? Is there an error thrown?

fla_91 July 19, 2016

it works now, i think the problem was i only set the project-id and the description.. after i set all the required fields (summary, issuetype, reporter-id) it created a new issue smile

 

Another question: 
how can i set an ApplicationUser? now i set him as the logged-in one.. but later it should be always the same user with admin-permissions..

can i do it that way:

ApplicationUser currUser = userManager.getUserByName("admin");
Petar Petrov (Appfire)
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.
July 19, 2016

Yes, you could do that, but you need to make sure that user has sufficient permissions to create issues. Also hardcoding users in your code has its own drawbacks - the user name may change, the user may get deleted, etc. If you really want to have a service user which will be used to create issues, maybe you should think about making that user selectable via a configuration UI so that at least it's not hardcoded in you plug-in code.

fla_91 July 20, 2016

ok thanks a lot for your detailed answer, i will regard this.

now i had an other prolbem: i want to set the Components and the Status of the IssueInputParameters. i do it like that:

IssueInputParameters par;
 
par.setComponentIds(getComponents(item));
par.setStatusId(getStatusId(item));
 
getComponents(Object item) {
	// gets all the components from Jira, parse them with a List from the item-object, if there is a match save it to a Long-Array and after return the Long-Array 
}
 
getStatusId(Object item) {
	// gets all statuses from Jira and parse them with the one from the item-object, returns a String with the ID of the Status from Jira
}

The methods "getComponents" and "getStatusId" return a valid Long-Array and String, i proof that... the components exist in Jira, i could select them directly from JIRA ..

Maybe you have an idea what i do wrong

thanks for your great help smile

Petar Petrov (Appfire)
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.
July 21, 2016

Hm, I don't think you can set the status ID like that - you'll need to transition the issue to that status after the issue is created.

You didn't mention what goes wrong - does validateCreate return errors or just the issue is created with no components?

fla_91 July 21, 2016

components work now. First I create an issue without components and after i update that issue with the components.. that works fine now

i try the same with the status but it do not change the status (the status i want to set is valid: f.e.: i want to set it from "Open" to "In Progress"). there are no errors about that

 

Another thing.. now my plugin is a Scheduler that runs every X minutes.. updating issues work but if it should create a new issue, i get this error:

Errors: {pid=Anonymous users do not have permission to create issues in this project. Please try logging in first.}

so i think i have to tell my plugin to login a user that can create issues.. but how can i do that? how can i login a user within a plugin?

Suggest an answer

Log in or Sign up to answer