GitHub Actions Integration
Relevant source files
The following files were used as context for generating this wiki page:
The GitHub Actions Integration for the Duplicate Code Detection Tool allows developers to automatically analyze pull requests for source code similarities. It functions as a Docker-based action that scans specified directories, identifies potential duplications, and reports findings directly as comments on the relevant GitHub Issue or Pull Request.
This integration is designed to serve as a quality gate in CI/CD pipelines, enabling automated enforcement of code quality standards. By utilizing configurable thresholds, it can warn developers of similarities or fail builds if duplication levels exceed acceptable limits.
Sources: action.yml:1-5, README.md:10-15, run_action.py:141-160
Integration Architecture
The integration follows a multi-stage execution flow involving a shell entrypoint, a Python runner, and the core detection engine.
Execution Flow
- Entrypoint Initialization: The
entrypoint.shscript triggers upon action execution, extracting the Pull Request ID from the GitHub event context and cloning the repository. - Environment Setup: It fetches the specific PR head branch to ensure analysis is performed on the latest proposed changes.
- Action Runner: The
run_action.pyscript bridges the GitHub environment variables (defined inaction.yml) and the coreduplicate_code_detection.pylogic. - Reporting: Results are formatted into Markdown tables and posted to the GitHub API.
The following diagram illustrates the high-level data flow from the GitHub environment through the detection tool and back to the Pull Request interface:
Configuration Inputs
The action is configured via the action.yml file, which defines the parameters passed from a workflow file to the execution environment. These inputs control the scope of the analysis, failure conditions, and reporting style.
| Input Name | Description | Default |
|---|---|---|
github_token | Required token for API authentication | N/A |
directories | Comma-separated list of directories to scan | N/A |
ignore_directories | Directories to exclude from analysis | '' |
file_extensions | Extensions to check (e.g., h, cpp, py, java) | h, hpp, c, cpp, cc, java, py, cs |
ignore_below | Minimum similarity % to include in report | 10 |
fail_above | Similarity % that triggers action failure | 100 |
warn_above | Similarity % that triggers a warning emoji | 100 |
only_code | If true, strips comments/docstrings before check | false |
one_comment | If true, updates a single comment instead of new ones | false |
Sources: action.yml:3-48
Core Component Logic
Action Runner (run_action.py)
This script acts as the orchestrator. It retrieves input parameters from os.environ (prefixed with INPUT_) and calls the duplicate_code_detection.run() function. It handles the transformation of raw similarity data into a human-readable Markdown format, specifically using the similarities_to_markdown and make_markdown_table functions.
Key logic includes:
- Threshold Validation: Compares results against
fail_aboveto determine the final exit code. - Markdown Generation: Creates collapsible
<details>blocks for each file, containing a table of similar files and their percentages. - GitHub API Interaction: Uses the
requestslibrary to POST or PATCH comments on the PR.
Sources: run_action.py:16-95, run_action.py:175-215
Detection Engine (duplicate_code_detection.py)
The engine performs the heavy lifting by tokenizing source code and using gensim for similarity calculations.
Reporting and Failures
The integration uses specific ReturnCode values to communicate status back to the GitHub Actions runner. If duplicate_code_detection.run returns THRESHOLD_EXCEEDED, the action will signal a failure in the workflow UI.
- SUCCESS (0): Analysis completed, no thresholds exceeded.
- BAD_INPUT (1): Invalid directories or file paths provided.
- THRESHOLD_EXCEEDED (2): A file pair exceeded the
fail_abovepercentage.
Sources: duplicate_code_detection.py:27-30, run_action.py:157-164
Report Content
The report generated by the tool includes:
- Header Message: A customizable string (default:
## 📌 Duplicate code detection tool report) used to identify and update previous comments. - Collapsible Sections: One for each analyzed file containing similarities.
- Similarity Table: Columns for "File" (linked to GitHub source) and "Similarity (%)".
- Warning Indicator: A
⚠️suffix is appended to similarity values exceeding thewarn_abovethreshold.
Sources: run_action.py:61-80, action.yml:43-49
Conclusion
The GitHub Actions Integration provides a robust mechanism for maintaining code quality by surfacing duplications during the peer review process. By leveraging Docker and the GitHub Issues API, it automates the detection of code clones across multiple languages, ensuring that technical debt is identified before it is merged into the codebase.