Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env ruby
require 'trello' # ruby-trello gem
require 'csv'
#
# export TRELLO_DEVELOPER_PUBLIC_KEY=yourkeyfrom # https://trello.com/app-key
# export BOARD_NAME=nameofboardtoimportto
# export MEMBER_NAME=yourusername
# run: ./import_trello_cards filename.csv
class TrelloCardsFromCSV
include Trello
attr_accessor :filename, :board_name, :board, :member_name, :member, :label
# Initialize the object and process the card file
#
# filename - the relative name of the file to read card data from (must be well-formed CSV)
# board_name - the name of the board to add cards to (comes from an environment variable)
# member_name - the name of the member to add cards as (comes from an environment variable)
# label - the label to apply to created cards (optional, but in this script defaults to 'blue')
#
def initialize(filename, board_name, member_name, label = nil)
self.filename = filename
self.board_name = board_name
self.member_name = member_name
self.label = label
Trello.configure do |config|
key = ENV['TRELLO_DEVELOPER_PUBLIC_KEY']
config.developer_public_key = key
token = ENV['TRELLO_MEMBER_TOKEN'] || begin
`open "https://trello.com/1/authorize?key=#{key}&name=Trello+Import&expiration=1day&response_type=token&scope=read,write"`
puts "Accept permissions and paste the token in here: "
STDIN.gets.chomp
end
config.member_token = token
end
# Find the member to use when looking for boards
self.member = Member.find(self.member_name)
puts "Using member: #{member.inspect}"
# Find the board by it's name
self.board = self.member.boards.select { |b| b.name == self.board_name }.first
# Stop running the script if a matching board could not be found
raise "No board found" unless board
puts "Using board #{board.name}"
# Assume first list is the one we want to import to
list_id = self.board.lists.first.id
puts "Using list #{self.board.lists.first.name}"
# Open file and import
CSV.open(filename, headers: true).each do |row|
name = row["name"]
url = "http://iasummit.org" + row["path"]
speakers = row.fetch("speakers", "").to_s.split(",").map(&:strip).map {|markup| markup.match(/\>(.*)\</)[1]}
# The card will be created with a name such as 001 User Authentication
# and a description which will be whatever is in the final column of the CSV
card = Card.create({
:list_id => list_id,
:name => name + ' == ' + speakers.join(', '),
:description => url
})
# A card label is applied here if one is passed in - note that 'colors' must be used
# rather than any text applied to the label.
# card.add_label(self.label) if self.label
puts "Imported #{card.name}"
end
end
end
# Set up and run the script, passing in environment variables except for the filename
# , which is passed in from the command, and the label, which is fixed.
TrelloCardsFromCSV.new(
ARGV.first,
ENV['BOARD_NAME'],
ENV['MEMBER_NAME'],
'blue'
)
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.