name: Release
on:
  push:
    branches:
      - alpha
      - beta
      - master
jobs:
  context:
    runs-on: ubuntu-latest
    outputs:
      branch: ${{ steps.branch.outputs.branch }}
    steps:
      - run: |
          echo $GITHUB_CONTEXT
        env:
          GITHUB_CONTEXT: ${{ toJson(github) }}
      - id: branch
        shell: bash
        run: |
          echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
  release:
    if: github.event_name == 'push'
    runs-on: ubuntu-latest
    needs: context
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - uses: actions/setup-node@v2.1.0
        with:
          always-auth: false
          node-version: "14.4.0"
          registry-url: https://registry.npmjs.org
          scope: "@sentrei"
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
      - id: yarn-cache-path
        run: echo "::set-output name=dir::$(yarn cache dir)"
      - id: cache
        uses: actions/cache@master
        with:
          path: |
            node_modules
            */*/node_modules
            ${{ steps.yarn-cache-path.outputs.dir }}
          key: |
            yarn-v1-${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            yarn-v1-${{ runner.os }}-
      - run: |
          yarn install --frozen-lockfile
      - run: |
          yarn run typescript
      - run: |
          yarn run release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - id: version
        run: |
          version=$(node -pe "require('./package.json').version")
          sed -i -e "s/^version: .*/version: ${version}/" packages/mobile/pubspec.yaml
          for i in $(find packages -name package.json); do
            sed -i -e "s/^  \"version\": .*/  \"version\": \"${version}\",/" $i
            cat $i
          done
          echo "::set-output name=release::sentrei@${version}"
      - if: needs.context.outputs.branch != 'master'
        run: |
          echo "::set-env name=DIST_TAG::${{ needs.context.outputs.branch }}"
      - if: needs.context.outputs.branch == 'master'
        run: |
          echo "::set-env name=DIST_TAG::latest"
      - run: |
          cat <<< $(jq '.private = false' package.json) > package.json && npm publish . --tag $DIST_TAG || true
          cat <<< $(jq '.private = true' package.json) > package.json
          git stash && yarn run publish:lerna && git stash pop || true
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
      - run: |
          yarn run sentry
        env:
          SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
          SENTRY_ORG: ${{ github.repository_owner }}
          SENTRY_ENVIRONMENT: ${{ needs.context.outputs.branch }}
          SENTRY_RELEASE: ${{ steps.version.outputs.release }}
      - run: |
          yarn run fix
      - uses: peter-evans/create-pull-request@v2
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          branch: create-pull-request/${{ needs.context.outputs.branch }}
          title: "chore: [sentrei-auto-pr]"
          commit-message: "chore: [sentrei-auto-pr]"
        env:
          HUSKY_SKIP_HOOKS: 1
  blog:
    if: contains(toJson(github.event.commits), '[sentrei-auto-pr]') == true
    runs-on: ubuntu-latest
    needs: context
    steps:
      - uses: peter-evans/repository-dispatch@v1
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          event-type: deploy-cmd
          client-payload: '{"base": "${{ needs.context.outputs.branch }}", "branch": "${{ needs.context.outputs.branch }}", "comment_id": "", "environment": "production", "event_name": "blog", "pull_request_number": "", "release": "true", "task": "blog", "tag": "blog", "url_prefix": ""}'
  dartdoc:
    if: contains(toJson(github.event.commits), '[sentrei-auto-pr]') == true
    runs-on: ubuntu-latest
    needs: context
    steps:
      - uses: peter-evans/repository-dispatch@v1
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          event-type: deploy-cmd
          client-payload: '{"base": "${{ needs.context.outputs.branch }}", "branch": "${{ needs.context.outputs.branch }}", "comment_id": "", "environment": "production", "event_name": "dartdoc", "pull_request_number": "", "release": "true", "task": "${{ needs.context.outputs.branch }}", "tag": "${{ needs.context.outputs.branch }}", "url_prefix": "${{ needs.context.outputs.branch }}."}'
  design:
    if: contains(toJson(github.event.commits), '[sentrei-auto-pr]') == true
    runs-on: ubuntu-latest
    needs: context
    steps:
      - uses: peter-evans/repository-dispatch@v1
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          event-type: deploy-cmd
          client-payload: '{"base": "${{ needs.context.outputs.branch }}", "branch": "${{ needs.context.outputs.branch }}", "comment_id": "", "environment": "production", "event_name": "design", "pull_request_number": "", "release": "true", "task": "design", "tag": "design", "url_prefix": ""}'
  fastlane:
    if: contains(toJson(github.event.commits), '[sentrei-auto-pr]') == true
    runs-on: ubuntu-latest
    needs: context
    env:
      flutter_channel: "stable"
      flutter_version: "1.12.13+hotfix.9"
    steps:
      - uses: peter-evans/repository-dispatch@v1
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          event-type: deploy-cmd
          client-payload: '{"base": "${{ needs.context.outputs.branch }}", "branch": "${{ needs.context.outputs.branch }}", "comment_id": "", "environment": "production", "event_name": "fastlane", "pull_request_number": "", "release": "false", "task": "fastlane", "tag": "fastlane", "url_prefix": ""}'
  functions:
    if: contains(toJson(github.event.commits), '[sentrei-auto-pr]') == true
    runs-on: ubuntu-latest
    needs: context
    steps:
      - uses: peter-evans/repository-dispatch@v1
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          event-type: deploy-cmd
          client-payload: '{"base": "${{ needs.context.outputs.branch }}", "branch": "${{ needs.context.outputs.branch }}", "comment_id": "", "environment": "production", "event_name": "functions", "pull_request_number": "", "release": "true", "task": "${{ needs.context.outputs.branch }}", "tag": "${{ needs.context.outputs.branch }}", "url_prefix": ""}'
  pioneer:
    if: contains(toJson(github.event.commits), '[sentrei-auto-pr]') == true
    runs-on: ubuntu-latest
    needs: context
    steps:
      - uses: peter-evans/repository-dispatch@v1
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          event-type: deploy-cmd
          client-payload: '{"base": "${{ needs.context.outputs.branch }}", "branch": "${{ needs.context.outputs.branch }}", "comment_id": "", "environment": "production", "event_name": "pioneer", "pull_request_number": "", "release": "true", "task": "pioneer", "tag": "pioneer", "url_prefix": ""}'
  typedoc:
    if: contains(toJson(github.event.commits), '[sentrei-auto-pr]') == true
    runs-on: ubuntu-latest
    needs: context
    steps:
      - uses: peter-evans/repository-dispatch@v1
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          event-type: deploy-cmd
          client-payload: '{"base": "${{ needs.context.outputs.branch }}", "branch": "${{ needs.context.outputs.branch }}", "comment_id": "", "environment": "production", "event_name": "typedoc", "pull_request_number": "", "release": "true", "task": "${{ needs.context.outputs.branch }}", "tag": "${{ needs.context.outputs.branch }}", "url_prefix": "${{ needs.context.outputs.branch }}."}'
  ui:
    if: contains(toJson(github.event.commits), '[sentrei-auto-pr]') == true
    runs-on: ubuntu-latest
    needs: context
    steps:
      - uses: peter-evans/repository-dispatch@v1
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          event-type: deploy-cmd
          client-payload: '{"base": "${{ needs.context.outputs.branch }}", "branch": "${{ needs.context.outputs.branch }}", "comment_id": "", "environment": "production", "event_name": "ui", "pull_request_number": "", "release": "true", "task": "ui", "tag": "ui", "url_prefix": ""}'
  web:
    if: contains(toJson(github.event.commits), '[sentrei-auto-pr]') == true
    runs-on: ubuntu-latest
    needs: context
    steps:
      - uses: peter-evans/repository-dispatch@v1
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          event-type: deploy-cmd
          client-payload: '{"base": "${{ needs.context.outputs.branch }}", "branch": "${{ needs.context.outputs.branch }}", "comment_id": "", "environment": "production", "event_name": "web", "pull_request_number": "", "release": "true", "task": "${{ needs.context.outputs.branch }}", "tag": "${{ needs.context.outputs.branch }}", "url_prefix": "${{ needs.context.outputs.branch }}."}'
