From c8c2a716e2ee7461aaaad3d98ee63d6b73e09851 Mon Sep 17 00:00:00 2001 From: knanao Date: Thu, 14 Jul 2022 14:45:35 +0900 Subject: [PATCH 1/3] Add the chery-pick script as a hack command --- hack/cherry-pick.sh | 80 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100755 hack/cherry-pick.sh diff --git a/hack/cherry-pick.sh b/hack/cherry-pick.sh new file mode 100755 index 0000000000..58d9591a4b --- /dev/null +++ b/hack/cherry-pick.sh @@ -0,0 +1,80 @@ +#!/usr/bin/env bash + +# Copyright 2022 The PipeCD 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 -o errexit +set -o nounset +set -o pipefail + +BRANCH=$1; shift 1 +PULL_REQUESTS=($@) + +# Check gh authentication +echo "+++ Checking gh authentication..." +gh auth status + +COMMIT_HASHS=() +for pull in "${PULL_REQUESTS[@]}"; do + hash="$(gh pr view ${pull} --json mergeCommit --jq .mergeCommit.oid)" + COMMIT_HASHS+=($hash) +done + +function join { local IFS="$1"; shift; echo "$*"; } +PULL_DASH=$(join - "${PULL_REQUESTS[@]/#/#}") +PULL_SUBJ=$(join " " "${PULL_REQUESTS[@]/#/#}") +NEWBRANCH="cherry-pick-${PULL_DASH}-to-${BRANCH}" + +# Update all remote branches +echo "+++ Updating remote branches..." +git remote update +echo + +# Create local branch +echo "+++ Creating a local branch..." +git checkout -b ${NEWBRANCH} "origin/${BRANCH}" +echo + +# Cherry-pick pull requests +COMMITS=$(join " " "${COMMIT_HASHS[@]}") +echo "+++ Cherry-picking pull requests" +git cherry-pick ${COMMITS} +echo + +# Push commits to remote branch +echo "+++ Pushing commits to remote branch..." +git push origin ${NEWBRANCH} +echo + +# Create a pull request +echo "+++ Creating a pull request..." +pull_title="Cherry-pick ${PULL_SUBJ}" +pull_body=$(cat < +\`\`\`release-note + +\`\`\` +EOF +) +gh pr create --title="${pull_title}" --body="${pull_body}" --head "${NEWBRANCH}" --base "${BRANCH}" From be56acbd201ca932aa622e8a9e56bbacfd6aeebe Mon Sep 17 00:00:00 2001 From: knanao Date: Thu, 14 Jul 2022 20:02:57 +0900 Subject: [PATCH 2/3] Make it enable to select whether creating a pull request or not --- hack/cherry-pick.sh | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/hack/cherry-pick.sh b/hack/cherry-pick.sh index 58d9591a4b..5e2387d612 100755 --- a/hack/cherry-pick.sh +++ b/hack/cherry-pick.sh @@ -18,6 +18,18 @@ set -o errexit set -o nounset set -o pipefail +QUIET='false' +while getopts 'q' opt; do + case "$opt" in + q) + QUIET='true' ;; + ?) + echo "Usage: hack/cherry-pick.sh [-q] " >&2 + exit 1 ;; + esac +done +shift "$(($OPTIND -1))" + BRANCH=$1; shift 1 PULL_REQUESTS=($@) @@ -52,6 +64,15 @@ echo "+++ Cherry-picking pull requests" git cherry-pick ${COMMITS} echo +# Check whether to push commits and create a pull request or not +if [[ !${QUIET} ]]; then + read -p "+++ Do you push commits and create a pull request? [y/n] " -r + if ! [[ "${REPLY}" =~ ^[yY]$ ]]; then + echo "Skipped." >&2 + exit 0 + fi +fi + # Push commits to remote branch echo "+++ Pushing commits to remote branch..." git push origin ${NEWBRANCH} From 39760a6a92ba3a0a1e1d47680c018f6b0710fb29 Mon Sep 17 00:00:00 2001 From: knanao Date: Thu, 14 Jul 2022 20:32:24 +0900 Subject: [PATCH 3/3] Add an option to delete a working branch --- hack/cherry-pick.sh | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/hack/cherry-pick.sh b/hack/cherry-pick.sh index 5e2387d612..9da6309d61 100755 --- a/hack/cherry-pick.sh +++ b/hack/cherry-pick.sh @@ -19,12 +19,15 @@ set -o nounset set -o pipefail QUIET='false' -while getopts 'q' opt; do - case "$opt" in +CLEANUP_BRANCH='false' +while getopts 'qc' opt; do + case $opt in q) QUIET='true' ;; + c) + CLEANUP_BRANCH='true' ;; ?) - echo "Usage: hack/cherry-pick.sh [-q] " >&2 + echo "Usage: hack/cherry-pick.sh [-q] [-c] " >&2 exit 1 ;; esac done @@ -65,7 +68,7 @@ git cherry-pick ${COMMITS} echo # Check whether to push commits and create a pull request or not -if [[ !${QUIET} ]]; then +if ! ${QUIET}; then read -p "+++ Do you push commits and create a pull request? [y/n] " -r if ! [[ "${REPLY}" =~ ^[yY]$ ]]; then echo "Skipped." >&2 @@ -99,3 +102,10 @@ If no, just write "NONE" in the release-note block below. EOF ) gh pr create --title="${pull_title}" --body="${pull_body}" --head "${NEWBRANCH}" --base "${BRANCH}" + +# Delete a working branch +if ${CLEANUP_BRANCH}; then + echo "+++ Deleting a working branch..." + git checkout - + git branch -D ${NEWBRANCH} +if