Skip to content

Commit

Permalink
Merge pull request #81 from druids/feat/gitlab-create-merge-request
Browse files Browse the repository at this point in the history
feat(gitlab): support for creating MRs in GitLab
  • Loading branch information
radimsuckr authored Apr 30, 2021
2 parents d65f40e + 4029f0c commit c4caa57
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions developers_chamber/bin/pydev.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from developers_chamber.scripts.docker import *
from developers_chamber.scripts.ecs import *
from developers_chamber.scripts.git import *
from developers_chamber.scripts.gitlab import *
from developers_chamber.scripts.jira import *
from developers_chamber.scripts.project import *
from developers_chamber.scripts.qa import *
Expand Down
24 changes: 24 additions & 0 deletions developers_chamber/gitlab_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from urllib.parse import quote_plus

import requests
from click import UsageError


def create_merge_request(api_url, token, title, description, source_branch, target_branch, project):
# TODO: Add support for assigning these MRs to the person who started deploy
response = requests.post(
f'{api_url}/projects/{quote_plus(project)}/merge_requests',
headers={
'PRIVATE-TOKEN': token,
},
json={
'source_branch': source_branch,
'target_branch': target_branch,
'title': title,
'description': description,
},
)

if response.status_code != 201:
raise UsageError(f'GitLab error: {response.content.decode("utf-8")}')
return response.json()['web_url']
46 changes: 46 additions & 0 deletions developers_chamber/scripts/gitlab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import os

import click

from developers_chamber.git_utils import get_current_branch_name
from developers_chamber.gitlab_utils import \
create_merge_request as create_merge_request_func
from developers_chamber.scripts import cli

DEFAULT_API_URL = os.environ.get('GITLAB_API_URL', 'https://gitlab.com/api/v4')
DEFAULT_PROJECT = os.environ.get('GITLAB_PROJECT')
DEFAULT_TARGET_BRANCH = os.environ.get('GITLAB_TARGET_BRANCH', 'next')
DEFAULT_TOKEN = os.environ.get('GITLAB_TOKEN')


@cli.group()
def gitlab():
"""GitLab commands"""


@gitlab.command()
@click.option('--api-url', help='GitLab instance API URL (defaults to gitlab.com)', type=str, required=True,
default=DEFAULT_API_URL)
@click.option('--token', help='token (can be set as env variable GITLAB_TOKEN)', type=str, required=True,
default=DEFAULT_TOKEN)
@click.option('--source-branch', help='source Git branch', type=str)
@click.option('--target-branch', help='target Git branch (defaults to env variable GITLAB_TARGET_BRANCH)', type=str,
default=DEFAULT_TARGET_BRANCH)
@click.option('--project', help='GitLab project name (defaults to env variable GITLAB_PROJECT)', type=str,
required=True, default=DEFAULT_PROJECT)
def create_release_merge_request(api_url, token, source_branch, target_branch, project):
"""Create a new merge request in GitLab project after release"""
if not source_branch:
source_branch = get_current_branch_name()

mr_url = create_merge_request_func(
api_url=api_url,
token=token,
title=f'Merge branch "{source_branch}"',
description='',
source_branch=source_branch,
target_branch=target_branch,
project=project,
)

click.echo(f'Merge request was successfully created: {mr_url}')

0 comments on commit c4caa57

Please sign in to comment.