Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore][CI/CD] Fix bugs in get-codeowners.sh #29746

Merged
merged 8 commits into from
Dec 12, 2023
27 changes: 20 additions & 7 deletions .github/workflows/scripts/get-codeowners.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,31 @@

set -euo pipefail

get_component_type() {
echo "${COMPONENT}" | cut -f 1 -d '/'
}

get_codeowners() {
echo "$((grep -m 1 "${1}" .github/CODEOWNERS || true) | sed 's/ */ /g' | cut -f3- -d ' ')"
echo "$((grep -m 1 "^${1}/" .github/CODEOWNERS || true) | \
crobert-1 marked this conversation as resolved.
Show resolved Hide resolved
sed 's/ */ /g' | \
cut -f3- -d ' ')"
}

if [[ -z "${COMPONENT:-}" ]]; then
echo "COMPONENT has not been set, please ensure it is set."
exit 1
fi

COMPONENT_TYPE=$(get_component_type "${COMPONENT}")
CUR_DIRECTORY=$(dirname "$0")
VALID_COMPONENT=$(bash "${CUR_DIRECTORY}/get-components.sh" | grep -x "${COMPONENT}" || true)
VALID_COMPONENT_WITH_TYPE=$(bash "${CUR_DIRECTORY}/get-components.sh" | grep -x "${COMPONENT}${COMPONENT_TYPE}" || true)

if [[ -z "${VALID_COMPONENT:-}" ]] && [[ -z "${VALID_COMPONENT_WITH_TYPE:-}" ]]; then
echo ""
exit 0
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)
Expand All @@ -26,14 +42,11 @@ RESULT=$(grep -c "${COMPONENT}" .github/CODEOWNERS || true)
# if so, try to narrow things down by appending the component
# or a forward slash to the label.
if [[ ${RESULT} != 1 ]]; then
COMPONENT_TYPE=$(echo "${COMPONENT}" | cut -f 1 -d '/')
OWNERS="$(get_codeowners "${COMPONENT}${COMPONENT_TYPE}")"
fi

if [[ -z "${OWNERS:-}" ]]; then
OWNERS="$(get_codeowners "${COMPONENT}/")"
fi
else
OWNERS="$(get_codeowners $COMPONENT)"
if [[ -z "${OWNERS:-}" ]]; then
OWNERS="$(get_codeowners "${COMPONENT}")"
fi

echo "${OWNERS}"