【问题标题】:VSTS - Add Work Item with PullRequestVSTS - 使用 PullRequest 添加工作项
【发布时间】:2017-01-11 04:09:25
【问题描述】:

我们有一个 C# 中的自动化工具并使用“libgit2sharp”API,它将执行克隆存储库、暂存文件、提交和推送更改,通过 REST API 创建拉取请求。

现在必须使用拉取请求添加一个工作项,需要建议才能继续。

谢谢,

【问题讨论】:

  • 在提交信息中添加#1234
  • 您尝试将拉取请求关联到工作项后的结果是什么(参考我的解决方案)?

标签: c# azure-devops libgit2sharp


【解决方案1】:

不支持通过 REST API 或客户端 SDK API 将工作项关联到拉取请求,但您可以通过 REST API 将拉取请求链接到工作项。所以工作流程会是这样的:

  1. 通过 REST API 创建拉取请求
  2. 通过 REST API 将该拉取请求链接到工作项。

更多信息,您可以查看此线程以获取详细信息:Associate Work Items to a Pull Request Programmatically(包含 C# 代码)

【讨论】:

    【解决方案2】:
    public static bool AddWorkItem()
        {
            HttpWebResponse response = null;
            string workItem = "12345678";
            string pullReqId = string.Empty;
            string artifactId = string.Empty;
            string moduleName = "abcd";
    
    
            pullReqId = "123456";
            artifactId = "vstfs:///Git/PullRequestId/xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx";
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://tfs-glo-xxxxxxxxx.visualstudio.com/_apis/wit/workItems/" + workItem);
    
                request.Accept = "application/json;api-version=3.0;excludeUrls=true";
                request.ContentType = "application/json-patch+json";
                request.Referer = "https://tfs-glo-xxxxxxxxx.visualstudio.com/Apps/_git/" + moduleName + "/pullrequest/" + pullReqId + "?_a=overview";
    
                string _auth = string.Format("{0}:{1}", "GITUserName", "GITPassword");
                string _enc = Convert.ToBase64String(Encoding.ASCII.GetBytes(_auth));
                string _cred = string.Format("{0} {1}", "Basic", _enc);
                request.Headers[HttpRequestHeader.Authorization] = _cred;
    
                request.Method = "PATCH";
    
                string body = @"[{""op"":0,""path"":""/relations/-"",""value"":{""attributes"":{""name"":""Pull Request""},""rel"":""ArtifactLink"",""url"":" + "\"" + artifactId + "\"" + "}}]";
                byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(body);
                request.ContentLength = postBytes.Length;
                Stream stream = request.GetRequestStream();
                stream.Write(postBytes, 0, postBytes.Length);
                stream.Close();
    
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (Exception ex)
            {
                Log.Write("Add Work Item: ", ex);
                return false;
            }
            return true;
        }
    

    【讨论】:

      猜你喜欢
      • 2018-11-17
      • 1970-01-01
      • 2018-10-31
      • 1970-01-01
      • 2017-02-03
      • 1970-01-01
      • 1970-01-01
      • 2018-10-28
      • 2017-10-22
      相关资源
      最近更新 更多