|
| 1 | +# Copyright (c) 2020 Intel Corporation |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +# |
| 5 | + |
| 6 | +name: Ensure PR has required porting labels |
| 7 | + |
| 8 | +on: |
| 9 | + pull_request_target: |
| 10 | + types: |
| 11 | + - opened |
| 12 | + - reopened |
| 13 | + - labeled |
| 14 | + - unlabeled |
| 15 | + |
| 16 | +jobs: |
| 17 | + check-pr-porting-labels: |
| 18 | + runs-on: ubuntu-latest |
| 19 | + steps: |
| 20 | + - name: Install hub |
| 21 | + run: | |
| 22 | + HUB_ARCH="amd64" |
| 23 | + HUB_VER=$(curl -sL "https://api.github.com/repos/github/hub/releases/latest" |\ |
| 24 | + jq -r .tag_name | sed 's/^v//') |
| 25 | + curl -sL \ |
| 26 | + "https://github.com/github/hub/releases/download/v${HUB_VER}/hub-linux-${HUB_ARCH}-${HUB_VER}.tgz" |\ |
| 27 | + tar xz --strip-components=2 --wildcards '*/bin/hub' && \ |
| 28 | + sudo install hub /usr/local/bin |
| 29 | +
|
| 30 | + - name: Checkout code to allow hub to communicate with the project |
| 31 | + uses: actions/checkout@v2 |
| 32 | + with: |
| 33 | + token: ${{ secrets.KATA_GITHUB_ACTIONS_TOKEN }} |
| 34 | + |
| 35 | + - name: Stop PR being merged unless it has atleast one backport label and one forward port label |
| 36 | + env: |
| 37 | + GITHUB_TOKEN: ${{ secrets.KATA_GITHUB_ACTIONS_TOKEN }} |
| 38 | + run: | |
| 39 | + pr=${{ github.event.number }} |
| 40 | +
|
| 41 | + # Every PR must have two porting labels before it can be merged. |
| 42 | + # This is to ensure it is clear that a reviewer has considered both |
| 43 | + # porting directions. |
| 44 | + backport_labels=("needs-backport" "no-backport-needed" "backport") |
| 45 | + forward_port_labels=("needs-forward-port" "no-forward-port-needed" "forward-port") |
| 46 | +
|
| 47 | + labels=$(hub api "/repos/{owner}/{repo}/labels" | jq -r '.[].name') |
| 48 | +
|
| 49 | + for label in ${backport_labels[@]} ${forward_port_labels[@]} |
| 50 | + do |
| 51 | + { echo "$labels"| egrep -q "^${label}$"; ret=$?; } || true |
| 52 | +
|
| 53 | + [ $ret -eq 0 ] || { |
| 54 | + echo "::error::Expected label '$label' not available in repository ${{ github.repository }}" |
| 55 | + exit 1 |
| 56 | + } |
| 57 | + done |
| 58 | +
|
| 59 | + pr_details=$(hub pr list -f '%I;%L%n' | grep "^${pr}" || true) |
| 60 | +
|
| 61 | + [ -z "$pr_details" ] && { |
| 62 | + echo "::error::Cannot determine details for PR $pr" |
| 63 | + exit 1 |
| 64 | + } |
| 65 | +
|
| 66 | + pr_labels=$(echo "$pr_details"|cut -d';' -f2|tr ',' ' ') |
| 67 | +
|
| 68 | + [ -z "$pr_labels" ] && { |
| 69 | + printf "::error::PR %s does not have required porting labels (expected one of '%s' and one of '%s')\n" \ |
| 70 | + "$pr" \ |
| 71 | + $(echo "$backport_labels" | tr ' ' ',') \ |
| 72 | + $(echo "$forward_port_labels" | tr ' ' ',') |
| 73 | + exit 1 |
| 74 | + } |
| 75 | +
|
| 76 | + backport_labels_found=() |
| 77 | +
|
| 78 | + for label in ${backport_labels[@]} |
| 79 | + do |
| 80 | + echo "$pr_labels" | grep -qw "$label" \ |
| 81 | + && backport_labels_found+=("$label") |
| 82 | + done |
| 83 | +
|
| 84 | + [ "${#backport_labels_found[@]}" -eq 0 ] && { |
| 85 | + printf "::error::PR %s missing a backport label (expected one of '%s')\n" \ |
| 86 | + "$pr" \ |
| 87 | + $(echo "$backport_labels" | tr ' ' ',') |
| 88 | + exit 1 |
| 89 | + } |
| 90 | +
|
| 91 | + [ "${#backport_labels_found[@]}" -gt 1 ] && { |
| 92 | + printf "::error::PR %s has too many backport labels (expected one of '%s', found '%s')\n" \ |
| 93 | + "$pr" \ |
| 94 | + $(echo "$backport_labels" | tr ' ' ',') \ |
| 95 | + $(echo "$backport_labels_found" | tr ' ' ',') |
| 96 | + exit 1 |
| 97 | + } |
| 98 | +
|
| 99 | + forward_port_labels_found=() |
| 100 | +
|
| 101 | + for label in ${forward_port_labels[@]} |
| 102 | + do |
| 103 | + echo "$pr_labels" | grep -qw "$label" \ |
| 104 | + && forward_port_labels_found+=("$label") |
| 105 | + done |
| 106 | +
|
| 107 | + [ "${#forward_port_labels_found[@]}" -eq 0 ] && { |
| 108 | + printf "::error::PR %s missing a forward port label (expected one of '%s')\n" \ |
| 109 | + "$pr" \ |
| 110 | + $(echo "$forward_port_labels" | tr ' ' ',') |
| 111 | + exit 1 |
| 112 | + } |
| 113 | +
|
| 114 | + [ "${#forward_port_labels_found[@]}" -gt 1 ] && { |
| 115 | + printf "::error::PR %s has too many forward port labels (expected one of '%s', found '%s')\n" \ |
| 116 | + "$pr" \ |
| 117 | + $(echo "$forward_port_labels" | tr ' ' ',') \ |
| 118 | + $(echo "$forward_port_labels_found" | tr ' ' ',') |
| 119 | + exit 1 |
| 120 | + } |
| 121 | +
|
| 122 | + [ "$backport_labels_found" = 'backport-pr' ] && \ |
| 123 | + [ "$forward_port_labels_found" = 'forward-port-pr' ] && { |
| 124 | + printf "::error::PR %s cannot be labelled as both backport ('%s') and forward port ('%s')\n" \ |
| 125 | + "$pr" \ |
| 126 | + "$backport_labels_found" \ |
| 127 | + "$forward_port_labels_found" |
| 128 | + exit 1 |
| 129 | + } |
| 130 | +
|
| 131 | + printf "::debug::PR %s has required porting labels (backport label '%s', forward port label '%s')\n" \ |
| 132 | + "$pr" \ |
| 133 | + "$backport_labels_found" \ |
| 134 | + "$forward_port_labels_found" |
0 commit comments