2016-12-13 49 views
-1

我们在c#中使用自动化工具并使用“libgit2sharp”API,它将执行克隆存储库,阶段文件,提交和推送更改,通过REST API完成请求创建。VSTS - 使用PullRequest添加工作项目

现在必须添加一个工作项目与合并请求,需要建议继续。

感谢,

+1

加上'#1234'。 – jessehouwing

+0

尝试将拉请求与工作项关联后,结果如何?(请参阅我的解决方案)? –

回答

0
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; 
    } 
1

它不支持的工作项目,通过REST API或客户端SDK API拉入请求联系在一起,但你可以链接拉动请求通过REST API的工作项目。因此,工作流程是这样的:

  1. 通过REST API创建pull请求
  2. 链接那拉请求通过REST API的工作项目。

的更多信息,你可以检查此线程细节:Associate Work Items to a Pull Request Programmatically(包括C#代码)在提交信息

相关问题