JIRA是一个缺陷跟踪管理系统,被广泛应用于缺陷跟踪、客户服务、需求收集、流程审批、任务跟踪、项目跟踪和敏捷管理等工作领域,当我们需要把第三方业务系统集成进来时,可以调用他的API。
  JIRA本身的API非常强大,几乎你能想象到的都能实现。但他的API是一个底层的API体系,并不是一个易用的接口,不利于开发和拓展,所以需要我们二次包装。
  于是写了一个包装类,可以在任意地方调用。
  jira的Rest API  文档官网:
  https://docs.atlassian.com/jira/REST/latest/
  首先,必须要了解JIRA api的接口结构,其中<resource-name>可以理解成api的方法,比如project,是项目处理,user是用户处理,issue是问题处理....
  http://hostname/rest/<api-name>/<api-version>/<resource-name>
  JIRA's REST API is provided by a plugin that is anchored under the URI path component /rest/. Hence, if your JIRA site is running at:
  还要搞清楚jira api的认证体系,摘自官网:
  the first step in using the JIRA REST API is to authenticate a user account with your JIRA site. For the purposes of this tutorial we will use HTTP BASIC Authentication, but any authentication that works against JIRA will work against the REST API. This includes:
  OAuth
  HTTP Cookies
  Trusted Applications
  os_username/os_password query parameters
  为方便使用,我们采用Basic Auth
  Basic Auth headers
  If you need to you may construct and send basic auth headers yourself. To do this you need to perform the following steps:
  Build a string of the form username:password
  Base64 encode the string
  Supply an "Authorization" header with content "Basic " followed by the encoded string. For example, the string "fred:fred" encodes to "ZnJlZDpmcmVk" in base64, so you would make the request as follows.
  一个curl的例子,注意红色字符串是对“username:password”的Base64编码
  curl -D- -X GET -H "Authorization: Basic ZnJlZDpmcmVk" -H "Content-Type: application/json" "http://kelpie9:8081/rest/api/2/issue/QA-31"
  搞清楚了Basic Auth,可以开搞代码
  API包装类
1 public class JiraApi
2     {
3         private string m_Username;
4         private string m_Password;
5
6         public JiraApi(string username, string password)
7         {
8             m_Username = username;
9             m_Password = password;
10         }
11
12         /// <summary>
13         /// 处理post请求,执行新建、编辑、删除等操作
14         /// </summary>
15         /// <param name="sData">json输入字符</param>
16         /// <param name="uri">api的具体地址,一般是baseurl + 业务处理资源关键字</param>
17         /// <returns>Jira返回的WebResponse输出</returns>
18         public string DoPost(string sData, string uri)
19         {
20             Uri address = new Uri(uri);
21             HttpWebRequest request;
22             //HttpWebResponse response1 = null;
23             StreamReader sr;
24             string returnXML = string.Empty;
25             if (address == null) { throw new ArgumentNullException("address"); }
26             try
27             {
28                 request = WebRequest.Create(address) as HttpWebRequest;
29                 request.Method = "POST";
30                 request.ContentType = "application/json";
31                 string base64Credentials = GetEncodedCredentials();
32                 request.Headers.Add("Authorization", "Basic " + base64Credentials);
33                 //request.Credentials = new NetworkCredential(sUsername, sPassword);
34                 if (sData != null)
35                 {
36                     byte[] byteData = UTF8Encoding.UTF8.GetBytes(sData);
37                     request.ContentLength = byteData.Length;
38                     using (Stream postStream = request.GetRequestStream())
39                     {
40                         postStream.Write(byteData, 0, byteData.Length);
41                     }
42                     using (HttpWebResponse response1 = request.GetResponse() as HttpWebResponse)
43                     {
44                         StreamReader reader = new StreamReader(response1.GetResponseStream());
45                         string str = reader.ReadToEnd();
46                         return str;
47
48                     }
49                 }
50                 return "error";
51
52             }
53             catch (WebException wex)
54             {
55
56                 if (wex.Response != null)
57                 {
59                     using (HttpWebResponse errorResponse = (HttpWebResponse)wex.Response)
60                     {
61                         try
62                         {
63                             string sError = string.Format("The server returned '{0}' with the status code {1} ({2:d}).",
64                             errorResponse.StatusDescription, errorResponse.StatusCode,
65                             errorResponse.StatusCode);
66                             sr = new StreamReader(errorResponse.GetResponseStream(), Encoding.UTF8);
67                             returnXML = sr.ReadToEnd();
68                             return returnXML;
69
70                         }
71                         finally
72                         {
73                             if (errorResponse != null) errorResponse.Close();
74                         }
75                     }
76                 }
77                 else
78                 {
79                     //throw new Exception(wex.Message);
80                     return wex.Message;
81
82                 }
83             }
84         }
85
86
87
88         /// <summary>
89         /// 处理get请求,执行查询操作
90         /// </summary>
91         /// <param name="resource">输入的业务处理资源关键字,必填项</param>
92         /// <param name="argument">参数,用于获取具体查询操作,非必填项</param>
93         /// <param name="data">暂时没用处,非必填项</param>
94         /// <param name="method">默认为GET,非必填项</param>
95         /// <returns></returns>
96         public string DoQuery(
97             string resource,
98             string argument = null,
99             string data = null,
100             string method = "GET")
101         {
102             string url = string.Format("{0}{1}/", Config.BaseURL, resource.ToString());
103
104             if (argument != null)
105             {
106                 url = string.Format("{0}{1}/", url, argument);
107             }
108
109             HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
110             request.ContentType = "application/json";
111             request.Method = method;
112
113             if (data != null)
114             {
115                 using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
116                 {
117                     writer.Write(data);
118                 }
119             }
120
121             string base64Credentials = GetEncodedCredentials();
122             request.Headers.Add("Authorization", "Basic " + base64Credentials);
123
124             HttpWebResponse response = request.GetResponse() as HttpWebResponse;
125
126             string result = string.Empty;
127             using (StreamReader reader = new StreamReader(response.GetResponseStream()))
128             {
129                 result = reader.ReadToEnd();
130             }
131
132             return result;
133
134         }
135
136         private string GetEncodedCredentials()
137         {
138             string mergedCredentials = string.Format("{0}:{1}", m_Username, m_Password);
139             byte[] byteCredentials = UTF8Encoding.UTF8.GetBytes(mergedCredentials);
140             return Convert.ToBase64String(byteCredentials);
141         }
142     }