通过 Jira REST API、Atlassian.Net SDK 将问题状态更改为完成

Posted

技术标签:

【中文标题】通过 Jira REST API、Atlassian.Net SDK 将问题状态更改为完成【英文标题】:Changing issue status to Done via Jira REST API, Atlassian.Net SDK 【发布时间】:2019-12-25 12:03:35 【问题描述】:

我在 Atlassian.NET SDK 的帮助下使用 Visual Studio (c#) 使用 Jira REST API,并尝试构建这样的模型:

搜索带有项目、问题摘要和状态的工单; 如果发现问题,那么我想将其状态从“待办”更改为“完成”; 如果没有发现问题,则什么也不做。

搜索功能运行良好,问题在于更改其状态。这是我的代码:

static async System.Threading.Tasks.Task Main(string[] args)


            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; //TLS 1.2

            try
            

                // Connection to JIRA using REST client
                var jira = Jira.CreateRestClient("https://XXX.atlassian.net", "user", "token");

                // LINQ syntax for retrieving issues
                var issue = (from i in jira.Issues.Queryable
                             where i.Project == "QA" && i.Summary == "Summary" && i.Status == "To Do"
                             orderby i.Created
                             select i).First();

                await issue.SaveChangesAsync();

                string ticketid = issue.Key.Value;
                string ticketsummary = issue.Summary;
                string ticketkey = issue.JiraIdentifier;

                //Updating found issue
                var closeticket = await jira.Issues.GetIssueAsync(ticketid);
                closeticket.Status = "Done";

                await closeticket.SaveChangesAsync();                

            
            catch (Exception ex)  /*Result: SUCCESS [no issues found]*/ 
      

使用此代码,我尝试在项目“QA”中搜索,其中问题摘要为“Summary”,状态为“To Do”。然后我想将其状态从“待办”关闭为“完成”。但是我在 closeticket.Status = "Done";带文字:

错误 CS0200 无法将属性或索引器“Issue.Status”分配给 -- 它是只读的

请给我一些建议,否则我要上吊了……

提前谢谢你。

编辑: 我忘了提到我还尝试在 httpclient 的帮助下进行状态更改案例。但是有一个问题,当我连接到问题转换时,它返回状态码为 404 的错误。

 var handler = new HttpClientHandler();
            handler.AllowAutoRedirect = true;
            handler.Proxy = new WebProxy("https://XXX.atlassian.net", true, null, CredentialCache.DefaultNetworkCredentials);
            // need to pass a valid username + password to JIRA
            var jiraCredentials = UTF8Encoding.UTF8.GetBytes("user:token");
            var httpClient = new HttpClient(handler);
            httpClient.MaxResponseContentBufferSize = int.MaxValue;
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(jiraCredentials));
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));


            string transitionURL = "https://XXX.atlassian.net" + "/rest/api/2/issue/" + "QA-10" + "/transitions?expand=transitions.fields";
            string jsonString = " \"update\":,\"transition\":  \"id\": \"" + "31" + "\", \"fields\":   ";
            var sContent = new StringContent(jsonString, Encoding.UTF8, "application/json");
            var httpClientt = new HttpClient();
            HttpResponseMessage transitionResponse = httpClientt.PostAsync(transitionURL, sContent).Result;
            httpClient.Dispose();

【问题讨论】:

【参考方案1】:

经过深入研究,我终于找到了正确的方法,即http请求。

如果有人遇到问题,这里有一个代码:

 using (var httpClient = new HttpClient())
            
                using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://XXX.atlassian.net/rest/api/2/issue/TST-4/transitions?expand=transitions.fields"))
                
                    var base64authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes("user:token"));
                    request.Headers.TryAddWithoutValidation("Authorization", $"Basic base64authorization");

                    request.Content = new StringContent("\"transition\":\"id\":\"31\"");
                    request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                    var response = await httpClient.SendAsync(request);
                
            

【讨论】:

以上是关于通过 Jira REST API、Atlassian.Net SDK 将问题状态更改为完成的主要内容,如果未能解决你的问题,请参考以下文章

不允许通过 REST api 和 jquery 405 方法创建 Jira 问题

Python无法通过以下方式连接Jira API

通过 Jira REST API、Atlassian.Net SDK 将问题状态更改为完成

如何使用 JIRA REST API 将 JIRA Agile 问题分配给当前用户的当前 sprint?

JIRA REST java client API实际应用

如何使用 JIRA Cloud REST API 创建对 jira 问题的内部评论