Skip to content

Commit

Permalink
[MINOR] Adding scripts to checkout and push to PRs
Browse files Browse the repository at this point in the history
 - Tested the checkout_pr.sh locally
 - Tested a dryrun of pr_push_command.sh
  • Loading branch information
vinothchandar committed Sep 24, 2020
1 parent 3201665 commit 207e282
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
61 changes: 61 additions & 0 deletions scripts/checkout_pr.sh
Original file line number Diff line number Diff line change
@@ -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/<PR_NUMBER>", to enable pr_push_command.sh to work
# in tandem.
#
set -eou pipefail

function printUsage {
echo "Usage: $(basename "${0}") [-r REMOTE] [-f] <PR_NUMBER>" 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}
60 changes: 60 additions & 0 deletions scripts/pr_push_command.sh
Original file line number Diff line number Diff line change
@@ -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}"

0 comments on commit 207e282

Please sign in to comment.