From 9483f1b128755128b3436c97e57c5c81c52f1ce3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lubo=C5=A1=20M=C3=A1tl?= Date: Fri, 21 Jun 2024 14:27:55 +0200 Subject: [PATCH] Added gitlab commands --- developers_chamber/gitlab_utils.py | 29 ++++++- developers_chamber/scripts/gitlab.py | 116 ++++++++++++++++++++++++++- setup.py | 2 +- 3 files changed, 142 insertions(+), 5 deletions(-) diff --git a/developers_chamber/gitlab_utils.py b/developers_chamber/gitlab_utils.py index 703ea5a..b2a57e5 100644 --- a/developers_chamber/gitlab_utils.py +++ b/developers_chamber/gitlab_utils.py @@ -5,9 +5,8 @@ def create_merge_request( - api_url, token, title, description, source_branch, target_branch, project + api_url, token, title, description, source_branch, target_branch, project, assignee_id=None ): - # 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={ @@ -18,9 +17,35 @@ def create_merge_request( "target_branch": target_branch, "title": title, "description": description, + "assignee_id": assignee_id }, ) if response.status_code != 201: raise UsageError(f'GitLab error: {response.content.decode("utf-8")}') return response.json()["web_url"] + + + +def run_job( + api_url, token, project, ref, variables +): + response = requests.post( + f"{api_url}/projects/{quote_plus(project)}/pipeline", + headers={ + "PRIVATE-TOKEN": token, + }, + json={ + "ref": ref, + "variables": [ + { + "key": key, + "secret_value": value, + "variable_type": "env_var", + } + for key, value in variables.items() + ] + }, + ) + if response.status_code != 201: + raise UsageError(f'GitLab error: {response.content.decode("utf-8")}') diff --git a/developers_chamber/scripts/gitlab.py b/developers_chamber/scripts/gitlab.py index 134c073..495ee76 100644 --- a/developers_chamber/scripts/gitlab.py +++ b/developers_chamber/scripts/gitlab.py @@ -2,9 +2,9 @@ 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, + run_job as run_job_func, ) from developers_chamber.scripts import cli @@ -48,11 +48,20 @@ def gitlab(): required=True, default=DEFAULT_PROJECT, ) -def create_release_merge_request(api_url, token, source_branch, target_branch, project): +@click.option( + "--assignee-id", + help="User ID to assign the merge request", + type=str, + required=False, + default=DEFAULT_PROJECT, +) +def create_release_merge_request(api_url, token, source_branch, target_branch, project, assignee_id=None): """ Create a new merge request in a GitLab project. It is often used after the project release. """ if not source_branch: + from developers_chamber.git_utils import get_current_branch_name + source_branch = get_current_branch_name() mr_url = create_merge_request_func( @@ -63,6 +72,109 @@ def create_release_merge_request(api_url, token, source_branch, target_branch, p source_branch=source_branch, target_branch=target_branch, project=project, + assignee_id=assignee_id ) 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("--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( + "--title", + help="Merge request title", + 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, +) +@click.option( + "--assignee-id", + help="User ID to assign the merge request", + type=str, + required=False, + default=DEFAULT_PROJECT, +) +def create_merge_request(api_url, token, source_branch, target_branch, title, project, assignee_id=None): + """ + Create a new merge request in a GitLab project. It is often used after the project release. + """ + mr_url = create_merge_request_func( + api_url=api_url, + token=token, + title=title, + description="", + source_branch=source_branch, + target_branch=target_branch, + project=project, + assignee_id=assignee_id + ) + + 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( + "--branch", + help="Branch name", + type=str, + required=True, +) +@click.option( + "--variables", + help="Variables", + type=str, + required=False, +) +def run_job(api_url, token, project, branch, variables): + """ + Run a job in a GitLab project. + """ + variables = dict([var.split('=') for var in variables.split(',')]) if variables else [] + run_job_func(api_url, token, project, f'refs/heads/{branch}', variables) + diff --git a/setup.py b/setup.py index 14f7cff..b2da074 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name="developers-chamber", - version="0.1.10", + version="0.1.11", description="A small plugin which help with development, deployment, git", keywords="django, skripts, easy live, git, bitbucket, Jira", author="Druids team",