Skip to content

Instantly share code, notes, and snippets.

@TheConnMan
Forked from omegahm/create_labels.sh
Last active December 10, 2015 18:43
Show Gist options
  • Save TheConnMan/279fc72660a01ebd099f to your computer and use it in GitHub Desktop.
Save TheConnMan/279fc72660a01ebd099f to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Colours picked from https://robinpowered.com/blog/best-practice-system-for-organizing-and-tagging-github-issues/
###
# Label definitions
###
declare -A LABELS
# Platform
LABELS["P1"]="E11D21"
LABELS["P2"]="EB6420"
LABELS["P3"]="FBCA04"
LABELS["P4"]="207DE5"
# Problems
LABELS["bug"]="BFD4F2"
LABELS["permissions"]="BFD4F2"
# Experience
LABELS["ux"]="FFC274"
# Feedback
LABELS["discussion"]="CC317C"
LABELS["question"]="CC317C"
# Improvements
LABELS["enhancement"]="5EBEFF"
LABELS["optimization"]="5EBEFF"
# Additions
LABELS["feature"]="91CA55"
LABELS["infrastructure"]="91CA55"
# Inactive
LABELS["wontfix"]="D2DAE1"
LABELS["duplicate"]="D2DAE1"
LABELS["on hold"]="D2DAE1"
LABELS["blocked"]="D2DAE1"
###
# Get a token from Github
###
if [ ! -f ".token" ]; then
read -p "Please enter your Github username: " user
read -p "Please enter your 6 digit two-factor-authentication code: " otp_code
curl -u "$user" -H "X-Github-OTP: $otp_code" -d '{"scopes":["repo", "public_repo"], "note":"Creating Labels"}' "https://api.github.com/authorizations" | jq -r '.token' > .token
fi
TOKEN=$(cat .token)
read -p "Who owns the repo you want labels on?: " owner
read -p "What repo do you want labels on?: " repo
for K in "${!LABELS[@]}"; do
CURL_OUTPUT=$(curl -s -H "Authorization: token $TOKEN" -X POST "https://api.github.com/repos/$owner/$repo/labels" -d "{\"name\":\"$K\", \"color\":\"${LABELS[$K]}\"}")
HAS_ERROR=$(echo "$CURL_OUTPUT" | jq -r '.errors')
if [[ (! -z "$HAS_ERROR") && !("$HAS_ERROR" == "null") ]]; then
ERROR=$(echo "$CURL_OUTPUT" | jq -r '.errors[0].code')
if [ "$ERROR" == "already_exists" ]; then
# We update
echo "'$K' already exists. Updating..."
CURL_OUTPUT=$(curl -s -H "Authorization: token $TOKEN" -X PATCH "https://api.github.com/repos/$owner/$repo/labels/${K/ /%20}" -d "{\"name\":\"$K\", \"color\":\"${LABELS[$K]}\"}")
else
echo "Unknown error: $ERROR"
echo "Output from curl: "
echo "$CURL_OUTPUT"
echo "Exiting..."
exit;
fi
else
echo "Created '$K'."
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment