Forked from jakub-g/open-atlassian-stash-pullrequest.sh
Last active
February 22, 2017 06:09
-
-
Save mrleolink/2b7618de2a384cb985b5a5bc8ea3066c to your computer and use it in GitHub Desktop.
Open Atlassian Stash pull request from command line (open browser at the right URL)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
################################################################## | |
# Open Atlassian Stash pull request from command line. | |
# (opens the default browser at the proper URL with data prefilled) | |
# | |
# It infers current branch name, repo name, current user name, from git config. | |
############################### CONFIG ########################### | |
URL_PREFIX="https://YOU_GIT_DOMAIN.com/your/project/path/compare/commits?commits&" | |
# ~!!!!!! no commas between the items !!!!!! | |
# this is the list of target branches for PRs; presence here implies being a forbidden input branch | |
AVAILABLE_TARGET_BRANCHES=("develop" "qa") | |
# additional forbidden input branches, other than the available target branches | |
ADDITIONAL_FORBIDDEN_INPUT_BRANCHES=("master") | |
################################################################## | |
SCRIPTNAME="pullrequest" # $0 | |
if [ "$1" == "--help" ] || [ "$1" == "-h" ]; then | |
echo "Usage:" | |
echo "$SCRIPTNAME does 'git push -f' and opens the pull request on Stash" | |
echo "$SCRIPTNAME --no-push opens the pull request on Stash without pushing" | |
echo "$SCRIPTNAME --help prints this help page" | |
exit | |
fi | |
DO_PUSH=true | |
if [ "$1" == "--no-push" ] ; then | |
DO_PUSH=false | |
fi | |
# figure out the name of the current branch | |
CURRENT_BRANCH_NAME=$( git rev-parse --abbrev-ref HEAD ) | |
if [ "$CURRENT_BRANCH_NAME" == "HEAD" ]; then | |
echo "You are not currently in a branch. Please checkout a branch." | |
exit 1 | |
fi | |
# prevent users from opening pull requests from "releases/1.4.0" etc. | |
for forbiddenInputBranch in "${AVAILABLE_TARGET_BRANCHES[@]}" "${ADDITIONAL_FORBIDDEN_INPUT_BRANCHES[@]}" | |
do | |
if [ "$forbiddenInputBranch" == "$CURRENT_BRANCH_NAME" ] ; then | |
echo "Please don't open pull requests directly from '${CURRENT_BRANCH_NAME}'; this breaks Stash automatic branch mirroring!" | |
echo "Please create a feature branch instead!" | |
exit 1 | |
fi | |
done | |
if [ "$DO_PUSH" = true ] ; then | |
echo "Pushing to your remote git repo..." | |
git push origin ${CURRENT_BRANCH_NAME} | |
returnCode=$? | |
if [ "$returnCode" -ne "0" ] ; then | |
exit | |
fi | |
echo -e "\n=====================================================================================================\n" | |
fi | |
# figure out the user based on git remote settings value; taking advantage of "/~USER/" substring in Atlassian Stash URLs | |
USER_NAME=$( git config --get remote.origin.url | perl -l -ne '/\/~([^\/]+)\// && print $1' ) | |
# figure out the repo name: substring after last "/", stripped from ".git" | |
REPO_NAME=$( git config --get remote.origin.url | sed 's/.*\///' | sed 's/.git//' ) | |
echo "Opening pull request from '${USER_NAME}/${CURRENT_BRANCH_NAME}' to '${REPO_NAME}'..." | |
if [ "$DO_PUSH" = false ] ; then | |
echo "(make sure you pushed to the branch before!)" | |
fi | |
echo "" | |
NUM_OF_AVAILABLE_BRANCHES="${#AVAILABLE_TARGET_BRANCHES[@]}" | |
if [ -n "$1" -a "$1" != -* ] ; then | |
TARGET_BRANCH="$1" | |
elif [ "${NUM_OF_AVAILABLE_BRANCHES}" == "1" ] ; then | |
TARGET_BRANCH="${AVAILABLE_TARGET_BRANCHES[0]}" | |
else | |
# select destination branch from the list | |
echo "Select the destination branch of the pull request:" | |
PS3='Type the number: ' | |
select TARGET_BRANCH in "${AVAILABLE_TARGET_BRANCHES[@]}" | |
do | |
goodBranchChosen=false | |
for availableBranch in "${AVAILABLE_TARGET_BRANCHES[@]}" | |
do | |
if [ "$TARGET_BRANCH" == "$availableBranch" ] ; then | |
goodBranchChosen=true | |
break # break for | |
fi | |
done | |
if [ "$goodBranchChosen" = false ] ; then | |
echo "Invalid option chosen, select a number from the list" | |
else | |
break # break select | |
fi | |
done | |
fi | |
# finalize: create the URL and open it | |
#URL_BASE="/git/users/${USER_NAME}/repos/${REPO_NAME}/pull-requests" | |
#URL_PARAMS="?create&targetBranch=refs%2Fheads%2F${TARGET_BRANCH}&sourceBranch=refs%2Fheads%2F${CURRENT_BRANCH_NAME}" | |
#URL_VIEW=${URL_PREFIX}${URL_BASE} | |
#URL_OPEN=${URL_VIEW}${URL_PARAMS} | |
# echo $URL | |
# note: if using start you have to put dummy first param, or escape & with ^, hence it's better to use explorer | |
# we are opening two URLs in a quick succession, this is a hack because first time Stash is opened it performs | |
# some authentication which does a redirection at the end, which loses the query string... | |
#open "$URL_VIEW" | |
#sleep 1 | |
#open "$URL_OPEN" | |
#open -a /Applications/Google\ Chrome.app "$USER_VIEW" | |
URL=$URL_PREFIX"targetBranch=refs%2Fheads%2F${TARGET_BRANCH}&sourceBranch=refs%2Fheads%2F${CURRENT_BRANCH_NAME}" | |
echo "Opening: $URL" | |
open -a "/Applications/Google Chrome.app" "$URL" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment