Skip to content

Commit

Permalink
Added time between merge request creation and automerge
Browse files Browse the repository at this point in the history
  • Loading branch information
matllubos committed Nov 28, 2024
1 parent 5c57f2a commit c76cd12
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 16 deletions.
35 changes: 20 additions & 15 deletions developers_chamber/gitlab_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from urllib.parse import quote_plus

import time
import requests
from click import UsageError

Expand All @@ -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(
Expand Down
43 changes: 43 additions & 0 deletions developers_chamber/scripts/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down
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.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",
Expand Down

0 comments on commit c76cd12

Please sign in to comment.