0.0.291 • Published 8 months ago

@cgft/ci-testgen v0.0.291

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

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:

  1. Create or Update a Pull/Merge Request: When a developer creates or updates a pull/merge request, the generate-tests command 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.

  2. 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.

  3. Approve Tests: To approve a test, the developer should react with a 👍 emoji to the comment containing the test they want to approve.

  4. 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-tests command in the CI/CD pipeline.

  5. Apply Approved Tests: The apply-tests command 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 @cgft/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 @cgft/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 }}

Add GitHub secrets like AZURE_API_KEY by following: https://docs.github.com/en/actions/security-for-github-actions/security-guides/using-secrets-in-github-actions#creating-secrets-for-a-repository

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 @cgft/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: always

Adding 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 @cgft/ci-testgen apply-tests --gitRemote=gitlab --token=$ACCESS_TOKEN --repoDir=$CI_PROJECT_DIR --projectId=$CI_PROJECT_ID --apiUrl=$CI_API_V4_URL --triggerPayloadPath=$TRIGGER_PAYLOAD

To create an Orphaned Branch:

  1. Create the branch
git checkout --orphan ci-testgen-apply-test-workflow
git rm -rf . # Clear the staging area
  1. Add the above .gitlab-ci.yml file to the branch.
git add .gitlab-ci.yml
  1. Commit and Push the Branch:
git commit -m "Add apply-tests workflow in orphaned branch"
git push origin ci-testgen-apply-test-workflow

Webhook Setup for GitLab

  1. Provision a Pipeline Trigger Token:

    • Go to Settings > CI/CD > Pipeline Trigger Tokens.
    • Add a new token and call it Testgen Token.
  2. Add a Webhook:

    • Go to Settings > Webhooks and 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)

CI/CD Variables Setup for GitLab

  1. Provision an Access Token:

    • Go to Settings > Access Token and 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
  2. Store the Access Token:

    • Go to Settings > CI/CD > Variables and store the token with the key as ACCESS_TOKEN.
    • Either choose "masked" or "masked and hidden".
    • Untick the "Protect variable" option.

(Also, add AZURE_API_KEY to the variable)

Contributing

Check out our guide at CONTRIBUTING.md

Additional Resources

For more information, refer to the official GitHub and GitLab documentation.

0.0.291

8 months ago

0.0.290

8 months ago

0.0.289

8 months ago

0.0.288

8 months ago

0.0.287

8 months ago

0.0.286

8 months ago

0.0.284

8 months ago

0.0.283

8 months ago

0.0.282

8 months ago

0.0.281

8 months ago

0.0.280

8 months ago

0.0.279

8 months ago

0.0.278

8 months ago

0.0.277

8 months ago

0.0.276

8 months ago

0.0.275

8 months ago

0.0.274

8 months ago

0.0.273

8 months ago

0.0.272

8 months ago

0.0.271

8 months ago

0.0.270

8 months ago

0.0.269

8 months ago

0.0.268

8 months ago

0.0.267

8 months ago

0.0.266

8 months ago

0.0.265

8 months ago

0.0.264

8 months ago

0.0.263

8 months ago

0.0.262

8 months ago

0.0.261

8 months ago

0.0.260

8 months ago

0.0.259

8 months ago

0.0.258

8 months ago

0.0.257

8 months ago

0.0.256

8 months ago

0.0.255

8 months ago

0.0.254

8 months ago

0.0.253

8 months ago

0.0.252

9 months ago

0.0.251

9 months ago

0.0.250

9 months ago

0.0.249

9 months ago

0.0.247

9 months ago

0.0.246

9 months ago

0.0.245

9 months ago

0.0.244

9 months ago

0.0.243

9 months ago

0.0.242

9 months ago

0.0.241

9 months ago

0.0.240

9 months ago

0.0.239

9 months ago

0.0.238

9 months ago

0.0.237

9 months ago

0.0.236

9 months ago

0.0.235

9 months ago

0.0.234

9 months ago

0.0.233

9 months ago

0.0.232

9 months ago

0.0.231

9 months ago

0.0.230

9 months ago

0.0.229

9 months ago

0.0.228

9 months ago

0.0.227

9 months ago

0.0.226

9 months ago

0.0.225

9 months ago

0.0.224

9 months ago

0.0.223

9 months ago

0.0.222

9 months ago

0.0.221

9 months ago

0.0.220

9 months ago

0.0.219

9 months ago

0.0.218

9 months ago

0.0.217

9 months ago

0.0.216

9 months ago

0.0.215

9 months ago

0.0.214

9 months ago

0.0.213

9 months ago

0.0.212

9 months ago

0.0.211

9 months ago

0.0.210

9 months ago

0.0.209

9 months ago

0.0.208

9 months ago

0.0.207

9 months ago

0.0.206

9 months ago

0.0.205

9 months ago

0.0.204

9 months ago

0.0.203

9 months ago

0.0.202

9 months ago

0.0.201

9 months ago

0.0.200

9 months ago

0.0.199

9 months ago

0.0.198

9 months ago

0.0.197

9 months ago

0.0.196

9 months ago

0.0.195

9 months ago

0.0.194

9 months ago

0.0.193

9 months ago

0.0.192

9 months ago

0.0.191

9 months ago

0.0.190

10 months ago

0.0.189

10 months ago

0.0.188

10 months ago

0.0.187

10 months ago

0.0.186

10 months ago

0.0.185

10 months ago

0.0.184

10 months ago

0.0.183

10 months ago

0.0.182

10 months ago

0.0.181

11 months ago

0.0.180

11 months ago

0.0.179

11 months ago

0.0.178

11 months ago

0.0.177

11 months ago

0.0.176

11 months ago

0.0.175

11 months ago

0.0.174

11 months ago

0.0.173

11 months ago

0.0.172

11 months ago

0.0.171

11 months ago

0.0.170

11 months ago

0.0.168

11 months ago

0.0.167

11 months ago

0.0.166

11 months ago

0.0.165

11 months ago

0.0.164

11 months ago

0.0.163

11 months ago

0.0.162

11 months ago

0.0.161

11 months ago

0.0.160

11 months ago

0.0.159

11 months ago

0.0.158

11 months ago

0.0.157

11 months ago

0.0.156

11 months ago

0.0.155

11 months ago

0.0.154

11 months ago

0.0.153

11 months ago

0.0.152

11 months ago

0.0.151

11 months ago

0.0.150

11 months ago

0.0.149

11 months ago

0.0.148

11 months ago

0.0.147

11 months ago

0.0.146

11 months ago

0.0.145

11 months ago

0.0.144

11 months ago

0.0.143

11 months ago

0.0.142

11 months ago

0.0.141

11 months ago

0.0.140

11 months ago

0.0.139

11 months ago

0.0.138

11 months ago

0.0.137

11 months ago

0.0.136

11 months ago

0.0.135

11 months ago

0.0.134

11 months ago

0.0.133

11 months ago

0.0.132

11 months ago

0.0.131

11 months ago

0.0.130

11 months ago

0.0.129

11 months ago

0.0.128

11 months ago

0.0.127

11 months ago

0.0.126

11 months ago

0.0.125

11 months ago

0.0.124

11 months ago

0.0.123

11 months ago

0.0.122

11 months ago

0.0.121

11 months ago

0.0.120

11 months ago

0.0.119

11 months ago

0.0.118

11 months ago

0.0.117

11 months ago

0.0.116

11 months ago

0.0.115

11 months ago

0.0.114

11 months ago

0.0.113

11 months ago

0.0.112

11 months ago

0.0.111

11 months ago

0.0.110

11 months ago

0.0.109

11 months ago

0.0.108

11 months ago

0.0.107

11 months ago

0.0.106

11 months ago

0.0.105

11 months ago

0.0.104

11 months ago

0.0.103

11 months ago

0.0.102

11 months ago

0.0.101

11 months ago

0.0.100

11 months ago

0.0.99

11 months ago

0.0.98

11 months ago

0.0.97

11 months ago

0.0.96

11 months ago

0.0.95

11 months ago

0.0.94

11 months ago

0.0.93

11 months ago

0.0.92

12 months ago

0.0.91

12 months ago

0.0.90

12 months ago

0.0.89

12 months ago

0.0.88

12 months ago

0.0.87

12 months ago

0.0.86

12 months ago

0.0.85

12 months ago

0.0.84

12 months ago

0.0.83

12 months ago

0.0.82

12 months ago

0.0.81

12 months ago

0.0.80

12 months ago

0.0.79

12 months ago

0.0.78

12 months ago

0.0.77

12 months ago

0.0.76

12 months ago

0.0.75

12 months ago

0.0.74

12 months ago

0.0.73

12 months ago

0.0.72

12 months ago

0.0.71

12 months ago

0.0.70

12 months ago

0.0.69

12 months ago

0.0.68

12 months ago

0.0.67

12 months ago

0.0.66

12 months ago

0.0.65

12 months ago

0.0.64

12 months ago

0.0.63

12 months ago

0.0.62

12 months ago

0.0.61

12 months ago

0.0.60

12 months ago

0.0.59

12 months ago

0.0.58

12 months ago

0.0.56

12 months ago

0.0.55

12 months ago

0.0.54

12 months ago

0.0.53

12 months ago

0.0.52

12 months ago

0.0.51

12 months ago

0.0.50

12 months ago

0.0.49

12 months ago

0.0.48

12 months ago

0.0.47

12 months ago

0.0.46

12 months ago

0.0.45

12 months ago

0.0.44

12 months ago

0.0.43

12 months ago

0.0.42

12 months ago

0.0.41

12 months ago

0.0.40

12 months ago

0.0.39

12 months ago

0.0.38

1 year ago

0.0.37

1 year ago

0.0.36

1 year ago

0.0.35

1 year ago

0.0.34

1 year ago

0.0.33

1 year ago

0.0.32

1 year ago

0.0.31

1 year ago

0.0.30

1 year ago

0.0.29

1 year ago

0.0.28

1 year ago

0.0.27

1 year ago

0.0.26

1 year ago

0.0.25

1 year ago

0.0.24

1 year ago

0.0.23

1 year ago

0.0.22

1 year ago

0.0.21

1 year ago

0.0.20

1 year ago

0.0.19

1 year ago

0.0.18

1 year ago

0.0.17

1 year ago

0.0.16

1 year ago

0.0.15

1 year ago

0.0.14

1 year ago

0.0.13

1 year ago

0.0.12

1 year ago

0.0.11

1 year ago

0.0.10

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago