-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from druids/feat/gitlab-create-merge-request
feat(gitlab): support for creating MRs in GitLab
- Loading branch information
Showing
3 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}') |