Run. Bike. Code.

Living the active life while maintaining some nerdiness.

Integrating Trello and Octopress

| Comments

In my last post I wrote about how I use Trello and Beeminder to keep a semi-regular flow of new content appearing in this blog. Today I want to fill you in on the automation that is involved.

From Trello to Vim

I’m lazy. That’s why I automate things instead of doing them manually over and over again. As mentioned in the previous post, I draft most of the blog content in Trello. However, Octopress needs the content in a file so that it can generate a static HTML page out of it. Instead of copying my draft content out of Trello and into an editor, I wrote a short Rake task to do the same thing:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
desc "Pull card from Trello and generate post from it"
task :new_trello_post do
  b = Trello::Board.find(ENV["TRELLO_BOARD"])
  content_cards = []
  b.cards.select {|c| c.list_id == ENV["TRELLO_LIST"] }.each do |card|
    card.labels.each do |label|
      if label.name == "CONTENT"
        content_cards << card
      end
    end
  end
  content_cards.each do |c|
    puts "Id: #{c.id} ('#{c.name}')"
  end
  c_id = ask("Which card? ",nil)
  card = Trello::Card.find(c_id)
  if not card.nil?
    Rake::Task["new_post"].invoke(card.name)
    new_post_file = ENV['NEW_POST']
    open(new_post_file, 'a') do |post|
      post.puts card.description
    end
  end
  `git commit -m 'Adds draft for #{card.name}. Start #{card.short_id}' #{new_post_file}`
end

Let’s walk quickly through it, shall we? I have the ID of my Trello board as well as the ID of the “Draft” list in my environment (lines 3 and 5). With those IDs, using the ruby-trello gem, I fetch all the cards of the “Draft” list that are labeled with “CONTENT” and present (myself) a list with these cards (lines 6-14). I can then choose one by ID and generate a new Octopress post by re-using the appropriate Rake task (line 18). I’ve modified this task to return the filename of the new post via the environment, so that I can automatically add the draft content (as pulled from Trello) into the file and submit the first draft to git (lines 19-24). With a magic commit message (see below), the card gets moved from the “Draft” list to my “Doing” list.

From Git to Trello

Now that I had the ability to feed my editor with the draft content from a Trello card, I wanted to be able to update the original Trello card. Not necessarily with the changes to the content, but more with a log of activity and especially the possibility to notify Trello once the card was done (i.e. the blog post was published). Turns out, Jason Fox had the same desire and wrote a detailed instruction on how to use a small app on Heroku to update Trello from Github. My setup is more or less a verbatim copy, obviously using the right board-id and list-ids.

As you can see above, I will automatically move a draft card by specifying the card.short_id in the commit message.

From Trello to Beeminder

Once I’m happy with the content and ready to publish I’ll commit the final version to git, using the magic word Finish #11 (the draft for this post happens to be card 11, quite a long time candidate to write about). After pushing to the Github repository, the web-hook will update the Trello card one final time, moving it to the “Done” list. And that’s where Beeminder will pick it up and count it towards my “Keep Blogging” goal.

After this short excursion into the technical details of this blog (remember, it’s not only Run and Bike, but also Code!), I will see to get some running content up next. After all, I’ll start my last race of 2013 tomorrow: A 10km New Year’s Eve run.

Comments