Skip to content

Instantly share code, notes, and snippets.

@vieko
Forked from omegahm/create_labels.sh
Last active December 18, 2017 23:08
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 vieko/ac9238b0815bf81177ecef8541a6c579 to your computer and use it in GitHub Desktop.
Save vieko/ac9238b0815bf81177ecef8541a6c579 to your computer and use it in GitHub Desktop.
Create Gtihub labels from Bash
#!/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["angular"]="6272a4"
LABELS["node"]="6272a4"
# Problems
LABELS["bug"]="ff5555"
LABELS["security"]="ff5555"
LABELS["production"]="ff5555"
# Mindless
LABELS["chore"]="f1fa8c"
LABELS["legal"]="f1fa8c"
# Experience
LABELS["copy"]="ffb86c"
LABELS["design"]="ffb86c"
LABELS["ux"]="ffb86c"
# Environment
LABELS["staging"]="ff79c6"
LABELS["test"]="ff79c6"
# Feedback
LABELS["discussion"]="bd93f9"
LABELS["question"]="bd93f9"
# Improvements
LABELS["enhancement"]="8be9fd"
LABELS["optimization"]="8be9fd"
# Additions
LABELS["feature"]="50fa7b"
# Pending
LABELS["in progress"]="44475a"
LABELS["watchlist"]="44475a"
# Inactive
LABELS["invalid"]="f8f8f2"
LABELS["wontfix"]="f8f8f2"
LABELS["duplicate"]="f8f8f2"
LABELS["on hold"]="f8f8f2"
###
# 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" ]; 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