name: release-workflow

on:
  push:
    branches:
      - "master"
      - "[0-9].[0-9]*"

jobs:
  config:
    runs-on: "ubuntu-22.04"
    outputs:
      has-secrets: ${{ steps.check.outputs.has-secrets }}
    steps:
      - name: "Check for secrets"
        id: check
        shell: bash
        run: |
          if [ -n "${{ (secrets.NPM_TOKEN != '' && secrets.GH_PERSONAL_ACCESS_TOKEN != '') || '' }}" ]; then
            echo "has-secrets=1" >> "$GITHUB_OUTPUT"
          fi

  build:
    needs: config
    if: needs.config.outputs.has-secrets
    name: Bump version and publish package(s)

    runs-on: ubuntu-22.04

    strategy:
      matrix:
        node-version: [18]

    steps:
      - uses: actions/checkout@v4
        with:
          # pulls all commits (needed for lerna / semantic release to correctly version)
          fetch-depth: 0
      - name: Get tags and filter trigger tags
        run: |
          if ! git fetch --depth=1 origin "+refs/tags/*:refs/tags/*"; then
            echo "::notice title=Workflow skipped::No tags present in repository"
            exit
          fi
          echo "HAS_TAGS=1" >> $GITHUB_ENV"
          git fetch --prune --unshallow
          git tag -d `git tag | grep -E '^trigger-'`

      - name: Use Node.js ${{ matrix.node-version }}
        if: env.HAS_TAGS
        uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}

      - name: Cache npm
        if: env.HAS_TAGS
        uses: actions/cache@v4
        with:
          path: ~/.npm # npm cache files are stored in `~/.npm` on Linux/macOS
          key: ${{ runner.OS }}-node-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.OS }}-node-
            ${{ runner.OS }}-

      - name: Get npm cache directory path
        if: env.HAS_TAGS
        id: npm-cache-dir-path
        run: echo "dir=$(npm config get cache)" >> $GITHUB_OUTPUT
      - name: Cache npm
        if: env.HAS_TAGS
        uses: actions/cache@v4
        id: npm-cache # use this to check for `cache-hit` (`steps.npm-cache.outputs.cache-hit != 'true'`)
        with:
          path: ${{ steps.npm-cache-dir-path.outputs.dir }}
          key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-npm-

      - name: Install dependencies
        if: env.HAS_TAGS
        working-directory: ./superset-frontend
        run: npm ci
      - name: Run unit tests
        if: env.HAS_TAGS
        working-directory: ./superset-frontend
        run: npm run test -- plugins packages
      - name: Build packages
        if: env.HAS_TAGS
        working-directory: ./superset-frontend
        run: npm run plugins:build

      - name: Configure npm and git
        if: env.HAS_TAGS
        run: |
          echo "@superset-ui:registry=https://registry.npmjs.org/" > .npmrc
          echo "registry=https://registry.npmjs.org/" >> .npmrc
          echo "//registry.npmjs.org/:_authToken=\${NPM_TOKEN}" >> $HOME/.npmrc 2> /dev/null
          npm whoami
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
        env:
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
          GITHUB_TOKEN: ${{ github.token }}

      - name: Bump version and publish package(s)
        if: env.HAS_TAGS
        working-directory: ./superset-frontend
        run: |
          git tag -d `git tag | grep -E '^trigger-'`
          npm run plugins:release-from-tag
        env:
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
          GITHUB_TOKEN: ${{ github.token }}
          GH_TOKEN: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }}
