VSTS - 使用PullRequest添加工作项

时间:2016-12-13 19:43:27

标签: c# azure-devops libgit2sharp

我们在c#中有一个自动化工具并使用“libgit2sharp”API,它将执行Clone Repository,Stage文件,Commit和Push更改,Pull Request创建由REST API完成。

现在必须使用Pull Request添加工作项,需要建议才能继续。

谢谢,

2 个答案:

答案 0 :(得分:1)

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

  1. 通过REST API创建拉取请求
  2. 通过REST API将请求拉到工作项的链接。
  3. 更多信息,您可以查看此主题的详细信息:Associate Work Items to a Pull Request Programmatically(包括C#代码)

答案 1 :(得分: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;
    }