name: Merge master into develop
on:
  push:
    branches:
      - master
jobs:
  merge-master-into-develop:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
          token: ${{ secrets.DEVELOP_PUSH_TOKEN }}
      - name: Set committer info
        run: |
          git config --local user.email "$(git log --format='%ae' HEAD^!)"
          git config --local user.name "$(git log --format='%an' HEAD^!)"
      - name: Checkout develop branch
        run: git checkout develop
      - name: Check for merge conflict
        id: check-conflict
        run: echo "::set-output name=merge_conflict::$(git merge-tree $(git merge-base HEAD master) master HEAD | egrep '<<<<<<<')"
      - name: Merge master into develop
        run: git merge master
        if: ${{ !steps.check-conflict.outputs.merge_conflict }}
      - name: Push
        run: git push
        if: ${{ !steps.check-conflict.outputs.merge_conflict }}
      - name: Checkout master
        run: git checkout master
        if: ${{ steps.check-conflict.outputs.merge_conflict }}
      - name: Determine name of new branch
        id: gen-names
        run: |
          echo "::set-output name=sha::$(git rev-parse --short HEAD)"
          echo "::set-output name=branch_name::$(git rev-parse --short HEAD)-master-into-develop"
        if: ${{ steps.check-conflict.outputs.merge_conflict }}
      - name: Create a copy of master on a new branch
        run: git checkout -b ${{ steps.gen-names.outputs.branch_name }} master
        if: ${{ steps.check-conflict.outputs.merge_conflict }}
      - name: Push branch to remote
        run: git push origin ${{ steps.gen-names.outputs.branch_name }}
        if: ${{ steps.check-conflict.outputs.merge_conflict }}
      - name: Create Pull Request
        uses: actions/github-script@v3
        with:
          script: |
            const pull = await github.pulls.create({
              owner: context.repo.owner,
              repo: context.repo.repo,
              base: 'develop',
              head: '${{ steps.gen-names.outputs.branch_name }}',
              title: 'chore: merge master (${{ steps.gen-names.outputs.sha }}) into develop',
              body: `There was a merge conflict when trying to automatically merge master into develop. Please resolve the conflict and complete the merge.

              DO NOT SQUASH AND MERGE

              @${context.actor}`,
              maintainer_can_modify: true,
            })
            await github.pulls.requestReviewers({
              owner: context.repo.owner,
              repo: context.repo.repo,
              pull_number: pull.data.number,
              reviewers: [context.actor],
            })
            await github.issues.addLabels({
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number: pull.data.number,
              labels: ['auto-merge'],
            })
        if: ${{ steps.check-conflict.outputs.merge_conflict }}
