# mimo-x/code-review-gpt-gitlab architecture

> Architecture diagram of the GitHub repository mimo-x/code-review-gpt-gitlab, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mimo-x/code-review-gpt-gitlab
- Repository: https://github.com/mimo-x/code-review-gpt-gitlab

## Overview

##### Main Components
- Webhook/API Layer – Represented by files like app.py and app/gitlab_webhook.py. This layer is responsible for receiving HTTP request events (e.g., Gitlab Merge Request triggers via the webhook URL).
- Gitlab Integration Module – Under gitlab_integration, which includes files such as webhook_listener.py and gitlab_fetcher.py. This layer manages parsing requests, interacting with Gitlab APIs, and retrieving necessary code or event data.
- Large Model / LLM Integration – Housed in the large_model directory. It provides an abstraction over multiple LLM APIs (i.e., default_api.py, llm_generator.py, and abstract_api.py). This layer encapsulates the logic for interfacing with various LLM providers (OpenAI, DeepSeek, etc.).
- Review Engine – Found within review_engine; it includes an abstract handler and concrete implementations (default_handler.py and review_engine.py) to process input from Gitlab events and generate code review suggestions.
- Response Module – Located in response_module. It abstracts various output or “response” targets (e.g., msg_response for DingTalk, gitlab_response, and template_response for other targets). This component takes the analysis result from the review engine and sends it out to the target channel.
- Utility and Config Modules – The config directory contains configuration settings (config.py). The utils directory offers helper functions like logging, argument checking, and Gitlab parsing.
- Deployment Support – The Dockerfile and accompanying instructions in the README indicate containerized deployment and potential integration with CI/CD pipelines.

##### Relationships & Interactions
- Incoming Gitlab Webhook Request: A user’s Gitlab merge request or similar event calls the HTTP endpoint in app.py, which directs the request to app/gitlab_webhook.py.
- Gitlab Integration: The webhook handler then passes the event data to the gitlab_integration module, which parses the event (possibly using utils/gitlab_parser.py) and may perform additional API requests to Gitlab (e.g., fetching code changes).
- Review Processing: The processed information is forwarded to the review_engine, which uses its default or customized handler logic (based on review_engine/abstract_handler.py) to determine what code review checks to initiate.
- LLM Processing: As part of the review logic, the review engine invokes the large_model module. Through its API abstraction, it calls one or more LLMs (GPT, DeepSeek, etc.) to process the code changes and generate review comments.
- Response Generation & Delivery: The resulting review response is sent to the response_module, which uses factories and controllers (e.g., response_factory.py and response_controller.py) to format the final output. Depending on configuration, it then decides the appropriate channel—this can be message responses to Gitlab or third-party integrations like DingTalk.
- Configuration/Utilities: Throughout these layers, the config module provides dynamic settings and the utils module aids in tasks like logging error messages and validating arguments.

##### Architectural Patterns & Design Principles
- Modular Design & Separation of Concerns: Each major function (event ingestion, review logic, LLM interface, and response handling) is encapsulated in its own package. This makes the solution both extensible and maintainable.
- Abstraction Over LLMs: The large_model module abstracts the complexity of supporting multiple LLM providers which is a key design principle that provides flexibility to swap or add models.
- Integration with External Systems: The design accounts for external interactions (Gitlab, messaging systems) via well-defined interfaces, making it easier to scale or modify integrations.

