#!/bin/bash #Copyright 2021 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 -euo pipefail ensure_command () { if command -v "$1" 1>/dev/null 2>&1; then return 0 else echo "$1 is not installed. Please install it to proceed." 1>&2 exit 1 fi } display_usage () { cat << EOF >/dev/stderr USAGE: $0 PR_ID GITHUB_USER BACKPORT_BRANCHES REVIEWERS [-c PER_BACKPORT_COMMAND] PR_ID: The ID of the PR to be backported. GITHUB_USER: Your GitHub username. BACKPORT_BRANCHES: A space-separated list of branches to which the source PR will be backported. REVIEWERS: A comma-separated list of users to add as both reviewer and assignee. PER_BACKPORT_COMMAND : An optional command to run after cherrypicking the PR to the target branch. If you use this option, ensure your working directory is clean, as "git add -A" will be used to incorporate any generated files. Try running "git clean -xdff" beforehand. Example: $0 25456 gnossen "v1.30.x v1.31.x v1.32.x v1.33.x v1.34.x v1.35.x v1.36.x" "menghanl,gnossen" Example: $0 25493 gnossen "\$(seq 30 33 | xargs -n1 printf 'v1.%s.x ')" "menghanl" -c ./tools/dockerfile/push_testing_images.sh EOF exit 1 } ensure_command "curl" ensure_command "egrep" ensure_command "hub" ensure_command "jq" if [ "$#" -lt "4" ]; then display_usage fi PR_ID="$1" GITHUB_USER="$2" BACKPORT_BRANCHES="$3" REVIEWERS="$4" shift 4 PER_BACKPORT_COMMAND="" while getopts "c:" OPT; do case "$OPT" in c ) PER_BACKPORT_COMMAND="$OPTARG" ;; \? ) echo "Invalid option: $OPTARG" >/dev/stderr display_usage ;; : ) echo "Invalid option: $OPTARG requires an argument." >/dev/stderr display_usage ;; esac done if [[ ! -z "$(git status --porcelain)" && ! -z "$PER_BACKPORT_COMMAND" ]]; then echo "Your working directory is not clean. Try running `git clean -xdff`. Warning: This is irreversible." > /dev/stderr exit 1 fi if [ -z "$GITHUB_TOKEN" ]; then echo "A GitHub token is required to run this script. See " \ "https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token" \ " for more information" >/dev/stderr exit 1 fi echo "This script will create a collection of backport PRs. You will probably " \ "have to touch your gnubby a frustrating number of times. C'est la vie." printf "Press any key to continue." read -r RESPONSE