Hamburger Cross Icon
Code Quality Guardrails - Lunar Policy for Testing And Quality

Code Quality Guardrails

Policy Experimental Testing And Quality

Enforce code-quality standards. Verifies a code-quality scanner ran, the tool's pass/fail signal is green, and coverage, duplication, and severity-bucketed issue counts meet configurable thresholds. Reads the normalized `.code_quality` path populated by the `sonarqube` collector.

Add code-quality to your lunar-config.yml:
uses: github://earthly/lunar-lib/policies/code-quality@v1.0.5

Included Guardrails

This policy includes 6 guardrails that enforce standards for your testing and quality.

Guardrail

executed

Verifies that a code-quality scanner was executed on the component. Fails if no scanner has written to .code_quality.

code-quality sonarqube scanning quality-gate testing
View Guardrail
Guardrail

passing

Verifies the scanner's overall pass/fail signal is green. Reads the tool-agnostic .code_quality.passing field, derived from the scanner's own quality gate outcome.

code-quality quality-gate sonarqube pass-fail
View Guardrail
Guardrail

min-coverage

Ensures line-coverage percentage meets a configurable minimum. Reads .code_quality.coverage_percentage. Fails if the field is missing.

code-quality coverage line-coverage testing
View Guardrail
Guardrail

max-duplication

Ensures duplicated-lines percentage stays under a configurable maximum. Reads .code_quality.duplication_percentage.

code-quality duplication maintainability copy-paste
View Guardrail
Guardrail

max-severity

Ensures no code-quality issues at or above the configured severity threshold. Configure min_severity to set the threshold (critical, high, medium, low).

code-quality critical high issues severity
View Guardrail
Guardrail

max-total

Ensures total code-quality issues are under a configurable threshold.

code-quality threshold issues total
View Guardrail

How Guardrails Fit into Lunar

Lunar guardrails define your engineering standards as code. They evaluate data collected by integrations and produce pass/fail checks with actionable feedback.

Policies support gradual enforcement—from silent scoring to blocking PRs or deployments—letting you roll out standards at your own pace without disrupting existing workflows.

Learn How Lunar Works
1
Integrations Gather Data
Collectors extract metadata from code, CI pipelines, tool outputs, and scans
2
{ } Centralized as JSON
All data merged into each component's unified metadata document
3
Guardrails Enforce Standards This Policy
Real-time feedback in PRs and AI workflows

Required Integrations

This policy evaluates data gathered by one or more of the following integration(s). Make sure to enable them in your lunar-config.yml.

Configuration

Configure this policy in your lunar-config.yml.

Inputs

Input Required Default Description
min_severity Optional high Minimum severity to fail on (critical, high, medium, low)
max_total_threshold Required Maximum total issues allowed (must be configured)
min_coverage_percentage Optional 80 Minimum line-coverage percentage (0-100)
max_duplication_percentage Optional 5 Maximum duplicated-lines percentage (0-100)

Documentation

View on GitHub

Code Quality Guardrails

Enforce code-quality standards across any scanner that writes to .code_quality.

Overview

Validates that a code-quality scanner ran, the tool's overall pass/fail signal is green, and that coverage, duplication, and severity-bucketed issue counts meet configurable thresholds. Reads the tool-agnostic .code_quality Component JSON path, which is populated by code quality collectors such as sonarqube. Apply broadly as a "code quality happens" guardrail, or bring-your-own thresholds per domain.

Policies

This plugin provides the following policies (use include to select a subset):

Policy Description Failure Meaning
executed Verifies a code-quality scanner ran No scanner has written to .code_quality
passing Tool's pass/fail signal is green .code_quality.passing is false (quality gate failed)
min-coverage Line-coverage meets minimum Coverage below configured threshold or missing
max-duplication Duplicated-lines under threshold Duplication above configured threshold
max-severity No issues at or above severity threshold Issues found at configured severity or higher
max-total Total issues under threshold Total issue count exceeds configured limit

Required Data

This policy reads from the following Component JSON paths:

Path Type Provided By
.code_quality object sonarqube collector
.code_quality.passing bool sonarqube collector (derived from the tool's quality gate)
.code_quality.coverage_percentage number sonarqube collector
.code_quality.duplication_percentage number sonarqube collector
.code_quality.issues.total number sonarqube collector
.code_quality.issues.critical number sonarqube collector
.code_quality.issues.high number sonarqube collector
.code_quality.issues.medium number sonarqube collector
.code_quality.issues.low number sonarqube collector

Installation

Add to your lunar-config.yml:

policies:
  - uses: github://earthly/lunar-lib/policies/code-quality@v1.0.0
    on: ["domain:your-domain"]
    enforcement: report-pr
    # include: [executed, passing]  # Only run specific checks
    with:
      min_severity: "high"                  # Fail on critical and high issues
      max_total_threshold: "50"             # Fail if more than 50 total issues
      min_coverage_percentage: "80"         # Fail under 80% line coverage
      max_duplication_percentage: "5"       # Fail above 5% duplicated lines

Examples

Passing Example

{
  "code_quality": {
    "source": { "tool": "sonarqube", "integration": "api" },
    "passing": true,
    "coverage_percentage": 82.5,
    "duplication_percentage": 3.1,
    "issues": { "total": 12, "critical": 0, "high": 0, "medium": 2, "low": 10 }
  }
}

Failing Example

{
  "code_quality": {
    "source": { "tool": "sonarqube", "integration": "api" },
    "passing": false,
    "coverage_percentage": 61.4,
    "duplication_percentage": 8.2,
    "issues": { "total": 120, "critical": 2, "high": 5, "medium": 30, "low": 83 }
  }
}

Failure messages:

  • executed: "No code-quality scanning data found. Ensure the sonarqube collector is configured."
  • passing: "Code-quality gate failed (.code_quality.passing is false)"
  • min-coverage: "Line coverage 61.4% is below minimum 80%"
  • max-duplication: "Duplication 8.2% exceeds maximum 5%"
  • max-severity: "Critical code-quality issues detected (2 found)"
  • max-total: "Total code-quality issues (120) exceeds threshold (50)"

Remediation

When this policy fails, you can resolve it by:

  1. executed failure: Configure the sonarqube collector — either by running sonar-scanner in CI (so api can read the results) or by letting auto run it.
  2. passing failure: Fix the specific quality-gate conditions flagged by the scanner.
  3. min-coverage failure: Add tests or configure coverage reporting for uncovered code paths.
  4. max-duplication failure: Refactor duplicated blocks into shared helpers.
  5. max-severity failure: Address issues at or above the configured severity in the scanner UI.
  6. max-total failure: Reduce overall issue count by fixing or acknowledging flagged items.

Open Source

This policy is open source and available on GitHub. Contribute improvements, report issues, or fork it for your own use.

View Repository

Ready to Automate Your Standards?

See how Lunar can turn your AGENTS.md, engineering wiki, compliance docs, or postmortem action items into automated guardrails with our 200+ built-in guardrails.

Works with any process
check AI agent rules & prompt files
check Post-mortem action items
check Security & compliance policies
check Testing & quality requirements
Automate Now
Paste your AGENTS.md or manual process doc and get guardrails in minutes
Book a Demo