问题
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