How to define a default merge strategy per Project

Still need help?

The Atlassian Community is here for you.

Ask the community

Purpose

Starting in Bitbucket 4.9 you are able to configure Pull Request merge strategies. Repository admins can choose which merge strategies to allow, and set a default merge strategy. Users can now choose a merge strategy from the merge dialog of each pull request.

Prior to Bitbucket Server 4.9, instance admins could choose a merge strategy within the bitbucket configuration file, but everyone would have to use the same merge strategy for all pull requests within a repository and instance. Changing merge strategies required you to restart the instance, which could cause disruption to sysadmins and downtime for end-users.

However, on the UI there is no way to define a default merge strategy per project so every the repo created under this project inherits this default strategy.

This document outlines an alternative way to configure that using our REST endpoint available to do so.

Solution

You need to do it using this REST endpoint as it is not available through the UI at the moment:

  • /rest/api/1.0/projects/{projectKey}/settings/pull-requests/{scmId}
    • This is the GET to see the current status for the PROJECT

      $ curl -H "Content-Type:application/json" -H "Accept:application/json" --user admin:admin -X GET http://tbomfim:7990/rest/api/1.0/projects/ATEST/settings/pull-requests/git
      
      {  
         "mergeConfig":{  
            "defaultStrategy":{  
               "description":"Always create a new merge commit and update the target branch to it, even if the source branch is already up to date with the target branch.",
               "enabled":true,
               "flag":"--no-ff",
               "id":"no-ff",
               "name":"Merge commit"
            },
            "strategies":[  
               {  
                  "description":"Always create a new merge commit and update the target branch to it, even if the source branch is already up to date with the target branch.",
                  "enabled":true,
                  "flag":"--no-ff",
                  "id":"no-ff",
                  "name":"Merge commit"
               },
               {  
                  "description":"If the source branch is out of date with the target branch, create a merge commit. Otherwise, update the target branch to the latest commit on the source branch.",
                  "enabled":false,
                  "flag":"--ff",
                  "id":"ff",
                  "name":"Fast-forward"
               },
               {  
                  "description":"If the source branch is out of date with the target branch, reject the merge request. Otherwise, update the target branch to the latest commit on the source branch.",
                  "enabled":false,
                  "flag":"--ff-only",
                  "id":"ff-only",
                  "name":"Fast-forward only"
               },
               {  
                  "description":"Combine all commits into one new non-merge commit on the target branch.",
                  "enabled":false,
                  "flag":"--squash",
                  "id":"squash",
                  "name":"Squash"
               },
               {  
                  "description":"If the source branch is out of date with the target branch, reject the merge request. Otherwise, combine all commits into one new non-merge commit on the target branch.",
                  "enabled":false,
                  "flag":"--squash --ff-only",
                  "id":"squash-ff-only",
                  "name":"Squash, fast-forward only"
               }
            ],
            "type":"DEFAULT"
         }
      }
    • You update the default strategy to squash for my ATEST PROJECTID and every repo I created from then on under that project had squash as a the default option:

      curl -H "Content-Type:application/json" -H "Accept:application/json" --user admin:admin -X POST -d '{
          "mergeConfig": {
              "defaultStrategy": {
                  "id": "squash"
              },
              "strategies": [
                  {
                      "id": "squash"
                  }
              ]
          }
      }' http://tbomfim:7990/rest/api/1.0/projects/ATEST/settings/pull-requests/git


 

 

Last modified on Nov 2, 2018

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.