|
| 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_exact_backport_label=false |
| 52 | + declare -A pr_exact_majors=() |
| 53 | + declare -A pr_floating_majors=() |
| 54 | +
|
| 55 | + echo "::group::Detected Labels" |
| 56 | +
|
| 57 | + for pr_label in "${pr_labels[@]}"; do |
| 58 | + if [ "${pr_label}" = "skip-backport" ]; then |
| 59 | + has_exact_backport_label=true |
| 60 | + echo "${pr_label}" |
| 61 | + continue |
| 62 | + fi |
| 63 | +
|
| 64 | + if [ -z "${all_backport_labels[${pr_label}]}" ]; then |
| 65 | + continue |
| 66 | + fi |
| 67 | +
|
| 68 | + if [[ "${pr_label}" =~ ${backport_regex} ]]; then |
| 69 | + major="${BASH_REMATCH[1]}" |
| 70 | + minor="${BASH_REMATCH[2]}" |
| 71 | + if [ "${minor}" != "x" ]; then |
| 72 | + pr_exact_majors["${major}"]=1 |
| 73 | + has_exact_backport_label=true |
| 74 | + else |
| 75 | + pr_floating_majors["${major}"]=1 |
| 76 | + fi |
| 77 | + fi |
| 78 | +
|
| 79 | + echo "${pr_label}" |
| 80 | + done |
| 81 | +
|
| 82 | + echo "::endgroup::" |
| 83 | +
|
| 84 | + if [ "${has_exact_backport_label}" != true ]; then |
| 85 | + echo "::error::No exact backport label found. Please add at least one of the"\ |
| 86 | + "'backport {major}.{minor}' labels or use 'skip-backport',"\ |
| 87 | + "if this PR should not be backported." |
| 88 | + exit 1 |
| 89 | + fi |
| 90 | +
|
| 91 | + # Validate that a floating backport label exists for each exact backport label major |
| 92 | + # version. |
| 93 | +
|
| 94 | + has_required_floating_labels=true |
| 95 | +
|
| 96 | + for pr_major in "${!pr_exact_majors[@]}"; do |
| 97 | + if [ -z "${all_floating_majors[${pr_major}]}" ]; then |
| 98 | + # There is no floating version branch for the given major version. |
| 99 | + continue |
| 100 | + fi |
| 101 | +
|
| 102 | + if [ -z "${pr_floating_majors[${pr_major}]}" ]; then |
| 103 | + has_required_floating_labels=false |
| 104 | + echo "::error::Missing floating backport label for '${pr_major}.x'" |
| 105 | + fi |
| 106 | + done |
| 107 | +
|
| 108 | + if [ "${has_required_floating_labels}" != true ]; then |
| 109 | + exit 1 |
| 110 | + fi |
| 111 | +
|
| 112 | + # Validate that an exact backport label exists for each floating backport label major |
| 113 | + # version. |
| 114 | +
|
| 115 | + has_required_exact_labels=true |
| 116 | +
|
| 117 | + for pr_floating_major in "${!pr_floating_majors[@]}"; do |
| 118 | + if [ -z "${pr_exact_majors[${pr_floating_major}]}" ]; then |
| 119 | + has_required_exact_labels=false |
| 120 | + echo "::error::Missing exact backport label for '${pr_floating_major}.x'" |
| 121 | + fi |
| 122 | + done |
| 123 | +
|
| 124 | + if [ "${has_required_exact_labels}" != true ]; then |
| 125 | + exit 1 |
| 126 | + fi |
0 commit comments