Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .github/workflows/lint-include.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ on:
required: false
type: boolean
default: false
lint-ruff:
description: "Run ruff (Python linting and formatting) when the repository has Python files — uploads SARIF"
required: false
type: boolean
default: false
lint-config-drift:
description: "Check local config files match central DevSecNinja/.github repo"
required: false
Expand All @@ -117,8 +122,8 @@ on:
type: boolean
default: false
upload-sarif:
description: "Upload checkov/trivy/zizmor results to GitHub code scanning (requires GitHub Advanced Security). Set
false for private repos without GHAS to gate on exit codes with console output instead."
description: "Upload checkov/trivy/zizmor/ruff results to GitHub code scanning (requires GitHub Advanced Security).
Set false for private repos without GHAS to gate on exit codes with console output instead."
required: false
type: boolean
default: true
Expand Down Expand Up @@ -165,6 +170,7 @@ jobs:
lint-checkov: ${{ inputs.lint-checkov }}
lint-trivy: ${{ inputs.lint-trivy }}
lint-zizmor: ${{ inputs.lint-zizmor }}
lint-ruff: ${{ inputs.lint-ruff }}
lint-config-drift: ${{ inputs.lint-config-drift }}
lint-inline-scripts: ${{ inputs.lint-inline-scripts }}
lint-inline-scripts-blocking: ${{ inputs.lint-inline-scripts-blocking }}
Expand Down
95 changes: 92 additions & 3 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ on:
required: false
type: boolean
default: true
lint-ruff:
description: "Run ruff (Python linting and formatting) when the repository has Python files — uploads SARIF"
required: false
type: boolean
default: true
lint-config-drift:
description: "Check local config files match central DevSecNinja/.github repo"
required: false
Expand All @@ -112,8 +117,8 @@ on:
type: boolean
default: false
upload-sarif:
description: "Upload checkov/trivy/zizmor results to GitHub code scanning (requires GitHub Advanced Security). Set
false for private repos without GHAS to gate on exit codes with console output instead."
description: "Upload checkov/trivy/zizmor/ruff results to GitHub code scanning (requires GitHub Advanced Security).
Set false for private repos without GHAS to gate on exit codes with console output instead."
required: false
type: boolean
default: true
Expand Down Expand Up @@ -674,6 +679,67 @@ jobs:
sarif_file: ${{ runner.temp }}/zizmor.sarif
category: zizmor

ruff:
name: ruff
if: ${{ inputs.lint-ruff && (inputs.lint-job-mode == 'parallel' || (inputs.lint-job-mode == 'auto' && !github.event.repository.private))
}}
continue-on-error: ${{ ! inputs.lint-fail-on-error }}
runs-on: ubuntu-24.04
timeout-minutes: 10
permissions:
contents: read
security-events: write
steps:
- name: Checkout lint scripts
# The linter scripts ship with this reusable workflow: fetch them from
# the same commit of DevSecNinja/.github that the caller pinned.
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
repository: ${{ job.workflow_repository }}
ref: ${{ job.workflow_sha }}
path: .devsecninja-lint
sparse-checkout: |-
scripts/lint/
scripts/lint-inline-scripts.py
sparse-checkout-cone-mode: false
persist-credentials: false

- name: Move lint scripts out of the workspace
# Outside the workspace the caller's linters don't scan them, and the
# code checkout doesn't delete them.
run: mv .devsecninja-lint "${RUNNER_TEMP}/devsecninja-lint"

- name: Checkout code
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false

- name: Find Python files
# Repositories without Python skip the mise install and ruff.
id: python-files
run: bash "${RUNNER_TEMP}/devsecninja-lint/scripts/lint/detect-language.sh" python

- name: Install mise
if: ${{ steps.python-files.outputs.found == 'true' }}
uses: jdx/mise-action@c2a87611a18de5b3828c5652fe268e992400cb5c # v4.3.0
with:
install: true
version: ${{ inputs.mise-version }}

- name: Run ruff
id: ruff
if: ${{ steps.python-files.outputs.found == 'true' }}
env:
UPLOAD_SARIF: ${{ inputs.upload-sarif }}
run: bash "${RUNNER_TEMP}/devsecninja-lint/scripts/lint/ruff.sh"

- name: Upload SARIF report
if: ${{ !cancelled() && steps.ruff.outputs.sarif == 'true' }}
uses: github/codeql-action/upload-sarif@9b3254085494b55a0f7c4c2b3d2d52cc712ce370 # v4.39.1
with:
sarif_file: ${{ runner.temp }}/ruff.sarif
category: ruff

config-drift:
name: config drift
if: ${{ inputs.lint-config-drift && (inputs.lint-job-mode == 'parallel' || (inputs.lint-job-mode == 'auto' && !github.event.repository.private))
Expand Down Expand Up @@ -798,11 +864,17 @@ jobs:
fetch-depth: ${{ !inputs.lint-gitleaks && 1 || 0 }}
persist-credentials: false

- name: Find Python files
# Repositories without Python skip ruff (and its mise install).
id: python-files
if: ${{ inputs.lint-ruff }}
run: bash "${RUNNER_TEMP}/devsecninja-lint/scripts/lint/detect-language.sh" python

- name: Install mise
if: >-
${{ inputs.lint-dprint || inputs.lint-yamlfmt || inputs.lint-yamllint || inputs.lint-actionlint || inputs.lint-gitleaks
|| inputs.lint-shellcheck || inputs.lint-shfmt || inputs.lint-checkov || inputs.lint-trivy || inputs.lint-zizmor
}}
|| steps.python-files.outputs.found == 'true' }}
uses: jdx/mise-action@c2a87611a18de5b3828c5652fe268e992400cb5c # v4.3.0
with:
install: true
Expand Down Expand Up @@ -986,6 +1058,23 @@ jobs:
sarif_file: ${{ runner.temp }}/zizmor.sarif
category: zizmor

- name: Run ruff
id: ruff
if: ${{ steps.python-files.outputs.found == 'true' }}
continue-on-error: true
env:
UPLOAD_SARIF: ${{ inputs.upload-sarif }}
run: bash "${RUNNER_TEMP}/devsecninja-lint/scripts/lint/ruff.sh"

- name: Upload ruff SARIF report
id: ruff-sarif
if: ${{ !cancelled() && steps.ruff.outputs.sarif == 'true' }}
continue-on-error: true
uses: github/codeql-action/upload-sarif@9b3254085494b55a0f7c4c2b3d2d52cc712ce370 # v4.39.1
with:
sarif_file: ${{ runner.temp }}/ruff.sarif
category: ruff

- name: Check config drift against central repo
id: config-drift
if: ${{ inputs.lint-config-drift }}
Expand Down
1 change: 1 addition & 0 deletions .mise.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ git-cliff = "2.14.2"
"pipx:checkov" = "3.3.30"
"pipx:json5" = "0.17.3"
"pipx:yamllint" = "1.38.0"
ruff = "0.17.5"
shellcheck = "0.11.0"
shfmt = "3.14.1"
trivy = "0.76.1"
Expand Down
81 changes: 59 additions & 22 deletions actions/ai-commits-badge/ai_commits_badge.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Outputs in --out: ai-commits.svg, ai-commits.json (Shields endpoint),
ai-commits-stats.json and ai-commits-card.svg (auto light/dark).
"""

import argparse
import json
import re
Expand Down Expand Up @@ -34,8 +35,21 @@
]
PURPLE, GREY = "#8250df", "#8c959f"
SPARKLE = "M5 0 6.2 3.8 10 5 6.2 6.2 5 10 3.8 6.2 0 5 3.8 3.8Z"
VERDANA = {" ": 3.9, "%": 11.9, "/": 4.7, "I": 4.6, "A": 7.5, "i": 3.1, "m": 10.7, "t": 4.3,
"s": 5.7, "c": 5.7, "o": 6.7, "n": 7.0, "a": 6.6}
VERDANA = {
" ": 3.9,
"%": 11.9,
"/": 4.7,
"I": 4.6,
"A": 7.5,
"i": 3.1,
"m": 10.7,
"t": 4.3,
"s": 5.7,
"c": 5.7,
"o": 6.7,
"n": 7.0,
"a": 6.6,
}


def text_width(s):
Expand All @@ -57,8 +71,11 @@ def ai_tool(identity):

def git(repo_dir, *args):
return subprocess.run(
["git", "-C", str(repo_dir), *args], check=True,
capture_output=True, text=True, encoding="utf-8",
["git", "-C", str(repo_dir), *args],
check=True,
capture_output=True,
text=True,
encoding="utf-8",
).stdout


Expand Down Expand Up @@ -114,10 +131,12 @@ def tooltip(st, name):
return f"{name}: no commits to measure"
window = f" since {st['since']}" if st["since"] else ""
by_tool = ", ".join(f"{t} {n}" for t, n in st["tools"].items()) or "none"
return (f"{name}: {st['ai_commits']:,} of {st['commits']:,} commits{window} ({message(st)}) involved AI: "
f"{st['agent']:,} by an AI agent, {st['coauthored']:,} co-authored with AI, "
f"{st['unattributed']:,} {NO_ATTRIBUTION}. "
f"By tool: {by_tool}. Excludes merges and {st['excluded_automation']:,} bot-automation commits.")
return (
f"{name}: {st['ai_commits']:,} of {st['commits']:,} commits{window} ({message(st)}) involved AI: "
f"{st['agent']:,} by an AI agent, {st['coauthored']:,} co-authored with AI, "
f"{st['unattributed']:,} {NO_ATTRIBUTION}. "
f"By tool: {by_tool}. Excludes merges and {st['excluded_automation']:,} bot-automation commits."
)


def badge_svg(st, name, label="AI commits"):
Expand All @@ -135,10 +154,12 @@ def badge_svg(st, name, label="AI commits"):

def txt(cx, t):
tl = text_width(t) * 10
return (f'<text x="{cx * 10:.0f}" y="135" fill="#010101" fill-opacity=".3" transform="scale(.1)" '
f'textLength="{tl:.0f}">{escape(t)}</text>'
f'<text x="{cx * 10:.0f}" y="125" transform="scale(.1)" fill="#fff" '
f'textLength="{tl:.0f}">{escape(t)}</text>')
return (
f'<text x="{cx * 10:.0f}" y="135" fill="#010101" fill-opacity=".3" transform="scale(.1)" '
f'textLength="{tl:.0f}">{escape(t)}</text>'
f'<text x="{cx * 10:.0f}" y="125" transform="scale(.1)" fill="#fff" '
f'textLength="{tl:.0f}">{escape(t)}</text>'
)

return (
f'<svg xmlns="http://www.w3.org/2000/svg" width="{w}" height="20" role="img" aria-label="{title}">'
Expand Down Expand Up @@ -202,9 +223,11 @@ def card_svg(st, name, theme="auto"):
x += w
share = f"{100 * st[key] / n:.0f}%" if n else "0%"
lx = 24 + i * 144
legend.append(f'<circle cx="{lx + 5}" cy="117" r="5" fill="{colour}"/>'
f'<text x="{lx + 15}" y="121" font-size="12" class="t">{legend_label(key, share)} '
f'<tspan font-weight="600">{share}</tspan></text>')
legend.append(
f'<circle cx="{lx + 5}" cy="117" r="5" fill="{colour}"/>'
f'<text x="{lx + 15}" y="121" font-size="12" class="t">{legend_label(key, share)} '
f'<tspan font-weight="600">{share}</tspan></text>'
)
title = escape(tooltip(st, name))
return (
f'<svg xmlns="http://www.w3.org/2000/svg" width="480" height="190" viewBox="0 0 480 190" role="img" '
Expand All @@ -214,12 +237,12 @@ def card_svg(st, name, theme="auto"):
'<text x="46" y="34" font-size="15" font-weight="600" class="t">Who wrote the commits?</text>'
'<clipPath id="name"><rect x="256" y="18" width="200" height="24"/></clipPath>'
f'<text x="456" y="34" font-size="12" text-anchor="end" class="m" clip-path="url(#name)">'
f'{escape(shown_name)}</text>'
f"{escape(shown_name)}</text>"
f'<text x="24" y="74"><tspan font-size="28" font-weight="700" fill="{PURPLE if n else GREY}">'
f'{escape(message(st))}</tspan><tspan dx="10" font-size="13" class="m">{escape(sub)}</tspan></text>'
'<clipPath id="bar"><rect x="24" y="88" width="432" height="10" rx="5"/></clipPath>'
f'<g clip-path="url(#bar)"><rect x="24" y="88" width="432" height="10" fill="{GREY}" opacity=".25"/>'
f'{"".join(bar)}</g>{"".join(legend)}'
f"{''.join(bar)}</g>{''.join(legend)}"
f'<text x="24" y="149" font-size="12" class="m">{escape(tools_line(st["tools"]))}</text>'
'<text x="24" y="172" font-size="11" class="m">Commit authors + Co-authored-by trailers · bots excluded</text>'
f'<text x="456" y="172" font-size="11" text-anchor="end" class="m">{st["generated_at"][:10]}</text>'
Expand All @@ -229,8 +252,13 @@ def card_svg(st, name, theme="auto"):

def endpoint_json(st, label="AI commits"):
logo = f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10"><path d="{SPARKLE}" fill="#fff"/></svg>'
return {"schemaVersion": 1, "label": label, "message": message(st),
"color": PURPLE.lstrip("#") if st["commits"] else "lightgrey", "logoSvg": logo}
return {
"schemaVersion": 1,
"label": label,
"message": message(st),
"color": PURPLE.lstrip("#") if st["commits"] else "lightgrey",
"logoSvg": logo,
}


def main():
Expand All @@ -241,8 +269,9 @@ def main():
ap.add_argument("--name", default="this repository", help="owner/repo shown in the tooltip")
ap.add_argument("--label", default="AI commits")
ap.add_argument("--out", default="badges")
ap.add_argument("--themes", action="store_true",
help="also write ai-commits-card-light.svg / -dark.svg for <picture> tags")
ap.add_argument(
"--themes", action="store_true", help="also write ai-commits-card-light.svg / -dark.svg for <picture> tags"
)
ap.add_argument("--github-output", help="append numeric step outputs to this file")
args = ap.parse_args()
try:
Expand All @@ -265,7 +294,15 @@ def main():
(out / filename).write_bytes((content + "\n").encode("utf-8"))
if args.github_output:
with Path(args.github_output).open("a", encoding="utf-8", newline="\n") as output:
for key in ("ai_percent", "commits", "ai_commits", "agent", "coauthored", "unattributed", "excluded_automation"):
for key in (
"ai_percent",
"commits",
"ai_commits",
"agent",
"coauthored",
"unattributed",
"excluded_automation",
):
value = "n/a" if st[key] is None else st[key]
output.write(f"{key.replace('_', '-')}={value}\n")
print(json.dumps(st, indent=2))
Expand Down
1 change: 1 addition & 0 deletions config-sync/templates/.mise.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ actionlint = "1.7.12"
dprint = "0.64.3"
gitleaks = "8.30.1"
lefthook = "2.2.2"
ruff = "0.17.5"
shellcheck = "0.11.0"
shfmt = "3.14.1"
trivy = "0.76.1"
Expand Down
9 changes: 9 additions & 0 deletions config-sync/templates/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,12 @@ repo’s tech stack.
## trivy.yaml

Base Trivy config. Add repo-specific `skip-files` entries as needed.

## ruff.toml

Config for ruff, which the shared Lint workflow runs when the repo has
Python files (`lint-ruff`). It sets a 120-character line length and skips the
same generated and vendored paths as the shared `dprint.json`. Config-sync
doesn't add it to a repo that configures ruff in `.ruff.toml` or in
`[tool.ruff]` of `pyproject.toml`, where `ruff.toml` would take precedence and
replace those settings.
25 changes: 25 additions & 0 deletions config-sync/templates/ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Template — adapt per repo. Config for ruff, which the shared Lint workflow
# (lint-ruff) runs on the repository's Python files: `ruff check` and
# `ruff format --diff`. Config-sync doesn't add it to a repo that configures
# ruff in .ruff.toml or [tool.ruff] of pyproject.toml.
line-length = 120

# Generated and vendored paths, as in the shared dprint.json. The Lint workflow
# skips them already; this keeps local `ruff check .` runs in line.
extend-exclude = [
"apm_modules",
".agents",
".claude",
".cursor",
".gemini",
".github/agents",
".github/chatmodes",
".github/instructions",
".github/prompts",
".github/skills",
".kiro",
".opencode",
".specify",
".venv*",
".windsurf",
]
Loading
Loading