backport_pr.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/bin/bash
  2. #Copyright 2021 The gRPC authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. set -euo pipefail
  16. ensure_command () {
  17. if command -v "$1" 1>/dev/null 2>&1; then
  18. return 0
  19. else
  20. echo "$1 is not installed. Please install it to proceed." 1>&2
  21. exit 1
  22. fi
  23. }
  24. display_usage () {
  25. cat << EOF >/dev/stderr
  26. USAGE: $0 PR_ID GITHUB_USER BACKPORT_BRANCHES REVIEWERS [-c PER_BACKPORT_COMMAND]
  27. PR_ID: The ID of the PR to be backported.
  28. GITHUB_USER: Your GitHub username.
  29. BACKPORT_BRANCHES: A space-separated list of branches to which the source PR will be backported.
  30. REVIEWERS: A comma-separated list of users to add as both reviewer and assignee.
  31. PER_BACKPORT_COMMAND : An optional command to run after cherrypicking the PR to the target branch.
  32. If you use this option, ensure your working directory is clean, as "git add -A" will be used to
  33. incorporate any generated files. Try running "git clean -xdff" beforehand.
  34. 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"
  35. Example: $0 25493 gnossen "\$(seq 30 33 | xargs -n1 printf 'v1.%s.x ')" "menghanl" -c ./tools/dockerfile/push_testing_images.sh
  36. EOF
  37. exit 1
  38. }
  39. ensure_command "curl"
  40. ensure_command "egrep"
  41. ensure_command "hub"
  42. ensure_command "jq"
  43. if [ "$#" -lt "4" ]; then
  44. display_usage
  45. fi
  46. PR_ID="$1"
  47. GITHUB_USER="$2"
  48. BACKPORT_BRANCHES="$3"
  49. REVIEWERS="$4"
  50. shift 4
  51. PER_BACKPORT_COMMAND=""
  52. while getopts "c:" OPT; do
  53. case "$OPT" in
  54. c )
  55. PER_BACKPORT_COMMAND="$OPTARG"
  56. ;;
  57. \? )
  58. echo "Invalid option: $OPTARG" >/dev/stderr
  59. display_usage
  60. ;;
  61. : )
  62. echo "Invalid option: $OPTARG requires an argument." >/dev/stderr
  63. display_usage
  64. ;;
  65. esac
  66. done
  67. if [[ ! -z "$(git status --porcelain)" && ! -z "$PER_BACKPORT_COMMAND" ]]; then
  68. echo "Your working directory is not clean. Try running `git clean -xdff`. Warning: This is irreversible." > /dev/stderr
  69. exit 1
  70. fi
  71. if [ -z "$GITHUB_TOKEN" ]; then
  72. echo "A GitHub token is required to run this script. See " \
  73. "https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token" \
  74. " for more information" >/dev/stderr
  75. exit 1
  76. fi
  77. echo "This script will create a collection of backport PRs. You will probably " \
  78. "have to touch your gnubby a frustrating number of times. C'est la vie."
  79. printf "Press any key to continue."
  80. read -r RESPONSE </dev/tty
  81. printf "\n"
  82. PR_DATA=$(curl -s -u "$GITHUB_USER:$GITHUB_TOKEN" \
  83. -H "Accept: application/vnd.github.v3+json" \
  84. "https://api.github.com/repos/grpc/grpc/pulls/$PR_ID")
  85. STATE=$(echo "$PR_DATA" | jq -r '.state')
  86. if [ "$STATE" != "open" ]; then
  87. TARGET_COMMITS=$(echo "$PR_DATA" | jq -r '.merge_commit_sha')
  88. FETCH_HEAD_REF=$(echo "$PR_DATA" | jq -r '.base.ref')
  89. SOURCE_REPO=$(echo "$PR_DATA" | jq -r '.base.repo.full_name')
  90. else
  91. COMMITS_URL=$(echo "$PR_DATA" | jq -r '.commits_url')
  92. COMMITS_DATA=$(curl -s -u "$GITHUB_USER:$GITHUB_TOKEN" \
  93. -H "Accept: application/vnd.github.v3+json" \
  94. "$COMMITS_URL")
  95. TARGET_COMMITS=$(echo "$COMMITS_DATA" | jq -r '. | map(.sha) | join(" ")')
  96. FETCH_HEAD_REF=$(echo "$PR_DATA" | jq -r '.head.sha')
  97. SOURCE_REPO=$(echo "$PR_DATA" | jq -r '.head.repo.full_name')
  98. fi
  99. PR_TITLE=$(echo "$PR_DATA" | jq -r '.title')
  100. PR_DESCRIPTION=$(echo "$PR_DATA" | jq -r '.body')
  101. LABELS=$(echo "$PR_DATA" | jq -r '.labels | map(.name) | join(",")')
  102. set -x
  103. git fetch "git@github.com:$SOURCE_REPO.git" "$FETCH_HEAD_REF"
  104. BACKPORT_PRS=""
  105. for BACKPORT_BRANCH in $BACKPORT_BRANCHES; do
  106. echo "Backporting $TARGET_COMMITS to $BACKPORT_BRANCH."
  107. git checkout "origin/$BACKPORT_BRANCH"
  108. BRANCH_NAME="backport_${PR_ID}_to_${BACKPORT_BRANCH}"
  109. # To make the script idempotent.
  110. git branch -D "$BRANCH_NAME" || true
  111. git checkout "$BACKPORT_BRANCH"
  112. git checkout -b "$BRANCH_NAME"
  113. for TARGET_COMMIT in $TARGET_COMMITS; do
  114. git cherry-pick -m 1 "$TARGET_COMMIT"
  115. done
  116. if [[ ! -z "$PER_BACKPORT_COMMAND" ]]; then
  117. git submodule update --init --recursive
  118. # To remove dangling submodules.
  119. git clean -xdff
  120. eval "$PER_BACKPORT_COMMAND"
  121. git add -A
  122. git commit --amend --no-edit
  123. fi
  124. BACKPORT_PR=$(hub pull-request -p -m "[Backport] $PR_TITLE" \
  125. -m "*Beep boop. This is an automatically generated backport of #${PR_ID}.*" \
  126. -m "$PR_DESCRIPTION" \
  127. -l "$LABELS" \
  128. -b "$GITHUB_USER:$BACKPORT_BRANCH" \
  129. -r "$REVIEWERS" \
  130. -a "$REVIEWERS" | tail -n 1)
  131. BACKPORT_PRS+="$BACKPORT_PR\n"
  132. # TODO: Turn on automerge once the Github API allows it.
  133. done
  134. printf "Your backport PRs have been created:\n$BACKPORT_PRS"