Complete TFS Pull Request programmatically

元气小坏坏 提交于 2019-12-11 01:27:42

问题


Using the Microsoft.TeamFoundationServer.Client (15.112.1) to connect to a TFS 2017 Update 2 server we can get details about an existing PR like this:

var connection = new VssConnection(collectionUri, credentials);
var client = connection.GetClient<GitHttpClient>();
var pr = await client.GetPullRequestByIdAsync(pullRequestId);

Also, we can create new PR like this:

var pr = await client.CreatePullRequestAsync(
        new GitPullRequest
        {
          SourceRefName = "master",
          TargetRefName = "develop",
          Title = "[Automatic Merge]"
        },
        projectName, repositoryName);

In addition, we can vote on the PR like this:

var pr = await client.CreatePullRequestReviewerAsync(
            reviewer, projectName, repositoryName, pullRequestId, authorizedIdenity.Id.ToString());

Is there any way to complete the PR (overriding or not existing branch policies) and proceed with the merge operation?


回答1:


The GitHttpClient has an UpdatePullRequestAsync method.

To complete the pull request you need to update the Status property of your pull request. and use the UpdatePullRequestAsync method to complete your PR.

Please make sure that to set the the CompletionOptions property to specify whether you are merging the commit, delete the source branch etc.

So your code would look like following

pr.Status = PullRequestStatus.Completed
pr.CompletionOption = new GitPullRequestCompletionOption() { SquashMerge = true };
client.UpdatePullRequest(pr, repositoryId, pullRequestId);

EDIT:

The ByPassPolicy is not available for the released version of Microsoft.TeamFoundationServer.ExtendedClient yet.

However, if you install the pre-release NuGet Package v15.122.1-preview of library Microsoft.TeamFoundationServer.ExtendedClient, you will see the option ByPassPolicy as a property in the GitPullrequestCompletionOptions class. You can set it to true to by pass policy.



来源:https://stackoverflow.com/questions/47339126/complete-tfs-pull-request-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!