Skip to content

Instantly share code, notes, and snippets.

@smhmic
Created March 21, 2018 19:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smhmic/e4cf438c78fa99285da5a02cff273987 to your computer and use it in GitHub Desktop.
Save smhmic/e4cf438c78fa99285da5a02cff273987 to your computer and use it in GitHub Desktop.
Create new BitBucket repo
# Create an OAuth consumer with write access.
# For a callback URL, you can use: `https://www.google.com/`
# https://bitbucket.org/account/user/USERNAME/oauth-consumers/new
# Optional: Hardcode username, client key, and client secret into script.
# (but be aware of the danger of hardcoding credentials into any code!!)
# (if skipped, the script will securely prompt for info).
# Edit values
NEW_REPO_USER_OR_ORG="editme"
NEW_REPO_NAME="editme"
# Create repo on bitbucket
source bitbucket_api_create_repo.sh
bitbucket_api_create_repo $NEW_REPO_USER_OR_ORG/$NEW_REPO_NAME
# Clone and open local repo
git clone git@bitbucket.org:$NEW_REPO_USER_OR_ORG/$NEW_REPO_NAME.git
cd $NEW_REPO_NAME
open . # Open folder in file browser
# pstorm . # Open in IDE
# Initialize readme
printf "# $NEW_REPO_NAME\n\n## Usage\n\n### Download\n\`\`\`\ngit clone git@bitbucket.org:$NEW_REPO_USER_OR_ORG/$NEW_REPO_NAME.git\n\`\`\`\n" > README.md
#!/bin/sh
###
### Interactive function to create a new repository on BitBucket.
### Usage: `bitbucket_api_create_repo USER_OR_ORG/REPO_NAME`
### Requires user input to run successfully, and opens URLs in default browser.
###
### This requires an OAuth consumer with repo write access, which you can create
### here: https://bitbucket.org/account/user/USERNAME/oauth-consumers/new
### For a callback URL, you can use: `https://www.google.com/`
### By default the script will prompt for the keys every time. To partially
### automate this, username, client key, and client secret can be hardcoded
### into the variables at the top of the script (but be aware of the danger
### of hardcoding credentials into any code!!).
### The script will always need to open the OAuth callback URL in a browser to
### provide the auth code, which will be appended as query parameter `code`.
### When prompted, copy the parameter value and paste into the terminal.
### When entering username, client key/secret, or auth code into the terminal,
### the entered value will be hidden (but it's there), and will be submitted
### by pressing Enter.
###
function bitbucket_api_create_repo {
# Can prefill these four vars, otherwise will prompt.
USERNAME='';
CLIENTKEY='';
CLIENTSECRET='';
CALLBACK_URL_BASE=''; # Optional, only for printing help text.
AUTHCODE='';
AUTHTOKEN='';
repo="$1";
# Validate input.
if [[ $repo =~ ^[a-zA-Z0-9_-]+/[a-zA-Z0-9_-]+$ ]]; then
echo "Attempting to create new BitBucket repo: '$repo' ...";
else
echo "Invalid repo path: '$repo'"
echo 'Usage: bitbucket_api_create_repo USER_OR_ORG/REPO_NAME';
return 1;
fi
if [ -z "$USERNAME" ]; then
echo 'Username? '; read -s USERNAME; fi;
if [ -z "$CLIENTKEY" -o -z "$CLIENTSECRET" ]; then
echo "Requires a BitBucket OAuth consumer with repo write access.";
echo "Opening 'https://bitbucket.org/account/user/$USERNAME/api' in browser ...";
open "https://bitbucket.org/account/user/$USERNAME/api";
if [ -z "$CLIENTKEY" ]; then
echo 'OAuth client key? '; read -s CLIENTKEY; fi;
if [ -z "$CLIENTSECRET" ]; then
echo ')Auth client secret? '; read -s CLIENTSECRET; fi;
fi;
function _prompt_for_auth_code {
open "https://bitbucket.org/site/oauth2/authorize?client_id=$CLIENTKEY&response_type=code";
if [ -n "$CALLBACK_URL_BASE" ]; then
echo 'URL should open in your browser in format 'google.com/?code={AUTH_CODE}' ...';
else
echo 'Oauth callback URL should open in your browser with the auth code ...';
fi
echo 'Please paste auth code here and press enter:'; read -s AUTHCODE;
}
if [ -z "$AUTHCODE" ]; then
_prompt_for_auth_code;
fi;
if [ -z "$AUTHTOKEN" ]; then
response=$(curl -s \
-X POST \
-u $CLIENTKEY:$CLIENTSECRET \
https://bitbucket.org/site/oauth2/access_token \
-d grant_type=authorization_code -d code=$AUTHCODE);
# check for specific error response
if [ "$response" == '{"error_description": "The specified code is not valid.", "error": "invalid_grant"}' ]; then
echo "The saved code \"$AUTHCODE\" is invalid, need a new code.";
_prompt_for_auth_code;
response=$(curl -s \
-X POST \
-u $CLIENTKEY:$CLIENTSECRET \
https://bitbucket.org/site/oauth2/access_token \
-d grant_type=authorization_code -d code=$AUTHCODE);
fi
# check for any error response
# if [[ "$response" =~ '"error' ]]; then echo "ERROR RESPONSE: $response"; return 1; fi;
# Extract data from response
if [[ $response =~ \"access_token\"[[:space:]]*:[[:space:]]*\"([^\"]+)\" ]]; then
AUTHTOKEN=${BASH_REMATCH[1]};
#alt method to extract:
#AUTHTOKEN=${response%\", \"scopes\"*};
#AUTHTOKEN=${AUTHTOKEN#{\"access_token\": \"};
else
echo "FAILED TO GET AUTH TOKEN. $response";
return 1;
fi
echo "Retrieved auth token: $AUTHTOKEN";
fi; # END if [ -z "$AUTHTOKEN" ];
response=$(curl -s \
-X POST \
-u $CLIENTKEY:$CLIENTSECRET \
-H "Authorization: Bearer $AUTHTOKEN" \
https://api.bitbucket.org/2.0/repositories/$repo \
-H "Content-Type: application/json" \
-d '{"scm": "git", "is_private": "true", "fork_policy": "no_public_forks", "has_wiki": "true", "has_issues": "true" }');
# TODO: prompt user for extra info, e.g. description, link, has_issues, etc
# https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D
# check for any error response
if [[ "$response" =~ '"error' ]]; then
echo -e "\nERROR RESPONSE:"; echo "$response"; echo -e "\n"; return; fi;
url_web="https://bitbucket.org/$repo";
echo -e "\nCreated BitBucket repository: $url_web\n";
open "$url_web";
return;
#NEW_REPO_USER="editme"
#NEW_REPO_NAME="editme"
#bitbucket_api_create_repo $NEW_REPO_USER/$NEW_REPO_NAME
#git clone git@bitbucket.org:$NEW_REPO_USER/$NEW_REPO_NAME.git
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment