Skip to content

Instantly share code, notes, and snippets.

@moznion
Created July 8, 2026 05:19
Show Gist options
  • Select an option

  • Save moznion/1ee990e058d074e72c7c6ec3bf6d1402 to your computer and use it in GitHub Desktop.

Select an option

Save moznion/1ee990e058d074e72c7c6ec3bf6d1402 to your computer and use it in GitHub Desktop.
cccc with GitHub Actions
name: Code Complexity
# Cognitive / Cyclomatic Complexity を cccc で計測し、
# octocov の Custom Metrics としてトラッキングする。
on:
pull_request:
branches: [develop]
push:
branches: [develop]
jobs:
complexity:
name: Measure & track complexity
runs-on: ubuntu-latest
permissions:
pull-requests: write # octocov が PR にコメントするために必要
contents: write # octocov が octocov-complexity-reports ブランチにレポートを保存するために必要
actions: read
steps:
- uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
# cccc で src/ 配下の複雑度を計測し、JSON を出力する。
# 閾値(max-cognitive/max-cyclomatic)は指定せず、計測のみ行う(CI を fail させない)。
- name: Measure complexity (cccc)
uses: moznion/cccc-action@8fa5a4b13bf907676709cece09147a047b7be7b0 # v1.0.0
with:
path: src
output-file: cccc.json
config: cccc.toml
# cccc の summary を octocov の Custom Metrics JSON 形式へ変換する。
# https://github.com/k1LoW/octocov#custom-metrics
- name: Build octocov custom metrics
run: |
jq '{
key: "code_complexity",
name: "Code Complexity (src)",
metrics: [
{ key: "function_count", name: "Function count", value: .summary.function_count },
{ key: "cognitive_sum", name: "Cognitive: total", value: .summary.cognitive.sum },
{ key: "cognitive_max", name: "Cognitive: max", value: .summary.cognitive.max },
{ key: "cognitive_median", name: "Cognitive: median", value: .summary.cognitive.median },
{ key: "cognitive_p90", name: "Cognitive: p90", value: .summary.cognitive.p90 },
{ key: "cyclomatic_sum", name: "Cyclomatic: total", value: .summary.cyclomatic.sum },
{ key: "cyclomatic_max", name: "Cyclomatic: max", value: .summary.cyclomatic.max },
{ key: "cyclomatic_median",name: "Cyclomatic: median", value: .summary.cyclomatic.median },
{ key: "cyclomatic_p90", name: "Cyclomatic: p90", value: .summary.cyclomatic.p90 }
]
}' cccc.json > custom_metrics_complexity.json
echo "::group::custom_metrics_complexity.json"
cat custom_metrics_complexity.json
echo "::endgroup::"
# 参考: 認知的複雑度が高い関数トップ20をジョブサマリーに出力する(人間向け)。
- name: Append top complex functions to job summary
run: |
{
echo "## Top 20 functions by Cognitive Complexity"
echo ""
echo "| Cognitive | Cyclomatic | Function | File:Line |"
echo "| --- | --- | --- | --- |"
jq -r '
[ .files[] | .path as $p | .functions[] | recurse(.children[]?)
| { path: $p, name: .name, line: .line, cognitive: .cognitive, cyclomatic: .cyclomatic } ]
| sort_by(-.cognitive) | .[:20][]
| "| \(.cognitive) | \(.cyclomatic) | `\(.name)` | \(.path):\(.line) |"
' cccc.json
} >> "$GITHUB_STEP_SUMMARY"
# octocov に Custom Metrics を渡してトラッキング(PR コメント / datastore 保存)。
- name: Run octocov
uses: k1LoW/octocov-action@b3b6ee60482a667950f87553abf1df63217235d9 # v1.5.1
with:
config: .octocov.complexity.yml
env:
OCTOCOV_CUSTOM_METRICS_COMPLEXITY: custom_metrics_complexity.json
repository: ${GITHUB_REPOSITORY}/complexity
comment:
if: is_pull_request
summary:
if: true
report:
if: is_default_branch
datastores:
- github://${GITHUB_REPOSITORY}@octocov/complexity-reports
diff:
datastores:
- github://${GITHUB_REPOSITORY}@octocov/complexity-reports
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment