Created
May 30, 2012 22:32
A Ruby script to import Trello cards from a CSV file
This file contains 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 | |
# You can skip this bit if you wish - you will need the 'ruby-trello' gem installed, and | |
# optionally, 'foreman' to run the script with. | |
require 'bundler/setup' | |
Bundler.require | |
require 'trello' | |
require 'csv' | |
# TrelloCardsFromCSV | |
# This class imports cards onto a Trello board from a CSV file file in the following format: | |
# - ID (can be anything, or blank - just a way of identifying a card | |
# - Name (name of card) | |
# - Description (description of card) | |
# | |
# This Ruby script is designed to be run using `foreman run trello-cards-from-csv.rb [filename]`, as it | |
# uses a number of environment variables. From a file named '.env'. This file should contain each of the variables below on a new line, but you'll only need to enter them once. | |
# Alternatively you can set up the variables you need before | |
# running the script, like so: | |
# TRELLO_APP_ID=xxx TRELLO_APP_SECRET=xxx TRELLO_USER_TOKEN=xxx BOARD_NAME="xxx" MEMBER_NAME="xxx" ./trello-cards-from-csv.rb | |
# | |
# Here's what each of these variables are: | |
# TRELLO_APP_ID, TRELLO_APP_SECRET - The Trello application details generated from: https://trello.com/1/appKey/generate | |
# TRELLO_USER_TOKEN - The token you get by visiting: https://trello.com/1/connect?key=[YOUR TRELLO APP ID]&name=MyApp&response_type=token&scope=read,write,account&expiration=never in your browser and granting permissions. | |
# BOARD_NAME - the name of the board to add cards to (be sure to quote) | |
# MEMBER_NAME - the name of the member to authenticate as | |
# | |
# See methods below for more documentation. | |
class TrelloCardsFromCSV | |
# Include Trello classes so that we can use them directly | |
include Trello | |
include Trello::Authorization | |
attr_accessor :filename, :board_name, :member, :member_name, :board, :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 | |
# Set up the Trello authorization. This uses a number of environment variables | |
# directly to set up the authorization with Trello. | |
Trello::Authorization.const_set :AuthPolicy, OAuthPolicy | |
OAuthPolicy.consumer_credential = OAuthCredential.new ENV['TRELLO_APP_ID'], ENV['TRELLO_APP_SECRET'] | |
OAuthPolicy.token = OAuthCredential.new ENV['TRELLO_USER_TOKEN'], nil | |
# Find the member to use when looking for boards | |
self.member = Member.find(self.member_name) | |
# 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.foreach(filename) do |row| | |
# 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 => row[0..1].join(" "), | |
:description => row[2] | |
}) | |
# 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
Thanks for this script, @joshmcarthur!
I ran into this error when trying to run it:
Any ideas? Was there a particular version of the trello gem that you were using? Maybe it's changed?