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
31 changes: 31 additions & 0 deletions .github/workflows/archlens-health.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: ArchLens health

# Runs ArchLens's own action on this repository: on demand and once a week.
# Not on every push: the score reads repository metadata, which rarely changes between commits.
on:
workflow_dispatch:
inputs:
min-score:
description: Fail when the score is below this (0-100). Empty means never fail.
required: false
default: ""
schedule:
- cron: "0 6 * * 1" # Mondays, 06:00 UTC

permissions:
contents: read

jobs:
health:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: ArchLens
id: archlens
uses: ./
with:
min-score: ${{ inputs.min-score }}
- name: Show outputs
run: |
echo "Score: ${{ steps.archlens.outputs.score }} (${{ steps.archlens.outputs.grade }})"
echo "Known vulnerabilities: ${{ steps.archlens.outputs.vulnerabilities }}"
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes to the ArchLens project will be documented in this file.
## [Unreleased]

### Added
- **GitHub Action:** `action.yml` (composite) runs the CLI on a repository (default: the current one) with the workflow token, writes the Markdown report to the job summary, sets outputs `score`, `grade` and `vulnerabilities`, fails below an optional `min-score`, and optionally posts or updates one pull-request comment. `.github/workflows/archlens-health.yml` runs it on ArchLens itself weekly and on demand. The CLI gained `--format rich|markdown|json`, `--min-score` (exit code 2 below it) and `--output-dir`.
- **Dependencies and known vulnerabilities:** reads `requirements*.txt`, `pyproject.toml`, `package.json` (+ lockfile) and `go.mod` from the repository root, lists direct dependencies as pinned or not, and checks pinned versions against OSV.dev with severity, advisory link and the version to upgrade to. Shown on the report page, in the JSON API (`dependency_scan`), the Markdown export and the CLI; stored in a new `metrics.dependencies_json` column (migration `a1c5e8f20b64`). OSV or GitHub trouble degrades to "couldn't check", never a failed analysis. The score formula is unchanged. See `docs/walkthroughs/01-dependency-vulnerabilities.md`.
- `docs/roadmap.md` now records the decision to build three features and why the other ideas were dropped or deferred.
- **Live progress:** `POST /api/v1/analyze/stream` streams an analysis's progress as newline-delimited JSON (`validate`, `reuse`, one `fetch` event per finished GitHub call, `analyze`, `score`, `save`, then `done` or `error`). The home page's "How it works" steps light up from these events; without JavaScript the form posts as before.
Expand Down
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,12 @@ From the command line:
```bash
archlens analyze psf/requests
archlens analyze https://github.com/pallets/flask --profile personal
archlens analyze psf/requests --format json --min-score 70 # machine-readable; exit code 2 if below 70
archlens analyze psf/requests --format markdown --output-dir out/ # also writes report.md, result.json, comment.md
```

`--format` is `rich` (default), `markdown` or `json`; the last two print only the report on stdout and send messages to stderr. Exit codes: `0` success, `1` the analysis failed, `2` the score is below `--min-score`.

### Configuration

| Variable | Default | Purpose |
Expand All @@ -126,6 +130,44 @@ archlens analyze https://github.com/pallets/flask --profile personal

Copy `.env.example` to `.env` to set these locally. `python scripts/build_examples.py` regenerates the bundled example reports from live analyses.

## GitHub Action

ArchLens ships a composite GitHub Action (`action.yml` at the repository root). It installs ArchLens from this repository, analyzes a repository with the workflow's own `GITHUB_TOKEN`, writes the Markdown report to the job summary, exposes outputs, can fail the job below a minimum score and can keep one comment up to date on pull requests.

```yaml
name: Repository health
on:
pull_request:
schedule:
- cron: "0 6 * * 1"

permissions:
contents: read
pull-requests: write # only needed for comment: true

jobs:
archlens:
runs-on: ubuntu-latest
steps:
- uses: vishnuatgit/ArchLens@main
id: health
with:
min-score: 60 # optional: fail the job below this score
comment: true # optional: one comment on pull requests, updated in place
- run: echo "Score ${{ steps.health.outputs.score }} (${{ steps.health.outputs.grade }}), ${{ steps.health.outputs.vulnerabilities }} known vulnerabilities"
```

| Input | Default | Meaning |
| :--- | :--- | :--- |
| `repository` | the current repository | `owner/name` to analyze |
| `profile` | `library` | `library`, `personal` or `enterprise` |
| `min-score` | empty (never fails) | fail the job when the score is lower |
| `comment` | `false` | on `pull_request` events, post or update a single ArchLens comment (score, grade, top fixes, dependency summary) |
| `github-token` | `github.token` | token used to read repository metadata |
| `python-version` | `3.11` | Python used to run ArchLens |

Outputs: `score`, `grade`, and `vulnerabilities` (known vulnerabilities in pinned dependencies; empty when they could not be checked). Notes: use an Ubuntu runner; pull requests from forks get a read-only token, so the comment is skipped with a warning and the rest still works; the score describes the repository's metadata, not the pull request's diff. This repository runs the action on itself weekly and on demand (`.github/workflows/archlens-health.yml`). Details in [docs/walkthroughs/02-github-action.md](docs/walkthroughs/02-github-action.md).

