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