##### Key Technologies, Frameworks, & Libraries
- Python – the project is written entirely in Python, with dependency management handled via requirements.txt.
- Docker – containerized deployment is provided through the Dockerfile.
- Web Framework (likely Flask or similar lightweight server) – suggested by the presence of app.py and HTTP endpoints.
- Gitlab API Integration – the gitlab_integration module encapsulates the logic here.
- LLM Integration Tools – such as UnionLLM and connections with various providers (as mentioned in the README).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Systems
    subgraph "External Systems"
        Gitlab["Gitlab (External)"]:::external
        Messaging["Messaging Platforms (DingTalk, etc)"]:::external
    end

    %% Webhook/API Layer
    subgraph "Webhook/API Layer"
        AppHandler["Webhook Handler (app.py)"]:::webhook
        GitlabWebhook["Gitlab Webhook (app/gitlab_webhook.py)"]:::webhook
    end

    %% Gitlab Integration Module
    subgraph "Gitlab Integration Module"
        WebhookListener["Webhook Listener (gitlab_integration/webhook_listener.py)"]:::integration
        GitlabFetcher["Gitlab Fetcher (gitlab_integration/gitlab_fetcher.py)"]:::integration
        GitlabParser["Gitlab Parser (utils/gitlab_parser.py)"]:::integration
    end

    %% Review Engine
    subgraph "Review Engine"
        AbstractHandler["Abstract Handler (review_engine/abstract_handler.py)"]:::review
        DefaultHandler["Default Handler (review_engine/handler/default_handler.py)"]:::review
        EngineCore["Review Engine Core (review_engine/review_engine.py)"]:::review
    end

    %% LLM Integration
    subgraph "LLM Integration"
        AbstractAPI["Abstract API (large_model/abstract_api.py)"]:::llm
        LLMGenerator["LLM Generator (large_model/llm_generator.py)"]:::llm
        DefaultAPI["Default API (large_model/api/default_api.py)"]:::llm
    end

    %% Response Module
    subgraph "Response Module"
        ResponseController["Response Controller (response_module/response_controller.py)"]:::response
        ResponseFactory["Response Factory (response_module/response_factory.py)"]:::response
        DingtalkResponse["DingTalk Response (response_module/response_target/msg_response/dingtalk_response.py)"]:::response
        GitlabResponse["Gitlab Response (response_module/response_target/msg_response/gitlab_response.py)"]:::response
        TemplateResponse["Template Response (response_module/response_target/other_type_response/template_response.py)"]:::response
    end

    %% Utilities & Config
    subgraph "Utilities & Config"
        Config["Config (config/config.py)"]:::utils
        ArgsCheck["Args Checker (utils/args_check.py)"]:::utils
        Logger["Logger (utils/logger.py)"]:::utils
        Tools["Tools (utils/tools.py)"]:::utils
    end

    %% Deployment Support
    subgraph "Deployment"
        Dockerfile["Dockerfile"]:::deployment
    end

    %% Data Flow
    Gitlab -- "webhookEvent" --> AppHandler
    AppHandler -- "routeRequest" --> GitlabWebhook
    GitlabWebhook -- "processEvent" --> WebhookListener
    WebhookListener -- "fetchData" --> GitlabFetcher
    GitlabFetcher -- "parseData" --> GitlabParser
    GitlabParser -- "passData" --> EngineCore
    EngineCore -- "invokeHandlers" --> AbstractHandler
    EngineCore -- "invokeHandlers" --> DefaultHandler
    EngineCore -- "reviewRequest" --> LLMGenerator
    LLMGenerator -- "useAPI" --> AbstractAPI
    LLMGenerator -- "useAPI" --> DefaultAPI
    EngineCore -- "generateResponse" --> ResponseController
    LLMGenerator -- "LLMOutput" --> ResponseFactory
    ResponseController -- "sendComment" --> GitlabResponse
    ResponseController -- "sendNotification" --> DingtalkResponse
    ResponseController -- "formatOutput" --> TemplateResponse
    GitlabResponse -- "deliver" --> Gitlab
    DingtalkResponse -- "deliver" --> Messaging
    TemplateResponse -- "deliver" --> Messaging

    %% Utilities support (dashed arrows)
    Config --- AppHandler:::utils
    Config --- WebhookListener:::utils
    Config --- EngineCore:::utils
    Config --- LLMGenerator:::utils
    Config --- ResponseController:::utils

    %% Class definitions for colors
    classDef external fill:#f9f,stroke:#333,stroke-width:2px;
    classDef webhook fill:#ffcc00,stroke:#333,stroke-width:2px;
    classDef integration fill:#66ccff,stroke:#333,stroke-width:2px;
    classDef review fill:#99ff99,stroke:#333,stroke-width:2px;
    classDef llm fill:#cc99ff,stroke:#333,stroke-width:2px;
    classDef response fill:#ff9966,stroke:#333,stroke-width:2px;
    classDef utils fill:#d3d3d3,stroke:#333,stroke-width:2px;
    classDef deployment fill:#ffccff,stroke:#333,stroke-width:2px;

    %% Click Events
    click AppHandler "https://github.com/mimo-x/code-review-gpt-gitlab/blob/main/app.py"
    click GitlabWebhook "https://github.com/mimo-x/code-review-gpt-gitlab/blob/main/app/gitlab_webhook.py"
    click WebhookListener "https://github.com/mimo-x/code-review-gpt-gitlab/blob/main/gitlab_integration/webhook_listener.py"
    click GitlabFetcher "https://github.com/mimo-x/code-review-gpt-gitlab/blob/main/gitlab_integration/gitlab_fetcher.py"
    click GitlabParser "https://github.com/mimo-x/code-review-gpt-gitlab/blob/main/utils/gitlab_parser.py"
    click AbstractAPI "https://github.com/mimo-x/code-review-gpt-gitlab/blob/main/large_model/abstract_api.py"
    click LLMGenerator "https://github.com/mimo-x/code-review-gpt-gitlab/blob/main/large_model/llm_generator.py"
    click DefaultAPI "https://github.com/mimo-x/code-review-gpt-gitlab/blob/main/large_model/api/default_api.py"
    click AbstractHandler "https://github.com/mimo-x/code-review-gpt-gitlab/blob/main/review_engine/abstract_handler.py"
    click DefaultHandler "https://github.com/mimo-x/code-review-gpt-gitlab/blob/main/review_engine/handler/default_handler.py"
    click EngineCore "https://github.com/mimo-x/code-review-gpt-gitlab/blob/main/review_engine/review_engine.py"
    click ResponseController "https://github.com/mimo-x/code-review-gpt-gitlab/blob/main/response_module/response_controller.py"
    click ResponseFactory "https://github.com/mimo-x/code-review-gpt-gitlab/blob/main/response_module/response_factory.py"
    click DingtalkResponse "https://github.com/mimo-x/code-review-gpt-gitlab/blob/main/response_module/response_target/msg_response/dingtalk_response.py"
    click GitlabResponse "https://github.com/mimo-x/code-review-gpt-gitlab/blob/main/response_module/response_target/msg_response/gitlab_response.py"
    click TemplateResponse "https://github.com/mimo-x/code-review-gpt-gitlab/blob/main/response_module/response_target/other_type_response/template_response.py"
    click Config "https://github.com/mimo-x/code-review-gpt-gitlab/blob/main/config/config.py"
    click ArgsCheck "https://github.com/mimo-x/code-review-gpt-gitlab/blob/main/utils/args_check.py"
    click Logger "https://github.com/mimo-x/code-review-gpt-gitlab/blob/main/utils/logger.py"
    click Tools "https://github.com/mimo-x/code-review-gpt-gitlab/blob/main/utils/tools.py"
    click Dockerfile "https://github.com/mimo-x/code-review-gpt-gitlab/tree/main/Dockerfile"
```
