diff --git a/build/main-branch-sync/update-owners.sh b/build/main-branch-sync/update-owners.sh index 6beb7906..4751e91e 100755 --- a/build/main-branch-sync/update-owners.sh +++ b/build/main-branch-sync/update-owners.sh @@ -11,10 +11,22 @@ # ######################## +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") @@ -22,25 +34,27 @@ 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 @@ -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:"