name: Version updates

on:
  pull_request_review:
    types: [submitted, edited]

permissions:
  id-token: write
  contents: write
  packages: write

env:
  NODE_VERSION: 17.0.1
  ANCHOR_VERSION: 0.22.0
  SOLANA_VERSION_STABLE: 1.9.22
  RUST_TOOLCHAIN: stable

jobs:
  dump-context:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Dump GitHub context
        env:
          GITHUB_CONTEXT: ${{ toJson(github) }}
        run: echo "$GITHUB_CONTEXT"

  get-changes-scope:
    if:
      contains(fromJson('["OWNER", "MEMBER", "CONTRIBUTOR"]'), github.event.review.author_association) == true &&
      contains(fromJson('["approved", "commented"]'), github.event.review.state) == true
    runs-on: ubuntu-latest
    outputs:
      changed-packages: ${{ steps.get-changed-package-scope.outputs.result }}
    steps:
      - uses: actions/checkout@v3
      - name: List changed files
        uses: ./.github/actions/list-changed-files
        id: list-changed-files
        with:
          pull-number: ${{ github.event.pull_request.number }}
          exclude: '[".github", "Cargo.lock", "Cargo.toml", "js/idl", "packge.json", "yarn.lock"]'

      # map fetched changed files to package / lang (list)
      - name: Get scope of changed packages
        uses: actions/github-script@v4
        id: get-changed-package-scope
        with:
          # update regexp to consider other subdirs - e.g. `rust|test|cli|<etc>`
          script: |
            const files = ${{ steps.list-changed-files.outputs.changed-files }}
            const regexp = /[a-zA-Z\-]+\/(js|program)/g
            const uniqueFilesObj = files.reduce((p, file) => {
              const match = file.match(regexp)
              if (match) {
                // use first match result
                if (!p[match[0]]) p[match[0]] = 1
              }
              return p
            }, {})
            return JSON.stringify(Array.from(Object.keys(uniqueFilesObj)).map((el) => `\"${el}\"`))

  get-version-scope:
    if:
      contains(fromJson('["OWNER", "MEMBER", "CONTRIBUTOR"]'), github.event.review.author_association) == true &&
      contains(fromJson('["approved", "commented"]'), github.event.review.state) == true
    runs-on: ubuntu-latest
    outputs:
      versioning: ${{ steps.parse-version-info.outputs.versioning }}
      has-versioning: ${{ steps.parse-version-info.outputs.has-versioning }}
    steps:
      - uses: actions/checkout@v3
      - name: Parse version info from review
        uses: ./.github/actions/parse-version-command
        id: parse-version-info
        with:
          body: ${{ github.event.review.body }}

  update-pr-with-changes:
    needs: [get-changes-scope, get-version-scope]
    if: ${{ needs.get-version-scope.outputs.has-versioning == 'true' }}
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: ./.github/actions/install-linux-build-deps
      - uses: ./.github/actions/install-solana
        with:
          solana_version: ${{ env.SOLANA_VERSION_STABLE }}
      - uses: ./.github/actions/install-rust
        with:
          toolchain: ${{ env.RUST_TOOLCHAIN }}
      - uses: ./.github/actions/install-anchor/

      - name: Make version changes
        uses: ./.github/actions/make-version-changes
        id: make-version-changes
        with:
          changed-packages: ${{ needs.get-changes-scope.outputs.changed-packages }}
          versioning: ${{ needs.get-version-scope.outputs.versioning }}
          branch: ${{ github.event.pull_request.head.ref }}