---
permissions: read-all

on:
  workflow_call:
    # Map the workflow outputs to job outputs
    outputs:
      last_release_ref:
        description: "Ref to last release"
        value: ${{ jobs.configure.outputs.last_release_ref }}
      test_ref:
        description: "Ref in test repo to be used"
        value: ${{ jobs.configure.outputs.test_ref }}
      lib_ref:
        description: "Ref in lib repo to be used"
        value: ${{ jobs.configure.outputs.lib_ref }}
      tools_version:
        description: "tools version in tool repo to be used"
        value: ${{ jobs.configure.outputs.tools_version }}

# This workflow configures variables that are useful for other jobs. Other
# jobs that depend on this one can access the variables via
# needs.<job-name>.outputs.<variable-name>
jobs:
  configure:
    runs-on: [self-hosted, linux]
    outputs:
      last_release_ref: ${{ env.last_release_ref }}
      test_ref: ${{ env.test_ref }}
      lib_ref: ${{ env.lib_ref }}
      tools_version: ${{env.tools_version}}
    env:
      last_release_ref: ''
      test_ref: ''
      lib_ref: ''
      tools_version: ''
    steps:

      - name: Cleanup workspace (Linux)
        if: always() && runner.os == 'Linux'
        run: sudo rm -rf ..?* .[!.]* *

      # Get ref of last release.

      - name: Checkout PR branch and all history
        uses: actions/checkout@v4
        with:
          path: source
          fetch-depth: 0
          ref: '${{ github.event.pull_request.head.sha }}'

      - name: Get tools version
        run: |
          cd source
          # Extract the version from version.txt and store it in a variable
          echo "tools_version=$(cat version.txt)" >> $GITHUB_ENV

      - name: Get ref of last release
        id: run
        run: |
          cd source
          echo "last_release_ref=$(git describe --abbrev=0 --tags --match=v*)" \
               >> $GITHUB_ENV

      # Get ref of test to be used.  If this is a pull request prefer a branch
      # of the same name as the branch being merged into otherwise try to use a
      # branch of the same name otherwise use main

      - name: Checkout tests from base_ref
        if: github.base_ref
        id: check-tests-from-base_ref
        uses: actions/checkout@v4
        continue-on-error: true
        with:
          repository: ${{ vars.TEST_REPO }}
          token: ${{ secrets.TEST_REPO_TOKEN }}
          path: tests
          fetch-depth: 0
          ref: ${{ github.base_ref }}

      - name: Use tests from base_ref
        if: steps.check-tests-from-base_ref.outcome == 'success'
        id: use-tests-from-base_ref
        run: |
          echo "test_ref=${{ github.base_ref }}" >> $GITHUB_ENV

      - name: Checkout tests from ref_name
        if: steps.check-tests-from-base_ref.outcome != 'success'
        id: check-tests-from-ref_name
        uses: actions/checkout@v4
        continue-on-error: true
        with:
          repository: ${{ vars.TEST_REPO }}
          token: ${{ secrets.TEST_REPO_TOKEN }}
          path: tests
          fetch-depth: 0
          ref: ${{ github.ref_name }}

      - name: Use tests from ref_name
        if: steps.check-tests-from-ref_name.outcome == 'success'
        id: use-tests-from-ref_name
        run: |
          echo "test_ref=${{ github.ref_name }}" >> $GITHUB_ENV

      - name: Use tests from default
        if: >
          steps.check-tests-from-base_ref.outcome != 'success'
          && steps.check-tests-from-ref_name.outcome != 'success'
        run: |
          echo "test_ref=main" >> $GITHUB_ENV

      # Get ref of lib to be used.  If this is a pull request prefer a branch
      # of the same name as the branch being merged into otherwise try to use a
      # branch of the same name otherwise use main

      - name: Checkout lib from base_ref
        if: github.base_ref
        id: check-lib-from-base_ref
        uses: actions/checkout@v4
        continue-on-error: true
        with:
          repository: ${{ vars.DISP_REPO }}
          token: ${{ secrets.DISP_REPO_TOKEN }}
          path: lib
          fetch-depth: 0
          ref: ${{ github.base_ref }}

      - name: Use lib from base_ref
        if: steps.check-lib-from-base_ref.outcome == 'success'
        id: use-lib-from-base_ref
        run: |
          echo "lib_ref=${{ github.base_ref }}" >> $GITHUB_ENV

      - name: Checkout lib from ref_name
        if: steps.check-lib-from-base_ref.outcome != 'success'
        id: check-lib-from-ref_name
        uses: actions/checkout@v4
        continue-on-error: true
        with:
          repository: ${{ vars.DISP_REPO }}
          token: ${{ secrets.DISP_REPO_TOKEN }}
          path: lib
          fetch-depth: 0
          ref: ${{ github.ref_name }}

      - name: Use lib from ref_name
        if: steps.check-lib-from-ref_name.outcome == 'success'
        id: use-lib-from-ref_name
        run: |
          echo "lib_ref=${{ github.ref_name }}" >> $GITHUB_ENV

      - name: Use lib from default
        if: >
          steps.check-lib-from-base_ref.outcome != 'success'
          && steps.check-lib-from-ref_name.outcome != 'success'
        run: |
          echo "lib_ref=main" >> $GITHUB_ENV

      - name: Report
        if: always()
        run: |
          echo "last_release_ref=${{ env.last_release_ref }}"
          echo "test_ref=${{ env.test_ref }}"
          echo "lib_ref=${{ env.lib_ref }}"

      - name: Cleanup workspace (Linux)
        if: always() && runner.os == 'Linux'
        run: sudo rm -rf ..?* .[!.]* *
