# This workflow will do a clean install of node dependencies, build the source
# code and run tests across different versions of node.
#
# In case there is a new version in package.json and it's the main branch, it
# will also tag and release this version.

name: Test and Release

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  test:
    name: Test
    runs-on: ubuntu-latest
    steps:
      - name: Setup Node.js environment
        uses: actions/setup-node@v2.1.1
      - name: Checkout Dropzone
        uses: actions/checkout@v2

      - run: yarn install
      - run: yarn build
      - run: yarn test

      - uses: cypress-io/github-action@v2
        with:
          start: yarn start-test-server
          wait-on: "http://localhost:8888"
          wait-on-timeout: 5

      - name: Upload dist folder in case of release
        uses: actions/upload-artifact@v2
        with:
          name: dist
          path: dist

  # Auto tag when the package.json version has changed. A different job will see
  # this tag, publish it, and create a release for it.
  tag:
    name: Tag if package.json version increased
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    needs: [test]
    outputs:
      tagname: ${{ steps.auto_tag.outputs.tagname }}
    steps:
      - name: Checkout Dropzone
        uses: actions/checkout@v2
      - name: Auto tag
        id: auto_tag
        uses: Klemensas/action-autotag@1.2.3
        env:
          GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
        with:
          tag_prefix: v
          tag_message: "Please check the changelog for a list of changes"

  npm-publish:
    name: Publish new version to npm
    runs-on: ubuntu-latest
    needs: [tag]
    if: github.ref == 'refs/heads/main' && needs.tag.outputs.tagname != null
    steps:
      - name: Checkout Dropzone
        uses: actions/checkout@v2
      - name: Download dist folder
        uses: actions/download-artifact@v2
        with:
          name: dist
          path: dist
      - name: Publish
        uses: JS-DevTools/npm-publish@v1
        with:
          token: ${{ secrets.NPM_TOKEN }}

  release:
    name: Create GitHub release
    runs-on: ubuntu-latest
    needs: [tag]
    if: github.ref == 'refs/heads/main' && needs.tag.outputs.tagname != null
    steps:
      - name: Checkout Dropzone
        uses: actions/checkout@v2
      - name: Download dist folder
        uses: actions/download-artifact@v2
        with:
          name: dist
          path: dist
      - name: Compress dist folder
        run: zip dist -r dist
      - name: Release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: "${{ needs.tag.outputs.tagname  }}"
          release_name: "Release ${{ needs.tag.outputs.tagname  }}"
          body: "Please check the [changelog](https://github.com/dropzone/dropzone/blob/${{ github.sha }}/CHANGELOG.md) for a list of changes"
          draft: false
          prerelease: false
      - name: Upload Release Asset
        id: upload-release-asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./dist.zip
          asset_name: dist.zip
          asset_content_type: application/zip

  trigger-remote-releases:
    name: Dispatch the "new-release" event to packagist and bower repo
    runs-on: ubuntu-latest
    needs:
      - tag # this is necessary otherwise the dispatch action hasn't access to it
      - release
    steps:
      - name: Dispatch "new-release" event to the dropzone-packagist repo.
        uses: peter-evans/repository-dispatch@v1
        with:
          token: ${{ secrets.DRONE_PAT }}
          repository: dropzone/dropzone-packagist
          event-type: new-release
          client-payload: '{"releaseTag": "${{ needs.tag.outputs.tagname  }}"}'
      - name: Dispatch "new-release" event to the dropzone-bower repo.
        uses: peter-evans/repository-dispatch@v1
        with:
          token: ${{ secrets.DRONE_PAT }}
          repository: dropzone/dropzone-bower
          event-type: new-release
          client-payload: '{"releaseTag": "${{ needs.tag.outputs.tagname  }}"}'
