Skip to content

Instantly share code, notes, and snippets.

@bartoszmajsak
Created October 25, 2022 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bartoszmajsak/db54cab0e5b0dcd9e09b70fd2d7e3b3b to your computer and use it in GitHub Desktop.
Save bartoszmajsak/db54cab0e5b0dcd9e09b70fd2d7e3b3b to your computer and use it in GitHub Desktop.
New branch - Prow jobs automation
#!/bin/bash
set -eo pipefail
die () {
echo >&2 "$@"
show_help
exit 1
}
show_help() {
local usage
usage="
$(basename "$0")
Usage:
./$(basename "$0") [flags]
Options:
--repo
name of the repository to clone jobs in
--file
file to apply changes to
--previous
new branch version
--new
new branch version
--dry-run
Boolean flag indicating if actual changes on the file or printed to STDOUT
-h, --help
Help message.
Example:
./$(basename "$0") --repo maistra/istio --from 2.2 --to 2.3 --file presubmits.yaml
"
echo "$usage"
}
TMP_FILE=$(mktemp -q /tmp/"${file}".XXXXXX)
function stdout_or_in_place {
if $dryRun; then
cat
else
cat > "${TMP_FILE}" && cat "${TMP_FILE}" > "${file}"
fi
}
dryRun=false
while test $# -gt 0; do
case "$1" in
-h|--help)
show_help
exit 0
;;
--dry-run)
dryRun=true
shift
;;
--repo)
if [[ $1 == "--"* ]]; then
repo="${2/--/}"
shift
fi
shift
;;
--previous)
if [[ $1 == "--"* ]]; then
previous="${2/--/}"
shift
fi
shift
;;
--new)
if [[ $1 == "--"* ]]; then
branch="${2/--/}"
shift
fi
shift
;;
--file)
if [[ $1 == "--"* ]]; then
file="${2/--/}"
shift
fi
shift
;;
*)
die "$(basename "$0"): unknown flag $(echo $1 | cut -d'=' -f 1)"
exit 1
;;
esac
done
if [ -z "$repo" ] || [ -z "$file" ] || [ -z "$branch" ] || [ -z "$previous" ]; then
die "Missing required flags!"
fi
if ! command -v yq &>/dev/null; then
echo "yq is required. Please follow installation guide https://github.com/mikefarah/yq/#install"
exit 1
fi
## Add new branch
matchingBranches="(.*.${repo}.[] | select (.branches[] == \"^${previous}$\"))"
yq "${matchingBranches}.branches |= reverse" "${file}" \
| yq "${matchingBranches}.branches += [ \"^${branch}$\" ]" - \
| yq "${matchingBranches}.branches |= reverse" - \
| stdout_or_in_place
#!/bin/bash
set -eo pipefail
die () {
echo >&2 "$@"
show_help
exit 1
}
show_help() {
local usage
usage="
$(basename "$0")
Usage:
./$(basename "$0") [flags]
Options:
--repo
name of the repository to clone jobs in
--file
file to apply changes to
--from
old branch version
--to
new branch version
--dry-run
Boolean flag indicating if actual changes on the file or printed to STDOUT
-h, --help
Help message.
Example:
./$(basename "$0") --repo maistra/istio --from 2.2 --to 2.3 --file presubmits.yaml
"
echo "$usage"
}
TMP_FILE=$(mktemp -q /tmp/"${file}".XXXXXX)
function stdout_or_in_place {
if $dryRun; then
cat
else
cat > "${TMP_FILE}" && cat "${TMP_FILE}" > "${file}"
fi
}
dryRun=false
while test $# -gt 0; do
case "$1" in
-h|--help)
show_help
exit 0
;;
--dry-run)
dryRun=true
shift
;;
--repo)
if [[ $1 == "--"* ]]; then
repo="${2/--/}"
shift
fi
shift
;;
--from)
if [[ $1 == "--"* ]]; then
from="${2/--/}"
shift
fi
shift
;;
--to)
if [[ $1 == "--"* ]]; then
to="${2/--/}"
shift
fi
shift
;;
--file)
if [[ $1 == "--"* ]]; then
file="${2/--/}"
shift
fi
shift
;;
*)
die "$(basename "$0"): unknown flag $(echo $1 | cut -d'=' -f 1)"
exit 1
;;
esac
done
if [ -z "$repo" ] || [ -z "$file" ] || [ -z "$from" ] || [ -z "$to" ]; then
die "Missing required flags!"
fi
if ! command -v yq &>/dev/null; then
echo "yq is required. Please follow installation guide https://github.com/mikefarah/yq/#install"
exit 1
fi
## Duplicate jobs
yq "[.*.${repo}.[] | select (.name == \"*-${from}\")]" "${file}" \
| sed "s/-${from}/-${to}/g" \
| yq ea "select(fi == 0) as \$job | select(fi==1) | .*.${repo} += \$job " - "${file}" \
| stdout_or_in_place
@bartoszmajsak
Copy link
Author

Usage (passing --dry-run won't overwrite the file):

REPOS=('maistra/istio-must-gather' 'maistra/header-append-filter')
printf '%s\n' ${REPOS[@]} | xargs -I REPO ./add-branch.sh --file presubmits.yaml --repo REPO --previous maistra-2.3 --new maistra-2.4
./clone-jobs.sh --repo maistra/istio --from 2.2 --to 2.3 --file presubmits.yaml  

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment