How danger/danger works
Project Type and Purpose
• Danger is a Ruby-based command-line tool that runs during continuous integration (CI) to enforce pull request (PR) etiquette. Its main purpose is to automate common code review chores by parsing “Dangerfiles”, running custom rules, and posting feedback to code hosting platforms like GitHub, GitLab, Bitbucket, or VSTS.
• The README specifically explains that Danger “runs during your CI process,” and the file tree showcases many integrations (in lib/danger/ci_source, lib/danger/request_sources, etc.), a plugin system, and command modules.
File Structure to Identify Key Architectural Components
• Top-Level Directories and Files:
• lib: Contains the core Danger functionality including Danger core modules, CLI commands, plugins, helpers, and extension points.
• bin: Houses executable scripts (e.g., “bin/console”, “bin/danger”) that likely serve as entry points.
• docs and CHANGELOG.md/VISION.md: Documents that give background on the vision and usage.
• Configuration files for CI (e.g. .circleci, .github/workflows, .travis.yml) indicate an emphasis on running Danger within various CI environments.
• Within lib:
• Modules like danger_core encapsulate processing of Dangerfiles and managing the execution of rules.
• Directories like “ci_source” and “request_sources” show that Danger supports multiple CI services and SCM providers, meaning it retrieves information about PRs, commits, and repository metadata.
• The danger_plugins directory shows support for extending Danger’s functionality.
• The commands directory (and corresponding spec tests) indicate a CLI command framework for executing tasks (e.g., “init”, “pr”, “local”, and “dry_run”).
Key Components
a. Danger CLI Entry Points:
• Represented by the “bin” scripts which initialize the app.
b. Danger Core Engine:
• The “lib/danger/danger_core” and “lib/danger/dangerfile” modules handle the parsing and execution of the Dangerfile DSL. Highlight that this is where teams codify their PR norms.
c. CI Source and Request Sources:
• “lib/danger/ci_source” contains modules for many CI environments (Circle, Travis, Jenkins, GitLab, etc.).
• “lib/danger/request_sources” contains integrations with external systems like GitHub, Bitbucket, GitLab, and VSTS.
d. SCM Source:
• “lib/danger/scm_source” ties in version control interactions.
e. Plugin System:
• “lib/danger/plugin_support” and the “danger_plugins” directory illustrate how Danger can be extended. Include this as a separate component showing the plugin interface.
f. Command Modules:
• “lib/danger/commands” indicates that Danger includes multiple command handlers (e.g. for initialization, executing Dangerfiles, staging, etc.).
Relationships and Interactions
• The CLI acts as the entry point and passes the Dangerfile to the Danger Core Engine.
• The Danger Core Engine uses the DSL to interpret custom rules set by teams while interacting with the Plugin system.
• Depending on the CI environment (detected via the “ci_source” modules), Danger pulls relevant metadata and PR details using Request Sources.
• The results of executing the Dangerfile rules are aggregated by the Message Aggregator, and eventually, Danger posts messages to the corresponding PR in the SCM via specialized clients (e.g., GitHub API clients).
• Configuration files and build scripts (like the Dockerfile, Gemfile, CI workflows, etc.) tie things together for deployment and testing, but they can be depicted as supporting infrastructure rather than core modules.
Architectural Patterns & Design Principles
• Modularization: Components for CLI, core logic, CI/sources, plugins, and request sources are clearly separated (following a plug-and-play design).
• Extensibility: Via the plugin system, Danger allows custom rules and integrations.
• Dependency Injection / Loose Coupling: Danger isolates different environments (CI sources, SCM sources) to make integrations manageable and testable (as seen in the extensive spec structure).