diff --git a/default_config.yaml b/default_config.yaml index dd6cf13..fcd6f71 100644 --- a/default_config.yaml +++ b/default_config.yaml @@ -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" diff --git a/src/config.yaml b/src/config.yaml index 719eab1..7e65f70 100644 --- a/src/config.yaml +++ b/src/config.yaml @@ -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" diff --git a/src/config/commands/commands.py b/src/config/commands/commands.py index dc63b29..c493750 100644 --- a/src/config/commands/commands.py +++ b/src/config/commands/commands.py @@ -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 \ No newline at end of file + disapproval: Disapproval + merge: Merge \ No newline at end of file diff --git a/src/config/commands/merge.py b/src/config/commands/merge.py new file mode 100644 index 0000000..8d56f1d --- /dev/null +++ b/src/config/commands/merge.py @@ -0,0 +1,4 @@ +from config.commands.respondable_command import RespondableCommand + +class Merge(RespondableCommand): + pass \ No newline at end of file diff --git a/src/services/gitlab/events/comment/comment_event_service.py b/src/services/gitlab/events/comment/comment_event_service.py index 328b313..12f3533 100644 --- a/src/services/gitlab/events/comment/comment_event_service.py +++ b/src/services/gitlab/events/comment/comment_event_service.py @@ -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 @@ -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): @@ -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 \ No newline at end of file