name: Build latest contract
on: 
  push:
    branches:
      - master
      - main

env:
  MB_GITHUB_TOKEN: ${{ secrets.MB_GITHUB_TOKEN }}
  TG_CHAT_ID: "-638121972"

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v2
        
      - name: Setup Node.js
        uses: actions/setup-node@v1
        with:
          node-version: '16.x'
      
      - name: Install dependencies
        run: yarn
      
      - name: Run tests
        run: yarn test
        
      - name: Build project
        run: yarn build

      - name: Generate Ethers typings
        run: yarn types-ethers

      - name: get-npm-version
        id: package-version
        uses: martinbeentjes/npm-get-version-action@master

      - name: Commit & push types
        uses: EndBug/add-and-commit@v8
        with:
          add: 'types'
          message: '[auto] types updated'
          github_token: ${{ env.MB_GITHUB_TOKEN }}

      - name: Get git commit SHA
        id: git-commit-sha
        run: echo "::set-output name=sha::$(git rev-parse HEAD)"
      
      - name: Tag new version
        uses: pkgdeps/action-package-version-to-git-tag@v1.5.0
        with:
          version: ${{ steps.package-version.outputs.current-version}}
          git_tag_prefix: "v"
          github_token: ${{ env.MB_GITHUB_TOKEN }}
          github_repo: ${{ github.repository }}
          git_commit_sha: ${{ steps.git-commit-sha.outputs.sha }}
      
      - name: telegram notify
        if: cancelled() == false
        uses: xinthink/action-telegram@v1.1
        with:
          botToken: ${{ secrets.MB_TELEGRAM_BOT_TOKEN }}
          chatId: ${{ env.TG_CHAT_ID }}
          jobStatus: ${{ job.status }}
