Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enforcing a Squash merge on Azure DevOps Pull Request using GitPullRequestCompletionOptions

I am attempting to use the Azure DevOps Services REST API to set the PR Completion Options to enforce a squash merge on a Pull Request.

Note: I can't set a branch policy to enforce a squash merge as I am testing certain conditions to see if a squash merge is required or not and attempting to enforce a squash as if the branch policy is set for that PR only.

When I make the following call:

PATCH https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullrequests/{pullRequestId}?api-version=5.0

{
  "completionOptions": {
    "squashMerge": true
  } 
}

The response shows the value is set

A section of the response, showing the value of 'squashMerge' set to true.

Yet when attempting to complete the request, I would expect the "Squash changes when merging" checkbox to be ticked and disabled.

Squash option is not selected, or disabled.

If I leave the form as-is and complete the merge, no squash is performed.

If I set bypassPolicy to true, I still see no difference in completion options.

So in summary, I know that the call is successful as the response is coming back with the options set, but the changes don't seem to be coming through to the Pull Request in Azure DevOps.

like image 829
FreeRangeEggs Avatar asked Feb 25 '19 11:02

FreeRangeEggs


People also ask

Can you squash commits in a pull request?

When you select the Squash and merge option on a pull request on GitHub.com, the pull request's commits are squashed into a single commit. Instead of seeing all of a contributor's individual commits from a topic branch, the commits are combined into one commit and merged into the default branch.

How do you commit squash in Azure DevOps?

You can choose to squash merge when completing a pull request in Azure Repos. Choose Squash commit under Merge type in the Complete pull request dialog to squash merge the topic branch.

How do I force merge squash in GitHub?

On GitHub.com, navigate to the main page of the repository. Under your repository name, click Settings. Under "Pull Requests", select Allow squash merging. This allows contributors to merge a pull request by squashing all commits into a single commit.

How do I merge an approved pull request Azure DevOps?

Create a PR from the Pull requests pageOn the Repos > Pull requests page, select New pull request at upper right. Select the branch with the changes and the branch you want to merge the changes into, such as the main branch. Enter your PR details and create the PR.


2 Answers

You could use a policy that enforces this.

If you go to branches in DevOps, select your branch -> policies you can allow only squash merges:

enter image description here

This should make all pull requests into the branch bound by the policy to be done using squash merge.

Here's how it works for Set auto-complete: enter image description here

like image 173
tymtam Avatar answered Oct 28 '22 19:10

tymtam


Microsoft responded in the developer community forums with the following answer.

In this case there is a difference between expected behavior in the UI and via the REST endpoint. Setting squashMerge in completion options tells the PR to complete with a squash only if you complete it with the REST endpoint. In the user interface we respect user settings to enable users to choose what they would like to do (if there is no policy enabled). In this case, you did not enable a policy so a user can either squash or not. The users preference actually supersedes what you do with the REST API. If they squash merged the previous PR we will remember this and create the same default for them on the next PR.

TLDR: You cannot necessarily control the form default values with the REST endpoint and should use policy to enforce squash merge (or expect your users can set the checkbox or not depending on their preference, but we do not force a default).

like image 1
FreeRangeEggs Avatar answered Oct 28 '22 18:10

FreeRangeEggs