Hi,
I have below json code in a .json file which I need to execute in a post function, so that a new issue is created.
I have tested this code in Jira's Web API browser and its creating a new issue, now I want to do this through groovy script. Please let me know how this can be done.
JSON CODE:
{ "fields": { "project": { "key": "RES" }, "summary": "Issue created from API", "issuetype": { "name": "Task" } } }
Community moderators have prevented the ability to post new answers.
What Vasiliy said. If you are creating the issue on the current instance you definitely don't want to use the REST API. If however you want to create it on a remote JIRA instance you can use code like this:
import groovyx.net.http.RESTClient import org.apache.http.HttpRequest import org.apache.http.HttpRequestInterceptor def constantsManager = ComponentAccessor.getConstantsManager() def bugId = constantsManager.allIssueTypeObjects.findByName("Bug").id def RESTClient http def baseUrl = "http://localhost:8080/jira/" http = new RESTClient(baseUrl) http.client.addRequestInterceptor(new HttpRequestInterceptor() { void process(HttpRequest httpRequest, HttpContext httpContext) { httpRequest.addHeader('Authorization', 'Basic ' + 'admin:admin'.bytes.encodeBase64().toString()) } }) def issue = http.request(POST, JSON) { uri.path = '/jira/rest/api/2/issue' body = [ fields: [ project: [ key: "JRA" ], summary: "test issue for rest api", issuetype: [ id: bugId ], reporter: [ name: "admin" ] ] ] }
Cool!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
JSON used for RESP API. I assume that you want to create an issue via java API. In this case you should use IssueService.create();
Here is a code example of my class for issue creation from same project. It creates an issue for each project if need.
public class CreateIssue { IssueService issueService = ComponentAccessor.getIssueService(); IssueInputParameters inputParameters = issueService.newIssueInputParameters(); ApplicationUser curAppUser = ComponentAccessor.getJiraAuthenticationContext().getUser(); ProjectFieldHelper prjFldHlp = new ProjectFieldHelper(); IssueService.CreateValidationResult createValidationResult; IssueService.IssueResult createResult; private final String DueDateAsString; private final String reporter = ComponentAccessor.getJiraAuthenticationContext().getUser().getKey(); public CreateIssue(){ Calendar reportDate = Calendar.getInstance(); reportDate.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY); DueDateAsString = (new SimpleDateFormat("dd/MM/yyyy")).format(new Date(reportDate.getTimeInMillis())); } public void createIssuesForProjectList(List<Project> _projects, Calendar _creationDate){ for(Project prj: _projects){ if( prjFldHlp.getProjectFieldValue("prjState", prj).equals("Выполняется")){ inputParameters.setProjectId(prj.getId()) .setIssueTypeId("10903") .setSummary("Еженедельный отчет") .setReporterId(reporter) .setAssigneeId(prj.getLeadUserKey()) .setEnvironment("Enviroment") .setStatusId("10016") .setPriorityId("2") .setDueDate(DueDateAsString); createValidationResult = issueService.validateCreate(curAppUser, inputParameters); if(createValidationResult.isValid()){ createResult = issueService.create(curAppUser, createValidationResult); } } } } }
ProjectFieldHelper - is my custom class. Just ignore this.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.