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
}
Leave A Comment