| 
 | 1 | +name: Check  | 
 | 2 | + | 
 | 3 | +on:  | 
 | 4 | +  pull_request:  | 
 | 5 | +    branches:  | 
 | 6 | +      - main  | 
 | 7 | +    types: [labeled, unlabeled, opened, reopened, synchronize]  | 
 | 8 | + | 
 | 9 | +permissions:  | 
 | 10 | +  pull-requests: "read"  | 
 | 11 | + | 
 | 12 | +jobs:  | 
 | 13 | +  check-backport-label:  | 
 | 14 | +    name: backport label   | 
 | 15 | +    runs-on: ubuntu-latest  | 
 | 16 | + | 
 | 17 | +    steps:  | 
 | 18 | +      - name: "Check backport label"  | 
 | 19 | +        env:  | 
 | 20 | +          GH_TOKEN: ${{ github.token }}  | 
 | 21 | +        run: |  | 
 | 22 | +          json_pr_labels='${{ toJSON(github.event.pull_request.labels) }}'  | 
 | 23 | +          readarray -t pr_labels < <(echo "${json_pr_labels}" | jq -r -c '.[].name')  | 
 | 24 | +
  | 
 | 25 | +          json_all_labels="$(gh label list --repo ${{ github.repository }} --json name --search "backport" --limit 1000)"  | 
 | 26 | +          readarray -t all_labels < <(echo "${json_all_labels}" | jq -r -c '.[].name')  | 
 | 27 | +
  | 
 | 28 | +          declare -A all_backport_labels=()  | 
 | 29 | +          declare -A all_floating_majors=()  | 
 | 30 | +
  | 
 | 31 | +          backport_regex="^backport ([0-9])+\.([0-9]+|x)$"  | 
 | 32 | +
  | 
 | 33 | +          echo "::group::Available Labels"  | 
 | 34 | +          echo "skip-backport"  | 
 | 35 | +
  | 
 | 36 | +          for label in "${all_labels[@]}"; do  | 
 | 37 | +            if [[ "${label}" =~ ${backport_regex} ]]; then  | 
 | 38 | +              major="${BASH_REMATCH[1]}"  | 
 | 39 | +              minor="${BASH_REMATCH[2]}"  | 
 | 40 | +              all_backport_labels["${label}"]=1  | 
 | 41 | +              echo "${label}"  | 
 | 42 | +
  | 
 | 43 | +              if [ "${minor}" = "x" ]; then  | 
 | 44 | +                all_floating_majors["${major}"]=1  | 
 | 45 | +              fi  | 
 | 46 | +            fi  | 
 | 47 | +          done  | 
 | 48 | +
  | 
 | 49 | +          echo "::endgroup::"  | 
 | 50 | +
  | 
 | 51 | +          has_skip_backport_label=false  | 
 | 52 | +          has_exact_backport_label=false  | 
 | 53 | +          declare -A pr_exact_majors=()  | 
 | 54 | +          declare -A pr_floating_majors=()  | 
 | 55 | +
  | 
 | 56 | +          echo "::group::Detected Labels"  | 
 | 57 | +
  | 
 | 58 | +          for pr_label in "${pr_labels[@]}"; do  | 
 | 59 | +            if [ "${pr_label}" = "skip-backport" ]; then  | 
 | 60 | +              has_skip_backport_label=true  | 
 | 61 | +              echo "${pr_label}"  | 
 | 62 | +              continue  | 
 | 63 | +            fi  | 
 | 64 | +
  | 
 | 65 | +            if [ -z "${all_backport_labels[${pr_label}]}" ]; then  | 
 | 66 | +              continue  | 
 | 67 | +            fi  | 
 | 68 | +
  | 
 | 69 | +            if [[ "${pr_label}" =~ ${backport_regex} ]]; then  | 
 | 70 | +              major="${BASH_REMATCH[1]}"  | 
 | 71 | +              minor="${BASH_REMATCH[2]}"  | 
 | 72 | +              if [ "${minor}" != "x" ]; then  | 
 | 73 | +                pr_exact_majors["${major}"]=1  | 
 | 74 | +                has_exact_backport_label=true  | 
 | 75 | +              else  | 
 | 76 | +                pr_floating_majors["${major}"]=1  | 
 | 77 | +              fi  | 
 | 78 | +            fi  | 
 | 79 | +
  | 
 | 80 | +            echo "${pr_label}"  | 
 | 81 | +          done  | 
 | 82 | +
  | 
 | 83 | +          echo "::endgroup::"  | 
 | 84 | +
  | 
 | 85 | +          if [ "${has_skip_backport_label}" = true ] && [ "${has_exact_backport_label}" = true ]; then  | 
 | 86 | +            echo "::error::The 'skip-backport' not be used in combination with another backport"\  | 
 | 87 | +                  "label."  | 
 | 88 | +            exit 1  | 
 | 89 | +          fi  | 
 | 90 | +
  | 
 | 91 | +          if [ "${has_skip_backport_label}" != true ] && [ "${has_exact_backport_label}" != true ]; then  | 
 | 92 | +            echo "::error::No exact backport label found. Please add at least one of the"\  | 
 | 93 | +                  "'backport {major}.{minor}' labels or use 'skip-backport',"\  | 
 | 94 | +                  "if this PR should not be backported."  | 
 | 95 | +            exit 1  | 
 | 96 | +          fi  | 
 | 97 | +
  | 
 | 98 | +          # Validate that a floating backport label exists for each exact backport label major  | 
 | 99 | +          # version.  | 
 | 100 | +
  | 
 | 101 | +          has_required_floating_labels=true  | 
 | 102 | +
  | 
 | 103 | +          for pr_major in "${!pr_exact_majors[@]}"; do  | 
 | 104 | +            if [ -z "${all_floating_majors[${pr_major}]}" ]; then  | 
 | 105 | +              # There is no floating version branch for the given major version.  | 
 | 106 | +              continue  | 
 | 107 | +            fi  | 
 | 108 | +
  | 
 | 109 | +            if [ -z "${pr_floating_majors[${pr_major}]}" ]; then  | 
 | 110 | +              has_required_floating_labels=false  | 
 | 111 | +              echo "::error::Missing floating backport label for '${pr_major}.x'"  | 
 | 112 | +            fi  | 
 | 113 | +          done  | 
 | 114 | +
  | 
 | 115 | +          if [ "${has_required_floating_labels}" != true ]; then  | 
 | 116 | +            exit 1  | 
 | 117 | +          fi  | 
 | 118 | +
  | 
 | 119 | +          # Validate that an exact backport label exists for each floating backport label major  | 
 | 120 | +          # version.  | 
 | 121 | +
  | 
 | 122 | +          has_required_exact_labels=true  | 
 | 123 | +
  | 
 | 124 | +          for pr_floating_major in "${!pr_floating_majors[@]}"; do  | 
 | 125 | +            if [ -z "${pr_exact_majors[${pr_floating_major}]}" ]; then  | 
 | 126 | +              has_required_exact_labels=false  | 
 | 127 | +              echo "::error::Missing exact backport label for '${pr_floating_major}.x'"  | 
 | 128 | +            fi  | 
 | 129 | +          done  | 
 | 130 | +
  | 
 | 131 | +          if [ "${has_required_exact_labels}" != true ]; then  | 
 | 132 | +            exit 1  | 
 | 133 | +          fi  | 
0 commit comments