使用 JIRA SDK 设置时间值(特别是 OriginalEstimate)

Set time values (specifically OriginalEstimate) using JIRA SDK

我一直在关注 this 通过 Atlassian SDK 与 JIRA 交互的指南。

但在使用 SDK 设置发布时间估计时,我 运行 遇到了麻烦。我想出了一个笨拙的解决方法,可以让您通过添加一个指定新时间的假工作日志来设置剩余时间估计:

jiraConn = Jira.CreateRestClient(JIRA_BASE_ADDRESS, J_USER_ID, J_PASSWORD);
Issue newIssue = jiraConn.CreateIssue("CL");
//Inadequate 'solution':
newIssue.AddWorklog("0h", WorklogStrategy.NewRemainingEstimate, "10d");

我对能够设置 OriginalEstimate 值特别感兴趣,但任何关于设置其他时间跟踪数据的建议都会很棒。我还尝试更改 issue.GetTimeTrackingData() 返回的 IssueTimeTrackingData 对象的值 - 但该对象似乎与 JIRA 数据库断开连接,因此更改不会提交。

旧版本的 JIRA SDK 具有 SetEstimate() 类型的方法。这些随后被删除,因此虽然您可以访问时间估计值,但它们不能更改(除了 RemainingTime 可以通过添加工作日志来更新,如问题中所述)。

我建议直接访问 REST API 以更新原始估计值

try
        {
            var httpWebRequest = (HttpWebRequest)WebRequest.Create("<Jira URL>/rest/api/2/issue/" + IssueKey);
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Accept = "application/json";
            httpWebRequest.Method = "PUT";

            String username = ConfigurationManager.AppSettings["JiraUserName"];
            String password = ConfigurationManager.AppSettings["JiraPassword"];
            String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
            httpWebRequest.Headers.Add("Authorization", "Basic " + encoded);

            using (StreamWriter sw = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json = "{" +
                               "\"fields\" : { " +
                               "\"timetracking\":{ \"originalEstimate\":\"12h\"  }" + 
                               "} }";
                sw.Write(json);
                sw.Flush();
                sw.Close();
            }
            using (HttpWebResponse httpResponse = (HttpWebResponse)httpWebRequest.GetResponse())
            {
                httpResponse.Close();
            }
        }
        catch (WebException ex)
        {

            WebResponse wbResponse = ex.Response;
            HttpWebResponse response = (HttpWebResponse)wbResponse;
            response.Close();
        }

所以我在寻找通过 Jira 的 API 设置原始估计时间的方法时多次偶然发现这个线程。非常不幸的是 C# 中的 Atlassian SDK 不允许更改原始估计时间。更糟糕的是,抛出的异常根本没有任何信息!

无论如何这是解决方案:

  var jiraConnection = Jira.CreateRestClient(BASE_URL, username, password)
  var restRequest = new RestRequest("rest/api/2/issue/" + issue.JiraIdentifier);
  restRequest.Method = Method.PUT;
  restRequest.RequestFormat = DataFormat.Json;
  var requestBody = new { update = new { timetracking = new[]{ new { edit = new { originalEstimate = work.ToString()+"h" } } } } };
  restRequest.AddJsonBody(requestBody);

  IRestResponse response = jiraConnection.RestClient.ExecuteRequest(restRequest);

  if (response.ResponseStatus != ResponseStatus.Completed)
  {
    throw new System.Net.WebException("Error creating planning issue");
  }

它基于此 solution 使用第一个示例。我首先尝试了第二种解决方案,但它没有用,至少对我来说,所以你必须使用更长的。