name: dist

on:
  push:
    branches: [ master ]
  pull_request_target:
    branches: [ master ]
    paths:
      - '**.js'
      - 'yarn.lock'

jobs:
  dist:
    runs-on: ubuntu-latest
    name: build distributable action
    defaults:
      run:
        shell: bash
    steps:
      - name: determine source repository (push)
        if: ${{ github.event_name == 'push' }}
        run: echo "::set-env name=repository::${{ github.repository }}"
      - name: determine source repository (PR)
        if: ${{ github.event_name != 'push' }}
        run: echo "::set-env name=repository::${{ github.event.pull_request.head.repo.full_name }}"
      - uses: actions/checkout@v2.3.2
        with:
          ref: ${{ github.head_ref }}
          repository: ${{ env.repository }}
          token: ${{ secrets.PAT }}
      - uses: bahmutov/npm-install@v1.4.3
      - name: running ncc
        run: yarn run ncc build action.js
      - name: creating pull request
        if: ${{ github.head_ref == '' }}
        uses: peter-evans/create-pull-request@v3.3.0
        with:
          commit-message: 'chore(dist): update the distributed action script'
          title: 'chore(dist): Update distributed action script'
          body: |
            This pull requests updates the distributed version of the action script as described in the [GitHub Action docs](https://docs.github.com/en/actions/creating-actions/creating-a-javascript-action).
            Every update on the main branche will cause an update of this pull request. It only needs to be merged once a release is due.
          labels: |
            chore
          branch: dist
      - name: committing changed files
        if: ${{ github.head_ref != '' }}
        uses: stefanzweifel/git-auto-commit-action@v4.5.0
        with:
          commit_message: 'chore(dist): update the distributed action script'
          file_pattern: 'dist/index.js'
