# 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 test

  # 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"


  build:
    name: Build dist files
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    needs: [test]
    steps:
      - name: Setup Node.js environment
        uses: actions/setup-node@v2.1.1
      - name: Checkout Dropzone
        uses: actions/checkout@v2
      - run: yarn install
      - name: Read package.json version
        uses: pchynoweth/action-get-npm-version@1.0.1
        id: package_version
      - name: Inject package version into javascript file
        uses: jacobtomlinson/gha-find-replace@master
        id: version_replace
        with:
          include: "src/dropzone.js"
          find: "version = \"dev\""
          replace: "version = \"${{ steps.package_version.outputs.version }}\""
      - name: Abort if version wasn't replaced
        if: steps.version_replace.outputs.modifiedFiles != 1
        run: exit 1
      - run: yarn build
      - name: Upload dist folder in case of release
        uses: actions/upload-artifact@v2
        with:
          name: dist
          path: dist

  release:
    name: Create GitHub release
    runs-on: ubuntu-latest
    needs: [build, 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


  npm-publish:
    name: Publish new version to npm
    runs-on: ubuntu-latest
    needs: [build,release]
    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 }}

  trigger-remote-releases:
    name: Dispatch the "new-release" event to packagist and bower repo
    runs-on: ubuntu-latest
    needs: [tag,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  }}"}'

