Skip to content

GitHub Actions

This workflow reviews every pull request and writes the findings to it as comments. It also prints them in the log and on the summary page of the run. Save it as .github/workflows/review.yml:

name: review
on:
pull_request:
permissions:
contents: read
pull-requests: write
jobs:
kerpeten:
if: github.event.pull_request.head.repo.full_name == github.repository
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v7
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 0
- run: sudo apt-get update && sudo apt-get install -y ripgrep
- run: npm install -g kerpeten
- name: Review
continue-on-error: true
env:
OPENROUTER_API_KEY: ${{ secrets.OPENROUTER_API_KEY }}
GH_TOKEN: ${{ github.token }}
run: |
code=0
kerpeten review > review.txt || code=$?
cat review.txt
{ echo '```text'; cat review.txt; echo '```'; } >> "$GITHUB_STEP_SUMMARY"
exit "$code"
  1. Run kerpeten init in the repository on your machine and commit the .kerpeten.yml it writes. Getting started shows how.
  2. Add the key of your provider as a repository secret: Settings → Secrets and variables → Actions → New repository secret. The example uses OPENROUTER_API_KEY; for another provider change the name in the workflow too.
Part Why
permissions The job reads the code, and reads and writes the pull request with its comments.
if: ...head.repo.full_name == github.repository Skips pull requests from forks. GitHub gives them no secrets, so the review could not run.
ref: ...pull_request.head.sha Checks out the last commit of the pull request. Without it GitHub checks out a merge commit, and the review stops because HEAD is another commit.
fetch-depth: 0 Fetches the whole history. The review needs the base commit, which a shallow clone does not have.
ripgrep The models search the code with it.
kerpeten review In a job for a pull_request or pull_request_target event, Kerpeten takes the number of the pull request from the event. --mr <n> names another one. Before 0.5.0, write --mr ${{ github.event.pull_request.number }}.
GH_TOKEN Kerpeten reads and writes the pull request with gh, which is on the runner already and takes its sign-in from this variable.
> review.txt The findings go to standard output. An error goes to standard error, so it shows in the log.
code=0, || code=$?, exit "$code" The review ends with exit code 1 when a comment could not be written, after it printed the findings. These lines show the findings first and then fail the step.
continue-on-error: true A failed review shows as a failed step with a warning on the run, and the job still passes, so it never blocks a merge. The reason is in the log. Remove it to make the job fail.

Two lines of the workflow make the review write:

In the workflow Why
pull-requests: write Lets the token of the workflow comment on the pull request
kerpeten review without --dry-run The review writes its comments

What the pull request gets:

On the pull request What it holds
One review A line comment for each finding that scores high enough
One summary comment The other findings. A later run updates it
  • The comments come from github-actions[bot], the identity of the workflow’s own token.
  • A second run for the same commit writes no comment twice.
  • A run after a push reviews only the new commits. fetch-depth: 0 is needed for this too: the last reviewed commit must be in the clone.
  • The step fails with exit code 1 when a comment could not be written. The log says which one and why.

Reviewing has the details, the exit codes and the error messages.

To have the comments come from an account you choose, give the job a token of that account as GH_TOKEN:

env:
OPENROUTER_API_KEY: ${{ secrets.OPENROUTER_API_KEY }}
GH_TOKEN: ${{ secrets.KERPETEN_BOT_TOKEN }}
  1. Sign in to GitHub as the bot account and create a fine-grained personal access token for the repository, with these repository permissions:

    Permission Access
    Pull requests Read and write
    Contents Read-only
  2. Add the token as a repository secret. The example calls it KERPETEN_BOT_TOKEN.

Three rules of an organization can stop such a token:

Rule What to know
Approval An organization can require that a new fine-grained token is approved. Until then every write answers HTTP 403.
Lifetime An organization can refuse tokens that live too long. Choose an expiry it accepts.
Resource owner The resource owner of the token must be the organization. It is chosen when the token is created and cannot be changed later.

With a token that may not write, the step fails and the log has a line that ends with not written: GitHub does not let the account gh is signed in to github.com with do this (HTTP 403): Resource not accessible by personal access token.

A pull request that already has a summary comment from another account may get a second one from the bot. Reviewing says when.

A push to the pull request starts a new run. A run that is still reviewing the commit before it then writes nothing and fails with no comment was written: pull request 12 moved to commit ....

To stop the older run instead, add this block at the top level of the workflow:

concurrency:
group: kerpeten-${{ github.event.pull_request.number }}
cancel-in-progress: true

It also keeps two runs for the same pull request from writing at the same time.

To review without writing a comment, change two lines of the workflow:

In the workflow Change it to
pull-requests: write pull-requests: read
kerpeten review kerpeten review --dry-run

The output then shows the comment plan: the comments the review would write.

--base needs only git. It writes no comment, so it needs --dry-run. Replace the review step with this one:

- name: Review
env:
OPENROUTER_API_KEY: ${{ secrets.OPENROUTER_API_KEY }}
run: kerpeten review --dry-run --base ${{ github.event.pull_request.base.sha }}
  • The reviewer gets the commit messages instead of the title and the description.
  • There is no comment plan.
  • The pull-requests permission and GH_TOKEN are not needed.

--json prints the whole result as JSON. These steps upload it as an artifact of the run:

- name: Review
env:
OPENROUTER_API_KEY: ${{ secrets.OPENROUTER_API_KEY }}
GH_TOKEN: ${{ github.token }}
run: kerpeten review --json > review.json
- uses: actions/upload-artifact@v7
if: ${{ !cancelled() }}
with:
name: kerpeten-review
path: review.json

if: ${{ !cancelled() }} uploads the file also when the review step failed because a comment could not be written.

A review with findings ends with exit code 0. To make the job fail when a finding scores high enough for a comment on its line, add a step after the review that wrote review.json:

- name: No finding may be strong enough for a line comment
run: jq -e '[.findings[] | select(.action == "inline" or .action == "suppressed_duplicate")] | length == 0' review.json

A finding whose line comment is already on the pull request has the action suppressed_duplicate in a later run for the same commit, so the check counts both actions.

  • The key variable follows the provider in .kerpeten.yml: OPENAI_API_KEY for openai, OPENROUTER_API_KEY for openrouter.
  • A model with its own base_url also needs KERPETEN_TRUSTED_ENDPOINTS in env. Configuration says what to put there.