# this will tag PRs that are ready for release and automerge them
name: Automerge Pull Requests
on:
  # issue_comment:
  #   types: [created]
  pull_request:
    branches: [master, next, alpha, beta]
    types: [labeled, closed]
jobs:
  automerge:
    name: automerge pr
    runs-on: ubuntu-latest
    env:
      GITHUB_TOKEN: ${{ secrets.TRILOM_BOT_TOKEN }}
      pr_number: ${{ format('{0}{1}', github.event.pull_request.number, github.event.issue.number) }}
# if event type is non fork PR or comment on PR from trilom with '/release'
    if: >-
      (
        github.event_name == 'pull_request' 
        && github.event.pull_request.head.repo.full_name == github.repository
        && contains(github.event.pull_request.labels.*.name, 'pretty')
        && contains(github.event.pull_request.labels.*.name, 'builds')
        && contains(github.event.pull_request.labels.*.name, 'tested-unit')
        && contains(github.event.pull_request.labels.*.name, 'tested-integration')
        && contains(github.event.pull_request.labels.*.name, 'lintdogged')
        && ! contains(github.event.pull_request.labels.*.name, 'automated merge')
        && ! contains(github.event.pull_request.labels.*.name, 'hold merge')
      ) || (
        github.event_name == 'issue_comment' 
        && github.event.issue.pull_request != '' 
        && contains(github.event.comment.body, '/release') 
        && github.actor == 'trilom'
        && contains(github.event.issue.labels.*.name, 'pretty')
        && contains(github.event.issue.labels.*.name, 'builds')
        && contains(github.event.issue.labels.*.name, 'tested-unit')
        && contains(github.event.issue.labels.*.name, 'tested-integration')
        && contains(github.event.issue.labels.*.name, 'lintdogged')
        && ! contains(github.event.issue.labels.*.name, 'automated merge')
        && ! contains(github.event.issue.labels.*.name, 'hold merge'))
    steps:
      - name: if pretty, builds, tested merge automerge pr
# if pretty, builds, and tested labels then merge
        uses: pascalgn/automerge-action@v0.7.5
        env:
          GITHUB_TOKEN: ${{ env.GITHUB_TOKEN }}
          MERGE_METHOD: merge
# this breaks the /release on issue_comment portion unless I get the head.ref from github-script
          MERGE_COMMIT_MESSAGE: 'Auto merge from ${{ github.event.pull_request.head.ref }} PR#{pullRequest.number}: {pullRequest.title}'
          UPDATE_METHOD: merge
          MERGE_LABELS: 'pretty,builds,tested-unit,tested-integration,lintdogged'
          UPDATE_LABELS: ''
# if failure, get payload of PR and notify
      - name: if failure, get pr payload
        uses: actions/github-script@0.8.0
        id: pr_json
        if: failure()
        with:
          github-token: ${{env.GITHUB_TOKEN}}
          script: |
            const result = await github.pulls.get({
              owner: '${{ github.repository }}'.split('/')[0],
              repo: '${{ github.repository }}'.split('/')[1],
              pull_number: ${{ env.pr_number }}
            })
            return result.data;
      - name: if failure, set pr payload outputs
        if: failure()
        id: pr
        run: |
          echo '${{ steps.pr_json.outputs.result }}' > pr.json
          echo "::set-output name=user::$( jq -r '.user.login' pr.json )"
          echo "::set-output name=head::$( jq -r '.head.repo.full_name' pr.json )"
          echo "::set-output name=head_url::$( jq -r '.head.repo.html_url' pr.json )"
          echo "::set-output name=base::$( jq -r '.base.repo.full_name' pr.json )"
          echo "::set-output name=base_url::$( jq -r '.base.repo.html_url' pr.json )"
      - name: if failure, notify
        uses: peter-evans/create-or-update-comment@v1
        if: failure()
        with:
          token: ${{ env.GITHUB_TOKEN }}
          issue-number: ${{ env.pr_number }}
          body: |
            @${{ steps.pr.outputs.user }}, @trilom - it appears that there was an issue with the merge.

            Head Repo/Branch: **[${{ steps.pr.outputs.head }}]**(${{ steps.pr.outputs.head_url }}) merge into **[${{ steps.pr.outputs.base }}]**(${{ steps.pr.outputs.base_url }})
            
            ## Event JSON
            ```json
              ${{ toJSON(steps.pr_json.outputs.result)}}
            ```
      - uses: actions/github-script@0.6.0
        with:
          github-token: ${{secrets.GITHUB_TOKEN}}
          script: |
            github.issues.addLabels({owner: context.repo.owner, repo: context.repo.repo, issue_number: context.issue.number,
              labels: ['automated merge']
            })