name: Storybook Visual Tests

on:
  pull_request:
    types: [opened, synchronize, reopened]
    branches: [master]
  # we need to rerun visual tests after merging to master to set correct commit sha
  push:
    branches: [master]

env:
  GITHUB_TOKEN: ${{ secrets.TOPTAL_DEVBOT_TOKEN }}
  REPOSITORY_NAME: ${{ github.event.repository.name }}
  HAPPO_API_KEY: ${{ secrets.HAPPO_API_KEY }}
  HAPPO_API_SECRET: ${{ secrets.HAPPO_API_SECRET }}

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

jobs:
  storybook-tests:
    name: Storybook:Visual Tests
    runs-on: ubuntu-latest
    steps:
      - name: Checkout project
        uses: actions/checkout@v3

      - name: Setup Node.js
        uses: actions/setup-node@v6
        with:
          node-version: 20

      - uses: toptal/davinci-github-actions/pnpm-install@v20.0.0

      - name: Visual Tests
        run: pnpm storybook:test:visual:ci
        env:
          HAPPO_PROJECT: ${{ env.REPOSITORY_NAME }}
          HAPPO_API_KEY: ${{ env.HAPPO_API_KEY }}
          HAPPO_API_SECRET: ${{ env.HAPPO_API_SECRET }}
