Skip to content

Commit

Permalink
Merge pull request #1 from chrissiedeist/refactor_1.0.0
Browse files Browse the repository at this point in the history
rewrite trello gateway
  • Loading branch information
raymondberg committed Sep 16, 2015
2 parents 5059509 + 9758017 commit c556bf9
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 105 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## Overview
Generates trello cards on a specified board for all open issues in specified github repo(s).
Generates trello cards on a specified board for all open issues in specified github repo(s).

## Usage

Expand All @@ -9,12 +9,12 @@ After completing [prerequisites](#prerequisites):
```
require 'github_to_trello'
public_key = "your_public_key"
token = "your_token"
board_id "your_trello_board_id"
repo_name = "your_name/your_repo"
GithubToTrello.new(public_key, token, board_id, repo_name).update
GithubToTrello.new(
:public_key => "your_public_key",
:token => "your_token",
:board_id => "your_trello_board_id",
:inbox_name => "your_name/your_repo",
).update
```

## Prerequisites
Expand All @@ -23,7 +23,7 @@ GithubToTrello.new(public_key, token, board_id, repo_name).update
2. A public key and token giving access to that trello account
- `PUBLIC_KEY` - Log into trello and visit https://trello.com/app-key
- `TOKEN` - Go to
https://trello.com/1/connect?key=...&name=MyApp&response_type=token&scope=read,write
https://trello.com/1/connect?key=...&name=MyApp&response_type=token&scope=read,write
(substituting the public key for ... a unique name for MyApp)

## Features
Expand All @@ -35,3 +35,4 @@ GithubToTrello.new(public_key, token, board_id, repo_name).update
Sites used for reference
- http://www.sitepoint.com/customizing-trello-ruby/
- https://github.com/jeremytregunna/ruby-trello/blob/master/lib/trello/card.rb

53 changes: 29 additions & 24 deletions lib/github_to_trello/trello_gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,68 @@
DAYS_TIL_REALLY_OLD = 28

class TrelloGateway
attr_reader :list, :board, :claimed_list, :done_list
attr_reader :board, :inbox

def initialize(options)
[:public_key, :token, :board_id, :inbox_name].each do |required_field|
_raise_argument_error(required_field) unless options[required_field]
end

def initialize(public_key, token, board_id, repo_name)
Trello.configure do |c|
c.developer_public_key = public_key
c.member_token = token
c.developer_public_key = options[:public_key]
c.member_token = options[:token]
end

@board = _board(board_id)
@list = _list(repo_name)
@claimed_list = _list("Claimed")
@done_list = _list("Done")
@board = _board(options[:board_id])
@inbox = _list(options[:inbox_name])
end

def create_or_update_card(issue)
existing_card = _existing_card?(issue)
existing_card = _existing_card(issue)
existing_card.nil? ? _create_card(issue) : existing_card
end

def _existing_card?(issue)
unclaimed_card = _list_contains_issue?(@list, issue)
claimed_card = _list_contains_issue?(@claimed_list, issue)
done_card = _list_contains_issue?(@done_list, issue)
def lists
board.lists
end

unclaimed_card || claimed_card || done_card
def _raise_argument_error(field)
raise "Argument '#{field}' is required yet missing"
end

def _existing_card(issue)
lists.each do |list|
list.cards.each do |card|
return card if card.name == issue.title
end
end
nil
end

def _create_card(issue)
card = Trello::Card.create(
:name => issue.title,
:list_id => @list.id,
:list_id => inbox.id,
:desc => issue.body + "\n" + issue.html_url + "\n" + issue.updated_at.to_s,
)
_add_checklist_to(card)
card
end

def _add_checklist_to(card)
checklist = Trello::Checklist.create(:name => "Todo", :board_id => @board.id)
checklist = Trello::Checklist.create(:name => "Todo", :board_id => board.id)
checklist.add_item("Initial Response")
card.add_checklist(checklist)
end

def _list_contains_issue?(list, issue)
list.cards.detect do |card|
card.name == issue.title
end
end

def _board(id)
Trello::Board.find(id)
end

def _list(name)
found_list = @board.lists.detect do |list|
found_list = board.lists.detect do |list|
list.name =~ /#{name}/
end
found_list || Trello::List.create(:name => name, :board_id => @board.id)
found_list || Trello::List.create(:name => name, :board_id => board.id)
end
end
116 changes: 43 additions & 73 deletions spec/trello_gateway_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

describe TrelloGateway do
before(:each) do
public_key = "56acdaa7404ebcc8bbaffab18428d4d2"
token = "08f4481d00aba0091592ad9e0ce7e025ac9e238ead31852fe4a75270fbd562e9"
board_id = "5jGWvKui"
repo = "django_blog"
@gateway = TrelloGateway.new(
:public_key => "56acdaa7404ebcc8bbaffab18428d4d2",
:token => "08f4481d00aba0091592ad9e0ce7e025ac9e238ead31852fe4a75270fbd562e9",
:board_id => "5jGWvKui",
:repo_name => "django_blog",
)

@gateway = TrelloGateway.new(public_key, token, board_id, repo)
@issue = double(:issue,
:title => "Test",
:id => "91374795",
Expand All @@ -21,53 +22,36 @@
after(:each) do
@gateway.board.lists.each do |list|
list.cards.each(&:delete)
list.close!
end
end

describe "initialization" do
it "has a list named for the repo" do
expect(@gateway.list).to be_a Trello::List
expect(@gateway.list.name).to eq("django_blog")
end

it "has a list called 'Claimed'" do
expect(@gateway.list).to be_a Trello::List
expect(@gateway.claimed_list.name).to eq("Claimed")
end

it "has a list called 'Done'" do
expect(@gateway.list).to be_a Trello::List
expect(@gateway.done_list.name).to eq("Done")
it "creates the inbox list" do
expect(@gateway.inbox).to be_a Trello::List
expect(@gateway.inbox.name).to eq("django_blog")
end

it "has a board" do
expect(@gateway.board).to be_a Trello::Board
end
end

describe "_existing_card?" do
it "returns the existing trello card if the card exists in it's repos list" do
describe "_existing_card" do
it "returns the existing trello card if the card exists in the inbox folder" do
card = @gateway.create_or_update_card(@issue)

existing_card = @gateway._existing_card?(@issue)
existing_card = @gateway._existing_card(@issue)
expect(existing_card).to be_a(Trello::Card)
expect(existing_card.name).to eq("Test")
end

it "returns the existing trello card if the card exists in the 'Claimed' list" do
it "returns the existing trello card if the card exists in list other than the inbox" do
other_list = Trello::List.create(:name => "some other list", :board_id => @gateway.board.id)
card = @gateway.create_or_update_card(@issue)
card.move_to_list(@gateway.claimed_list.id)
card.move_to_list(other_list.id)

existing_card = @gateway._existing_card?(@issue)
expect(existing_card).to be_a(Trello::Card)
expect(existing_card.name).to eq("Test")
end

it "returns the existing trello card if the card exists in the 'Done' list" do
card = @gateway.create_or_update_card(@issue)
card.move_to_list(@gateway.done_list.id)

existing_card = @gateway._existing_card?(@issue)
existing_card = @gateway._existing_card(@issue)
expect(existing_card).to be_a(Trello::Card)
expect(existing_card.name).to eq("Test")
end
Expand All @@ -83,11 +67,23 @@

@gateway.create_or_update_card(not_the_issue)

existing_card = @gateway._existing_card?(@issue)
existing_card = @gateway._existing_card(@issue)
expect(existing_card).to be_nil
end
end

describe "lists" do
it "presents all lists on the board" do
expect(@gateway.lists.count).to eq(1)
expect(@gateway.lists.first).to be_a Trello::List
expect(@gateway.lists.first.name).to eq("django_blog")
5.times do |number|
Trello::List.create(:name => "list_number_#{number}", :board_id => @gateway.board.id)
end
expect(@gateway.lists.count).to eq(6)
end
end

describe "create_or_update_card" do
it "creates a new card on the appropriate list if it does not exist" do
card = @gateway.create_or_update_card(@issue)
Expand All @@ -113,48 +109,22 @@
expect(card.name).to eq("Test")
end

it "does not add a duplicate card if card exists in a list called 'claimed'" do
issue = double(:issue,
:title => "Test",
:id => "91374795",
:updated_at => Date.today.to_s,
:body => "This is a test",
:html_url => "https://github.com/chrissiedeist/django_blog/issues/1",
)

@gateway.claimed_list.cards.length.should == 0

card = @gateway.create_or_update_card(issue)
card.list.name.should == "django_blog"

card.move_to_list(@gateway.claimed_list.id)
card = @gateway.create_or_update_card(issue)

card.list.name.should == "Claimed"
@gateway.claimed_list.cards.length.should == 1
@gateway.list.cards.length.should == 0
end

it "does not add a duplicate card if card exists in a list called 'done'" do
issue = double(:issue,
:title => "Test",
:id => "91374795",
:updated_at => Date.today.to_s,
:body => "This is a test",
:html_url => "https://github.com/chrissiedeist/django_blog/issues/1",
)

@gateway.done_list.cards.length.should == 0
it "does not add a duplicate card if card exists in a list" do
card = @gateway.create_or_update_card(@issue)
expect(card.list.name).to eq("django_blog")
expect(@gateway.inbox.cards.length).to eq(1)

card = @gateway.create_or_update_card(issue)
card.list.name.should == "django_blog"
other_list = Trello::List.create(:name => "some other list", :board_id => @gateway.board.id)
expect(other_list.cards.length).to eq(0)

card.move_to_list(@gateway.done_list.id)
card = @gateway.create_or_update_card(issue)
card.move_to_list(other_list.id)
expect(other_list.cards.length).to eq(1)
expect(@gateway.inbox.cards.length).to eq(0)

card.list.name.should == "Done"
@gateway.done_list.cards.length.should == 1
@gateway.list.cards.length.should == 0
card = @gateway.create_or_update_card(@issue)
expect(card.list.name).to eq("some other list")
expect(other_list.cards.length).to eq(1)
expect(@gateway.inbox.cards.length).to eq(0)
end
end
end

0 comments on commit c556bf9

Please sign in to comment.