Merge pull request #7114 from prometheus/superq/refactor_sync_makefiles

Refactor makefile script
This commit is contained in:
Ben Kochie 2020-04-15 13:50:49 +02:00 committed by GitHub
commit 59d01913b1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,5 +1,5 @@
#!/bin/bash #!/usr/bin/env bash
# vim: ts=2 et
# Setting -x is absolutely forbidden as it could leak the GitHub token. # Setting -x is absolutely forbidden as it could leak the GitHub token.
set -uo pipefail set -uo pipefail
@ -15,59 +15,84 @@ orgs="prometheus prometheus-community"
GITHUB_TOKEN="${GITHUB_TOKEN:-}" GITHUB_TOKEN="${GITHUB_TOKEN:-}"
if [ -z "${GITHUB_TOKEN}" ]; then if [ -z "${GITHUB_TOKEN}" ]; then
echo -e "\e[31mGitHub token (GITHUB_TOKEN) not set. Terminating.\e[0m" echo -e "\e[31mGitHub token (GITHUB_TOKEN) not set. Terminating.\e[0m"
exit 1 exit 1
fi fi
# Go to the root of the repo # Go to the root of the repo
cd "$(git rev-parse --show-cdup)" cd "$(git rev-parse --show-cdup)" || exit 1
source_makefile="$(pwd)/Makefile.common" source_makefile="$(pwd)/Makefile.common"
source_checksum="$(sha256sum Makefile.common | cut -d' ' -f1)" source_checksum="$(sha256sum Makefile.common | cut -d' ' -f1)"
tmp_dir=$(mktemp -d) tmp_dir="$(mktemp -d)"
trap "rm -rf ${tmp_dir}" EXIT trap 'rm -rf "${tmp_dir}"' EXIT
for org in $orgs fetch_repos() {
do local url="https://api.github.com/users/${1}/repos?per_page=100"
# Iterate over all repositories in ${org}. The GitHub API can return 100 items curl --retry 5 --silent -u "${git_user}:${GITHUB_TOKEN}" "${url}" 2>/dev/null |
# at most but it should be enough for us as there are less than 40 repositories jq -r '.[] | select( .name != "prometheus" ) | .name'
# currently. }
curl --retry 5 --silent -u "${git_user}:${GITHUB_TOKEN}" https://api.github.com/users/${org}/repos?per_page=100 2>/dev/null | jq -r '.[] | select( .name != "prometheus" ) | .name' | while read -r; do
repo="${REPLY}"
echo -e "\e[32mAnalyzing '${repo}'\e[0m"
target_makefile="$(curl -s --fail "https://raw.githubusercontent.com/${org}/${repo}/master/Makefile.common")" push_branch() {
if [ -z "${target_makefile}" ]; then # stdout and stderr are redirected to /dev/null otherwise git-push could leak
echo "Makefile.common doesn't exist in ${repo}" # the token in the logs.
continue git push --quiet \
fi "https://${GITHUB_TOKEN}:@github.com/${1}" \
target_checksum="$(echo ${target_makefile} | sha256sum | cut -d' ' -f1)" --set-upstream "${branch}" 1>/dev/null 2>&1
if [ "${source_checksum}" == "${target_checksum}" ]; then }
echo "Makefile.common is already in sync."
continue
fi
# Clone target repo to temporary directory and checkout to new branch post_template='{"title":"%s","base":"master","head":"%s","body":"%s"}'
git clone --quiet "https://github.com/${org}/${repo}.git" "${tmp_dir}/${repo}" post_json="$(printf "${post_template}" "${pr_title}" "${branch}" "${pr_msg}")"
cd "${tmp_dir}/${repo}" post_pull_request() {
git checkout -b "${branch}" curl --show-error --silent --fail \
-u "${git_user}:${GITHUB_TOKEN}" \
-d "${post_json}" \
"https://api.github.com/repos/${1}/pulls"
}
# Replace Makefile.common in target repo by one from prometheus/prometheus process_repo() {
cp -f "${source_makefile}" ./ local org_repo="$1"
if [ -n "$(git status --porcelain)" ]; then echo -e "\e[32mAnalyzing '${org_repo}'\e[0m"
git config user.email "${git_mail}"
git config user.name "${git_user}" target_makefile="$(curl -s --fail "https://raw.githubusercontent.com/${org_repo}/master/Makefile.common")"
git add . if [ -z "${target_makefile}" ]; then
git commit -s -m "${commit_msg}" echo "Makefile.common doesn't exist in ${org_repo}"
# stdout and stderr are redirected to /dev/null otherwise git-push could leak the token in the logs. return
if git push --quiet "https://${GITHUB_TOKEN}:@github.com/${org}/${repo}" --set-upstream "${branch}" 1>/dev/null 2>&1; then fi
curl --show-error --silent \ target_checksum="$(echo "${target_makefile}" | sha256sum | cut -d' ' -f1)"
-u "${git_user}:${GITHUB_TOKEN}" \ if [ "${source_checksum}" == "${target_checksum}" ]; then
-X POST \ echo "Makefile.common is already in sync."
-d "{\"title\":\"${pr_title}\",\"base\":\"master\",\"head\":\"${branch}\",\"body\":\"${pr_msg}\"}" \ return
"https://api.github.com/repos/${org}/${repo}/pulls" fi
fi
fi # Clone target repo to temporary directory and checkout to new branch
done git clone --quiet "https://github.com/${org_repo}.git" "${tmp_dir}/${org_repo}"
cd "${tmp_dir}/${org_repo}" || return 1
git checkout -b "${branch}" || return 1
# Replace Makefile.common in target repo by one from prometheus/prometheus
cp -f "${source_makefile}" ./
if [ -n "$(git status --porcelain)" ]; then
git config user.email "${git_mail}"
git config user.name "${git_user}"
git add .
git commit -s -m "${commit_msg}"
if push_branch "${org_repo}"; then
post_pull_request "${org_repo}"
fi
fi
}
for org in ${orgs}; do
mkdir -p "${tmp_dir}/${org}"
# Iterate over all repositories in ${org}. The GitHub API can return 100 items
# at most but it should be enough for us as there are less than 40 repositories
# currently.
fetch_repos "${org}" | while read -r repo; do
if ! process_repo "${org}/${repo}"; then
echo "Failed to process '${org}/${repo}'"
exit 1
fi
done
done done