Linter and Custom CI Checks
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
The 2ms project implements a suite of custom linting and Continuous Integration (CI) checks to ensure code quality, maintain consistency, and verify the integrity of secret detection rules. These checks extend beyond standard unit testing to include end-to-end (e2e) verification, forbidden pattern detection, and automated documentation updates.
The primary goal of these checks is to prevent the introduction of improper logging practices, ensure new rules from upstream dependencies are incorporated, and maintain a high standard of security within the codebase itself. Sources: CONTRIBUTING.md:10-18, README.md:113-138
Internal Code Linter
The project features a custom linter located in the tests/lint directory. This linter is specifically designed to identify and flag the use of "forbidden functions" or patterns that violate the project's architectural standards.
Forbidden Patterns and Logic
The linter scans Go source files for specific regular expression patterns. Currently, it focuses on enforcing the use of the internal logging system over standard printing functions.
| Forbidden Pattern | Reason | Suggested Alternative |
|---|---|---|
fmt.Print | Bypass internal logging levels | log.Info or equivalent |
log.Fatal() | Abrupt termination without proper cleanup | Standard error handling |
Sources: tests/lint.go:57-60, CONTRIBUTING.md:15
Linting Workflow
The linter operates by walking the directory tree, excluding specific directories such as .git, vendor, and tests. It processes .go files line-by-line, excluding test files (_test.go).
The diagram illustrates the recursive file walking and pattern matching logic used by the custom linter.
Sources: tests/lint.go:13-55, tests/lint.go:64-97
Implementation Details
- Exclusion Mechanism: Developers can bypass a linting error on a specific line by adding a
// lint:ignorecomment. Sources: tests/lint.go:11, tests/lint.go:82 - Ignored Directories: The linter explicitly skips
.git,.github,.vscode,vendor,tests, and.ci. Sources: tests/lint.go:14-21 - Integration: The linter is executed as a standard Go test via
TestLintIntegration. Sources: tests/lint_test.go:7-18
Continuous Integration Checks
The CI pipeline includes specialized scripts and tests to automate maintenance tasks and maintain parity with upstream rule sets.
Secret Rule Synchronization
The check .ci/check_new_rules.go compares the project's secret detection rules against the latest releases of gitleaks. If upstream rules are found that are missing in 2ms, the CI build fails, ensuring the tool remains comprehensive in its detection capabilities. Sources: CONTRIBUTING.md:16
Automated Documentation
The script .ci/update-readme.sh is used to automatically update the CLI help message within the README.md file. This ensures that the documentation always reflects the current command-line interface and available flags. Sources: CONTRIBUTING.md:17
CI/CD Integration Examples
The project provides templates for integrating these checks into various platforms:
- GitHub Actions: Uses a Docker-based approach to scan repositories, requiring a
fetch-depth: 0to analyze full commit history. Sources: README.md:113-138 - Azure DevOps: Utilizes a similar script-based Docker execution for pipeline integration. Sources: README.md:142-155
End-to-End (e2e) Verification
The tests/cli suite performs high-level validation by building the actual CLI binary and running it against test subjects.
The sequence shows how e2e tests build the tool and verify its detection logic against generated mock data.
Sources: tests/e2e.go:21-41, tests/e2e_test.go:13-33
Key E2E Test Scenarios
- Secret Detection: Verifies that at least one secret is found in a directory containing a mock GitHub token. Sources: tests/e2e_test.go:18-33
- Exclusion Logic: Confirms that specific files (e.g.,
go.sum) are ignored even if they contain patterns matching secret rules. Sources: tests/e2e_test.go:56-76 - Confluence Integration: Validates scanning of external platforms with optional validation flags enabled. Sources: tests/e2e_test.go:35-54
Summary
The linter and CI checks in 2ms provide a robust framework for maintaining both code quality and functional reliability. By combining custom regex-based linting for internal standards with automated rule synchronization and comprehensive e2e testing, the project ensures that its secret detection capabilities remain effective and its codebase remains maintainable.