github.com/kyma-project/kyma-environment-broker@v0.0.1/scripts/label_validator.sh (about) 1 #!/usr/bin/env bash 2 3 # standard bash error handling 4 set -o nounset # treat unset variables as an error and exit immediately. 5 set -o errexit # exit immediately when a command fails. 6 set -E # must be set if you want the ERR trap 7 set -o pipefail # prevents errors in a pipeline from being masked 8 9 GITHUB_ORG="kyma-project" 10 11 # From Github API Docs on why use API for Issue 12 # You can use the REST API to create comments on issues and pull requests. Every pull request is an issue, but not every issue is a pull request. 13 14 # Event which triggers execution of script. Can be RELEASE or PR 15 TRIGGER_EVENT=$1 16 PR_ID=${2:-NA} 17 18 function runOnRelease() { 19 latest=$(curl -sL \ 20 -H "X-GitHub-Api-Version: 2022-11-28" \ 21 -H "Authorization: Bearer ${GITHUB_TOKEN}" \ 22 -sS "https://api.github.com/repos/$GITHUB_ORG/kyma-environment-broker/releases/latest" | 23 jq -r 'if has("tag_name") then .tag_name else empty end') 24 25 if [[ -z $latest ]]; then 26 echo 'not found latest release, nothing to compare' 27 exit 1 28 fi 29 30 echo "latest release found: $latest" 31 32 supported_labels=$(yq eval '.changelog.categories.[].labels' ./.github/release.yml | grep "\- kind"| sed -e 's/- //g' | cut -d "#" -f 1) 33 supported_labels=$(echo "${supported_labels[*]}" | tr " " "\n" ) 34 35 notValidPrs=() 36 while read -r commit; do 37 if [[ -z $commit ]]; then 38 continue 39 fi 40 41 pr_id=$(curl -sL \ 42 -H "Accept: application/vnd.github+json" \ 43 -H "X-GitHub-Api-Version: 2022-11-28" \ 44 -H "Authorization: Bearer ${GITHUB_TOKEN}" \ 45 "https://api.github.com/search/issues?q=$commit+repo:$GITHUB_ORG/btp-manager+type:pr" | 46 jq 'if (.items | length) == 1 then .items[0].number else empty end') 47 48 if [[ -z $pr_id ]]; then 49 echo "not found PR for commit $commit" 50 continue 51 fi 52 53 echo "for commit $commit found PR $pr_id" 54 55 if [[ " ${notValidPrs[*]} " =~ " ${pr_id} " ]]; then 56 continue 57 fi 58 59 present_labels=$(curl -sL \ 60 -H "Accept: application/vnd.github+json" \ 61 -H "X-GitHub-Api-Version: 2022-11-28" \ 62 https://api.github.com/repos/$GITHUB_ORG/btp-manager/issues/${pr_id} | 63 jq -r 'if (.labels | length) > 0 then .labels[] | objects | .name else empty end') 64 65 if [[ -z $present_labels ]]; then 66 echo "PR $pr_id dosent have any label" 67 notValidPrs+=("$pr_id") 68 continue 69 fi 70 71 count_of_required_labels=$(grep -o -w -F -c "${supported_labels}" <<< "$present_labels" || true) 72 if [[ $count_of_required_labels -eq 0 ]]; then 73 echo "PR $pr_id dosent have any /kind label" 74 notValidPrs+=("$pr_id") 75 continue 76 fi 77 if [[ $count_of_required_labels -gt 1 ]]; then 78 (echo -n "PR $pr_id have $count_of_required_labels /kind labels -> " && echo $present_labels | tr "," "\t") 79 notValidPrs+=("$pr_id") 80 continue 81 fi 82 echo "commit $commit in PR $pr_id have 1 label $present_labels" 83 done <<< "$(git log "$latest"..HEAD --pretty=tformat:"%h")" 84 85 if [ ${#notValidPrs[@]} -gt 0 ]; then 86 echo "following PRs do not have correct number of /kind labels" 87 for pr in "${notValidPrs[@]}" 88 do 89 echo "https://github.com/$GITHUB_ORG/btp-manager/pull/$pr" 90 done 91 exit 1 92 fi 93 94 echo "label validation OK" 95 exit 0 96 } 97 98 function runOnPr() { 99 if [[ $PR_ID == "NA" ]]; then 100 echo "PR ID not passed" 101 exit 1 102 fi 103 104 supported_labels=() 105 106 help_message="**Add one of following labels** <br/><br/>" 107 108 while IFS= read -r label; do 109 parts=$(tr "#" " " <<< "$label") 110 set $parts 111 label_part=$1; help_message_part=$@ 112 help_message="${help_message} - $label_part -> $help_message_part <br/><br/>" 113 supported_labels+=($label_part) 114 done <<< "$(yq eval '.changelog.categories.[].labels' ./.github/release.yml | grep "\- kind"| sed -e 's/- //g')" 115 supported_labels=$(echo "${supported_labels[*]}" | tr " " "\n" ) 116 117 comments=$(curl -sL \ 118 -H "Accept: application/vnd.github+json" \ 119 -H "X-GitHub-Api-Version: 2022-11-28" \ 120 -H "Authorization: Bearer ${GITHUB_TOKEN}" \ 121 https://api.github.com/repos/$GITHUB_ORG/btp-manager/issues/${PR_ID}/comments | 122 jq -r 'if (.[] | length) > 0 then .[] | objects | .body else empty end') 123 124 if [[ ! " ${comments[*]} " =~ " ${help_message} " ]]; then 125 126 payload=$(jq -n \ 127 --arg body "$help_message" \ 128 '{ 129 "body": $body, 130 }') 131 132 response=$(curl -sL \ 133 -X POST \ 134 -H "Accept: application/vnd.github+json" \ 135 -H "Authorization: Bearer ${GITHUB_TOKEN}" \ 136 -H "X-GitHub-Api-Version: 2022-11-28" \ 137 https://api.github.com/repos/$GITHUB_ORG/btp-manager/issues/${PR_ID}/comments \ 138 -d "$payload") 139 140 echo "create comment with help result: $response" 141 fi 142 143 present_labels=$(curl -sL \ 144 -H "Accept: application/vnd.github+json" \ 145 -H "X-GitHub-Api-Version: 2022-11-28" \ 146 -H "Authorization: Bearer ${GITHUB_TOKEN}" \ 147 https://api.github.com/repos/$GITHUB_ORG/btp-manager/issues/${PR_ID} | 148 jq -r 'if (.labels | length) > 0 then .labels[] | objects | .name else empty end') 149 150 count_of_required_labels=$(grep -o -w -F -c "${supported_labels}" <<< "$present_labels" || true) 151 if [[ $count_of_required_labels -eq 1 ]]; then 152 echo "label validation OK" 153 exit 0 154 fi 155 156 echo "error: only 1 of following labels must be added to each PR before merge but found $count_of_required_labels:" 157 echo "${supported_labels}" 158 exit 1 159 } 160 161 case $TRIGGER_EVENT in 162 "RELEASE") 163 runOnRelease 164 ;; 165 "PR") 166 runOnPr 167 ;; 168 *) 169 echo "unsupported trigger event: $TRIGGER_EVENT" 170 exit 1 171 ;; 172 esac