Skip to content

Commit

Permalink
Fix owners update script
Browse files Browse the repository at this point in the history
- Make paths relative to the script
- Add upstream repos
- Add logic for when there are multiple OWNERS
files in a repo

Signed-off-by: Dale Haiducek <[email protected]>
  • Loading branch information
dhaiducek authored and openshift-merge-bot[bot] committed Dec 6, 2024
1 parent 26707bd commit 7bcd36a
Showing 1 changed file with 40 additions and 20 deletions.
60 changes: 40 additions & 20 deletions build/main-branch-sync/update-owners.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,50 @@
#
########################

set -eo pipefail

script_path="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"

CHECKOUT_BRANCH=${CHECKOUT_BRANCH:-""}
OWNERS_FILE_NAME="OWNERS"
GITHUB_ORG=${GITHUB_ORG:-"stolostron"}
REPOS="${REPOS:-$(cat repo.txt && cat repo-extra.txt)}"
REPOS="${REPOS:-"$(
{
${script_path}/fetch-repo-list.sh || exit 1
cat ${script_path}/repo.txt
cat ${script_path}/repo-extra.txt
} | awk -F'/' '!a[$2]++'
cat ${script_path}/repo-owners.txt
)"}"

UPDATED_REPOS=()
if [[ "${GITHUB_ORG}" != "stolostron" ]]; then
REPOS=$(echo "${REPOS}" | sed "s/stolostron/${GITHUB_ORG}/g")
fi

for repo in ${REPOS}; do
# Handle custom paths
OWNERS_PATH="."
if [[ "$repo" == *","* ]]; then
OWNERS_PATH="${OWNERS_PATH}/${repo##*,}"
ignore_clone_err=false
OWNERS_PATH="${OWNERS_FILE_NAME}"
if [[ "${repo}" == *","* ]]; then
ignore_clone_err=true
OWNERS_PATH="${repo##*,}/${OWNERS_FILE_NAME}"
repo="${repo%%,*}"
fi
OWNERS_PATH="${OWNERS_PATH}/${OWNERS_FILE_NAME}"

# Clone repo
echo "==="
printf '%s\n' "Updating $repo ${OWNERS_PATH} ..."
MESSAGE=$(curl -s https://api.github.com/repos/$repo | jq .message)
printf '%s\n' "* Updating ${repo} ${OWNERS_PATH} ..."
MESSAGE=$(curl -s https://api.github.com/repos/${repo} | jq .message)
if [[ "${MESSAGE}" == '"Not Found"' ]]; then
continue
fi
git clone --quiet https://github.com/$repo.git $repo
if ! git clone --quiet https://github.com/$repo.git ${script_path}/$repo && [[ ${ignore_clone_err} == false ]]; then
exit 1
fi

# Checkout target branch if specified
GIT="git -C ${repo}"
OWNERS_PATH="${repo}/${OWNERS_PATH}"
GIT="git -C ${script_path}/${repo}"
OWNERS_PATH="${script_path}/${repo}/${OWNERS_PATH}"
BRANCH_NAME="update-owners"
COMMIT_MSG="Update ${OWNERS_FILE_NAME}"
if [[ -n "${CHECKOUT_BRANCH}" ]]; then
Expand All @@ -50,30 +64,36 @@ for repo in ${REPOS}; do
fi
# Update OWNERS file
if [[ -f "${OWNERS_PATH}" ]]; then
${GIT} checkout -b ${BRANCH_NAME}
branch_update=false
${GIT} checkout -b ${BRANCH_NAME} || branch_update=true
if [[ -n "${NEW_OWNERS}" ]]; then
for owner_name in $NEW_OWNERS; do
for owner_name in ${NEW_OWNERS}; do
new_owner="${owner_name}" yq e '(.approvers, .reviewers) |= (. + env(new_owner) | unique)' -i ${OWNERS_PATH}
done
fi
if [[ -n "${DELETE_OWNERS}" ]]; then
for owner_name in $DELETE_OWNERS; do
for owner_name in ${DELETE_OWNERS}; do
delete_owner="${owner_name}" yq e '(.approvers[], .reviewers[]) |= del(select(. == env(delete_owner)))' -i ${OWNERS_PATH}
done
fi
sed -i '' 's/^ //g' ${OWNERS_PATH}
if ! git diff --exit-code &>/dev/null; then
${GIT} commit --signoff -am "${COMMIT_MSG}"
${GIT} push --set-upstream origin ${BRANCH_NAME} && UPDATED_REPOS+=("$repo")
if ! ${GIT} diff --exit-code; then
force=""
if [[ ${branch_update} == "false" ]]; then
${GIT} commit -S --signoff -am "${COMMIT_MSG}"
else
${GIT} commit -S --signoff -a --amend --no-edit
force="--force"
fi
${GIT} push ${force} --set-upstream origin ${BRANCH_NAME} && UPDATED_REPOS+=("${script_path}/${repo}")
else
echo "No changes detected. Skipping update."
echo " No changes detected. Skipping update."
fi
else
echo "File not found: ${OWNERS_PATH}"
echo " File not found: ${OWNERS_PATH}"
fi
done


echo "==="
if [[ "${#UPDATED_REPOS[@]}" != "0" ]]; then
echo "URLs to open PRs:"
Expand Down

0 comments on commit 7bcd36a

Please sign in to comment.