From e280776ca8a4094fc724d47336e61148ddb39e25 Mon Sep 17 00:00:00 2001 From: Eslam ElHusseiny Date: Sun, 16 Aug 2015 00:41:15 +0100 Subject: [PATCH] Adding bitbucket support. Adding bitbucket support for "git hf feature submit" command. --- git-hf-feature | 83 ++++++++++++++++++++++++++++++++-------------- hubflow-common | 90 ++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 139 insertions(+), 34 deletions(-) diff --git a/git-hf-feature b/git-hf-feature index 22eec5866..b81791f92 100644 --- a/git-hf-feature +++ b/git-hf-feature @@ -247,7 +247,7 @@ cmd_submit() { # sanity checks require_branch "$BRANCH" require_clean_working_tree - require_github_origin_repo + require_github_or_bitbucket_origin_repo require_remote_available hubflow_fetch_latest_changes_from_origin @@ -290,35 +290,70 @@ EOS warn "pull request because of this." fi - # submit pull request to GitHub and - resp=$(github_post \ - "/repos/$GITHUB_ORIGIN/pulls" \ - "{\"title\":\"$PR_TITLE\",\"body\":\"$PR_DESC\",\"head\":\"$BRANCH\",\"base\":\"$BASE\"}") + if [[ $TYPE == "github" ]]; then + # submit pull request to GitHub and + github_resp=$(github_post \ + "/repos/$GITHUB_ORIGIN/pulls" \ + "{\"title\":\"$PR_TITLE\",\"body\":\"$PR_DESC\",\"head\":\"$BRANCH\",\"base\":\"$BASE\"}") - # did it succeed? - if echo "$resp" | grep "Validation Failed" > /dev/null ; then - # no, it did not - if echo "$resp" | grep "pull request already exists" > /dev/null ; then - die "A pull request already exists for this feature" - elif echo "$resp" | grep "No commits between" > /dev/null ; then - die "You need to make some commits for this feature before you can make a pull request" - else - warn "An unexpected error was returned from GitHub. Here is the raw response:" + # did it succeed? + if echo "$github_resp" | grep "Validation Failed" > /dev/null ; then + # no, it did not + if echo "$github_resp" | grep "pull request already exists" > /dev/null ; then + die "A pull request already exists for this feature" + elif echo "$github_resp" | grep "No commits between" > /dev/null ; then + die "You need to make some commits for this feature before you can make a pull request" + else + warn "An unexpected error was returned from GitHub. Here is the raw response:" + warn + echo "$github_resp" + exit 1 + fi + fi + + # parse Pull Request URL from response + PR_URL=$(echo $github_resp | + awk -F"," '{for(i=1;i<=NF;i++){if($i~/html_url/){print $i"\n"}}}' | + grep 'pull' | + awk -F"\":" '{print $2}' | + awk -F"\"" '{print $2}') + + if [ -z "$PR_URL" ]; then + die "Failed to create Pull Request" + fi + elif [[ $TYPE == "bitbucket" ]]; then + ## Bitbucket + # submit pull request to BitBucket and + bitbucket_resp=$(bitbucket_post \ + "/$BITBUCKET_ORIGIN/pullrequests" \ + "{ \"title\": \"$PR_TITLE\", + \"description\": \"$PR_DESC\", + \"source\": { \"branch\": { \"name\": \"$BRANCH\" }, \"repository\": { \"full_name\": \"$BITBUCKET_ORIGIN\" } }, + \"destination\": { \"branch\": { \"name\": \"$BASE\" } }, + \"close_source_branch\": true }") + + # did it succeed? + if echo "$bitbucket_resp" | grep "error" > /dev/null ; then + # no, it did not + warn "An unexpected error was returned from BitBucket. Here is the raw response:" warn - echo "$resp" + echo "$bitbucket_resp" + exit 1 + elif [[ -z $bitbucket_resp ]]; then + warn "An unexpected error was returned from BitBucket." exit 1 fi - fi - # parse Pull Request URL from response - PR_URL=$(echo $resp | - awk -F"," '{for(i=1;i<=NF;i++){if($i~/html_url/){print $i"\n"}}}' | - grep 'pull' | - awk -F"\":" '{print $2}' | - awk -F"\"" '{print $2}') + # parse Pull Request URL from response + PR_URL=$(echo $bitbucket_resp | + awk -F"," '{for(i=1;i<=NF;i++){if($i~/html/){print $i"\n"}}}' | + grep 'pull' | + awk -F"\":" '{print $3}' | + awk -F"\"" '{print $2}') - if [ -z "$PR_URL" ]; then - die "Failed to create Pull Request" + if [ -z "$PR_URL" ]; then + die "Failed to create Pull Request" + fi fi echo diff --git a/hubflow-common b/hubflow-common index 9205dd519..587b330c3 100644 --- a/hubflow-common +++ b/hubflow-common @@ -206,7 +206,7 @@ hubflow_load_settings() { export MASTER_BRANCH=$(git config --get hubflow.branch.master) export DEVELOP_BRANCH=$(git config --get hubflow.branch.develop) export ORIGIN=$(git config --get hubflow.origin || echo origin) - export BRANCH=$(git_current_branch) + export BRANCH=$(git_current_branch) } hubflow_change_branch() { @@ -525,12 +525,22 @@ hubflow_resolve_nameprefix() { fi } +get_repo_type() { + CANDIDATE_ORIGIN=$(git remote show -n "$ORIGIN" | grep 'Fetch URL' | cut -c 14-) + if echo $CANDIDATE_ORIGIN | grep github > /dev/null ; then + TYPE="github" + elif echo $CANDIDATE_ORIGIN | grep bitbucket > /dev/null ; then + TYPE="bitbucket" + else + die "The remote repo for this branch, '$ORIGIN' is neither GitHub nor a BitBucket repository." + fi +} + # # GitHub speicific common functionality # GITHUB_API_URL="https://api.github.com" GITHUB_AUTH_TOKEN_KEY="github.oauth.token" - # # Gets the GitHub OAuth token for this user/repo # If no token has been created, the user will be prompted to create one. @@ -589,14 +599,6 @@ github_origin_repo() { #echo "$GITHUB_ORIGIN" 1>&2 } -require_github_origin_repo() { - github_origin_repo - - if [ -z $GITHUB_ORIGIN ]; then - die "The remote repo for this branch, '$ORIGIN' is not a GitHub repository." - fi -} - # Posts an authenticated request to GitHub and returns the response github_post() { require_github_auth_token @@ -604,6 +606,74 @@ github_post() { resp=$(curl -s -H "Authorization: bearer $GITHUB_TOKEN" -d "$2" "$GITHUB_API_URL$1") echo $resp } +# +# End of GitHub speicific common functionality +# + +# +# BitBucket speicific common functionality +# +BITBUCKET_API_URL="https://api.bitbucket.org/2.0/repositories" +# +# Gets the BitBucket OAuth token for this user/repo +# If no token has been created, the user will be prompted to create one. +# +# Not fully implemented, just depend on Username and Password for now +# TODO: check for bitbucket api to get OAuth token for user/repo +bitbucket_auth_token() { + read -p "BitBucket Username: " BITBUCKETUSER + read -s -p "BitBucket Password: " BITBUCKETPASS + echo + + if [ -z "$BITBUCKETUSER" ]; then + die "You must enter your BitBucket username to authenticate with" + fi + + if [ -z "$BITBUCKETPASS" ]; then + die "You must enter your BitBucket password to authenticate with" + fi +} + +# Ensures we have been authorized on BitBucket +require_bitbucket_auth_token() { + bitbucket_auth_token +} + +# Gets the name of the origin repo on BitBucket +# e.g. datasift/gitflow +bitbucket_origin_repo() { + # get the URL for the $ORIGIN repo + BITBUCKET_ORIGIN=$(git remote show -n "$ORIGIN" | grep 'Fetch URL' | cut -c 14-) + + # if the URL doesn't point at BitBucket, blank it + if echo "$BITBUCKET_ORIGIN" | grep 'git@bitbucket.org:' > /dev/null ; then + BITBUCKET_ORIGIN=$(echo $BITBUCKET_ORIGIN | cut -d : -f 2- | sed -e 's|\.git||g;' ) + elif echo "$BITBUCKET_ORIGIN" | grep 'https://.*bitbucket.org' > /dev/null ; then + BITBUCKET_ORIGIN=$(echo $BITBUCKET_ORIGIN | cut -c 20- | sed -e 's|\.git||g;' ) + else + BITBUCKET_ORIGIN= + fi +} + +# Posts an authenticated request to BitBucket and returns the response +bitbucket_post() { + require_bitbucket_auth_token + + resp=$(curl -s -X POST -H "Content-Type: application/json" -u $BITBUCKETUSER:$BITBUCKETPASS "$BITBUCKET_API_URL$1" -d "$2") + echo $resp +} +# +# End of BitBucket speicific common functionality +# + +require_github_or_bitbucket_origin_repo() { + get_repo_type + if [[ $TYPE == "github" ]] ; then + github_origin_repo + elif [[ $TYPE == "bitbucket" ]] ; then + bitbucket_origin_repo + fi +} # # Assertions for use in git-flow subcommands