name: Bump CLI version
on:
  workflow_dispatch:
    inputs:
      option:
        description: "Option"
        required: true
        default: 'replace'
        type: choice
        options:
          - prepend
          - replace
      version:
        description: |
          The version to prepend to the supported versions file. This should be in the form: `vA.B.C`.
        required: false
        type: string
  pull_request:
    branches: [main]
    paths:
      - .github/actions/create-pr/action.yml
      - .github/workflows/bump-cli.yml
  schedule:
  - cron: 0 0 */14 * * # run every 14 days

permissions:
  contents: write
  pull-requests: write

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 1
      - name: Bump CLI
        if: ${{ inputs.option == 'replace' }}
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          scripts/replace-cli-version.sh
      - name: Prepend another version
        if: ${{ inputs.option == 'prepend' }}
        run: |
          cat extensions/ql-vscode/supported_cli_versions.json | jq '. |= ["${{ inputs.version }}"] + .' > supported_cli_versions_temp.json
          mv supported_cli_versions_temp.json extensions/ql-vscode/supported_cli_versions.json
          echo "LATEST_VERSION=${{ inputs.version }}" >> $GITHUB_ENV
          echo "PREVIOUS_VERSION=`jq -r '.[1]' extensions/ql-vscode/supported_cli_versions.json`" >> $GITHUB_ENV
      - name: Commit, Push and Open a PR
        uses: ./.github/actions/create-pr
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          base-branch: main
          head-branch: github-action/bump-cli
          commit-message: Bump CLI version from ${{ env.PREVIOUS_VERSION }} to ${{ env.LATEST_VERSION }} for integration tests
          title: Bump CLI Version to ${{ env.LATEST_VERSION }} for integration tests
          body: >
            Bumps CLI version from ${{ env.PREVIOUS_VERSION }} to ${{ env.LATEST_VERSION }}
