Improve sync_repo_files.sh

* Add github_api function to make curl use consistent.
* Fix up some shellcheck warnings.
* Add some more output debugging to detect failed pushes.
* Fix git push auth string.
* Fix open PR pre-check.

Signed-off-by: Ben Kochie <superq@gmail.com>
pull/8605/head
Ben Kochie 4 years ago
parent 1cba174182
commit 2d9f3e34dd
No known key found for this signature in database
GPG Key ID: C646B23C9E3245F1

@ -30,35 +30,41 @@ source_dir="$(pwd)"
tmp_dir="$(mktemp -d)" tmp_dir="$(mktemp -d)"
trap 'rm -rf "${tmp_dir}"' EXIT trap 'rm -rf "${tmp_dir}"' EXIT
get_default_branch(){ github_api() {
local url="https://api.github.com/repos/${1}" local url
curl --retry 5 --silent -u "${git_user}:${GITHUB_TOKEN}" "${url}" 2>/dev/null | jq -r .default_branch url="https://api.github.com/${1}"
shift 1
curl --retry 5 --silent --fail -u "${git_user}:${GITHUB_TOKEN}" "${url}" "$@"
}
get_default_branch() {
github_api "repos/${1}" 2> /dev/null |
jq -r .default_branch
} }
fetch_repos() { fetch_repos() {
local url="https://api.github.com/users/${1}/repos?per_page=100" github_api "users/${1}/repos?per_page=100" 2> /dev/null |
curl --retry 5 --silent -u "${git_user}:${GITHUB_TOKEN}" "${url}" 2>/dev/null |
jq -r '.[] | select( .name != "prometheus" ) | .name' jq -r '.[] | select( .name != "prometheus" ) | .name'
} }
push_branch() { push_branch() {
local git_url
git_url="https://${git_user}:${GITHUB_TOKEN}@github.com/${1}"
# stdout and stderr are redirected to /dev/null otherwise git-push could leak # stdout and stderr are redirected to /dev/null otherwise git-push could leak
# the token in the logs. # the token in the logs.
# Delete the remote branch in case it was merged but not deleted. # Delete the remote branch in case it was merged but not deleted.
git push --quiet "https://${git_user}:${GITHUB_TOKEN}:@github.com/${1}" \ git push --quiet "${git_url}" ":${branch}" 1>/dev/null 2>&1
":${branch}" 1>/dev/null 2>&1 git push --quiet "${git_url}" --set-upstream "${branch}" 1>/dev/null 2>&1
git push --quiet \
"https://${git_user}:${GITHUB_TOKEN}:@github.com/${1}" \
--set-upstream "${branch}" 1>/dev/null 2>&1
} }
post_pull_request() { post_pull_request() {
post_template='{"title":"%s","base":"%s","head":"%s","body":"%s"}' local repo="$1"
post_json="$(printf "${post_template}" "${pr_title}" "${2}" "${branch}" "${pr_msg}")" local default_branch="$2"
curl --show-error --silent --fail \ local post_json
-u "${git_user}:${GITHUB_TOKEN}" \ post_json="$(printf '{"title":"%s","base":"%s","head":"%s","body":"%s"}' "${pr_title}" "${default_branch}" "${branch}" "${pr_msg}")"
-d "${post_json}" \ echo "Posting PR to ${default_branch} on ${repo}"
"https://api.github.com/repos/${1}/pulls" github_api "repos/${repo}/pulls" --data "${post_json}" --show-error |
jq -r '"PR URL " + .html_url'
} }
check_license() { check_license() {
@ -67,14 +73,17 @@ check_license() {
} }
process_repo() { process_repo() {
local org_repo="$1" local org_repo
local default_branch
org_repo="$1"
echo -e "\e[32mAnalyzing '${org_repo}'\e[0m" echo -e "\e[32mAnalyzing '${org_repo}'\e[0m"
default_branch="$(get_default_branch ${1})" default_branch="$(get_default_branch "${org_repo}")"
if [[ -z "${default_branch}" ]]; then if [[ -z "${default_branch}" ]]; then
echo "Can't get the default branch." echo "Can't get the default branch."
return return
fi fi
echo "Default branch: ${default_branch}"
local needs_update=() local needs_update=()
for source_file in ${SYNC_FILES}; do for source_file in ${SYNC_FILES}; do
@ -100,6 +109,7 @@ process_repo() {
echo "${source_file} is already in sync." echo "${source_file} is already in sync."
continue continue
fi fi
echo "${source_file} needs updating."
needs_update+=("${source_file}") needs_update+=("${source_file}")
done done
@ -114,17 +124,22 @@ process_repo() {
git checkout -b "${branch}" || return 1 git checkout -b "${branch}" || return 1
# Update the files in target repo by one from prometheus/prometheus. # Update the files in target repo by one from prometheus/prometheus.
for source_file in ${needs_update[@]}; do for source_file in "${needs_update[@]}"; do
cp -f "${source_dir}/${source_file}" "./${source_file}" cp -f "${source_dir}/${source_file}" "./${source_file}"
done done
if [ -n "$(git status --porcelain)" ]; then if [[ -n "$(git status --porcelain)" ]]; then
git config user.email "${git_mail}" git config user.email "${git_mail}"
git config user.name "${git_user}" git config user.name "${git_user}"
git add . git add .
git commit -s -m "${commit_msg}" git commit -s -m "${commit_msg}"
if push_branch "${org_repo}"; then if push_branch "${org_repo}"; then
post_pull_request "${org_repo}" "${default_branch}" if ! post_pull_request "${org_repo}" "${default_branch}"; then
return 1
fi
else
echo "Pushing ${branch} to ${org_repo} failed"
return 1
fi fi
fi fi
} }
@ -136,9 +151,8 @@ for org in ${orgs}; do
# currently. # currently.
fetch_repos "${org}" | while read -r repo; do fetch_repos "${org}" | while read -r repo; do
# Check if a PR is already opened for the branch. # Check if a PR is already opened for the branch.
prLink=$(curl --show-error --silent \ fetch_uri="repos/${org}/${repo}/pulls?state=open&head=${org}:${branch}"
-u "${git_user}:${GITHUB_TOKEN}" \ prLink="$(github_api "${fetch_uri}" --show-error | jq -r '.[0].html_url')"
"https://api.github.com/repos/${org}/${repo}/pulls?head=${repo}:${branch}" | jq '.[0].url')
if [[ "${prLink}" != "null" ]]; then if [[ "${prLink}" != "null" ]]; then
echo "Pull request already opened for branch '${branch}': ${prLink}" echo "Pull request already opened for branch '${branch}': ${prLink}"
echo "Either close it or merge it before running this script again!" echo "Either close it or merge it before running this script again!"

Loading…
Cancel
Save