ci-testgen v0.0.7
CI TestGen
This project is designed to automate the generation, showing, and applying of tests for your GitHub and GitLab projects.
Prerequisites
- Node.js (version 20 or higher)
 - GitHub or GitLab account with appropriate permissions
 
How to Use
This tool is available via npx but is meant to be used in the CI/CD pipeline. Once you have finished setting up the tool with GitHub / GitLab, the workflow is as follows:
Create or Update a Pull/Merge Request: When a developer creates or updates a pull/merge request, the
generate-testscommand will automatically run in the CI/CD pipeline. This will generate tests based on the changes and post them as comments in the pull/merge request.Review Generated Tests: The developer will see the generated tests as comments in the pull/merge request. They can review these tests and decide which ones to approve.
Approve Tests: To approve a test, the developer should react with a 👍 emoji to the comment containing the test they want to approve.
Trigger Test Application: After approving the desired tests, the developer should type "testgen" into a new comment in the pull/merge request. This will trigger the
apply-testscommand in the CI/CD pipeline.Apply Approved Tests: The
apply-testscommand will apply the approved tests and push the changes to the branch associated with the pull/merge request.
Setting up with GitHub
Adding generate-tests workflow
Create .github/workflows/generate-tests.yml with the following content:
name: Generate Tests
on:
  pull_request:
    branches:
      - master
      - main
jobs:
  generate_tests:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: write
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 1
      - name: Generate and post tests
        run: npx ci-testgen generate-tests --gitRemote=github --token=${{ secrets.GITHUB_TOKEN }} --repoDir=$GITHUB_WORKSPACE --owner=${{ github.repository_owner }} --repo=${{ github.event.repository.name }} --pullRequestId=${{ github.event.number }}Adding apply-tests workflow
Create .github/workflows/apply-tests.yml with the following content:
name: Apply Tests
on:
  issue_comment:
    types:
      - created
      - edited
jobs:
  apply-test:
    if: ${{ github.event.issue.pull_request && startsWith(github.event.comment.body, 'testgen') }}
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: write
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 1
      - name: Apply approved tests
        run: npx ci-testgen apply-tests --gitRemote=github --token=${{ secrets.GITHUB_TOKEN }} --repoDir='.' --owner=${{ github.repository_owner }} --repo=${{ github.event.repository.name }} --pullRequestId=${{ github.event.issue.number }} --triggerCommentId=${{ github.event.comment.id }} --triggerCommentBody=${{ github.event.comment.body }}Done! You have completed the setup with GitHub.
Setting up with GitLab
Adding generate-tests workflow
Modify the .gitlab-ci.yml of the repo to add the new stage and job.
stages:
# ...existing stages...
  - generate_tests
# ...existing jobs...
generate_tests_job:
  stage: generate_tests
  needs: []
  image: node:20
  variables:
    GIT_DEPTH: 1
  script:
    - npx ci-testgen generate-tests --gitRemote=gitlab --token=$ACCESS_TOKEN --repoDir=$CI_PROJECT_DIR --projectId=$CI_PROJECT_ID --apiUrl=$CI_API_V4_URL --mergeRequestId=$CI_MERGE_REQUEST_IID
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
      when: alwaysAdding apply-tests workflow
Since GitLab doesn't allow specific triggering of jobs, the following .gitlab-ci.yml should live inside an orphaned branch to ensure it only runs when explicitly triggered and doesn't interfere with other pipelines.
stages:
  - api_stage
api_only_job:
  stage: api_stage
  image: node:20
  rules:
    - if: $CI_PIPELINE_SOURCE == "trigger"
  variables:
    GIT_DEPTH: 1
  script:
    - npx ci-testgen apply-tests --gitRemote=gitlab --token=$ACCESS_TOKEN --repoDir=$CI_PROJECT_DIR --projectId=$CI_PROJECT_ID --apiUrl=$CI_API_V4_URL --triggerPayloadPath=$TRIGGER_PAYLOADTo create an Orphaned Branch:
- Create the branch
 
git checkout --orphan ci-testgen-apply-test-workflow
git rm -rf . # Clear the staging area- Add the above 
.gitlab-ci.ymlfile to the branch. 
git add .gitlab-ci.yml- Commit and Push the Branch:
 
git commit -m "Add apply-tests workflow in orphaned branch"
git push origin ci-testgen-apply-test-workflowWebhook Setup for GitLab
Provision a Pipeline Trigger Token:
- Go to 
Settings > CI/CD > Pipeline Trigger Tokens. - Add a new token and call it 
Testgen Token. 
- Go to 
 Add a Webhook:
- Go to 
Settings > Webhooksand click "Add new webhook". - For the URL, use: 
https://gitlab.com/api/v4/projects/<project-id>/ref/ci-testgen-apply-test-workflow/trigger/pipeline?token=<pipeline-trigger-token>. Replace \<project-id> with your GitLab project ID. - Under 'Trigger' section, check "Comments".
 - Under 'Name', use: 'Apply Testgen Test' (optional)
 
- Go to 
 
CI/CD Variables Setup for GitLab
Provision an Access Token:
- Go to 
Settings > Access Tokenand click "Add new token". - Use "Testgen" as the token name.
 - Select role to be developer and give the following scopes: 
api,read_api,read_repository,write_repository. - Set an appropriate expiration date for the token
 
- Go to 
 Store the Access Token:
- Go to 
Settings > CI/CD > Variablesand store the token with the key asACCESS_TOKEN. - Either choose "masked" or "masked and hidden".
 - Untick the "Protect variable" option.
 
- Go to 
 
Contributing
Check out our guide at CONTRIBUTING.md
Additional Resources
For more information, refer to the official GitHub and GitLab documentation.