簡體   English   中英

C#使用REST API關閉Jira中的問題

[英]C# Closing an issue in Jira using the REST API

我知道之前曾有人問過這個問題,但我似乎無法解決這個問題。

我能夠通過Jira進行身份驗證並使用JSON字符串創建新票證,但是嘗試關閉同一問題會產生“ 400錯誤請求”錯誤。

碼:

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

為了清楚起見,我對代碼進行了一些解釋,但是“ sendHTTPtoJIRA”方法是逐字的,而jiraJSON字符串也是逐字的。

使用此代碼,我可以打開一個Issue並將其分配給我自己,但是當我嘗試關閉Issue時,我收到一個“ 400 Bad Request”,告訴我“ closeJira”方法中的jiraJSON字符串是“不正確。

異常落在“使用(使用(響應= request.GetResponse()作為HttpWebResponse)”)行上,並引用“ jiraResponse = sendHTTPtoJIRA(jiraJSON,“ close”,jiraKey);“ 作為調用該方法的行,所以我知道它在嘗試解決問題時出了問題。

我已解決的其他帖子中的常見問題:

  1. 我正在使用的用戶帳戶具有關閉問題的權限。
  2. 我已經嘗試過“ POST”和“ PUT”方法。 使用“ PUT”會產生“ 405方法不允許”錯誤。
  3. 轉義大括號和引號。

我用來解決此問題的擴展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時,我得到以下輸出(這是我在jiraJSON中獲得ID的“ 51”的方式)串):

{
    "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字符串呢? 任何建議,將不勝感激。 謝謝!

帶有雙引號的格式化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);

我知道這個問題已經回答了一段時間,但是對於其他遭受Jiras“ 400錯誤請求”困擾的人,您可以通過在下面添加catch語句來捕獲更有意義的錯誤。

catch (System.Net.WebException ex)
{
    WebResponse resp = ex.Response;
    string JiraErrorMessages = (new System.IO.StreamReader(resp.GetResponseStream(), true)).ReadToEnd();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM