Commit 35880a5676e417deab3087841ad218ed49603b09

Authored by Nick Recobra
1 parent f707fd46
Exists in master and in 1 other branch production

Lighthouseapp tracker model.

@@ -6,6 +6,7 @@ gem 'mongoid', '~> 2.0.0.rc.7' @@ -6,6 +6,7 @@ gem 'mongoid', '~> 2.0.0.rc.7'
6 gem 'haml' 6 gem 'haml'
7 gem 'will_paginate' 7 gem 'will_paginate'
8 gem 'devise', '~> 1.1.8' 8 gem 'devise', '~> 1.1.8'
  9 +gem 'lighthouse-api'
9 10
10 platform :ruby do 11 platform :ruby do
11 gem 'bson_ext', '~> 1.2' 12 gem 'bson_ext', '~> 1.2'
@@ -46,6 +46,9 @@ GEM @@ -46,6 +46,9 @@ GEM
46 railties (>= 3.0.0) 46 railties (>= 3.0.0)
47 haml (3.0.25) 47 haml (3.0.25)
48 i18n (0.5.0) 48 i18n (0.5.0)
  49 + lighthouse-api (2.0)
  50 + activeresource (>= 3.0.0)
  51 + activesupport (>= 3.0.0)
49 mail (2.2.15) 52 mail (2.2.15)
50 activesupport (>= 2.3.6) 53 activesupport (>= 2.3.6)
51 i18n (>= 0.4.0) 54 i18n (>= 0.4.0)
@@ -110,6 +113,7 @@ DEPENDENCIES @@ -110,6 +113,7 @@ DEPENDENCIES
110 devise (~> 1.1.8) 113 devise (~> 1.1.8)
111 factory_girl_rails 114 factory_girl_rails
112 haml 115 haml
  116 + lighthouse-api
