Skip to main content

Integration & Example

This section shows how to integrate Early Catch for Pull Request into your GitHub workflow.


GitHub Actions Workflow Example

Below is an example of a GitHub Actions workflow that runs Early Catch for Pull Request when a PR is opened against your target branch.
You can copy the example and adapt it to your repository, branch naming, and environment settings.


GitHub Actions Workflow Example

name: earlyai test generation - 2

permissions:
packages: read
contents: write
pull-requests: read

on:
pull_request:
types:
- opened
branches:
- main

jobs:
run-tests:
runs-on: ubuntu-latest
container:
image: ghcr.io/earlyai/early-github-action:qa
credentials:
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/setup-node@v4
with:
node-version: 22

- name: Setup CLI
run: |
EARLYAI_PATH=$(ls -d /tmp/early-ext/early-ai*)
chmod +x "$EARLYAI_PATH/packages/cli/dist/cli.js"
cd "$EARLYAI_PATH/packages/cli/"
npm install -g .
which early-ai

- uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}

- run: npm install

- name: Generate test runner config
id: generate-json
uses: actions/github-script@v7
with:
result-encoding: string
script: |
const pr = context.payload.pull_request;
if (!pr) core.setFailed("No pull request context.");

const gitRepo = `https://github.com/${context.repo.owner}/${context.repo.repo}.git`;
const files = await github.paginate(
github.rest.pulls.listFiles,
{ owner: context.repo.owner, repo: context.repo.repo, pull_number: pr.number, per_page: 100 }
);

const isTestFile = f => f.includes('__tests__/') || f.endsWith('.spec.ts') || f.endsWith('.test.ts');

const paths = files
.filter(f => (f.status === 'added' || f.status === 'modified') && f.filename.endsWith('.ts') && !isTestFile(f.filename))
.map(f => ({ file: f.filename, names: ["*"] }));

return JSON.stringify({ git_repo: gitRepo, paths }, null, 2);

- run: echo '${{ steps.generate-json.outputs.result }}' > tests_runner_config.json

- run: /entrypoint.sh
env:
EARLY_ACCESS_TOKEN: ${{ secrets.EARLY_SECRET_TOKEN }}
EARLY_ACTION: generate_tests
timeout-minutes: 15

- uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: "chore: auto-generated tests"
branch: ${{ github.head_ref }}
file_pattern: "*.early.test.ts"

Steps to Integrate

  1. Add the workflow file to your .github/workflows/ folder in your repository.
  2. Set required secrets in your repository settings:
  3. Adjust coverage thresholds if you want different limits than the defaults:
    • Lower threshold (start generating tests): default 0%
    • Upper threshold (stop enhancing tests): default 80%
  4. Commit and push the workflow file to your repository.
  5. On the next Pull Request creation, Early will:
    • Identify changed files
    • Scan functions for coverage
    • Generate tests for eligible functions
    • Commit only passing tests back to the PR branch