Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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["ruby"]="BFD4F2"
LABELS["rails"]="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"
# Environment
LABELS["staging"]="FAD8C7"
LABELS["test"]="FAD8C7"
# Feedback
LABELS["discussion"]="CC317C"
LABELS["rfc"]="CC317C"
LABELS["question"]="CC317C"
# Improvements
LABELS["enhancement"]="5EBEFF"
LABELS["optimizaiton"]="5EBEFF"
# Additions
LABELS["feature"]="91CA55"
# Pending
LABELS["in progress"]="FBCA04"
LABELS["watchlist"]="FBCA04"
# 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]}\"}")
else
echo "Unknown error: $ERROR"
echo "Output from curl: "
echo "$CURL_OUTPUT"
echo "Exiting..."
exit;
fi
else
echo "Created '$K'."
fi
done
@omegahm

This comment has been minimized.

Copy link
Owner Author

commented Apr 7, 2015

The colours have been taken from https://robinpowered.com/blog/best-practice-system-for-organizing-and-tagging-github-issues/

This of course only works if you are using two-factor-authentication (which you are!)

It also requires jq for parsing JSON via cURL.

@kevinSuttle

This comment has been minimized.

Copy link

commented Apr 7, 2015

@omegahm This is fantastic. Are these colors custom, or the ones that GitHub has pre-defined?

@tenhobi

This comment has been minimized.

Copy link

commented Apr 7, 2015

Hi, how to run this script on GitHub? :)

@omegahm

This comment has been minimized.

Copy link
Owner Author

commented Apr 7, 2015

@kevenSuttle These are the colours from the above mentioned link. I took the Google Chrome color picker and put it into the bash script.

@hobi You'll have to download it as create_labels.sh and then run

$ chmod +x create_labels.sh
$ ./create_labels.sh

from your terminal. The first time it'll prompt you for a username and a 6 digit 2fa code. Then it'll prompt you for a repo-owner and an actual repo to put these labels on.

@ChrisTerBeke

This comment has been minimized.

Copy link

commented Apr 7, 2015

I'm getting this:

./create_labels.sh: line 8: declare: -A: invalid option
declare: usage: declare [-afFirtx] [-p] [name[=value] ...]
./create_labels.sh: line 45: in progress: syntax error in expression (error token is "progress")
./create_labels.sh: line 52: on hold: syntax error in expression (error token is "hold")
@arianf

This comment has been minimized.

Copy link

commented Apr 8, 2015

You're probably running on bash < 4.0.

Associative arrays (-A) require bash >= 4.0

Mac OS X comes with bash 3.2

@arianf

This comment has been minimized.

Copy link

commented Apr 8, 2015

Also there is an error with this code.

if [ "$ERROR" == "already_exists" ]; then
   # some code
else
   # else code
   exit;
fi

If $ERROR is not equal to already_exists... the program will exit... prematurely.

@omegahm

This comment has been minimized.

Copy link
Owner Author

commented Apr 8, 2015

@ChrisTerBeke This script might be full of errors for all I know 😏 It was thrown together rather quickly. What's even better is that I get this:

bash-4.3$ man declare
No manual entry for declare

but as @arianf says, I am running bash < 4.0.


@arianf Yes, if there's an error, and that error isn't already_exists, then it's a more "serious" error, which this script doesn't handle, thus it exits.

@Chompas

This comment has been minimized.

Copy link

commented Aug 28, 2015

I made some modifications because I was having trouble with the token creation and error notifications, here's my edited version:

https://gist.github.com/Chompas/fb158eb01204d03f783d

@alanguerin

This comment has been minimized.

Copy link

commented Oct 31, 2015

Thanks for creating this script. Couple of issues:

  • As well as installing bash 4, you may need to install jq - https://stedolan.github.io/jq/download/.
  • The script was having trouble retrieving the personal access token from GitHub so I had to manually copy it into the .token file.

All working beautifully now. Thanks again!

@MatthiasKunnen

This comment has been minimized.

Copy link

commented Oct 9, 2016

I had an issue that caused the script to exit on each creation of a label. It ran without errors when updating. I was able to fix it by replacing line 71 with

HAS_ERROR=$(echo "$CURL_OUTPUT" | jq 'has("errors")')

and then using

if [ "$HAS_ERROR" = true ]; then

on line 73.
I forked it here: https://gist.github.com/MatthiasKunnen/b7c4f812ddc6a8bb217160ab4f048e0c.

@MaffooBristol

This comment has been minimized.

Copy link

commented Apr 26, 2017

Shame it doesn't work for Gitlab! Do you think that would be an easy thing to add?

@Berkmann18

This comment has been minimized.

Copy link

commented Dec 4, 2018

Looks great, how would that work if you had users who didn't have 2FA enabled?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.