Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to execute json file through groovy script in Script Runner Plugin(Jira)

aditya verma December 22, 2015

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"        }    } }  

2 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

5 votes
JamieA
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.
December 23, 2015

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"
            ]

        ]
    ]
}
Vasiliy Zverev
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.
December 23, 2015

Cool!

0 votes
Vasiliy Zverev
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.
December 22, 2015

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.
TAGS
AUG Leaders

Atlassian Community Events