Skip to content

Instantly share code, notes, and snippets.

@donald-pinckney
Created April 22, 2018 21:22
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 donald-pinckney/1554de2a1bf0372e07e720654e1e5d7e to your computer and use it in GitHub Desktop.
Save donald-pinckney/1554de2a1bf0372e07e720654e1e5d7e to your computer and use it in GitHub Desktop.
Create repo and push to GitHub
#!/bin/bash
token=$GITHUB_SCRIPT_TOKEN
private=true
has_issues=true
has_projects=true
has_wiki=true
otherData=""
shouldPush=1
dryrun=0
function get_repo_root_name()
{
local temp=$(basename "$(git rev-parse --show-toplevel)")
retVal=$?
if [ $retVal -ne 0 ]; then
name=""
else
name="$temp"
usingLocal=1
fi
}
get_repo_root_name
function show_help()
{
echo -e "Script to create a GitHub Repo.\n\nIf currently in a local repository, it will create the GitHub repo, and then push to it. Otherwise it will simply create it.\n"
echo -e "Usage:"
echo -e "\tcreate_repo [options]"
echo -e "Options:"
echo -e "\t--name=[name] \t\t\t\tName of the repository. Defaults to root directory name of the local repository. Required if there is no local repository."
echo -e "\t--description=[description] \t\tDescription of the repository. Defaults to empty string."
echo -e "\t--homepage=[homepage] \t\t\tThe homepage of the respository. Defaults to the empty string."
echo -e "\t--priavte=[true | false] \t\tIf the repository should be private. Defaults to true."
echo -e "\t--gitignore_template=[template] \tThe .gitignore template to apply, see https://github.com/github/gitignore for a list. A valid example is C++, for example. Defaults to no .gitignore file"
echo -e "\t--license_template=[license_keyword] \tThe open source license template to apply, see https://help.github.com/articles/licensing-a-repository/#searching-github-by-license-type for a list of license keywords. Defaults to no license being created (default copyright laws)."
echo -e "\t--noissues \t\t\t\tDisable GitHub issues. Defaults to having issues."
echo -e "\t--noprojects \t\t\t\tDisable GitHub projects. Defaults to having projects."
echo -e "\t--nowiki \t\t\t\tDisable GitHub wiki. Defaults to having a wiki."
echo -e "\t--nopush \t\t\t\tDo not push to the GitHub repo, only create it. If create_repo.sh is run in a local repository, it will push by default, otherwise not."
echo -e "\t--dryrun \t\t\t\tDo not do anything, just print what would be done."
exit 0
}
# Parse arguments
while [ $# -gt 0 ]; do
case "$1" in
--name=*)
name="${1#*=}"
;;
--description=*)
description="${1#*=}"
;;
--public)
private=false
;;
--noissues)
has_issues=false
;;
--noprojects)
has_projects=false
;;
--nowiki)
has_wiki=false
;;
--nopush)
shouldPush=""
;;
--homepage=*)
homepage="${1#*=}"
otherData="$otherData, \"homepage\":\"$homepage\""
;;
--gitignore_template=*)
gitignore_template="${1#*=}"
otherData="$otherData, \"gitignore_template\":\"$gitignore_template\""
hasTemplate=1
;;
--license_template=*)
license_template="${1#*=}"
otherData="$otherData, \"license_template\":\"$license_template\""
hasTemplate=1
;;
--dryrun)
dryrun=1
;;
--help)
show_help
;;
*)
printf "***************************\n"
printf "* Error: Invalid argument.*\n"
printf "***************************\n"
exit 1
esac
shift
done
if [ -z "$name" ]
then
echo "--name=[name] is required if not inside a local repository."
exit 1
fi
jsonData="{\"name\":\"$name\",\"description\":\"$description\",\"private\": $private, \"has_issues\": $has_issues, \"has_projects\": $has_projects, \"has_wiki\": $has_wiki$otherData}"
if [ "$dryrun" -eq 1 ]
then
echo "$jsonData"
exit 0
fi
tempFile=$(mktemp)
# Perform request
echo "$jsonData" | curl -s -H "Authorization: token $GITHUB_SCRIPT_TOKEN" -X POST https://api.github.com/user/repos -d @- --header "Content-Type: application/json" > "$tempFile"
# Check for error
grep "failed" "$tempFile" >/dev/null
retVal=$?
if [ $retVal -eq 0 ]; then
echo -e "Error creating repository:\n\n"
cat "$tempFile"
rm "$tempFile"
exit 1
fi
url=$(cat "$tempFile" | python3 -c "import sys, json; print(json.load(sys.stdin)['html_url'])")
echo "$url"
if [ -z "$usingLocal" ]
then
rm "$tempFile"
exit 0
fi
if [ -z "$shouldPush" ]
then
rm "$tempFile"
exit 0
fi
if [ -z "$hasTemplate" ]
then
git remote add origin "$url" >/dev/null
git push --set-upstream origin master >/dev/null
git pull >/dev/null
git branch --set-upstream-to=origin/master master >/dev/null
else
git remote add origin "$url" >/dev/null
git fetch >/dev/null
git branch --set-upstream-to=origin/master master >/dev/null
git pull --allow-unrelated-histories --no-edit >/dev/null
git push --set-upstream origin master >/dev/null
fi
rm "$tempFile"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment