Skip to content

Instantly share code, notes, and snippets.

@warmwaffles
Forked from omegahm/create_labels.sh
Last active August 29, 2015 14:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save warmwaffles/29a82a47e8b3003be4d7 to your computer and use it in GitHub Desktop.
Save warmwaffles/29a82a47e8b3003be4d7 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["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
#!/usr/bin/env ruby
require 'json'
require 'optparse'
require 'faraday'
require 'base64'
require 'pp'
options = {}
parser = OptionParser.new do |opts|
opts.banner = 'Usage: gh-labels [options] <owner> <repo>'
opts.on("-v", "--verbose", "Run verbosely") do |v|
options[:verbose] = v
end
opts.on('--token STRING', 'The authorization token') do |v|
options[:token] = v
end
opts.on('--otp STRING', 'The two-factor auth code') do |v|
options[:otp] = v
end
end
parser.parse!(ARGV)
if ARGV.length != 2
puts 'You must provide an owner and repo as an argument'
puts
puts parser.to_s
exit(1)
end
options[:owner] = ARGV[0]
options[:repo] = ARGV[1]
connection = Faraday.new(url: 'https://api.github.com')
if options[:token].nil?
unless options.key?(:otp)
puts 'You must provide your two factor auth code. It is 6 digits long'
puts 'OR'
puts 'Provide the --token'
puts
puts parser.to_s
exit(1)
end
# ###
# # 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
# acquire token
response = connection.post do |req|
req.url '/authorizations'
req.headers['Authorization'] = Base64.encode64([ENV['GITHUB_USER'], ENV['GITHUB_PASSWORD']].join(':')).gsub!("\n", '')
req.headers['X-Github-OTP'] = options[:otp]
req.headers['Accept'] = 'application/json'
req.body = JSON.dump({
scopes: ['repo', 'public_repo'],
note: 'Creating Labels'
})
end
if response.status == 401
puts "Github Error: #{response.body}"
exit(1)
end
end
labels = {
"platform" => {
"ruby" => "BFD4F2",
"rails" => "BFD4F2"
},
"problems" => {
"bug" => "EE3F46",
"security" => "EE3F46",
"production" => "F45D43"
},
"mindless" => {
"chore" => "FEF2C0",
"legal" => "FFF2C1"
},
"experience" => {
"copy" => "FFC274",
"design" => "FFC274",
"ux" => "FFC274"
},
"environment" => {
"staging" => "FAD8C7",
"test" => "FAD8C7"
},
"feedback" => {
"discussion" => "CC317C",
"rfc" => "CC317C",
"question" => "CC317C"
},
"improvements" => {
"enhancement" => "5EBEFF",
"optimizaiton" => "5EBEFF"
},
"additions" => {
"feature" => "91CA55"
},
"pending" => {
"in progress" => "FBCA04",
"watchlist" => "FBCA04"
},
"inactive" => {
"invalid" => "D2DAE1",
"wontfix" => "D2DAE1",
"duplicate" => "D2DAE1",
"on hold" => "D2DAE1"
}
}
labels.each do |category, sub|
sub.each do |label, color|
name = "#{category}:#{label}"
response = connection.post do |req|
req.url "repos/#{options[:owner]}/#{options[:repo]}/labels"
req.headers['Authorization'] = "token #{options[:token]}"
req.body = JSON.dump({
name: name,
color: color
})
end
body = JSON.parse(response.body)
if body['errors']
if body['errors'][0]['code'] == 'already_exists'
response = connection.patch do |req|
req.url "repos/#{options[:owner]}/#{options[:repo]}/labels/#{name.gsub(' ', '%20')}"
req.headers['Authorization'] = "token #{options[:token]}"
req.body = JSON.dump({
name: name,
color: color
})
end
end
end
puts response.body
end
end
# 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
{
"platform": {
"ruby": "BFD4F2",
"rails": "BFD4F2"
},
"problems": {
"bug": "EE3F46",
"security": "EE3F46",
"production": "F45D43"
},
"mindless": {
"chore": "FEF2C0",
"legal": "FFF2C1"
},
"experience": {
"copy": "FFC274",
"design": "FFC274",
"ux": "FFC274"
},
"environment": {
"staging": "FAD8C7",
"test": "FAD8C7"
},
"feedback": {
"discussion": "CC317C",
"rfc": "CC317C",
"question": "CC317C"
},
"improvements": {
"enhancement": "5EBEFF",
"optimizaiton": "5EBEFF"
},
"additions": {
"feature": "91CA55"
},
"pending": {
"in progress": "FBCA04",
"watchlist": "FBCA04"
},
"inactive": {
"invalid": "D2DAE1",
"wontfix": "D2DAE1",
"duplicate": "D2DAE1",
"on hold": "D2DAE1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment