-
-
Save fearoffish/8e57a24e5815edc16f92248ffd9fab30 to your computer and use it in GitHub Desktop.
Create Gtihub labels from Bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/bash | |
# Colours picked from https://robinpowered.com/blog/best-practice-system-for-organizing-and-tagging-github-issues/ | |
# Updates for making it work on OS X with a homebrew bash and ignoring null errors | |
### | |
# Label definitions | |
### | |
declare -A LABELS | |
# Platform | |
LABELS["node"]="BFD4F2" | |
LABELS["react"]="BFD4F2" | |
# Problems | |
LABELS["bug"]="EE3F46" | |
LABELS["security"]="EE3F46" | |
LABELS["production"]="F45D43" | |
# Mindless | |
LABELS["chore"]="FEF2C0" | |
LABELS["legal"]="FFF2C1" | |
# Experience | |
LABELS["copy"]="FFC274" | |
LABELS["design"]="FFC274" | |
LABELS["ux"]="FFC274" | |
# Feedback | |
LABELS["rfc"]="CC317C" | |
LABELS["question"]="CC317C" | |
LABELS["code review"]="CC317C" | |
# Improvements | |
LABELS["enhancement"]="5EBEFF" | |
LABELS["optimizaiton"]="5EBEFF" | |
# Environment | |
LABELS["staging"]="FAD8C7" | |
LABELS["test"]="FAD8C7" | |
# Additions | |
LABELS["feature"]="91CA55" | |
# Inactive | |
LABELS["invalid"]="D2DAE1" | |
LABELS["wontfix"]="D2DAE1" | |
LABELS["duplicate"]="D2DAE1" | |
LABELS["on hold"]="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" ]; 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]}\"}") | |
elif [ "$ERROR" == "null" ]; then | |
echo "Added $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