Skip to content

Instantly share code, notes, and snippets.

@Pierozi
Forked from MatthiasKunnen/create_labels.sh
Last active January 24, 2017 17:54
Show Gist options
  • Save Pierozi/4e2fca36b0bf350c9d318a3c9649e856 to your computer and use it in GitHub Desktop.
Save Pierozi/4e2fca36b0bf350c9d318a3c9649e856 to your computer and use it in GitHub Desktop.
Create Gtihub labels from Bash
#!/usr/bin/env bash
###
# Label definitions
###
declare -A LABELS
# Inactive
LABELS["invalid"]="D2DAE1"
LABELS["wontfix"]="D2DAE1"
LABELS["duplicate"]="D2DAE1"
LABELS["on hold"]="D2DAE1"
# Feedback
LABELS["discussion"]="CC317C"
LABELS["rfc"]="CC317C"
LABELS["question"]="CC317C"
LABELS["help wanted"]="CC317C"
LABELS["needs-reproducing-case"]="CC317C"
# Problems
LABELS["bug"]="EE3F46"
LABELS["security"]="EE3F46"
LABELS["breaking change"]="B93136"
# Difficulty
LABELS["difficulty: casual"]="F6B1C3"
LABELS["difficulty: medium"]="F0788C"
LABELS["difficulty: hard"]="DE264C"
# Status
LABELS["status: ready"]="EEEEEE"
LABELS["status: in progress"]="EDEDED"
LABELS["status: freeze"]="28CDD2"
LABELS["status: approval pending"]="FF8000"
LABELS["status: documentation"]="207DE5"
LABELS["status: Staging OK"]="0e8a16"
LABELS["status: Staging Test"]="fef2c0"
# Improvements
LABELS["enhancement"]="84B6EB"
LABELS["optimizaiton"]="0052CC"
LABELS["feature"]="5319E7"
LABELS["epic"]="fbca04"
# Experience
LABELS["design"]="000000"
LABELS["api"]="000000"
LABELS["ux"]="000000"
LABELS["infrastructure"]="000000"
###
# 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 '$otp_code'"}' "https://api.github.com/authorizations" | jq -r '.token' > ~/.token
fi
TOKEN=$(cat ~/.token)
if [[ ! -s ~/.token ]] || [[ $TOKEN == 'null' ]]; then
echo "Auth fail"
rm ~/.token
exit;
fi
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 'has("errors")')
if [ "$HAS_ERROR" = true ]; 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