From 27b8241380d5f02fdcf00a832c281767ef258d9f Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Wed, 4 May 2022 14:25:18 +0200 Subject: add approve-merge addon, give addons descriptions --- labbot/__main__.py | 3 ++- labbot/addons/approve-merge.py | 30 ++++++++++++++++++++++++++++++ labbot/addons/merge-label.py | 9 +++++++-- 3 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 labbot/addons/approve-merge.py diff --git a/labbot/__main__.py b/labbot/__main__.py index 5e0a3ae..9f0e3ba 100644 --- a/labbot/__main__.py +++ b/labbot/__main__.py @@ -64,7 +64,8 @@ def run(name, port): ) load_addons(instance, [ - "merge-label" + "merge-label", + "approve-merge" ]) click.echo(f"Started {name}") diff --git a/labbot/addons/approve-merge.py b/labbot/addons/approve-merge.py new file mode 100644 index 0000000..0fdeaa9 --- /dev/null +++ b/labbot/addons/approve-merge.py @@ -0,0 +1,30 @@ +""" +automatically merges merge requests if a certain number of approvals +is reached. +""" + +required_approval_count = 2 + +async def merge_label_hook(event, gl, *args, **kwargs): + title = event.object_attributes["title"] + state = event.object_attributes["state"] + merge_status = event.object_attributes["merge_status"] + + if state != "opened" or title.lower().startswith("draft"): + return + + approvals_url = f"/projects/{event.project_id}/merge_requests/{event.object_attributes['iid']}/approvals" + merge_url = f"/projects/{event.project_id}/merge_requests/{event.object_attributes['iid']}/merge" + + data = await gl.getitem(approvals_url) + + approval_count = len(data["approved_by"]) + + if approval_count >= required_approval_count: + if merge_status == "can_be_merged": + print("test") + await gl.put(merge_url) + +def setup(bot): + bot.register(merge_label_hook, "Merge Request Hook") + pass \ No newline at end of file diff --git a/labbot/addons/merge-label.py b/labbot/addons/merge-label.py index 1b2a768..9379b59 100644 --- a/labbot/addons/merge-label.py +++ b/labbot/addons/merge-label.py @@ -1,3 +1,8 @@ +""" +automatically labels issues referenced in merge requests with +specified labels. +""" + import os import re @@ -13,7 +18,7 @@ state_label = { "opened": "C-R Bestanden", "merged": "Testing", } -async def merge_request_hook(event, gl, *args, **kwargs): +async def merge_label_hook(event, gl, *args, **kwargs): state = event.object_attributes["state"] related_issues = [] @@ -69,5 +74,5 @@ async def merge_request_hook(event, gl, *args, **kwargs): pass def setup(bot): - bot.register(merge_request_hook, "Merge Request Hook") + bot.register(merge_label_hook, "Merge Request Hook") pass \ No newline at end of file -- cgit v1.2.3