Atlassian Development: Create an issue in Jira from your plugin

To create an issue in Jira, first you need to import the following classes in your java file

import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.bc.issue.IssueService;
import com.atlassian.jira.bc.issue.IssueService.CreateValidationResult;
import com.atlassian.jira.bc.issue.IssueService.IssueResult;
import com.atlassian.jira.issue.IssueInputParameters;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.util.ErrorCollection;
import com.atlassian.jira.component.ComponentAccessor;

After importing these file in your java files, write the below code in the function where you want to create an issue

First create an object of IssueService class from the class ComponentAccessor

IssueService issueService = ComponentAccessor.getIssueService();

Create an object of IssueInputParameters, later these parameter will be validated

IssueInputParameters issueInputParameters = issueService.newIssueInputParameters();

Set the parameters to the issueInputParameters object, most important parameters are:

projectId-> Id of the project to which you want the issue to add

reporterId->Reporter of the issue like admin etc.

assigneeId->User to whom the issue is assigned

issueInputParameters.setProjectId(projectId).setIssueTypeId(“1”).setSummary(“issue summary”)
.setReporterId(reporterId).setAssigneeId(assigneeId).setDescription(“issue description”)
.setStatusId(“1”).setPriorityId(“2”);

Now we need to fetch the current user, by the following lines, we are fetching the logged in user

JiraAuthenticationContext authenticationContext = ComponentAccessor.getJiraAuthenticationContext();
User user = authenticationContext.getLoggedInUser();

Validate the issueInputParameters and user

CreateValidationResult createValidationResult = issueService.validateCreate(user, issueInputParameters);

if createValidationResult return true, it means all is well so far then

MutableIssue issue=null;

IssueResult createResult = issueService.create(user, createValidationResult);

if (createResult.isValid()) {
issue = createResult.getIssue();
} else {
Collection<String> errorMessages = createResult.getErrorCollection().getErrorMessages();
for (String errorMessage : errorMessages) {
//wite the exception in logs
}
}

if createValidationResult return false, then fetch the exceptions and errors and write these to the logs

Collection<String> errorMessages = createValidationResult.getErrorCollection().getErrorMessages();
for (String errorMessage : errorMessages) {
//wite the exception in logs
}
Map<String, String> errors = createValidationResult.getErrorCollection().getErrors();
Set<String> errorKeys = errors.keySet();
for (String errorKey : errorKeys) {
//wite the error in logs
}

Cheers

 

Rajeev Kumar

Rajeev has over 20 years of experience in IT managing large software development programs, organization change initiatives and Agile transformations. Prior to joining Mirketa Rajeev worked at startups and fortune 500 companies in senior management roles. He founded www.mytafi.in and www.finacast.com, personal finance management applications for Indian and US market. Rajeev is also the founder of www.wednik.com, a wedding planning application with online market for wedding planners and wedding merchandise.
Posted in AGILE Tools, JIRA.

Leave a Reply

Your email address will not be published. Required fields are marked *