-
-
Save baob/aa277f622e85ba1623d8 to your computer and use it in GitHub Desktop.
Ruby script to create Trello cards
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/bin/env ruby | |
# If you're not using rbenv in this script's dir, you may wanna run | |
# these as `sudo gem install ruby-trello`, etc. | |
['ruby-trello', 'dotenv'].each do |gem_name| | |
begin | |
gem gem_name | |
rescue LoadError | |
puts "Running `gem install #{gem_name}`..." | |
puts "If this takes too long, you may want to run it manually, as `sudo` if needed." | |
system("gem install #{gem_name}") | |
Gem.clear_paths | |
end | |
end | |
require 'trello' | |
require 'dotenv' | |
# Configure stuff in ~/.trello file or directly in here | |
Dotenv.load('~/.trello') | |
conf = { | |
key: ENV['TRELLO_KEY'], | |
token: ENV['TRELLO_TOKEN'], | |
list_id: ENV['TRELLO_LIST_ID'], | |
} | |
empty_keys = conf.map{ |k,v| k if v.nil? || v == "" }.compact | |
unless empty_keys.empty? | |
puts "Please configure keys #{empty_keys.join(', ').upcase} in ~/.trello . Here's a sample .trello:" | |
puts <<-eos | |
TRELLO_KEY=123xxXX123 # Get key on https://trello.com/1/appKey/generate | |
TRELLO_TOKEN=123xxXX123 # Renew every 30 days from https://trello.com/1/authorize?key=TRELLO_KEY&expiration=30days&response_type=token&scope=read,write" | |
TRELLO_LIST_ID=123abc123 # Find out with `Trello::Board.find('board_id_from_the_board_url').lists.reduce({}){|lists,l| lists.merge(l.name => l.id)}` | |
TRELLO_MEMBERS_IDS=21b2,12b # Find out with `Trello::Member.find('your-trello-username').id`. Comma-separated list | |
TRELLO_LABELS=orange,yellow # Can be yellow, orange, green, purple or blue. Comma-separated list | |
eos | |
exit(1) | |
end | |
# Optional conf | |
conf[:member_ids] = ENV['TRELLO_MEMBERS_IDS'].split(',').map(&:strip) if ENV['TRELLO_MEMBERS_IDS'] | |
conf[:labels] = ENV['TRELLO_LABELS'].downcase.split(',').map(&:strip).map(&:to_sym) if ENV['TRELLO_LABELS'] | |
client = Trello::Client.new( | |
developer_public_key: conf[:key], | |
member_token: conf[:token], | |
) | |
begin | |
client.find(:lists, conf[:list_id]) | |
rescue Trello::InvalidAccessToken | |
puts "Token expired. Renew it at this URL and update your settings:" | |
puts "https://trello.com/1/authorize?key=#{conf[:key]}&expiration=30days&response_type=token&scope=read,write" | |
exit(1) | |
end | |
def multi_line_prompt(prompt_message) | |
puts "#{prompt_message}" | |
puts "(2 blank lines in a row to submit, ctrl+c to cancel)" | |
desc = "" | |
until desc[-3..-1] == "\n\n\n" do | |
desc << "#{gets}" | |
end | |
desc[0..-4] | |
end | |
puts "Card name:" | |
name = gets | |
desc = multi_line_prompt("What description for your card?") | |
puts "Gotcha! Creating the card..." | |
card = client.create(:card, { | |
'name' => name, | |
'idList' => conf[:list_id], | |
'labels' => conf[:labels], | |
'idMembers' => conf[:member_ids], | |
}) | |
puts 'Card created!' | |
puts card.url | |
puts | |
# TODO: Only needed until we can use methods from the next Gem release, | |
# see TODOS below | |
require 'ostruct'; require 'json' | |
def post(client, url, params) | |
OpenStruct.new JSON.parse client.post(url, params) | |
end | |
puts "Done when? (<enter> to skip)" | |
done_when = gets | |
unless done_when.nil? || done_when.strip == "" | |
puts "Gotcha! Creating checklist..." | |
#TODO: Replace with `client.create_new_checklist('Done when')` on gem release >= 1.1.2 | |
checklist = post(client, "/cards/#{card.id}/checklists", name: 'Done When') | |
#TODO: Replace with `checklist.add_item(done_when)` on gem release >= 1.1.2 | |
client.post("/checklists/#{checklist.id}/checkItems", {:name => done_when}) | |
puts '"Done When" checklist created...' | |
end | |
puts | |
puts "Add standard Tasks checklist with tasks PR and Deploy?" | |
puts "(<y> to add, <enter> or anything else to skip)" | |
if gets.strip.downcase == 'y' | |
puts "Gotcha! Creating checklist..." | |
#TODO: Replace with `client.create_new_checklist('Tasks')` on gem release >= 1.1.2 | |
checklist = post(client, "/cards/#{card.id}/checklists", name: 'Tasks') | |
#TODO: Replace with `checklist.add_item('PR')` on gem release >= 1.1.2 | |
client.post("/checklists/#{checklist.id}/checkItems", {:name => 'PR'}) | |
#TODO: Replace with `checklist.add_item('Deploy')` on gem release >= 1.1.2 | |
client.post("/checklists/#{checklist.id}/checkItems", {:name => 'Deploy'}) | |
puts '"Task" checklist created...' | |
end | |
puts card.url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment