0.0.291 • Published 1 year ago

@cgft/ci-testgen v0.0.291

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year 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

1 year ago

0.0.290

1 year ago

0.0.289

1 year ago

0.0.288

1 year ago

0.0.287

1 year ago

0.0.286

1 year ago

0.0.284

1 year ago

0.0.283

1 year ago

0.0.282

1 year ago

0.0.281

1 year ago

0.0.280

1 year ago

0.0.279

1 year ago

0.0.278

1 year ago

0.0.277

1 year ago

0.0.276

1 year ago

0.0.275

1 year ago

0.0.274

1 year ago

0.0.273

1 year ago

0.0.272

1 year ago

0.0.271

1 year ago

0.0.270

1 year ago

0.0.269

1 year ago

0.0.268

1 year ago

0.0.267

1 year ago

0.0.266

1 year ago

0.0.265

1 year ago

0.0.264

1 year ago

0.0.263

1 year ago

0.0.262

1 year ago

0.0.261

1 year ago

0.0.260

1 year ago

0.0.259

1 year ago

0.0.258

1 year ago

0.0.257

1 year ago

0.0.256

1 year ago

0.0.255

1 year ago

0.0.254

1 year ago

0.0.253

1 year ago

0.0.252

1 year ago

0.0.251

1 year ago

0.0.250

1 year ago

0.0.249

1 year ago

0.0.247

1 year ago

0.0.246

1 year ago

0.0.245

1 year ago

0.0.244

1 year ago

0.0.243

1 year ago

0.0.242

1 year ago

0.0.241

1 year ago

0.0.240

1 year ago

0.0.239

1 year ago

0.0.238

1 year ago

0.0.237

1 year ago

0.0.236

1 year ago

0.0.235

1 year ago

0.0.234

1 year ago

0.0.233

1 year ago

0.0.232

1 year ago

0.0.231

1 year ago

0.0.230

1 year ago

0.0.229

1 year ago

0.0.228

1 year ago

0.0.227

1 year ago

0.0.226

1 year ago

0.0.225

1 year ago

0.0.224

1 year ago

0.0.223

1 year ago

0.0.222

1 year ago

0.0.221

1 year ago

0.0.220

1 year ago

0.0.219

1 year ago

0.0.218

1 year ago

0.0.217

1 year ago

0.0.216

1 year ago

0.0.215

1 year ago

0.0.214

1 year ago

0.0.213

1 year ago

0.0.212

1 year ago

0.0.211

1 year ago

0.0.210

1 year ago

0.0.209

1 year ago

0.0.208

1 year ago

0.0.207

1 year ago

0.0.206

1 year ago

0.0.205

1 year ago

0.0.204

1 year ago

0.0.203

1 year ago

0.0.202

1 year ago

0.0.201

1 year ago

0.0.200

1 year ago

0.0.199

1 year ago

0.0.198

1 year ago

0.0.197

1 year ago

0.0.196

1 year ago

0.0.195

1 year ago

0.0.194

1 year ago

0.0.193

1 year ago

0.0.192

1 year ago

0.0.191

1 year ago

0.0.190

1 year ago

0.0.189

1 year ago

0.0.188

1 year ago

0.0.187

1 year ago

0.0.186

1 year ago

0.0.185

1 year ago

0.0.184

1 year ago

0.0.183

1 year ago

0.0.182

1 year ago

0.0.181

1 year ago

0.0.180

1 year ago

0.0.179

1 year ago

0.0.178

1 year ago

0.0.177

1 year ago

0.0.176

1 year ago

0.0.175

1 year ago

0.0.174

1 year ago

0.0.173

1 year ago

0.0.172

1 year ago

0.0.171

1 year ago

0.0.170

1 year ago

0.0.168

1 year ago

0.0.167

1 year ago

0.0.166

1 year ago

0.0.165

1 year ago

0.0.164

1 year ago

0.0.163

1 year ago

0.0.162

1 year ago

0.0.161

1 year ago

0.0.160

1 year ago

0.0.159

1 year ago

0.0.158

1 year ago

0.0.157

1 year ago

0.0.156

1 year ago

0.0.155

1 year ago

0.0.154

1 year ago

0.0.153

1 year ago

0.0.152

1 year ago

0.0.151

1 year ago

0.0.150

1 year ago

0.0.149

1 year ago

0.0.148

1 year ago

0.0.147

1 year ago

0.0.146

1 year ago

0.0.145

1 year ago

0.0.144

1 year ago

0.0.143

1 year ago

0.0.142

1 year ago

0.0.141

1 year ago

0.0.140

1 year ago

0.0.139

1 year ago

0.0.138

1 year ago

0.0.137

1 year ago

0.0.136

1 year ago

0.0.135

1 year ago

0.0.134

1 year ago

0.0.133

1 year ago

0.0.132

1 year ago

0.0.131

1 year ago

0.0.130

1 year ago

0.0.129

1 year ago

0.0.128

1 year ago

0.0.127

1 year ago

0.0.126

1 year ago

0.0.125

1 year ago

0.0.124

1 year ago

0.0.123

1 year ago

0.0.122

1 year ago

0.0.121

1 year ago

0.0.120

1 year ago

0.0.119

1 year ago

0.0.118

1 year ago

0.0.117

1 year ago

0.0.116

1 year ago

0.0.115

1 year ago

0.0.114

1 year ago

0.0.113

1 year ago

0.0.112

1 year ago

0.0.111

1 year ago

0.0.110

1 year ago

0.0.109

1 year ago

0.0.108

1 year ago

0.0.107

1 year ago

0.0.106

1 year ago

0.0.105

1 year ago

0.0.104

1 year ago

0.0.103

1 year ago

0.0.102

1 year ago

0.0.101

1 year ago

0.0.100

1 year ago

0.0.99

1 year ago

0.0.98

1 year ago

0.0.97

1 year ago

0.0.96

1 year ago

0.0.95

1 year ago

0.0.94

1 year ago

0.0.93

1 year ago

0.0.92

1 year ago

0.0.91

1 year ago

0.0.90

1 year ago

0.0.89

1 year ago

0.0.88

1 year ago

0.0.87

1 year ago

0.0.86

1 year ago

0.0.85

1 year ago

0.0.84

1 year ago

0.0.83

1 year ago

0.0.82

1 year ago

0.0.81

1 year ago

0.0.80

1 year ago

0.0.79

1 year ago

0.0.78

1 year ago

0.0.77

1 year ago

0.0.76

1 year ago

0.0.75

1 year ago

0.0.74

1 year ago

0.0.73

1 year ago

0.0.72

1 year ago

0.0.71

1 year ago

0.0.70

1 year ago

0.0.69

1 year ago

0.0.68

1 year ago

0.0.67

1 year ago

0.0.66

1 year ago

0.0.65

1 year ago

0.0.64

1 year ago

0.0.63

1 year ago

0.0.62

1 year ago

0.0.61

1 year ago

0.0.60

1 year ago

0.0.59

1 year ago

0.0.58

1 year ago

0.0.56

1 year ago

0.0.55

1 year ago

0.0.54

1 year ago

0.0.53

1 year ago

0.0.52

1 year ago

0.0.51

1 year ago

0.0.50

1 year ago

0.0.49

1 year ago

0.0.48

1 year ago

0.0.47

1 year ago

0.0.46

1 year ago

0.0.45

1 year ago

0.0.44

1 year ago

0.0.43

1 year ago

0.0.42

1 year ago

0.0.41

1 year ago

0.0.40

1 year ago

0.0.39

1 year 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