Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit 35a63d1

Browse files
committed
Adds check-pick file to automate generating cherry pick PRs
Signed-off-by: JoshVanL <[email protected]>
1 parent d15cd62 commit 35a63d1

File tree

1 file changed

+243
-0
lines changed

1 file changed

+243
-0
lines changed

hack/cherry-pick-pull.sh

Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
#!/bin/bash
2+
3+
# +skip_license_check
4+
5+
# Copyright 2015 The Kubernetes Authors.
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
19+
# Checkout a PR from GitHub. (Yes, this is sitting in a Git tree. How
20+
# meta.) Assumes you care about pulls from remote "upstream" and
21+
# checks thems out to a branch named:
22+
# automated-cherry-pick-of-<pr>-<target branch>-<timestamp>
23+
24+
set -o errexit
25+
set -o nounset
26+
set -o pipefail
27+
28+
declare -r KUBE_ROOT="$(dirname "${BASH_SOURCE}")/.."
29+
cd "${KUBE_ROOT}"
30+
31+
declare -r STARTINGBRANCH=$(git symbolic-ref --short HEAD)
32+
declare -r REBASEMAGIC="${KUBE_ROOT}/.git/rebase-apply"
33+
DRY_RUN=${DRY_RUN:-""}
34+
REGENERATE_DOCS=${REGENERATE_DOCS:-""}
35+
UPSTREAM_REMOTE=${UPSTREAM_REMOTE:-upstream}
36+
FORK_REMOTE=${FORK_REMOTE:-origin}
37+
38+
if [[ -z ${GITHUB_USER:-} ]]; then
39+
echo "Please export GITHUB_USER=<your-user> (or GH organization, if that's where your fork lives)"
40+
exit 1
41+
fi
42+
43+
if ! which hub > /dev/null; then
44+
echo "Can't find 'hub' tool in PATH, please install from https://github.com/github/hub"
45+
exit 1
46+
fi
47+
48+
if [[ "$#" -lt 2 ]]; then
49+
echo "${0} <remote branch> <pr-number>...: cherry pick one or more <pr> onto <remote branch> and leave instructions for proposing pull request"
50+
echo
51+
echo " Checks out <remote branch> and handles the cherry-pick of <pr> (possibly multiple) for you."
52+
echo " Examples:"
53+
echo " $0 upstream/release-3.14 12345 # Cherry-picks PR 12345 onto upstream/release-3.14 and proposes that as a PR."
54+
echo " $0 upstream/release-3.14 12345 56789 # Cherry-picks PR 12345, then 56789 and proposes the combination as a single PR."
55+
echo
56+
echo " Set the DRY_RUN environment var to skip git push and creating PR."
57+
echo " This is useful for creating patches to a release branch without making a PR."
58+
echo " When DRY_RUN is set the script will leave you in a branch containing the commits you cherry-picked."
59+
echo
60+
echo " Set the REGENERATE_DOCS environment var to regenerate documentation for the target branch after picking the specified commits."
61+
echo " This is useful when picking commits containing changes to API documentation."
62+
echo
63+
echo " Set UPSTREAM_REMOTE (default: upstream) and FORK_REMOTE (default: origin)"
64+
echo " To override the default remote names to what you have locally."
65+
exit 2
66+
fi
67+
68+
if git_status=$(git status --porcelain --untracked=no 2>/dev/null) && [[ -n "${git_status}" ]]; then
69+
echo "!!! Dirty tree. Clean up and try again."
70+
exit 1
71+
fi
72+
73+
if [[ -e "${REBASEMAGIC}" ]]; then
74+
echo "!!! 'git rebase' or 'git am' in progress. Clean up and try again."
75+
exit 1
76+
fi
77+
78+
declare -r BRANCH="$1"
79+
shift 1
80+
declare -r PULLS=( "$@" )
81+
82+
function join { local IFS="$1"; shift; echo "$*"; }
83+
declare -r PULLDASH=$(join - "${PULLS[@]/#/#}") # Generates something like "#12345-#56789"
84+
declare -r PULLSUBJ=$(join " " "${PULLS[@]/#/#}") # Generates something like "#12345 #56789"
85+
86+
echo "+++ Updating remotes..."
87+
git remote update "${UPSTREAM_REMOTE}" "${FORK_REMOTE}"
88+
89+
if ! git log -n1 --format=%H "${BRANCH}" >/dev/null 2>&1; then
90+
echo "!!! '${BRANCH}' not found. The second argument should be something like ${UPSTREAM_REMOTE}/release-0.21."
91+
echo " (In particular, it needs to be a valid, existing remote branch that I can 'git checkout'.)"
92+
exit 1
93+
fi
94+
95+
declare -r NEWBRANCHREQ="automated-cherry-pick-of-${PULLDASH}" # "Required" portion for tools.
96+
declare -r NEWBRANCH="$(echo "${NEWBRANCHREQ}-${BRANCH}" | sed 's/\//-/g')"
97+
declare -r NEWBRANCHUNIQ="${NEWBRANCH}-$(date +%s)"
98+
echo "+++ Creating local branch ${NEWBRANCHUNIQ}"
99+
100+
cleanbranch=""
101+
prtext=""
102+
gitamcleanup=false
103+
function return_to_kansas {
104+
if [[ "${gitamcleanup}" == "true" ]]; then
105+
echo
106+
echo "+++ Aborting in-progress git am."
107+
git am --abort >/dev/null 2>&1 || true
108+
fi
109+
110+
# return to the starting branch and delete the PR text file
111+
if [[ -z "${DRY_RUN}" ]]; then
112+
echo
113+
echo "+++ Returning you to the ${STARTINGBRANCH} branch and cleaning up."
114+
git checkout -f "${STARTINGBRANCH}" >/dev/null 2>&1 || true
115+
if [[ -n "${cleanbranch}" ]]; then
116+
git branch -D "${cleanbranch}" >/dev/null 2>&1 || true
117+
fi
118+
if [[ -n "${prtext}" ]]; then
119+
rm "${prtext}"
120+
fi
121+
fi
122+
}
123+
trap return_to_kansas EXIT
124+
125+
SUBJECTS=()
126+
function make-a-pr() {
127+
local rel="$(basename "${BRANCH}")"
128+
echo
129+
echo "+++ Creating a pull request on GitHub at ${GITHUB_USER}:${NEWBRANCH}"
130+
131+
# This looks like an unnecessary use of a tmpfile, but it avoids
132+
# https://github.com/github/hub/issues/976 Otherwise stdin is stolen
133+
# when we shove the heredoc at hub directly, tickling the ioctl
134+
# crash.
135+
prtext="$(mktemp -t prtext.XXXX)" # cleaned in return_to_kansas
136+
cat >"${prtext}" <<EOF
137+
Automated cherry pick of ${PULLSUBJ}
138+
139+
Cherry pick of ${PULLSUBJ} on ${rel}.
140+
141+
$(printf '%s\n' "${SUBJECTS[@]}")
142+
EOF
143+
144+
hub pull-request -F "${prtext}" -h "${GITHUB_USER}:${NEWBRANCH}" -b "jetstack:${rel}"
145+
}
146+
147+
git checkout -b "${NEWBRANCHUNIQ}" "${BRANCH}"
148+
cleanbranch="${NEWBRANCHUNIQ}"
149+
150+
gitamcleanup=true
151+
for pull in "${PULLS[@]}"; do
152+
echo "+++ Downloading patch to /tmp/${pull}.patch (in case you need to do this again)"
153+
curl -o "/tmp/${pull}.patch" -sSL "https://github.com/jetstack/kube-oidc-proxy/pull/${pull}.patch"
154+
echo
155+
echo "+++ About to attempt cherry pick of PR. To reattempt:"
156+
echo " $ git am -3 /tmp/${pull}.patch"
157+
echo
158+
git am -3 "/tmp/${pull}.patch" || {
159+
conflicts=false
160+
while unmerged=$(git status --porcelain | grep ^U) && [[ -n ${unmerged} ]] \
161+
|| [[ -e "${REBASEMAGIC}" ]]; do
162+
conflicts=true # <-- We should have detected conflicts once
163+
echo
164+
echo "+++ Conflicts detected:"
165+
echo
166+
(git status --porcelain | grep ^U) || echo "!!! None. Did you git am --continue?"
167+
echo
168+
echo "+++ Please resolve the conflicts in another window (and remember to 'git add / git am --continue')"
169+
read -p "+++ Proceed (anything but 'y' aborts the cherry-pick)? [y/n] " -r
170+
echo
171+
if ! [[ "${REPLY}" =~ ^[yY]$ ]]; then
172+
echo "Aborting." >&2
173+
exit 1
174+
fi
175+
done
176+
177+
if [[ "${conflicts}" != "true" ]]; then
178+
echo "!!! git am failed, likely because of an in-progress 'git am' or 'git rebase'"
179+
exit 1
180+
fi
181+
}
182+
183+
# set the subject
184+
subject=$(grep -m 1 "^Subject" "/tmp/${pull}.patch" | sed -e 's/Subject: \[PATCH//g' | sed 's/.*] //')
185+
SUBJECTS+=("#${pull}: ${subject}")
186+
187+
# remove the patch file from /tmp
188+
rm -f "/tmp/${pull}.patch"
189+
done
190+
gitamcleanup=false
191+
192+
# Re-generate docs (if needed)
193+
if [[ -n "${REGENERATE_DOCS}" ]]; then
194+
echo
195+
echo "Regenerating docs..."
196+
if ! hack/generate-docs.sh; then
197+
echo
198+
echo "hack/generate-docs.sh FAILED to complete."
199+
exit 1
200+
fi
201+
fi
202+
203+
if [[ -n "${DRY_RUN}" ]]; then
204+
echo "!!! Skipping git push and PR creation because you set DRY_RUN."
205+
echo "To return to the branch you were in when you invoked this script:"
206+
echo
207+
echo " git checkout ${STARTINGBRANCH}"
208+
echo
209+
echo "To delete this branch:"
210+
echo
211+
echo " git branch -D ${NEWBRANCHUNIQ}"
212+
exit 0
213+
fi
214+
215+
if git remote -v | grep ^${FORK_REMOTE} | grep kubernetes/kubernetes.git; then
216+
echo "!!! You have ${FORK_REMOTE} configured as your kubernetes/kubernetes.git"
217+
echo "This isn't normal. Leaving you with push instructions:"
218+
echo
219+
echo "+++ First manually push the branch this script created:"
220+
echo
221+
echo " git push REMOTE ${NEWBRANCHUNIQ}:${NEWBRANCH}"
222+
echo
223+
echo "where REMOTE is your personal fork (maybe ${UPSTREAM_REMOTE}? Consider swapping those.)."
224+
echo "OR consider setting UPSTREAM_REMOTE and FORK_REMOTE to different values."
225+
echo
226+
make-a-pr
227+
cleanbranch=""
228+
exit 0
229+
fi
230+
231+
echo
232+
echo "+++ I'm about to do the following to push to GitHub (and I'm assuming ${FORK_REMOTE} is your personal fork):"
233+
echo
234+
echo " git push ${FORK_REMOTE} ${NEWBRANCHUNIQ}:${NEWBRANCH}"
235+
echo
236+
read -p "+++ Proceed (anything but 'y' aborts the cherry-pick)? [y/n] " -r
237+
if ! [[ "${REPLY}" =~ ^[yY]$ ]]; then
238+
echo "Aborting." >&2
239+
exit 1
240+
fi
241+
242+
git push "${FORK_REMOTE}" -f "${NEWBRANCHUNIQ}:${NEWBRANCH}"
243+
make-a-pr

0 commit comments

Comments
 (0)