Skip to content

Commit

Permalink
Add merge command
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasstoffers committed Dec 23, 2023
1 parent a3126a9 commit 3dd22a1
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
6 changes: 6 additions & 0 deletions default_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ commands:
strict_match: true
only_for_members: null
message: Merge request has been disapproved.
merge:
keyword: '@LGTM please merge'
ignore_case: true
strict_match: true
only_for_members: null
message: Merge request will be merged when pipeline succeeds.
uvicorn:
reload: false
host: "0.0.0.0"
Expand Down
6 changes: 6 additions & 0 deletions src/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ commands:
strict_match: true
only_for_members: null
message: Merge request has been disapproved.
merge:
keyword: '@LGTM please merge'
ignore_case: true
strict_match: true
only_for_members: null
message: Merge request will be merged when pipeline succeeds.
uvicorn:
reload: false
host: "0.0.0.0"
Expand Down
4 changes: 3 additions & 1 deletion src/config/commands/commands.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from pydantic import BaseModel
from config.commands.approval import Approval
from config.commands.disapproval import Disapproval
from config.commands.merge import Merge

class Commands(BaseModel):
approval: Approval
disapproval: Disapproval
disapproval: Disapproval
merge: Merge
4 changes: 4 additions & 0 deletions src/config/commands/merge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from config.commands.respondable_command import RespondableCommand

class Merge(RespondableCommand):
pass
20 changes: 11 additions & 9 deletions src/services/gitlab/events/comment/comment_event_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
from config.config_manager import get_config
from config.config import Config
from config.commands.command import Command
from typing import Callable

class CommentEventService():
gitlab_client: GitlabClient
config: Config

def __init__(self, gitlab_client: GitlabClient):
def __init__(self, gitlab_client: GitlabClient, config: Config):
self.gitlab_client = gitlab_client
self.config = config

def __is_command_invocation(self, event: CommentEvent, command: Command):
keyword = command.keyword
Expand All @@ -27,11 +28,12 @@ def __is_command_invocation(self, event: CommentEvent, command: Command):
def __handle_merge_comment(self, event: CommentEvent):
if event.merge_request is None:
raise Exception("Invalid event object")
config: Config = get_config()
if self.__is_command_invocation(event, config.commands.approval):
self.gitlab_client.approve_merge_request(event.project.id, event.merge_request.iid, config.commands.approval.message)
elif self.__is_command_invocation(event, config.commands.disapproval):
self.gitlab_client.disapprove_merge_request(event.project.id, event.merge_request.iid, config.commands.disapproval.message)
if self.__is_command_invocation(event, self.config.commands.approval):
self.gitlab_client.approve_merge_request(event.project.id, event.merge_request.iid, self.config.commands.approval.message)
elif self.__is_command_invocation(event, self.config.commands.disapproval):
self.gitlab_client.disapprove_merge_request(event.project.id, event.merge_request.iid, self.config.commands.disapproval.message)
elif self.__is_command_invocation(event, self.config.commands.merge):
self.gitlab_client.merge(event.project.project_id, event.merge_request.iid, message=self.config.commands.merge.message)


def handle_comment_event(self, event: CommentEvent):
Expand All @@ -40,8 +42,8 @@ def handle_comment_event(self, event: CommentEvent):

service: CommentEventService | None = None

def get_service(gitlab_client: GitlabClient = Depends(get_client)) -> CommentEventService:
def get_service(gitlab_client: GitlabClient = Depends(get_client), config: Config = Depends(get_config)) -> CommentEventService:
global service
if service is None:
service = CommentEventService(gitlab_client)
service = CommentEventService(gitlab_client, config)
return service

0 comments on commit 3dd22a1

Please sign in to comment.