$dsl: ./pipeline.dsl.yml

name: issue-test-cases
description: Turn an issue or epic into test cases, and sync them to Testomat.io.
target: issue

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

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

prerequisites:
  issue:
    $value!: vars.ISSUE_URL != null
    message: No issue. Set ISSUE_URL to the issue or epic to cover.
  testomatio:
    $value!: vars.TESTOMATIO != null
    message: No Testomat.io project. Set TESTOMATIO API Key to import test cases

when!: prerequisites.issue

env:
  # https://github.com/org/repo/issues/42 → org-repo-issues-42
  TESTOMATIO_BRANCH!: 'if matches(vars.ISSUE_URL, "^[a-z]+://") then slug(replace(vars.ISSUE_URL, "^[a-z]+://[^/]*|[?#].*$", "")) else null'

stages:
  - id: requirements
    title: Requirements
    emoji: 📋
    session: analyst
    task: Review the requirements in {{ vars.ISSUE_URL }}. /qa-requirement-reviewer

  - id: test-cases
    title: Test cases
    emoji: 📝
    session: writer
    task: |
      Write test cases from requirements.md. /qa-write-test-cases
      Put them in test-cases/, one markdown file per suite.
    inputs: [requirements.md]
    outputs: [test-cases/]

  - id: sync
    title: Sync to Testomat.io
    emoji: 🔄
    when!: prerequisites.testomatio
    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 issue names the Testomat.io branch
    vars: { ISSUE_URL: "https://github.com/org/repo/issues/42" }
    expect: { when: true, env.TESTOMATIO_BRANCH: org-repo-issues-42, stages.sync.when: false }
  - about: An issue that is no URL names no branch
    vars: { ISSUE_URL: PROJ-42 }
    expect: { when: true, env.TESTOMATIO_BRANCH: null }
  - about: Without an issue nothing runs
    vars: {}
    expect: { when: false, env.TESTOMATIO_BRANCH: null }
