name: "Publish"

on:
  workflow_dispatch:
    inputs:
      version:
        description: "version"
        required: true

      schemaTag:
        description: "vk-api-schema tag version"
        required: false

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          token: ${{ secrets.DEVTOOLS_GITHUB_TOKEN }}

      - uses: actions/setup-node@v1
        with:
          node-version: 14
          registry-url: "https://registry.npmjs.org"

      - run: yarn install --frozen-lockfile

      - name: Set Git credentials
        run: |
          git config --local user.email "actions@github.com"
          git config --local user.name "GitHub Action"

      - name: Update vk-api-schema JSON package
        run: yarn add -D '@vkontakte/api-schema@https://github.com/VKCOM/vk-api-schema.git#v${{ github.event.inputs.schemaTag }}'
        if: ${{ github.event.inputs.schemaTag }}

      - name: Generate scheme
        run: yarn generate

      - name: Commit generated scheme
        run: git add -A && git diff-index --quiet HEAD || git commit -m "Generate TypeScript files"

      - name: Bump version
        run: yarn version --new-version ${{ github.event.inputs.version }} --no-commit-hooks

      - name: Pushing changes
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.DEVTOOLS_GITHUB_TOKEN }}
          branch: ${{ github.ref }}

      - name: Publushing release
        run: yarn publish --non-interactive
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPMJS_PUBLISH_TOKEN }}
