VSTS - 使用 PullRequest 添加工作项

VSTS - Add Work Item with PullRequest

我们在 c# 中有一个自动化工具并使用 "libgit2sharp" API,它将执行克隆存储库、暂存文件、提交和推送更改、拉取请求创建由 REST 完成 API.

现在必须通过合并请求添加一个工作项,需要建议才能继续。

谢谢,

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

  1. 通过 REST 创建拉取请求API
  2. Link 通过 REST API.
  3. 向工作项拉取请求

更多信息,您可以查看此线程以获取详细信息:(包括 C# 代码)

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