Skip to content

Instantly share code, notes, and snippets.

@grantmacken
Created December 19, 2014 01:29
Show Gist options
  • Save grantmacken/d1979e66fbe6f7e9062b to your computer and use it in GitHub Desktop.
Save grantmacken/d1979e66fbe6f7e9062b to your computer and use it in GitHub Desktop.
a bash function I will use to create a repo in current folder ussing github api
#!/bin/bash +x
function reposCreateNewRepo(){
local apiUrlBase='https://api.github.com'
GITHUB_ACCESS_TOKEN=$( cat ${GITHUB_ACCESS_TOKEN_LOCATION} )
PROJECT_NAME=$( echo ${PWD##*/} )
PROJECT_DESCRIPTION="website project ${PROJECT_NAME}"
local chkGitStatus=$( git status 2>/dev/null )
echo "CHECK! if ${PROJECT_NAME} under git control"
if [[ -n ${chkGitStatus} ]] ; then
echo "YEP! ${PROJECT_NAME} is already under git control"
echo ${chkGitStatus}
else
echo "NOPE! ${PROJECT_NAME} *not* under git control"
fi
local doRequest=
local jsn=
echo "TASK! git init ${PROJECT_NAME}"
git init
if [[ ! -e 'README.md' ]] ; then
echo "TASK! add readme"
touch 'README.md'
git add README.md
git commit -m 'first commit'
fi
chkIsInRepo=$(
curl ${API_USER_REPOS} | json -a name | grep "${PROJECT_NAME}"
)
echo "CHECK! is ${PROJECT_NAME} in our github repo"
if [[ -n ${chkIsInRepo} ]] ; then
echo "YEP! ${chkIsInRepo} is in repo"
else
echo "NOPE! ${PROJECT_NAME} is *not* repo"
echo "TASK! create repo on github"
jsn="{\"name\":\"${PROJECT_NAME}\",\"description\":\"${PROJECT_DESCRIPTION}\"}"
doRequest=$(
curl \
-H "Authorization: token ${GITHUB_ACCESS_TOKEN}" \
-o /dev/null -w "%{http_code}" \
${apiUrlBase}/user/repos \
-d "${jsn}"
)
if [[ ${doRequest} = 201 ]] ; then
echo "DONE! ${doRequest}"
echo "TASK! get the repo SSH_URL"
SSH_URL=$(
curl ${apiUrlBase}/repos/${GITHUB_USER}/${PROJECT_NAME} |
json -a ssh_url
)
if [[ ${doRequest} = 201 ]] ; then
echo "DONE! ${SSH_URL}"
echo "TASK! add remote origin"
git remote add origin $SSH_URL
echo "TASK! push origin master "
git push origin master
echo "TASK! show git status"
git status
fi
fi
fi
}
@grantmacken
Copy link
Author

A few vars loaded from a properties file, but you should get the flow of the code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment