diff --git a/.github/workflows/scripts/get-codeowners.sh b/.github/workflows/scripts/get-codeowners.sh index 3bd3c2f3edb5..8d84b99e7787 100755 --- a/.github/workflows/scripts/get-codeowners.sh +++ b/.github/workflows/scripts/get-codeowners.sh @@ -14,7 +14,13 @@ get_component_type() { } get_codeowners() { - echo "$((grep -m 1 "^${1}/\s" .github/CODEOWNERS || true) | \ + # grep arguments explained: + # -m 1: Match the first occurrence + # ^: Match from the beginning of the line + # ${1}: Insert first argument given to this function + # [\/]\?: Match 0 or 1 instances of a forward slash + # \s: Match any whitespace character + echo "$((grep -m 1 "^${1}[\/]\?\s" .github/CODEOWNERS || true) | \ sed 's/ */ /g' | \ cut -f3- -d ' ')" } @@ -24,20 +30,11 @@ if [[ -z "${COMPONENT:-}" ]]; then exit 1 fi -# grep exits with status code 1 if there are no matches, -# so we manually set RESULT to 0 if nothing is found. -RESULT=$(grep -c "${COMPONENT}" .github/CODEOWNERS || true) +OWNERS="$(get_codeowners "${COMPONENT}")" -# there may be more than 1 component matching a label -# if so, try to narrow things down by appending the component -# or a forward slash to the label. -if [[ ${RESULT} != 1 ]]; then +if [[ -z "${OWNERS:-}" ]]; then COMPONENT_TYPE=$(get_component_type "${COMPONENT}") OWNERS="$(get_codeowners "${COMPONENT}${COMPONENT_TYPE}")" fi -if [[ -z "${OWNERS:-}" ]]; then - OWNERS="$(get_codeowners "${COMPONENT}")" -fi - echo "${OWNERS}"