# This action is centrally managed in https://github.com/asyncapi/.github/
# Don't make changes to this file in this repo as they will be overwritten with changes made to the same file in above mentioned repo

# It does magic only if there is package.json file in the root of the project
name: Version bump - if Node.js project

on: 
  release:
    types: 
      - published

jobs:
  version_bump:
    name: Generate assets and bump NodeJS
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          # target branch of release. More info https://docs.github.com/en/rest/reference/repos#releases
          # in case release is created from release branch then we need to checkout from given branch
          # if @semantic-release/github is used to publish, the minimum version is 7.2.0 for proper working
          ref: ${{ github.event.release.target_commitish }}
      - name: Check if Node.js project and has package.json
        id: packagejson
        run: test -e ./package.json && echo "exists=true" >> $GITHUB_OUTPUT || echo "exists=false" >> $GITHUB_OUTPUT
      - if: steps.packagejson.outputs.exists == 'true'
        name: Determine what node version to use
        # This workflow is from our own org repo and safe to reference by 'master'.
        uses: asyncapi/.github/.github/actions/get-node-version-from-package-lock@master # //NOSONAR
        with:
          node-version: ${{ vars.NODE_VERSION }}
        id: lockversion
      - if: steps.packagejson.outputs.exists == 'true'
        name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "${{ steps.lockversion.outputs.version }}"
          cache: 'npm'
          cache-dependency-path: '**/package-lock.json'
      - if: steps.packagejson.outputs.exists == 'true'
        name: Install dependencies
        run: npm ci
      - if: steps.packagejson.outputs.exists == 'true'
        name: Assets generation
        run: npm run generate:assets --if-present
      - if: steps.packagejson.outputs.exists == 'true'  
        name: Bump version in package.json
        # There is no need to substract "v" from the tag as version script handles it
        # When adding "bump:version" script in package.json, make sure no tags are added by default (--no-git-tag-version) as they are already added by release workflow
        # When adding "bump:version" script in package.json, make sure --allow-same-version is set in case someone forgot and updated package.json manually and we want to avoide this action to fail and raise confusion
        env:
          VERSION: ${{github.event.release.tag_name}}
        run: npm run bump:version
      - if: steps.packagejson.outputs.exists == 'true'
        name: Create Pull Request with updated asset files including package.json
        uses: peter-evans/create-pull-request@38e0b6e68b4c852a5500a94740f0e535e0d7ba54 # use 4.2.4 https://github.com/peter-evans/create-pull-request/releases/tag/v4.2.4
        env:
          RELEASE_TAG: ${{github.event.release.tag_name}}
          RELEASE_URL: ${{github.event.release.html_url}}
        with:
          token: ${{ secrets.GH_TOKEN }}
          commit-message: 'chore(release): ${{ env.RELEASE_TAG }}'
          committer: asyncapi-bot <info@asyncapi.io>
          author: asyncapi-bot <info@asyncapi.io>
          title: 'chore(release): ${{ env.RELEASE_TAG }}'
          body: 'Version bump in package.json for release [${{ env.RELEASE_TAG }}](${{ env.RELEASE_URL }})'
          branch: version-bump/${{ env.RELEASE_TAG }}
      - if: failure() # Only, on failure, send a message on the 94_bot-failing-ci slack channel
        name: Report workflow run status to Slack
        uses: 8398a7/action-slack@28ba43ae48961b90635b50953d216767a6bea486 #using https://github.com/8398a7/action-slack/releases/tag/v3.16.2
        with:
          status: ${{ job.status }}
          fields: repo,action,workflow
          text: 'Unable to bump the version in package.json after the release'
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_CI_FAIL_NOTIFY }}