我知道之前已经问过这个问题,但我似乎无法将其付诸实践。
我能够对Jira进行身份验证并使用JSON字符串创建新票证,但尝试关闭同一问题会产生错误请求"错误。
代码:
public string jiraJSON;
public void openJira()
{
jiraJSON = string.Format(@"{{""fields"":{{""assignee"":{{""name"":""{0}""}},""project"":{{""key"":""TS""}},""summary"":""{1}"",""description"":""{2}"",""issuetype"":{{""name"":""Unplanned Event""}} }} }}", jiraUsernameTextBox.Text, jiraSummary, jiraDescription);
Dictionary<string, string> jiraResponse = sendHTTPtoJIRA(jiraJSON,"open","");
}
public void closeJira(string jiraKey)
{
jiraJSON = @"{{""update"":{{""comment"":[{{""add"":{{""body"":""Done""}}}}]}},""fields"":{{""resolution"":{{""name"":""Done""}}}},""transition"":{{""id"":""51""}}}}";
jiraResponse = sendHTTPtoJIRA(jiraJSON,"close",jiraKey);
}
private Dictionary<string,string> sendHTTPtoJIRA(string json, string operation,string issueID)
{
string restURL="";
string method = "";
switch (operation)
{
case "open":
restURL = string.Format("{0}rest/api/2/issue/", jiraURL);
method = "POST";
break;
case "close":
restURL = string.Format("{0}rest/api/2/issue/{1}/transitions/?expand=transitions.fields", jiraURL,issueID);
method = "POST";
break;
}
HttpWebResponse response = null;
HttpWebRequest request = WebRequest.Create(restURL) as HttpWebRequest;
request.Method = method;
request.Accept = "application/json";
request.ContentType = "application/json";
request.Headers.Add("Authorization", "Basic " + authenticateJira());
byte[] data = Encoding.UTF8.GetBytes(json);
using (var requestStream = request.GetRequestStream())
{
requestStream.Write(data, 0, data.Length);
requestStream.Close();
}
using (response = request.GetResponse() as HttpWebResponse)
{
var reader = new StreamReader(response.GetResponseStream());
string str = reader.ReadToEnd();
displayMessages(string.Format("The server returned '{0}'\n{1}", response.StatusCode, str), "white", "purple");
var jss = new System.Web.Script.Serialization.JavaScriptSerializer();
var sData = jss.Deserialize<Dictionary<string, string>>(str);
sData.Add("code", response.StatusCode.ToString());
request.Abort();
return sData;
}
}
为了清楚起见,我稍微解释了一下代码,但是&#34; sendHTTPtoJIRA&#34;方法是逐字的,jiraJSON字符串也是逐字的。
使用此代码,我可以打开一个问题并将其分配给自己,但是当我尝试关闭该问题时,我会得到一个错误的请求&#34;它告诉我我的jiraJSON字符串在&#34; closeJira&#34;方法不正确。
异常落在线上&#34;使用(response = request.GetResponse()作为HttpWebResponse)&#34;和引用&#34; jiraResponse = sendHTTPtoJIRA(jiraJSON,&#34; close&#34;,jiraKey);&#34;作为调用该方法的行,所以当我尝试关闭该问题时,我知道它是错误的。
我已解决的其他帖子的常见问题:
扩展的JSON字符串我用来关闭问题:
{{
""update"":{{""comment"":[{{""add"":{{""body"":""Done""}}}}]}},
""fields"":{{""resolution"":{{""name"":""Done""}}}},
""transition"":{{""id"":""51""}}
}}
我当然尝试过各种变体。似乎没什么用。我还包括带有和没有引号的51无济于事。
当我浏览http://jira/rest/api/2/issue/TS-1000/transitions/?expand=transitions.fields时 我得到以下输出(这是我得到的&#34; 51&#34;我的jiraJSON字符串中的ID):
{
"expand": "transitions",
"transitions": [{
"id": "11",
"name": "Start Progress",
"to": {
"self": "http://jira/rest/api/2/status/3",
"description": "This issue is being actively worked on at the moment by the assignee.",
"iconUrl": "http://jira/images/icons/statuses/inprogress.png",
"name": "In Progress",
"id": "3",
"statusCategory": {
"self": "http://jira/rest/api/2/statuscategory/4",
"id": 4,
"key": "indeterminate",
"colorName": "yellow",
"name": "In Progress"
}
},
"fields": {
"attachment": {
"required": false,
"schema": {
"type": "array",
"items": "attachment",
"system": "attachment"
},
"name": "Attachment",
"operations": []
},
"assignee": {
"required": true,
"schema": {
"type": "user",
"system": "assignee"
},
"name": "Assignee",
"autoCompleteUrl": "http://jira/rest/api/latest/user/assignable/search?issueKey=TS-2034&username=",
"operations": ["set"]
}
}
}, {
"id": "51",
"name": "Close Issue",
"to": {
"self": "http://jira/rest/api/2/status/6",
"description": "The issue is considered finished, the resolution is correct. Issues which are closed can be reopened.",
"iconUrl": "http://jira/images/icons/statuses/closed.png",
"name": "Closed",
"id": "6",
"statusCategory": {
"self": "http://jira/rest/api/2/statuscategory/3",
"id": 3,
"key": "done",
"colorName": "green",
"name": "Done"
}
},
"fields": {
"resolution": {
"required": true,
"schema": {
"type": "resolution",
"system": "resolution"
},
"name": "Resolution",
"operations": ["set"],
"allowedValues": [{
"self": "http://jira/rest/api/2/resolution/1",
"name": "Fixed",
"id": "1"
}, {
"self": "http://jira/rest/api/2/resolution/5",
"name": "Cannot Reproduce",
"id": "5"
}, {
"self": "http://jira/rest/api/2/resolution/3",
"name": "Duplicate",
"id": "3"
}, {
"self": "http://jira/rest/api/2/resolution/4",
"name": "Incomplete",
"id": "4"
}, {
"self": "http://jira/rest/api/2/resolution/7",
"name": "Review Completed",
"id": "7"
}, {
"self": "http://jira/rest/api/2/resolution/6",
"name": "Unresolved",
"id": "6"
}, {
"self": "http://jira/rest/api/2/resolution/2",
"name": "Won't Fix",
"id": "2"
}, {
"self": "http://jira/rest/api/2/resolution/10000",
"name": "Done",
"id": "10000"
}, {
"self": "http://jira/rest/api/2/resolution/10100",
"name": "Edgewater Review",
"id": "10100"
}, {
"self": "http://jira/rest/api/2/resolution/10200",
"name": "Active Project",
"id": "10200"
}, {
"self": "http://jira/rest/api/2/resolution/10300",
"name": "Won't Do",
"id": "10300"
}
]
},
"customfield_10652": {
"required": false,
"schema": {
"type": "string",
"custom": "com.atlassian.jira.plugin.system.customfieldtypes:textarea",
"customId": 10652
},
"name": "Resolution Activity",
"operations": ["set"]
}
}
}
]
}
那我用JSON字符串做什么呢?任何建议,将不胜感激。谢谢!
答案 0 :(得分:0)
带双引号的格式化JSON字符串总是出错。所以使用Json.Net dll并使用JObject来形成json字符串。
以下是更新自定义字段值“
的示例 JObject customFiledObject = new JObject(
new JProperty("fields",
new JObject(new JProperty("customfield_1100", new JArray(10)))));
准备好所需的JSON对象后,将对象格式化为Json字符串,如下所示
string jSonString = customFiledObject.ToString(Newtonsoft.Json.Formatting.Indented);
答案 1 :(得分:0)
我知道这个问题已经回答了一段时间,但是对于任何患有Jiras&#39; 400 Bad Request&#39;的人来说,你可以通过添加下面的catch语句来捕获更有意义的错误。
catch (System.Net.WebException ex)
{
WebResponse resp = ex.Response;
string JiraErrorMessages = (new System.IO.StreamReader(resp.GetResponseStream(), true)).ReadToEnd();
}