 # if a push comes in then this will test it for a release and create a release PR if needed
name: Push to release branches
on:
  push:
    branches: [master, next, alpha, beta]
    tags-ignore: ['**']
jobs:
  # semantic release an auto-merged branch to github package repo, npm, github actions
  release:
    name: Release to NPM, Github, Github Actions Marketplace
    runs-on: ubuntu-latest
    needs: [build, test-unit, test-integration, lintdog]
    if: >
      github.actor != 'semantic-release-bot'
      && ( (contains(github.event.head_commit.message, 'trilom/1.') 
        || contains(github.event.head_commit.message, 'trilom/2.'))
        && ! contains(github.event.head_commit.message, 'chore(release):'))
    env:
      GITHUB_TOKEN: ${{ secrets.TRILOM_BOT_TOKEN }}
      SEMANTIC_RELEASE_PACKAGE: '@${{ github.repository }}'
      NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
      SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - name: semantic-release
        uses: cycjimmy/semantic-release-action@v2
        id: semantic
        with:
          semantic_version: 15.14.0
          extra_plugins: |
            @semantic-release/git@7.0.18
            @semantic-release/changelog
            semantic-release-slack-bot
          dry_run: false
      - name: echo release outputs
        if: steps.semantic.outputs.new_release_published == 'true'
        run: |
          echo ${{ steps.semantic.outputs.new_release_version }}
          echo ${{ steps.semantic.outputs.new_release_major_version }}
          echo ${{ steps.semantic.outputs.new_release_minor_version }}
          echo ${{ steps.semantic.outputs.new_release_patch_version }}
      - name: Setup Node.js with GitHub Package Registry
        if: steps.semantic.outputs.new_release_published == 'true'
        uses: actions/setup-node@v1
        with:
          node-version: 12
          registry-url: 'https://npm.pkg.github.com'
          scope: trilom
      - name: Publish To GitHub Package Registry
        if: steps.semantic.outputs.new_release_published == 'true'
        run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{ env.GITHUB_TOKEN }}
  # create PR from release branch to master to prepare for release
  check-release:
    name: Check if we need to release
    runs-on: ubuntu-latest
    needs: [build, test-unit, test-integration, lintdog]
    if: >
      github.actor != 'semantic-release-bot'
      && ! contains(github.event.head_commit.message, 'trilom/1.')
      && ! contains(github.event.head_commit.message, 'trilom/2.')
      && ! contains(github.event.head_commit.message, 'chore(release):')
    env:
      GITHUB_TOKEN: ${{ secrets.TRILOM_BOT_TOKEN }}
      NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - name: commit format changes and create authors file
        run: |
          git config --local user.email "trilom-bot@trailmix.me"
          git config --local user.name "trilom-bot"
          yarn build
          yarn format
          git add -A
          git diff-index --quiet HEAD || git commit -m "Adding format changes 🤖" -a
          yarn build-release
          git add -A
          git diff-index --quiet HEAD || git commit -m "Adding release changes ⚙️" -a
          git log --format='%aN <%aE>%n%cN <%cE>' | sort -u > AUTHORS
          sed -i '/trilom-bot/d' AUTHORS
          sed -i '/semantic-release-bot/d' AUTHORS
          sed -i '/carnoco@gmail.com/d' AUTHORS
          sed -i '/GitHub <noreply@github.com>/d' AUTHORS
          sed -i '/dependabot/d' AUTHORS
          echo -e "\r\n$(date)" >> AUTHORS
          git add -A
          git diff-index --quiet HEAD || git commit -m "Updating AUTHORS 📓" -a
      # see if we need to release, if so create a automerge release PR and notify the original creator
      - name: semantic-release
        uses: cycjimmy/semantic-release-action@v2
        id: semantic
        env:
          SEMANTIC_RELEASE_PACKAGE: '@${{ github.repository }}'
          SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
        with:
          semantic_version: 15.14.0
          extra_plugins: |
            @semantic-release/git@7.0.18
            @semantic-release/changelog
            semantic-release-slack-bot
          dry_run: true
      - name: echo release outputs
        if: steps.semantic.outputs.new_release_published == 'true'
        run: |
          echo ${{ steps.semantic.outputs.new_release_version }}
          echo ${{ steps.semantic.outputs.new_release_major_version }}
          echo ${{ steps.semantic.outputs.new_release_minor_version }}
          echo ${{ steps.semantic.outputs.new_release_patch_version }}
      - name: push potential formatting changes since there is no release
        if: steps.semantic.outputs.new_release_published == 'false'
        run: |
          git config --local user.email "trilom-bot@trailmix.me"
          git config --local user.name "trilom-bot"
          git push -f https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git HEAD:${{ github.ref }}
      - name: get changed files and format for automerge PR body
        id: file_changes
        uses: trilom/file-changes-action@master
        if: steps.semantic.outputs.new_release_published == 'true'
        with:
          githubToken: ${{ env.GITHUB_TOKEN }}
          output: '_<br />&nbsp;&nbsp;_'
      - name: get original PR number
        uses: actions/github-script@0.6.0
        id: pr
        if: steps.semantic.outputs.new_release_published == 'true'
        with:
          github-token: ${{env.GITHUB_TOKEN}}
          result-encoding: string
          script: |
            const result = await github.repos.listPullRequestsAssociatedWithCommit({
              owner: context.payload.repository.owner.name,
              repo: context.payload.repository.name,
              commit_sha: context.payload.head_commit.id
            })
            if (result.data.length >= 1) {
              return result.data[0].number
            } else return 87
      - name: get original PR user
        uses: actions/github-script@0.6.0
        id: login
        if: steps.pr.outputs.result != 0 && steps.semantic.outputs.new_release_published == 'true'
        with:
          github-token: ${{env.GITHUB_TOKEN}}
          result-encoding: string
          script: |
            const result = await github.pulls.get({
              owner: context.payload.repository.owner.name,
              repo: context.payload.repository.name,
              pull_number: ${{ steps.pr.outputs.result }}
            })
            if (result.data.user === true && result.data.user.login === true) {
              return result.data.user.login
            } else return 'trilom';
      - name: create release PR
        id: create-pr
        uses: peter-evans/create-pull-request@v2
        if: steps.semantic.outputs.new_release_published == 'true'
        with:
          token: ${{ env.GITHUB_TOKEN }}
          commit-message: '${{ github.event.head_commit.message }}'
          committer: trilom-bot <trilom-bot@trailmix.me>
          author: ${{ steps.login.outputs.result }} <${{ steps.login.outputs.result }}@users.noreply.github.com>
          title: 'releases/v${{ steps.semantic.outputs.new_release_version }} [@${{ steps.login.outputs.result }}] - ${{ github.event.head_commit.message }}'
          body: |
            # @${{ steps.login.outputs.result }} would like to merge into file-changes-action
            [**compare link**](${{ github.event.compare }})

            ## Commits
            ```json
            ${{ toJSON(github.event.commits)}}
            ```
            
            ## Files
            
            &nbsp;&nbsp;_${{ steps.file_changes.outputs.files}}_
            
            ## Files modified
            
            &nbsp;&nbsp;_${{ steps.file_changes.outputs.files_modified}}_
            
            ## Files added
            
            &nbsp;&nbsp;_${{ steps.file_changes.outputs.files_added}}_
            
            ## Files removed
            
            &nbsp;&nbsp;_${{ steps.file_changes.outputs.files_removed}}_
          labels: 'automated pr'
          assignees: '${{ steps.login.outputs.result }},trilom'
          reviewers: trilom
          branch: '${{ steps.semantic.outputs.new_release_version }}'
      - name: notify initial commiter of change
        uses: peter-evans/create-or-update-comment@v1
        if: steps.login.outputs.result != '' && steps.semantic.outputs.new_release_published == 'true'
        with:
          token: ${{ env.GITHUB_TOKEN }}
          issue-number: ${{ steps.pr.outputs.result }}
          body: |
            Hey @${{ steps.login.outputs.result }},

            This merge has triggered a release, hurray!
            
            [Here you can follow the release.](https://github.com/trilom/file-changes-action/pull/${{ steps.create-pr.outputs.pr_number }})

            Please use this new **Pull Request** if there are any issues to communicate further.

            Thanks!
      # - uses: actions/github-script@0.6.0
      #   if: steps.create-pr.outputs.pr_number != '' && steps.semantic.outputs.new_release_published == 'true'
      #   with:
      #     github-token: ${{ secrets.TRILOM_BOT_TOKEN }}
      #     script: |
      #       github.issues.addLabels({owner: context.repo.owner, repo: context.repo.repo, issue_number: ${{ steps.create-pr.outputs.pr_number }},
      #         labels: ['${{ steps.semantic.outputs.new_release_version }}']
      #       })
  # make sure we can build
  build:
    name: yarn install && tsc
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - run: yarn build
  # unit test with jest
  test-unit:
    name: jest unit tests
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - run: yarn build
      - run: yarn test-coverage
      - run: bash <(curl -s https://codecov.io/bash)
  # integration test with jest
  test-integration:
    name: jest integration tests
    needs: test-unit
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - run: yarn build
      - run: yarn test-integration
        env:
          GITHUB_TOKEN: ${{ secrets.TRILOM_BOT_TOKEN }}
  # lint code and comment back if possible
  lintdog:
    name: eslintdog (reviewdog)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Lint and report push
        uses: reviewdog/action-eslint@v1
        with:
          github_token: ${{ secrets.TRILOM_BOT_TOKEN }}
          reporter: github-check
          eslint_flags: 'src/**/*.ts'