2017-01-23 127 views
0

我知道这个问题已经被问过,但我似乎无法得到它太工作。C#使用REST API在Jira中关闭一个问题

我能够通过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串也是一字不差。

使用此代码,我可以打开一个问题并将其分配给我自己,但是当我尝试关闭该问题时,我收到一个“400错误请求”,它告诉我我的jiraJSON字符串在“closeJira “方法不正确。

异常着陆在“使用(response = request.GetResponse()as HttpWebResponse)”和引用“jiraResponse = sendHTTPtoJIRA(jiraJSON,”close“,jiraKey);”作为调用该方法的路线,所以我知道它在尝试解决问题时是错误的。

从其他职位的常见问题,我已经解决:

  1. 我使用有权限关闭问题的用户帐户。
  2. 我试过了“POST”和“PUT”方法。使用“PUT”会产生“405方法不允许”的错误。
  3. 躲过大括号和引号。
  4. 我使用关闭问题

压缩扩大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 我得到下面的输出(这是我得到了“51”在我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字符串在做什么?任何建议,将不胜感激。谢谢!

+1

您是否验证过您生产合法的json?你应该考虑使用一个库,比如Json.Net,然后将对象层次序列化为json。如果您的数据包含撇号或类似的无效字符,除非正确处理,否则使用当前代码可能会导致问题。 –

回答

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

0

我知道问题有后已经回答了一段时间,但对于其他患有Jiras'400 Bad Request'的人,您可以通过添加下面的catch语句来捕捉更有意义的错误。

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