From 83d2e03cf7f51059c292c2acf1d124fe69259d65 Mon Sep 17 00:00:00 2001 From: vinoth chandar Date: Thu, 24 Sep 2020 15:01:32 -0700 Subject: [PATCH] [MINOR] Adding scripts to checkout and push to PRs (#2109) - Tested the checkout_pr.sh locally - Tested a dryrun of pr_push_command.sh --- scripts/checkout_pr.sh | 61 ++++++++++++++++++++++++++++++++++++++ scripts/pr_push_command.sh | 60 +++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100755 scripts/checkout_pr.sh create mode 100755 scripts/pr_push_command.sh diff --git a/scripts/checkout_pr.sh b/scripts/checkout_pr.sh new file mode 100755 index 000000000..fd1878b1f --- /dev/null +++ b/scripts/checkout_pr.sh @@ -0,0 +1,61 @@ +#!/bin/bash + +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +# +# Usage: ./scripts/checkout_pr.sh +# +# Checkout a PR given the PR number into a local branch. PR branches are named +# using the convention "pull/", to enable pr_push_command.sh to work +# in tandem. +# +set -eou pipefail + +function printUsage { + echo "Usage: $(basename "${0}") [-r REMOTE] [-f] " 2>&1 + echo ' -r REMOTE remote to grab PR from (default: apache)' + echo ' -f force overwrite of local branch (default: fail if exists)' + exit 1 +} + +if [[ ${#} -eq 0 ]]; then + printUsage +fi + +REMOTE="origin" +FORCE="" +while getopts ":r:f" arg; do + case "${arg}" in + r) + REMOTE="${OPTARG}" + ;; + f) + FORCE="--force" + ;; + ?) + printUsage + ;; + esac +done +shift "$(($OPTIND -1))" + +# Debug output +PR_NUM=$1 + +# Checkout the PR into a local branch. +git fetch ${REMOTE} pull/${PR_NUM}/head:pull/${PR_NUM} ${FORCE} +git checkout pull/${PR_NUM} diff --git a/scripts/pr_push_command.sh b/scripts/pr_push_command.sh new file mode 100755 index 000000000..433f5ef3a --- /dev/null +++ b/scripts/pr_push_command.sh @@ -0,0 +1,60 @@ +#!/bin/bash + +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +# +# Usage: ./scripts/pr_push_command.sh +# +# When run from a PR branch checked out by checkout_pr.sh, provides a command +# that can push local PR branch to its corresponding remote. +# NOTE: Always double check correctness of command, before pushing. +# + +set -eou pipefail + +CURR_BRANCH=$(git status | grep "On branch" | awk '{print $NF}') +REMOTE="apache" + +# Get PR number from branch +if [[ ${CURR_BRANCH} = pull/* ]] +then + PR_NUM=$(echo "${CURR_BRANCH}" | awk -F'/' '{print $NF}') +else + echo "Not on a PR branch?" + exit 1 +fi + +# Parse the pr's remote, branch & add a remote if needed. +PR_RESP=$(curl https://api.github.com/repos/${REMOTE}/hudi/pulls/${PR_NUM}) +if ! echo ${PR_RESP} | jq '.url' | grep "hudi/pulls/${PR_NUM}" ; then + echo "Unable to find PR number ${PR_NUM} in remote ${REMOTE}" + exit 1 +fi + +PR_SSH_URL=$(echo ${PR_RESP} | jq -r '.head.repo.ssh_url') +PR_REMOTE_BRANCH=$(echo ${PR_RESP} | jq -r '.head.ref') +PR_REMOTE=$(echo ${PR_RESP} | jq -r '.head.repo.owner.login') + +# Add a new remote, if needed. +if ! git remote -v | grep ${PR_REMOTE} ; then + echo "Adding new remote ${PR_REMOTE} with ssh url ${PR_SSH_URL}" + git remote add ${PR_REMOTE} ${PR_SSH_URL} +fi + +# Push local branch to PR remote/branch +echo "If you are sure, execute the following command to push" +echo " git push ${PR_REMOTE} ${CURR_BRANCH}:${PR_REMOTE_BRANCH}"