Skip to content

Instantly share code, notes, and snippets.

@AndrewFarley
Created February 5, 2019 21:21
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndrewFarley/9177df89666410ab45b7c248e4f24d53 to your computer and use it in GitHub Desktop.
Save AndrewFarley/9177df89666410ab45b7c248e4f24d53 to your computer and use it in GitHub Desktop.
This simple Python code force squashes, if you call this via a webhook automatically via gitlab, then "magic" all merges will squash
import os
import flask
import requests
BASE_URL = 'https://YOURGITLABURL/api/v4'
TOKEN = 'YOURADMINAPITOKEN'
app = flask.Flask(__name__)
@app.route('/', methods=['POST'])
def squash_remove_branch():
data = flask.request.json
if data['event_type'] != 'merge_request':
return '', 200
if data['object_attributes']['action'] != 'open':
return '', 200
project_id = data['project']['id']
mr_id = data['object_attributes']['iid']
url = '{0}/projects/{1}/merge_requests/{2}'.format(
BASE_URL, project_id, mr_id)
update_data = {'remove_source_branch': True, 'squash': True}
resp = requests.put(
url, data=update_data, headers={'private-token': TOKEN})
if resp.status_code != 200:
print('Error updating MR {0}: {1} {2}'.format(
mr_id, resp.status_code, resp.text))
return '', 200
@app.route('/alive', methods=['GET'])
def alive():
return 'OK', 200
if __name__ == '__main__':
# Bind to PORT if defined, otherwise default to 5000.
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)
@Ch00k
Copy link

Ch00k commented Jul 26, 2019

Great stuff!

@AndrewFarley
Copy link
Author

Great stuff!

Haha hey Ch00k! This is totally your baby, I hope you don't mind, I gist-ed this to share with a bunch of people on Gitlab.org complaining about the lack of this goddamn feature. Nice work on this btw. If I had the time/interest this would make its own great github repo with dockerfile and dockerhub for ease of use. :P No free time. Hope you're well man!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment