Skip to content

Commit

Permalink
Added gitlab commands
Browse files Browse the repository at this point in the history
  • Loading branch information
matllubos committed Jun 21, 2024
1 parent 8aec1c3 commit 9483f1b
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 5 deletions.
29 changes: 27 additions & 2 deletions developers_chamber/gitlab_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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={
Expand All @@ -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")}')
116 changes: 114 additions & 2 deletions developers_chamber/scripts/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand All @@ -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)

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 9483f1b

Please sign in to comment.