name: CI

on:
  push:
  workflow_dispatch:

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

env:
  AZURE_STORAGE_ACCOUNT: go1review
  NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
  AZURE_STORAGE_KEY: "${{ secrets.AZURE_STORAGE_KEY }}"

jobs:
  create_release_id:
    uses: go1com/gh-actions/.github/workflows/ci-create-release-id.yml@main

  build:
    runs-on: ubuntu-latest
    timeout-minutes: 60
    needs: create_release_id
    steps:
      - name: Set up Node
        uses: actions/setup-node@v4
        with:
          node-version: "16.x"

      - name: Checkout code
        uses: actions/checkout@v4

      - name: Build
        run: |
          yarn install
          yarn npm:build

      - name: cache save
        id: cache
        uses: actions/cache/save@v4
        with:
          key: go1d-${{ needs.create_release_id.outputs.tag }}
          enableCrossOsArchive: true
          path: |
            node_modules
            build

  test-linting:
    if: >-
      ${{
        always()
        && (github.ref_type == 'branch')
      }}
    runs-on: ubuntu-latest
    timeout-minutes: 60
    needs:
      - create_release_id
      - build
    steps:
      - name: Set up Node
        uses: actions/setup-node@v4
        with:
          node-version: "16.x"

      - name: Checkout code
        uses: actions/checkout@v4

      - name: cache restore
        uses: actions/cache/restore@v4
        with:
          key: go1d-${{ needs.create_release_id.outputs.tag }}
          enableCrossOsArchive: true
          fail-on-cache-miss: true
          path: |
            node_modules
            build

      - run: yarn run eslint

  test-unit:
    if: >-
      ${{
        always()
        && (needs.build.result == 'success' || needs.build.result == 'skipped')
        && (github.ref_type == 'branch')
      }}
    runs-on: ubuntu-latest
    timeout-minutes: 60
    needs:
      - create_release_id
      - build
    steps:
      - name: Set up Node
        uses: actions/setup-node@v4
        with:
          node-version: "16.x"

      - name: Checkout code
        uses: actions/checkout@v4

      - name: cache restore
        uses: actions/cache/restore@v4
        with:
          key: go1d-${{ needs.create_release_id.outputs.tag }}
          enableCrossOsArchive: true
          fail-on-cache-miss: true
          path: |
            node_modules
            build

      - run: |
          yarn test --no-cache --coverage

  review:
    if: >-
      ${{
        always()
        && (needs.build.result == 'success' || needs.build.result == 'skipped')
        && (github.ref_type == 'branch' && !(github.ref_name == 'master') && !(github.ref_name == 'main'))
      }}
    needs:
      - create_release_id
      - build
      - test-unit
    runs-on: ubuntu-latest
    timeout-minutes: 60
    env:
      CI_ENVIRONMENT_SLUG: "go1d/${{ github.ref_name }}"
    environment:
      name: go1d/${{ github.ref_name }}
      url: https://go1review.z8.web.core.windows.net/go1d/${{ github.ref_name }}/
    steps:
      - name: Set up Node
        uses: actions/setup-node@v4
        with:
          node-version: "16.x"

      - name: Checkout code
        uses: actions/checkout@v4

      - name: cache restore
        uses: actions/cache/restore@v4
        with:
          key: go1d-${{ needs.create_release_id.outputs.tag }}
          enableCrossOsArchive: true
          fail-on-cache-miss: true
          path: |
            node_modules
            build
      - run: |
          yarn storybook:build:ci
          az storage blob upload-batch -s ./dist_storybook -d '$web' --destination-path $CI_ENVIRONMENT_SLUG --overwrite

  npm-publish:
    runs-on: ubuntu-latest
    if: >-
      ${{
        always()
        && (needs.build.result == 'success' || needs.build.result == 'skipped')
        && (github.ref_type == 'branch' && (github.ref_name == 'master' || github.ref_name == 'main'))
      }}
    timeout-minutes: 60
    needs:
      - create_release_id
      - build
      - test-unit
    steps:
      - name: Set up Node
        uses: actions/setup-node@v4
        with:
          node-version: "16.x"
      - uses: actions/checkout@v4
      - name: cache restore
        uses: actions/cache/restore@v4
        with:
          key: go1d-${{ needs.create_release_id.outputs.tag }}
          enableCrossOsArchive: true
          fail-on-cache-miss: true
          path: |
            node_modules
            build

      - name: Setup git for push
        run: |
          git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git config --global user.name "github-actions[bot]"

      - name: Npm setup
        run: |
          npm config set scope go1d --global
          echo '//registry.npmjs.org/:_authToken=${NPM_TOKEN}'>.npmrc

      - name: Publish to NPM
        run: |
          rm readme.md
          mv npm-readme.md readme.md
          yarn changeset status || yarn changeset add --empty
          yarn changeset version
          git add package.json .changeset/ CHANGELOG.md
          git commit -m "[NPM-CI] Version packages [skip ci]"
          yarn changeset publish
          git push --follow-tags
