Associate Work Items to a Pull Request Programmati

2019-07-09 05:57发布

I can get (https://www.visualstudio.com/en-us/docs/integrate/api/git/pull-requests#get-a-pull-request) a pull request, manage reviewers and complete it. The branch has a policy requiring work items and it fails as the pull request does not automatically add the work items associated with the underlying commits.

Poking at the PATCH against the pull request with bad parameters nets a

"You can only update reviewers, descriptions, titles, merge status, and status"

I can get a list of work items using the pull requests url + /workitems but POST, PUT and PATCH all are not supported on the collection.

I do not see a way to associate a work item with the pull request?

3条回答
混吃等死
2楼-- · 2019-07-09 06:10

You can associate work item to a pull request by updating the work item links just as starain mentioned.

Not sure which code language you use, I added a C# code sample for your reference:

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;
        }
    }
}
查看更多
再贱就再见
3楼-- · 2019-07-09 06:10

It is not supported to associate work items to a pull request through REST API or client SDK API.

You can vote this user voice (https://visualstudio.uservoice.com/forums/330519-team-services/suggestions/15954904-rest-api-create-or-update-pull-request-with-work-i)

The workaround is that you could link pull request to work item. https://www.visualstudio.com/en-us/docs/integrate/api/wit/work-items#add-a-link (The pull request URL could be found in the response after you link another work item, so you could link a work item to pull request manually, then link another work item to that work item through REST API and check the response)

查看更多
Melony?
4楼-- · 2019-07-09 06:13

This is achievable now. Here is sample code snippet:

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;
查看更多
登录 后发表回答