以编程方式将工作项关联到Pull请求

时间:2016-09-06 15:29:04

标签: git azure-devops pull-request

我可以获得(https://www.visualstudio.com/en-us/docs/integrate/api/git/pull-requests#get-a-pull-request)拉取请求,管理审阅者并完成它。分支有一个需要工作项的策略,它失败,因为pull请求不会自动添加与底层提交相关联的工作项。

针对具有错误参数的拉取请求对PATCH进行攻击

  

“您只能更新评论者,说明,标题,合并状态,   和状态“

我可以使用pull requests url + / workitems获取工作项列表,但集合上不支持POST,PUT和PATCH。

我没有看到将工作项与拉取请求相关联的方法吗?

3 个答案:

答案 0 :(得分:1)

您可以通过更新工作项链接将工作项与拉取请求相关联,就像starain所提到的那样。

不确定您使用的是哪种代码语言,我添加了一个C#代码示例供您参考:

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.SourceControl.WebApi;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
using Microsoft.VisualStudio.Services.WebApi.Patch.Json;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models;

namespace PRWIl
{
    class Program
    {
        static void Main(string[] args)
        {
            string tfsurl = "https://xxx.visualstudio.com/";
            TfsTeamProjectCollection ttpc = new TfsTeamProjectCollection(new Uri(tfsurl));
            GitHttpClient ghc = ttpc.GetClient<GitHttpClient>();
            string project = "ProjectName";
            string repoid = "repositoryid";
            int pullrequestid = 1;
            int workitemid = 1;
            GitPullRequest gpr = ghc.GetPullRequestAsync(project,repoid,pullrequestid).Result;

            WorkItemTrackingHttpClient withc = ttpc.GetClient<WorkItemTrackingHttpClient>();

            JsonPatchDocument json = new JsonPatchDocument();

            string pullrequesturl = "vstfs:///Git/PullRequestId/" + gpr.Repository.ProjectReference.Id + "%2F" + gpr.Repository.Id + "%2F" + gpr.PullRequestId;
            json.Add(new JsonPatchOperation
            {
                Operation = Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Add,
                Path = "/relations/-",
                Value = new WorkItemRelation() {Rel = "ArtifactLink", Url = pullrequesturl }
            });
            WorkItem result = withc.UpdateWorkItemAsync(json,workitemid).Result;
        }
    }
}

答案 1 :(得分:0)

不支持通过REST API或客户端SDK API将工作项与拉取请求相关联。

您可以投票此用户语音(https://visualstudio.uservoice.com/forums/330519-team-services/suggestions/15954904-rest-api-create-or-update-pull-request-with-work-i

解决方法是您可以将拉取请求链接到工作项。 https://www.visualstudio.com/en-us/docs/integrate/api/wit/work-items#add-a-link(链接另一个工作项后,可以在响应中找到拉取请求URL,因此您可以手动将工作项链接到拉取请求,然后通过REST API将另一个工作项链接到该工作项并检查响应)

答案 2 :(得分:0)

这是可以实现的。以下是示例代码段:

List<GitPullRequest> pullRequests = gitClient.GetPullRequestsAsync(
    repoId, GetPullRequestSearchCriteria(devBranch)).Result.Where
    p => p.ClosedDate > masterPullRequest.ClosedDate).ToList();

List<ResourceRef> resourceRef = new List<ResourceRef>();

foreach (GitPullRequest pullRequest in pullRequests)
{
    resourceRef.AddRange(gitClient.GetPullRequestAsync(
        repoId, pullRequest.PullRequestId, includeWorkItemRefs: true).Result.WorkItemRefs);
}

GitPullRequest gitPullRequest = new GitPullRequest()
{
    Title = "Sprint 9",
    TargetRefName = masterBranch,
    SourceRefName = devBranch,
    WorkItemRefs = resourceRef.ToArray()
};

GitPullRequest g = gitClient.CreatePullRequestAsync(gitPullRequest, repoId).Result;