Skip to content

Instantly share code, notes, and snippets.

@sergiitk
Forked from ejona86/backport.sh
Last active December 16, 2023 00:01
Show Gist options
  • Save sergiitk/af606cbab72b00d8db8a53d21e766ee9 to your computer and use it in GitHub Desktop.
Save sergiitk/af606cbab72b00d8db8a53d21e766ee9 to your computer and use it in GitHub Desktop.
Custom backport script for grpc repos
#!/usr/bin/env bash
# Copyright 2020 The gRPC Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -e
display_usage() {
cat <<EOF >/dev/stderr
Usage: $0 REPO PR MINOR_VERSIONS
EXAMPLES:
$0 grpc-go 5434 47 46 45
$0 grpc 5434 47
$0 grpc-java 5434 {47..38}
$0 grpc-node 2208 5 6 7
EOF
exit 1
}
urlencode() {
python3 -c "import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1]))" "$*"
}
main() {
if (( $# < 3 )); then
display_usage
fi
readonly repo="$1"
readonly pr="$2"
readonly labels="${LABELS:-}"
shift 2
readonly minors="$@"
readonly owner="$(git remote get-url origin | sed -E "s#.*[:/]([a-zA-Z0-9-]+)/${repo}.*#\1#")"
readonly pr_metadata="$(curl -sSf -H "Accept: application/vnd.github.v3+json" "https://api.github.com/repos/grpc/${repo}/pulls/$pr")"
readonly assignee="$(echo "$pr_metadata" | jq -r '.assignee.login')"
readonly commits="$(echo "$pr_metadata" | jq -r '.merge_commit_sha')"
readonly branch_id="$(echo "$pr_metadata" | jq -r '.head.ref')"
readonly title="$(echo "$pr_metadata" | jq -r '.title')"
readonly body="$(echo "$pr_metadata" | jq -r '.body')"
cat <<EOT
PR: https://github.com/grpc/${repo}/pull/$pr
Versions: ${minors}
Commits: ${commits}
Branch id: ${branch_id}
Repo: ${owner}
Assignee: ${assignee}
Labels: ${labels}
EOT
read -n 1 -p "Continue? (y/N) " answer
if [ "$answer" != "${answer#[Yy]}" ] ;then
echo
else
echo
echo "Exit"
exit 1
fi
git fetch upstream
readonly orig_branch="$(git rev-parse --abbrev-ref HEAD)"
local pick_result
local backport_branch
local arg_title
local arg_body
local version_tag
local url
for minor in $minors; do
backport_branch="backport-1.$minor-${branch_id}"
if [[ "${repo}" == "grpc-node" ]]; then
version_tag="@grpc/grpc-js@1.${minor}.x"
else
version_tag="v1.${minor}.x"
fi
git checkout -b "${backport_branch}" "upstream/${version_tag}"
pick_result=0
git cherry-pick --rerere-autoupdate -m 1 $commits || pick_result=$?
if [[ $pick_result -ne 0 ]]; then
read -n 1 -p "Conflict detected. Resolve manually in another shell. Resolved? (y/N) " answer
if [ "$answer" != "${answer#[Yy]}" ] ;then
echo
echo "Continuing..."
else
echo
echo "Aborting. Cleaning up..."
git checkout -f "${orig_branch}"
git branch -D "${backport_branch}"
exit 1
fi
fi
git push --set-upstream origin "${backport_branch}"
arg_title="${title} (${version_tag} backport)"
arg_body=$(cat <<EOT
Backport of #${pr} to ${version_tag}.
---
${body}
EOT
)
# https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/using-query-parameters-to-create-a-pull-request
url="https://github.com/grpc/${repo}/compare"
url="${url}/$(urlencode "${version_tag}")...${owner}:${backport_branch}?quick_pull=1"
url="${url}&title=$(urlencode "${arg_title}")"
url="${url}&body=$(urlencode "${arg_body}")"
if [[ "${assignee}" != "null" ]]; then
url="${url}&assignees=${assignee}"
fi
if [[ -n "${labels}" ]]; then
url="${url}&labels=$(urlencode "${labels}")"
fi
echo
echo
echo ------------------------------------------------------
echo "${url}"
echo ------------------------------------------------------
echo
echo
done
git checkout "${orig_branch}"
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment