2016-12-13 79 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#代碼)在提交信息

相關問題