name: ci-subaction

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

on:
  workflow_dispatch:
  schedule:
    - cron: '0 10 * * *'
  push:
    branches:
      - 'master'
      - 'releases/v*'
    tags:
      - 'v*'
    paths:
      - '.github/workflows/ci-subaction.yml'
      - 'subaction/**'
      - 'test/**'
  pull_request:
    paths:
      - '.github/workflows/ci-subaction.yml'
      - 'subaction/**'
      - 'test/**'

jobs:
  list-targets:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        include:
          -
            testdir: group
            expected: >
              ["t1","t2"]
          -
            testdir: group-matrix
            target: validate
            expected: >
              ["lint-default","lint-labs","lint-nydus","lint-proto","lint-yaml","validate-doctoc","validate-vendor"]
          -
            testdir: multi-files
            files: |
              docker-bake.json
              docker-bake.hcl
            expected: >
              ["v1-tag","v2-tag"]
    steps:
      -
        name: Checkout
        uses: actions/checkout@v5
      -
        name: Matrix gen
        id: gen
        uses: ./subaction/list-targets
        with:
          workdir: ./test/${{ matrix.testdir }}
          files: ${{ matrix.files }}
          target: ${{ matrix.target }}
      -
        name: Check output
        uses: actions/github-script@v7
        env:
          INPUT_TARGETS: ${{ steps.gen.outputs.targets }}
          INPUT_EXPECTED: ${{ matrix.expected }}
        with:
          script: |
            const targets = JSON.stringify(JSON.parse(core.getInput('targets')));
            const expected = JSON.stringify(JSON.parse(core.getInput('expected')));
            if (targets !== expected) {
              throw new Error(`Targets do not match expected values: ${targets} != ${expected}`);
            } else {
              core.info(`✅`);
            }

  matrix:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        include:
          -
            testdir: group
            expected: >
              [{"target":"t1"},{"target":"t2"}]
          -
            testdir: group-matrix
            target: validate
            expected: >
              [{"target":"lint-default"},{"target":"lint-labs"},{"target":"lint-nydus"},{"target":"lint-proto"},{"target":"lint-yaml"},{"target":"validate-doctoc"},{"target":"validate-vendor"}]
          -
            testdir: group-with-platform
            target: validate
            expected: >
              [{"target":"lint"},{"target":"lint-gopls"},{"target":"validate-docs"},{"target":"validate-vendor"}]
          -
            testdir: group-with-platform
            target: validate
            fields: platforms
            expected: >
              [{"target":"lint","platforms":"darwin/amd64"},{"target":"lint","platforms":"darwin/arm64"},{"target":"lint","platforms":"linux/amd64"},{"target":"lint","platforms":"linux/arm64"},{"target":"lint","platforms":"linux/s390x"},{"target":"lint","platforms":"linux/ppc64le"},{"target":"lint","platforms":"linux/riscv64"},{"target":"lint","platforms":"windows/amd64"},{"target":"lint","platforms":"windows/arm64"},{"target":"lint-gopls","platforms":"darwin/amd64"},{"target":"lint-gopls","platforms":"darwin/arm64"},{"target":"lint-gopls","platforms":"linux/amd64"},{"target":"lint-gopls","platforms":"linux/arm64"},{"target":"lint-gopls","platforms":"linux/s390x"},{"target":"lint-gopls","platforms":"linux/ppc64le"},{"target":"lint-gopls","platforms":"linux/riscv64"},{"target":"lint-gopls","platforms":"windows/amd64"},{"target":"lint-gopls","platforms":"windows/arm64"},{"target":"validate-docs"},{"target":"validate-vendor"}]
          -
            testdir: group-with-platform
            target: validate
            fields: platforms,dockerfile
            expected: >
               [{"target":"lint","dockerfile":"./hack/dockerfiles/lint.Dockerfile"},{"target":"lint","dockerfile":"./hack/dockerfiles/lint.Dockerfile","platforms":"darwin/amd64"},{"target":"lint","dockerfile":"./hack/dockerfiles/lint.Dockerfile","platforms":"darwin/arm64"},{"target":"lint","dockerfile":"./hack/dockerfiles/lint.Dockerfile","platforms":"linux/amd64"},{"target":"lint","dockerfile":"./hack/dockerfiles/lint.Dockerfile","platforms":"linux/arm64"},{"target":"lint","dockerfile":"./hack/dockerfiles/lint.Dockerfile","platforms":"linux/s390x"},{"target":"lint","dockerfile":"./hack/dockerfiles/lint.Dockerfile","platforms":"linux/ppc64le"},{"target":"lint","dockerfile":"./hack/dockerfiles/lint.Dockerfile","platforms":"linux/riscv64"},{"target":"lint","dockerfile":"./hack/dockerfiles/lint.Dockerfile","platforms":"windows/amd64"},{"target":"lint","dockerfile":"./hack/dockerfiles/lint.Dockerfile","platforms":"windows/arm64"},{"target":"lint-gopls","dockerfile":"./hack/dockerfiles/lint.Dockerfile"},{"target":"lint-gopls","dockerfile":"./hack/dockerfiles/lint.Dockerfile","platforms":"darwin/amd64"},{"target":"lint-gopls","dockerfile":"./hack/dockerfiles/lint.Dockerfile","platforms":"darwin/arm64"},{"target":"lint-gopls","dockerfile":"./hack/dockerfiles/lint.Dockerfile","platforms":"linux/amd64"},{"target":"lint-gopls","dockerfile":"./hack/dockerfiles/lint.Dockerfile","platforms":"linux/arm64"},{"target":"lint-gopls","dockerfile":"./hack/dockerfiles/lint.Dockerfile","platforms":"linux/s390x"},{"target":"lint-gopls","dockerfile":"./hack/dockerfiles/lint.Dockerfile","platforms":"linux/ppc64le"},{"target":"lint-gopls","dockerfile":"./hack/dockerfiles/lint.Dockerfile","platforms":"linux/riscv64"},{"target":"lint-gopls","dockerfile":"./hack/dockerfiles/lint.Dockerfile","platforms":"windows/amd64"},{"target":"lint-gopls","dockerfile":"./hack/dockerfiles/lint.Dockerfile","platforms":"windows/arm64"},{"target":"validate-docs","dockerfile":"./hack/dockerfiles/docs.Dockerfile"},{"target":"validate-vendor","dockerfile":"./hack/dockerfiles/vendor.Dockerfile"}]
    steps:
      -
        name: Checkout
        uses: actions/checkout@v5
      -
        name: Matrix gen
        id: gen
        uses: ./subaction/matrix
        with:
          workdir: ./test/${{ matrix.testdir }}
          target: ${{ matrix.target }}
          fields: ${{ matrix.fields }}
      -
        name: Check output
        uses: actions/github-script@v7
        env:
          INPUT_MATRIX: ${{ steps.gen.outputs.matrix }}
          INPUT_EXPECTED: ${{ matrix.expected }}
        with:
          script: |
            const matrix = JSON.stringify(JSON.parse(core.getInput('matrix')));
            const expected = JSON.stringify(JSON.parse(core.getInput('expected')));
            if (matrix !== expected) {
              throw new Error(`Matrix do not match expected values: ${matrix} != ${expected}`);
            } else {
              core.info(`✅`);
            }