113 mongoid (~> 2.0.0.rc.7) 117 mongoid (~> 2.0.0.rc.7)
114 nokogiri 118 nokogiri
115 rails (= 3.0.5) 119 rails (= 3.0.5)
@@ -89,6 +89,11 @@ for you. Checkout [Hoptoad](http://hoptoadapp.com) from the guys over at @@ -89,6 +89,11 @@ for you. Checkout [Hoptoad](http://hoptoadapp.com) from the guys over at
89 89
90 4. Enjoy! 90 4. Enjoy!
91 91
  92 +Lighthouseapp integration
  93 +-------------------------
  94 +
  95 +* Get an API token with full access to the project (visit http://help.lighthouseapp.com/kb/api/how-do-i-get-an-api-token to learn how to get it)
  96 +
92 TODO 97 TODO
93 ---- 98 ----
94 99
app/controllers/apps_controller.rb
@@ -26,6 +26,7 @@ class AppsController < ApplicationController @@ -26,6 +26,7 @@ class AppsController < ApplicationController
26 26
27 def edit 27 def edit
28 @app.watchers.build if @app.watchers.none? 28 @app.watchers.build if @app.watchers.none?
  29 + @app.issue_trackers.build if @app.issue_trackers.none?
29 end 30 end
30 31
31 def create 32 def create
app/models/app.rb
@@ -16,6 +16,7 @@ class App @@ -16,6 +16,7 @@ class App
16 16
17 embeds_many :watchers 17 embeds_many :watchers
18 embeds_many :deploys 18 embeds_many :deploys
  19 + embeds_many :issue_trackers
19 references_many :errs, :dependent => :destroy 20 references_many :errs, :dependent => :destroy
20 21
21 before_validation :generate_api_key, :on => :create 22 before_validation :generate_api_key, :on => :create
@@ -24,9 +25,12 @@ class App @@ -24,9 +25,12 @@ class App
24 validates_uniqueness_of :name, :allow_blank => true 25 validates_uniqueness_of :name, :allow_blank => true
25 validates_uniqueness_of :api_key, :allow_blank => true 26 validates_uniqueness_of :api_key, :allow_blank => true
26 validates_associated :watchers 27 validates_associated :watchers
  28 + validate :check_issue_trackers
27 29
28 accepts_nested_attributes_for :watchers, :allow_destroy => true, 30 accepts_nested_attributes_for :watchers, :allow_destroy => true,
29 :reject_if => proc { |attrs| attrs[:user_id].blank? && attrs[:email].blank? } 31 :reject_if => proc { |attrs| attrs[:user_id].blank? && attrs[:email].blank? }
  32 + accepts_nested_attributes_for :issue_trackers, :allow_destroy => true,
  33 + :reject_if => proc { |attrs| !%w( lighthouseapp ).include?(attrs[:issue_tracker_type]) }
30 34
31 # Mongoid Bug: find(id) on association proxies returns an Enumerator 35 # Mongoid Bug: find(id) on association proxies returns an Enumerator
32 def self.find_by_id!(app_id) 36 def self.find_by_id!(app_id)
@@ -46,5 +50,13 @@ class App @@ -46,5 +50,13 @@ class App
46 def generate_api_key 50 def generate_api_key
47 self.api_key ||= ActiveSupport::SecureRandom.hex 51 self.api_key ||= ActiveSupport::SecureRandom.hex
48 end 52 end
49 - 53 +
  54 + def check_issue_trackers
  55 + issue_trackers.map(&:valid?)
  56 + issue_trackers.each do |tracker|
  57 + tracker.errors.full_messages.each do |error|
  58 + errors[:base] << error
  59 + end if tracker.errors
  60 + end
  61 + end
50 end 62 end
app/models/issue_tracker.rb 0 → 100644
@@ -0,0 +1,18 @@ @@ -0,0 +1,18 @@
  1 +class IssueTracker
  2 + include Mongoid::Document
  3 + include Mongoid::Timestamps
  4 +
  5 + validate :check_lighthouseapp_params
  6 +
  7 + embedded_in :app, :inverse_of => :issue_trackers
  8 +
  9 + field :account, :type => String
  10 + field :api_token, :type => String
  11 + field :project_id, :type => String
  12 + field :issue_tracker_type, :type => String, :default => 'lighthouseapp'
  13 +
  14 + protected
  15 + def check_lighthouseapp_params
  16 + errors.add(:base, "You must specify your Lighthouseapp account, token and project id") if %w( api_token project_id account ).map {|m| self[m].blank? }.any?
  17 + end
  18 +end
spec/controllers/apps_controller_spec.rb
@@ -147,7 +147,6 @@ describe AppsController do @@ -147,7 +147,6 @@ describe AppsController do
147 describe "PUT /apps/:id" do 147 describe "PUT /apps/:id" do
148 before do 148 before do
149 @app = Factory(:app) 149 @app = Factory(:app)
150 - App.stub(:find).with(@app.id).and_return(@app)  
151 end 150 end
152 151
153 context "when the update is successful" do 152 context "when the update is successful" do
@@ -172,11 +171,60 @@ describe AppsController do @@ -172,11 +171,60 @@ describe AppsController do
172 171
173 context "when the update is unsuccessful" do 172 context "when the update is unsuccessful" do
174 it "should render the edit page" do 173 it "should render the edit page" do
175 - @app.should_receive(:update_attributes).and_return(false)  
176 - put :update, :id => @app.id, :app => {} 174 + put :update, :id => @app.id, :app => { :name => '' }
177 response.should render_template(:edit) 175 response.should render_template(:edit)
178 end 176 end
179 end 177 end
  178 +
  179 + context "setting up issue tracker", :cur => true do
  180 + context "unknown tracker type" do
  181 + before(:each) do
  182 + put :update, :id => @app.id, :app => { :issue_trackers_attributes => { '0' => {
  183 + :issue_tracker_type => 'unknown', :project_id => '1234', :api_token => '123123', :account => 'myapp'
  184 + } } }
  185 + @app.reload
  186 + end
  187 +
  188 + it "should not create issue tracker" do
  189 + @app.issue_trackers.should be_empty
  190 + end
  191 + end
  192 +
  193 + context "lighthouseapp" do
  194 + it "should save tracker params" do
  195 + put :update, :id => @app.id, :app => { :issue_trackers_attributes => { '0' => {
  196 + :issue_tracker_type => 'lighthouseapp', :project_id => '1234', :api_token => '123123', :account => 'myapp'
  197 + } } }
  198 + @app.reload
  199 +
  200 + tracker = @app.issue_trackers.first
  201 + tracker.issue_tracker_type.should == 'lighthouseapp'
  202 + tracker.project_id.should == '1234'
  203 + tracker.api_token.should == '123123'
  204 + tracker.account.should == 'myapp'
  205 + end
  206 +
  207 + it "should show validation notice when sufficient params are not present" do
  208 + put :update, :id => @app.id, :app => { :issue_trackers_attributes => { '0' => {
  209 + :issue_tracker_type => 'lighthouseapp', :project_id => '1234', :api_token => '123123'
  210 + } } }
  211 + @app.reload
  212 +
  213 + @app.issue_trackers.should be_empty
  214 + response.body.should match(/You must specify your Lighthouseapp account, token and project id/)
  215 + end
  216 +
  217 + it "should show validation notice when sufficient params are not present" do
  218 + put :update, :id => @app.id, :app => { :issue_trackers_attributes => { '0' => {
  219 + :issue_tracker_type => 'lighthouseapp', :project_id => '1234', :api_token => '123123'
  220 + } } }
  221 + @app.reload
  222 +
  223 + @app.issue_trackers.should be_empty
  224 + response.body.should match(/You must specify your Lighthouseapp account, token and project id/)
  225 + end
  226 + end
  227 + end
180 end 228 end
181 229
182 describe "DELETE /apps/:id" do 230 describe "DELETE /apps/:id" do