## Tests

The same checks CI runs:
Expand Down
145 changes: 145 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
name: ArchLens health check
description: >-
Score a GitHub repository's engineering health (0-100), write the report to the job summary,
optionally fail below a minimum score, and optionally keep one pull-request comment up to date.
author: vishnuatgit
branding:
icon: activity
color: blue

inputs:
repository:
description: Repository to analyze as owner/name. Defaults to the repository running the workflow.
required: false
default: ${{ github.repository }}
profile:
description: "Scoring profile: library, personal or enterprise."
required: false
default: library
min-score:
description: Fail the job when the score is below this number (0-100). Leave empty to never fail.
required: false
default: ""
comment:
description: >-
"true" to post or update a single ArchLens comment on pull requests (needs
`pull-requests: write` permission). Ignored on other events.
required: false
default: "false"
github-token:
description: Token used to read repository metadata from the GitHub API.
required: false
default: ${{ github.token }}
python-version:
description: Python version used to run ArchLens (3.11 or newer).
required: false
default: "3.11"

outputs:
score:
description: The 0-100 health score.
value: ${{ steps.analyze.outputs.score }}
grade:
description: The letter grade, A+ to F.
value: ${{ steps.analyze.outputs.grade }}
vulnerabilities:
description: Number of known vulnerabilities in pinned dependencies. Empty when they could not be checked.
value: ${{ steps.analyze.outputs.vulnerabilities }}

runs:
using: composite
steps:
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ inputs.python-version }}

- name: Install ArchLens
shell: bash
env:
ACTION_PATH: ${{ github.action_path }}
run: python -m pip install --quiet --editable "$ACTION_PATH"

- name: Analyze
id: analyze
shell: bash
env:
ACTION_PATH: ${{ github.action_path }}
GITHUB_TOKEN: ${{ inputs.github-token }}
REPOSITORY: ${{ inputs.repository }}
PROFILE: ${{ inputs.profile }}
MIN_SCORE: ${{ inputs.min-score }}
run: |
out="$RUNNER_TEMP/archlens"
mkdir -p "$out"
# A throwaway database: the CLI stores its report like the web app does
export DATABASE_URL="sqlite:///${out//\\//}/archlens.db"
export OUT_DIR="$out"
alembic -c "$ACTION_PATH/alembic.ini" upgrade head > /dev/null

args=(analyze "$REPOSITORY" --profile "$PROFILE" --format json --output-dir "$out")
if [ -n "$MIN_SCORE" ]; then args+=(--min-score "$MIN_SCORE"); fi

set +e
archlens "${args[@]}"
code=$?
set -e

# 1 means the analysis itself failed; 2 means the score is below min-score
if [ "$code" -eq 1 ]; then
echo "::error::ArchLens could not analyze $REPOSITORY. See the log above."
exit 1
fi

python - <<'PY'
import json
import os

with open(os.path.join(os.environ["OUT_DIR"], "result.json"), encoding="utf-8") as f:
result = json.load(f)
count = result["vulnerability_count"]
with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as f:
f.write(f"score={result['score']}\n")
f.write(f"grade={result['grade']}\n")
f.write(f"vulnerabilities={'' if count is None else count}\n")
PY
echo "exit-code=$code" >> "$GITHUB_OUTPUT"

- name: Write job summary
shell: bash
run: cat "$RUNNER_TEMP/archlens/report.md" >> "$GITHUB_STEP_SUMMARY"

- name: Post or update the pull-request comment
if: inputs.comment == 'true' && github.event_name == 'pull_request'
# Pull requests from forks get a read-only token and cannot comment: warn, do not fail
continue-on-error: true
uses: actions/github-script@v7
env:
COMMENT_FILE: ${{ runner.temp }}/archlens/comment.md
with:
github-token: ${{ inputs.github-token }}
script: |
const fs = require('fs');
const body = fs.readFileSync(process.env.COMMENT_FILE, 'utf8');
const marker = '<!-- archlens-health-comment -->';
const { owner, repo } = context.repo;
const issue_number = context.payload.pull_request.number;
const comments = await github.paginate(github.rest.issues.listComments, {
owner, repo, issue_number, per_page: 100,
});
const existing = comments.find((c) => c.body && c.body.includes(marker));
if (existing) {
await github.rest.issues.updateComment({ owner, repo, comment_id: existing.id, body });
} else {
await github.rest.issues.createComment({ owner, repo, issue_number, body });
}

- name: Enforce the minimum score
if: steps.analyze.outputs.exit-code == '2'
shell: bash
env:
SCORE: ${{ steps.analyze.outputs.score }}
MIN_SCORE: ${{ inputs.min-score }}
run: |
echo "::error::ArchLens score $SCORE is below the minimum of $MIN_SCORE."
exit 1
Loading
Loading