3

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?

Community
  • 1
  • 1
Miguel
  • 3,786
  • 2
  • 19
  • 32
  • You can also use the REST API to complete the PR : https://www.visualstudio.com/en-us/docs/integrate/api/git/pull-requests/pull-requests#auto-complete – Andy Li-MSFT Nov 17 '17 at 07:47
  • What about overriding the policies? – Miguel Nov 17 '17 at 15:57
  • A similar question is answered at this post https://stackoverflow.com/questions/52025417/how-to-programmatically-set-a-vsts-pullrequest-to-complete-automatically – PiJei Aug 29 '18 at 19:35
  • @pijei the version of the tfs is important in this case and this question is way before the other thread. I will check later proposed solution o check if it applies – Miguel Sep 03 '18 at 02:11

1 Answers1

4

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.

Hamid Shahid
  • 4,486
  • 3
  • 32
  • 41
  • I already did that and works perfectly when no policy is violated. Any idea of how to override policies using the client or how to set the autocomplete? – Miguel Nov 17 '17 at 15:56
  • 1
    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, you will see the option ByPassPolicy as a property in the GitPullrequestCompletionOptions class. You can set it to true to by pass policy. – Hamid Shahid Nov 17 '17 at 17:38