how to create an issue in jira via rest api?

百般思念 提交于 2019-11-27 01:34:28

问题


Is it possible to create an issue in jira using REST api? I didn't find this in the documentation (no POST for issues), but I suspect it's possible.

A wget or curl example would be nice.


回答1:


POST to this URL

https://<JIRA_HOST>/rest/api/2/issue/

This data:

{
"fields": {
   "project":
   { 
      "key": "<PROJECT_KEY>"
   },
   "summary": "REST EXAMPLE",
   "description": "Creating an issue via REST API",
   "issuetype": {
      "name": "Bug"
   }
  }
}

In received answer will be ID and key of your ISSUE:

{"id":"83336","key":"PROJECT_KEY-4","self":"https://<JIRA_HOST>/rest/api/2/issue/83336"}

Don't forget about authorization. I used HTTP-Basic one.




回答2:


The REST API in JIRA 5.0 contains methods for creating tasks and subtasks.

(At time of writing, 5.0 is not yet released, although you can access 5.0-m4 from the EAP page. The doco for create-issue in 5.0-m4 is here).




回答3:


As of the latest released version (4.3.3) it is not possible to do using the REST API. You can create issues remotely using the JIRA SOAP API.

See this page for an example Java client.




回答4:


To answer the question more direct, i.e. using cURL.

To use cURL to access JIRA REST API in creating a case, use

curl -D- -u <username>:<password> -X POST --data-binary "@<filename>"  -H "Content-Type: application/json" http://<jira-host>/rest/api/2/issue/

And save this in your < Filename> (please edit the field per your Jira case) and save in the folder you call the cURL command above.

{
    "fields": {
       "project":
       { 
           "key": "<PROJECT_KEY>"
       },
       "summary": "REST EXAMPLE",
       "description": "Creating an issue via REST API",
       "issuetype": {
           "name": "Bug"
       }
   }
}

This should works. (note sometimes if it errors, possibly your content in the Filename is incorrect).




回答5:


This is C# code:

string postUrl = "https://netstarter.jira.com/rest/api/latest/issue";

var httpWebRequest = (HttpWebRequest)WebRequest.Create(postUrl);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
httpWebRequest.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes("JIRAMMS:JIRAMMS"));

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
    string json = @"{""fields"":{""project"":{""key"": ""JAPI""},""summary"": ""REST EXAMPLE"",""description"": ""Creating an issue via REST API 2"",""issuetype"": {""name"": ""Bug""}}}";

    streamWriter.Write(json);
    streamWriter.Flush();
    streamWriter.Close();

    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
        var result = streamReader.ReadToEnd();
    }
}



回答6:


Now you can use REST + JSON to create issues.

To check which json fields you can set to create the issue use: https://jira.host.com/rest/api/2/issue/createmeta

For more information please see the JIRA rest documentation: https://docs.atlassian.com/jira/REST/6.2.4/




回答7:


To send the issue data with REST API we need to construct a valid JSON string comprising of issue details.

A basic example of JSON string:

 {“fields” : { “project” : { “key” : “@KEY@” } , “issuetype” : { “name” : “@IssueType@” } } }

Now, establish connection to JIRA and check for the user authentication. Once authentication is established, we POST the REST API + JSON string via XMLHTTP method. Process back the response and intimate user about the success or failure of the response.

So here JiraService being an XMLHTTP object, something like this will add an issue, where EncodeBase64 is a function which returns encrypted string.

Public Function addJIRAIssue() as String
With JiraService
    .Open "POST", <YOUR_JIRA_URL> & "/rest/api/2/issue/", False
    .setRequestHeader "Content-Type", "application/json"
    .setRequestHeader "Accept", "application/json"
    .setRequestHeader "Authorization", "Basic " & EncodeBase64
    .send YOUR_JSON_STRING

    If .Status <> 401 Then
        addJIRAIssue = .responseText
    Else
        addJIRAIssue = "Error: Invalid Credentials!"
    End If

End With

Set JiraService = Nothing
End Sub

You can check out a complete VBA example here




回答8:


Just stumbling on this and am having issues creating an issue via the REST API.

issue_dict = {
    'project': {'key': "<Key>"},
    'summary': 'New issue from jira-python',
    'description': 'Look into this one',
    'issuetype': {'name': 'Test'},
}
new_issue = jira.create_issue(issue_dict)

new_issue returns an already existing issue and doesn't create one.




回答9:


In order to create an issue, set a time estimate and assign it to yourself, use this:

  1. Generate an Atlassian token

  2. Generate & save a base64-encoded auth token:

    export b64token="$(echo "<your_email>:<generated_token>" | openssl base64)"

  3. Make a POST request:

curl -X POST \
  https://<your_jira_host>.atlassian.net/rest/api/2/issue/ \
     -H 'Accept: */*' \
     -H 'Authorization: Basic $b64token \
     -d '{
       "fields":{
         "project":{
           "key":"<your_project_key (*)>"
         },
         "issuetype":{
           "name":"Task"
         },
         "timetracking":{
           "remainingEstimate":"24h"
        },
         "assignee":{
           "name":"<your_name (**)>"
       },
       "summary":"Endpoint Development"
     }
   }'

Remarks:

(*) Usually a short, capitalized version of the project description such as: ...atlassian.net/projects/UP/.

(**) if you don't know your JIRA name, cURL GET with the same Authorization as above to https://<your_jira_host>.atlassian.net/rest/api/2/search?jql=project=<any_project_name> and look for issues.fields.assignee.name.



来源:https://stackoverflow.com/questions/5884960/how-to-create-an-issue-in-jira-via-rest-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!