# danger/golang architecture

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

- Interactive diagram: https://gitdiagram.com/danger/golang
- Repository: https://github.com/danger/golang

## Overview

##### Main components
a. Command-Line Interface (CLI) – Represented largely by the cmd/danger-go directory (including its main.go, runner components, and internal tests). This is the entry point for user commands (local, pr, ci).
b. API Layer – The api.go, along with its tests (api_test.go, api_internal_test.go), indicates an interface that abstracts Danger functionalities.
c. Danger JS Bridge – The danger-js directory holds types and integration specifics for Danger JS, handling cross-language interactions and type definitions (for commits, GitHub, GitLab, etc.).
d. CI/CD Configuration – The .github directory and the build/ci directory are key for configuring Continuous Integration workflows (with GitHub Actions and potential CI scripts such as dangerfile.go in build/ci).
e. Core Types and Shared Logic – The types.go file and other integral modules that provide the fundamental data structures and shared logic.

##### Relationships and interactions between components
- The CLI (from cmd/danger-go) accepts user commands which may invoke different behaviors based on the mode (local, pr, or ci).
- The CLI uses the API layer (api.go) to implement its logic. This API, in turn, communicates with Danger JS, leveraging its types and possibly calling underlying Node-based logic if required.
- The Danger JS bridge (under danger-js) provides types and interfaces to align with the existing Danger JS ecosystem. Its role is to “translate” or map the Go logic to Danger JS concepts.
- When integrated into CI (as configured in .github/workflows/main.yml or in build/ci), the CI scripts execute the tool, which then uses its API and Danger JS interface to gather data (e.g., pull request details, commit statistics) and output results accordingly.
- External integrations such as GitHub and GitLab (as seen by the types in danger-js) illustrate that the system can pull metadata for checks.

##### Architectural patterns and design principles
- Modularization: The logic is clearly separated into different packages (cmd, build/ci, danger-js) to isolate concerns between command execution, CI configuration, and cross-language integration.
- Wrapping and abstraction: The tool provides a wrapper around Danger JS functionality, abstracted behind a Go API.
- Test-driven and CI integrated: Notice the included tests (api_test.go, main_internal_test.go) and CI configurations that ensure the system is robust within automated pipelines.

- Rectangles for executable components.
- Rounded rectangles for configuration components.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% CLI Layer
    subgraph "CLI (danger-go)"
        CLI_Main["CLI Main"]:::cli
        CLI_Runner["Runner"]:::cli
    end

    %% API Layer
    subgraph "API Layer"
        API["API Layer"]:::api
        API_Test["API Tests"]:::api
    end

    %% Danger JS Bridge
    subgraph "Danger JS Bridge"
        DJS["Danger JS Bridge"]:::bridge
        DJS_Commit["Commit Types"]:::bridge
        DJS_Danger["Danger Types"]:::bridge
        DJS_GitHub["GitHub Types"]:::bridge
        DJS_GitLab["GitLab Types"]:::bridge
    end

    %% CI/CD Configuration
    subgraph "CI/CD Configuration"
        GHA["GitHub Workflow"]:::ci
        CI_DF["CI Dangerfile"]:::ci
    end

    %% Core Types
    subgraph "Core Types & Shared Logic"
        CT["Core Types"]:::core
    end

    %% External Systems
    External["External Systems (GitHub/GitLab)"]:::external

    %% Connections
    CLI_Main -->|"commandinvocation"| API
    CLI_Runner -->|"commandinvocation"| API
    API -->|"uses"| CT
    API -->|"bridgecall"| DJS
    DJS -->|"uses"| CT
    GHA -->|"CItrigger"| CLI_Main
    CI_DF -->|"CItrigger"| CLI_Main
    DJS -->|"fetchdata"| External
    DJS_Commit -->|"fetchdata"| External
    DJS_Danger -->|"fetchdata"| External
    DJS_GitHub -->|"fetchdata"| External
    DJS_GitLab -->|"fetchdata"| External

    %% Click Events
    click CLI_Main "https://github.com/danger/golang/blob/main/cmd/danger-go/main.go"
    click CLI_Runner "https://github.com/danger/golang/blob/main/cmd/danger-go/runner/runner.go"
    click API "https://github.com/danger/golang/blob/main/api.go"
    click API_Test "https://github.com/danger/golang/blob/main/api_test.go"
    click DJS "https://github.com/danger/golang/blob/main/danger-js/danger-js.go"
    click DJS_Commit "https://github.com/danger/golang/blob/main/danger-js/types_commit.go"
    click DJS_Danger "https://github.com/danger/golang/blob/main/danger-js/types_danger.go"
    click DJS_GitHub "https://github.com/danger/golang/blob/main/danger-js/types_github.go"
    click DJS_GitLab "https://github.com/danger/golang/blob/main/danger-js/types_gitlab.go"
    click GHA "https://github.com/danger/golang/blob/main/.github/workflows/main.yml"
    click CI_DF "https://github.com/danger/golang/blob/main/build/ci/dangerfile.go"
    click CT "https://github.com/danger/golang/blob/main/types.go"

    %% Class Definitions
    classDef cli fill:#add8e6,stroke:#000,stroke-width:1px;
    classDef api fill:#90ee90,stroke:#000,stroke-width:1px;
    classDef bridge fill:#f9d976,stroke:#000,stroke-width:1px;
    classDef ci fill:#ffcc99,stroke:#000,stroke-width:1px;
    classDef core fill:#dda0dd,stroke:#000,stroke-width:1px;
    classDef external fill:#ff9999,stroke:#000,stroke-dasharray: 5 5,stroke-width:1px;
```
