diff --git a/developers_chamber/gitlab_utils.py b/developers_chamber/gitlab_utils.py index a0618c6..25d18af 100644 --- a/developers_chamber/gitlab_utils.py +++ b/developers_chamber/gitlab_utils.py @@ -1,5 +1,6 @@ from urllib.parse import quote_plus +import time import requests from click import UsageError @@ -24,24 +25,28 @@ def create_merge_request( if response.status_code != 201: raise UsageError(f'GitLab error: {response.content.decode("utf-8")}') - message = f"Merge request was successfully created {response.json()['web_url']}" - if automerge: - merge_request_iid = response.json()["iid"] - merge_response = requests.put( - f"{api_url}/projects/{quote_plus(project)}/merge_requests/{merge_request_iid}/merge", - json={"merge_when_pipeline_succeeds": True}, - headers={ - "PRIVATE-TOKEN": token, - }, - ) - if merge_response.status_code != 200: - message += f' (Failed to activate auto-merge: {response.content.decode("utf-8")})' - else: - message += " (Automerge activated)" + time.sleep(5) + message = activate_automerge(api_url, token, project, response.json()['iid']) + return f"{response.json()['web_url']} ({message})" + + return response.json()['web_url'] - return message +def activate_automerge( + api_url, token, project, merge_request_id +): + merge_response = requests.put( + f"{api_url}/projects/{quote_plus(project)}/merge_requests/{merge_request_id}/merge", + json={"merge_when_pipeline_succeeds": True}, + headers={ + "PRIVATE-TOKEN": token, + }, + ) + if merge_response.status_code != 200: + raise "Automerge activation failed" + else: + return "Automerge activated" def run_job( diff --git a/developers_chamber/scripts/gitlab.py b/developers_chamber/scripts/gitlab.py index 1efe01f..309459f 100644 --- a/developers_chamber/scripts/gitlab.py +++ b/developers_chamber/scripts/gitlab.py @@ -4,6 +4,7 @@ from developers_chamber.gitlab_utils import ( create_merge_request as create_merge_request_func, + activate_automerge as activate_automerge_func, run_job as run_job_func, ) from developers_chamber.scripts import cli @@ -144,6 +145,48 @@ def create_merge_request(api_url, token, source_branch, target_branch, title, pr click.echo(f"Merge request was successfully created: {mr_url}") +@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( + "--project", + help="GitLab project name (defaults to env variable GITLAB_PROJECT)", + type=str, + required=True, + default=DEFAULT_PROJECT, +) +@click.option( + "--merge-request-id", + help="GitLab merge request ID", + type=str, + required=True, +) +def activate_merge_request_automerge(api_url, token, project, merge_request_id): + """ + Create a new merge request in a GitLab project. It is often used after the project release. + """ + message = activate_automerge_func( + api_url=api_url, + token=token, + project=project, + merge_request_id=merge_request_id + ) + + click.echo(f"{message}") + + @gitlab.command() @click.option( "--api-url", diff --git a/setup.py b/setup.py index 3b943fd..a9e63fd 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name="developers-chamber", - version="0.1.25", + version="0.1.26", description="A small plugin which help with development, deployment, git", keywords="django, skripts, easy live, git, bitbucket, Jira", author="Druids team",