name: CherryPickTest
run-name: "Cherry-Pick Tests for PR ${{github.event.number}}"
on:
  pull_request_target:
    types: [ labeled ]

concurrency:
  group: ${{github.workflow}}-${{github.event.number}}
  cancel-in-progress: true

env:
  PR_NUMBER:          ${{ github.event.number }}
  MODULES_BLACKLIST:  ${{ vars.GATETEST_MODULES_BLACKLIST }} ${{ vars.UNITTEST_MODULES_BLACKLIST }}

jobs:
  IdentifyBranches:
    name: IdentifyBranches
    if: ${{ github.event.label.name == vars.CHERRY_PICK_TEST_LABEL }}
    outputs:
      branches:     ${{ steps.getbranches.outputs.branches }}
      branch_count: ${{ steps.getbranches.outputs.branch_count }}
    runs-on: ubuntu-latest
    steps:
      - name: Remove Trigger Label, Add InProgress Label
        env:
          GH_TOKEN:          ${{ secrets.GITHUB_TOKEN }}
        run: | 
          gh pr edit --repo ${{github.repository}} \
            --remove-label ${{vars.CHERRY_PICK_TEST_LABEL}} \
            --remove-label ${{vars.CHERRY_PICK_CHECKS_PASSED_LABEL}} \
            --remove-label ${{vars.CHERRY_PICK_CHECKS_FAILED_LABEL}} \
            --remove-label ${{vars.CHERRY_PICK_GATES_PASSED_LABEL}} \
            --remove-label ${{vars.CHERRY_PICK_GATES_FAILED_LABEL}} \
            --remove-label ${{vars.CHERRY_PICK_TESTING_IN_PROGRESS}} \
            ${{env.PR_NUMBER}} || :

      - name: Get cherry-pick branches
        uses: asterisk/asterisk-ci-actions/GetCherryPickBranchesFromPR@main
        id: getbranches
        with:
          repo:                    ${{github.repository}}
          pr_number:               ${{env.PR_NUMBER}}
          cherry_pick_regex:       ${{vars.CHERRY_PICK_REGEX}}
          github_token:            ${{secrets.GITHUB_TOKEN}}

      - name: Check Branch Count
        if: ${{ steps.getbranches.outputs.branch_count > 0 }}
        env:
          GH_TOKEN:          ${{ secrets.GITHUB_TOKEN }}
        run: |
          gh pr edit --repo ${{github.repository}} \
          --add-label ${{vars.CHERRY_PICK_TESTING_IN_PROGRESS}} \
          ${{env.PR_NUMBER}} || :

  CherryPickUnitTestMatrix:
    needs: [ IdentifyBranches ]
    if: ${{ needs.IdentifyBranches.outputs.branch_count > 0 && ( success() || failure() ) }}
    continue-on-error: false
    strategy:
      fail-fast: false
      matrix:
        branch: ${{ fromJSON(needs.IdentifyBranches.outputs.branches) }}
    runs-on: ubuntu-latest
    steps:
      - name: Run Unit Tests for branch ${{matrix.branch}}
        uses: asterisk/asterisk-ci-actions/AsteriskUnitComposite@main
        with:
          asterisk_repo:     ${{github.repository}}
          pr_number:         ${{env.PR_NUMBER}}
          base_branch:       ${{matrix.branch}}
          is_cherry_pick:    true
          modules_blacklist: ${{env.MODULES_BLACKLIST}}
          github_token:      ${{secrets.GITHUB_TOKEN}}
          unittest_command:  ${{vars.UNITTEST_COMMAND}}

  CherryPickUnitTests:
    needs: [ IdentifyBranches, CherryPickUnitTestMatrix ]
    if: ${{ needs.IdentifyBranches.outputs.branch_count > 0 && ( success() || failure() ) }}
    runs-on: ubuntu-latest
    steps:
      - name: Check unit test matrix status
        env:
          RESULT:    ${{needs.CherryPickUnitTestMatrix.result}}
          GH_TOKEN:  ${{ secrets.GITHUB_TOKEN }}
        run: |
          case $RESULT in
            success)
              gh pr edit --repo ${{github.repository}} \
                --add-label ${{vars.CHERRY_PICK_CHECKS_PASSED_LABEL}} \
                ${{env.PR_NUMBER}} || :
              echo "::notice::All tests passed"
              exit 0
              ;;
            skipped)
              gh pr edit --repo ${{github.repository}} \
                --remove-label ${{vars.CHERRY_PICK_TESTING_IN_PROGRESS}} \
                --add-label ${{vars.CHERRY_PICK_CHECKS_FAILED_LABEL}} \
                ${{env.PR_NUMBER}} || :
              echo "::notice::Unit tests were skipped because of an earlier failure"
              exit 1
              ;;
            *)
              gh pr edit --repo ${{github.repository}} \
                --remove-label ${{vars.CHERRY_PICK_TESTING_IN_PROGRESS}} \
                --add-label ${{vars.CHERRY_PICK_CHECKS_FAILED_LABEL}} \
                ${{env.PR_NUMBER}} || :
              echo "::error::One or more tests failed ($RESULT)"
              exit 1
          esac

  CherryPickGateTestMatrix:
    needs: [ IdentifyBranches, CherryPickUnitTests ]
    if: ${{ success() }}
    continue-on-error: false
    strategy:
      fail-fast: false
      matrix:
        branch: ${{ fromJSON(needs.IdentifyBranches.outputs.branches) }}
        group: ${{ fromJSON(vars.GATETEST_LIST) }}
    runs-on: ubuntu-latest
    steps:
      - name: Run Gate Tests for ${{ matrix.group }}-${{matrix.branch}}
        uses: asterisk/asterisk-ci-actions/AsteriskGateComposite@main
        with:
          test_type:         Gate
          asterisk_repo:     ${{github.repository}}
          pr_number:         ${{env.PR_NUMBER}}
          base_branch:       ${{matrix.branch}}
          is_cherry_pick:    true
          modules_blacklist: ${{env.MODULES_BLACKLIST}}
          github_token:      ${{secrets.GITHUB_TOKEN}}
          testsuite_repo:    ${{vars.TESTSUITE_REPO}}
          gatetest_group:    ${{matrix.group}}
          gatetest_commands: ${{vars.GATETEST_COMMANDS}}

  CherryPickGateTests:
    needs: [ IdentifyBranches, CherryPickGateTestMatrix ]
    if: ${{ success() || failure() }}
    runs-on: ubuntu-latest
    steps:
      - name: Check test matrix status
        env:
          RESULT:    ${{needs.CherryPickGateTestMatrix.result}}
          GH_TOKEN:  ${{ secrets.GITHUB_TOKEN }}
        run: |
          gh pr edit --repo ${{github.repository}} \
            --remove-label ${{vars.CHERRY_PICK_TESTING_IN_PROGRESS}} \
            ${{env.PR_NUMBER}} || :
          case $RESULT in
            success)
              gh pr edit --repo ${{github.repository}} \
                --add-label ${{vars.CHERRY_PICK_GATES_PASSED_LABEL}} \
                ${{env.PR_NUMBER}} || :
              echo "::notice::All Testsuite tests passed"
              exit 0
              ;;
            skipped)
              echo "::error::Testsuite tests were skipped because of an earlier failure"
              exit 1
              ;;
            *)
              gh pr edit --repo ${{github.repository}} \
                --add-label ${{vars.CHERRY_PICK_GATES_FAILED_LABEL}} \
                ${{env.PR_NUMBER}} || :
              echo "::error::One or more Testsuite tests failed ($RESULT)"
              exit 1
          esac
