Skip to content

Instantly share code, notes, and snippets.

@tgautier
Forked from kdisneur/data.yml
Created July 19, 2016 09:53
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 tgautier/39a7534594e7420e77f42832aef47ed7 to your computer and use it in GitHub Desktop.
Save tgautier/39a7534594e7420e77f42832aef47ed7 to your computer and use it in GitHub Desktop.
types:
addition: "93c95c"
environment: "fad9c9"
experience: "ffc27a"
feedback: "cc317c"
improvement: "63c0fd"
inactive: "d3dbe2"
mindless: "fef2c0"
pending: "fbc92f"
platform: "c1d5f1"
problem: "ee0701"
labels:
bug: problem
chore: mindless
copy: experience
design: experience
discussion: feedback
duplicate: inactive
enhancement: improvement
feature: addition
"in progress": pending
invalid: inactive
legal: mindless
"on hold": inactive
optimization: improvement
production: problem
question: feedback
rfc: feedback
security: problem
staging: environment
test: environment
ux: experience
watchlist: pending
wontfix: inactive
require 'yaml'
require 'json'
require 'net/http'
# Usage: ruby import.rb username/repository personal-token [overrides]
# An override is composed of a type name followed by some label names (i.e: platform=elixir,react)
#
# Example:
# ruby import.rb johndoe/my-elixir-project afdf-0202-afdf-0202 platform=elixir,elm
# # => will create all the default values + elixir and elm using the platform color
# ruby import.rb johndoe/my-project afdf-0202-afdf-0202
# # => will create all the default values
# ruby import.rb johndoe/my-project afdf-0202-afdf-0202 environment=qa feedback=talkus
# # => will create all the default values + add qa using the environment color and talkus using the feedback color
class Program
attr_reader :filename, :repository, :token
def initialize(filename, repository, token)
@filename = filename
@repository = repository
@token = token
end
def run(custom_params)
raw_data = YAML.load(File.read(filename))
data = LabelManager.new
raw_data['types'].each(&data.method(:append_type))
raw_data['labels'].each(&data.method(:append))
custom_params.each do |argument|
type, labels = argument.split('=')
if labels
labels.split(',').each do |label|
data.append(label, type)
end
end
end
data.each(&github.method(:import))
end
def github
@github ||= GitHub.new(repository, token)
end
end
class Label
attr_reader :name, :hex_color
def initialize(name, hex_color)
@name = name
@hex_color = hex_color
end
def to_s
"<Label name=#{name} hex_color=#{hex_color}>"
end
end
class LabelManager
def initialize
@data = {}
@types = {}
end
def each(&block)
@data.values.each(&block)
end
def append(name, type)
@data[name] = Label.new(name, color_for_type(type))
self
end
def append_type(name, hex_color)
@types[name] = hex_color
self
end
private
def color_for_type(name)
@types[name]
end
end
class GitHub
attr_reader :repository, :token
def initialize(repository, token)
@repository = repository
@token = token
end
def import(label)
request = Net::HTTP::Post.new(uri.path, 'Authorization' => "token #{token}", 'Content-Type' => 'application/json')
request.body = { name: label.name, color: label.hex_color }.to_json
Net::HTTP.start(uri.host, uri.port, use_ssl: true) { |http| http.request(request) }
rescue => e
puts "Failed: #{e.inspect}"
end
def uri
URI("https://api.github.com/repos/#{repository}/labels")
end
end
Program.new('data.yml', ARGV.shift, ARGV.shift).run(ARGV)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment