使用REST API在Jira中创建问题是否可能?我在文档中没有找到相关信息(没有针对问题的POST请求),但我认为这是可能的。
一个wget或curl示例将是不错的。
将数据通过POST方式提交到此URL
https://<JIRA_HOST>/rest/api/2/issue/
这个数据:
{
"fields": {
"project":
{
"key": "<PROJECT_KEY>"
},
"summary": "REST EXAMPLE",
"description": "Creating an issue via REST API",
"issuetype": {
"name": "Bug"
}
}
}
从收到的答案中将会有你的ISSUE的ID和key:
{"id":"83336","key":"PROJECT_KEY-4","self":"https://<JIRA_HOST>/rest/api/2/issue/83336"}
不要忘记授权。我使用了HTTP-Basic授权。
这是C#代码:
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();
}
}
curl -D- -u <username>:<password> -X POST --data-binary "@<filename>" -H "Content-Type: application/json" http://<jira-host>/rest/api/2/issue/
{
"fields": {
"project":
{
"key": "<PROJECT_KEY>"
},
"summary": "REST EXAMPLE",
"description": "Creating an issue via REST API",
"issuetype": {
"name": "Bug"
}
}
}
这应该是有效的。(请注意,有时如果出现错误,可能是您的文件名中的内容不正确)。
生成并保存base64编码的身份验证令牌:
export b64token="$(echo "<your_email>:<generated_token>" | openssl base64)"
进行POST请求:
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"
}
}'
备注:
(*) 通常是项目描述的短小大写版本,例如:...atlassian.net/projects/UP/。
(**) 如果您不知道您的JIRA名称,请使用与上述相同的授权进行cURL GET到https://<your_jira_host>.atlassian.net/rest/api/2/search?jql=project=<any_project_name>
,并查找issues.fields.assignee.name
。
{“fields” : { “project” : { “key” : “@KEY@” } , “issuetype” : { “name” : “@IssueType@” } } }
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
您可以查看一个完整的VBA示例代码。
刚刚偶然发现这个问题,我正在尝试通过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返回一个已经存在的问题,而不是创建一个新的问题。
首选的身份验证方法是HTTP Basic(在使用SSL时)和OAuth,两者都在JIRA REST API教程中有记录。其他支持的方法包括:HTTP Cookies、可信应用程序和os_username/os_password查询参数。
- msangel