$dsl: ./pipeline.dsl.yml

name: pr-review
description: Review a pull request, propose test cases for it, and sync them to Testomat.io.
target: pr

$vars:
  TESTOMATIO: { optional: true, secret: true }

$functions:
  slug(text)!: lower case(replace(replace(text, "[^A-Za-z0-9]+", "-"), "^-+|-+$", ""))

prerequisites:
  code_changed:
    $value!changes:
      min_files: 3
      ignore: ["**/*.md", "**/*.lock", "docs/**"]
    message: Fewer than 3 code files changed.
  testcases_needed:
    $value!: vars.CREATE_TESTCASES != null
    message: 'No test cases created. Enable test case creation via CREATE_TESTCASES env var'
  testomatio:
    $value!: vars.TESTOMATIO != null
    message: No Testomat.io project. Set TESTOMATIO.

when!: prerequisites.code_changed

env:
  # feature/add-login → feature-add-login
  TESTOMATIO_BRANCH!: slug(vars.GIT_BRANCH)

stages:
  - id: requirements
    title: Requirements
    emoji: 📋
    session: reviewer
    task: Work out what this pull request is meant to do. /qa-pr-requirements-analyzer

  - id: review
    title: Review
    emoji: 🔍
    session: reviewer
    task: Review this pull request as a QA engineer against requirements.md. /qa-review-pr
    inputs: [requirements.md]

  - id: test-cases
    title: Test cases
    emoji: 📝
    session: writer
    when!: prerequisites.testcases_needed
    task: |
      If there is linked issue and it was already reviewed by Testeiya and have linked tests - skip this step
      Write test cases for this pull request from requirements.md and review.md. /qa-write-test-cases
      Put them in test-cases/, one markdown file per suite.
    inputs: [requirements.md, review.md]
    outputs: [test-cases/]

  - id: sync
    title: Sync to Testomat.io
    emoji: 🔄
    when!: prerequisites.testomatio and prerequisites.testcases_needed
    optional: true
    each_file: test-cases/*.md
    task: |
      Sync the test cases in {{ vars.ITEM }} to Testomat.io. /sync-test-cases-with-tms

$tests:
  - about: The branch names the Testomat.io branch
    vars: { GIT_BRANCH: feature/add-login, CREATE_TESTCASES: "1" }
    expect: { env.TESTOMATIO_BRANCH: feature-add-login, prerequisites.testcases_needed: true, stages.sync.when: false }
  - about: Test cases sync only when asked for and a project is set
    vars: { GIT_BRANCH: main, CREATE_TESTCASES: "1", TESTOMATIO: key }
    expect: { stages.sync.when: true }
