diff --git a/Gemfile b/Gemfile index 7f02765..f759356 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,7 @@ gem 'mongoid', '~> 2.0.0.rc.7' gem 'haml' gem 'will_paginate' gem 'devise', '~> 1.1.8' +gem 'lighthouse-api' platform :ruby do gem 'bson_ext', '~> 1.2' diff --git a/Gemfile.lock b/Gemfile.lock index 85800ec..02f7520 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -46,6 +46,9 @@ GEM railties (>= 3.0.0) haml (3.0.25) i18n (0.5.0) + lighthouse-api (2.0) + activeresource (>= 3.0.0) + activesupport (>= 3.0.0) mail (2.2.15) activesupport (>= 2.3.6) i18n (>= 0.4.0) @@ -110,6 +113,7 @@ DEPENDENCIES devise (~> 1.1.8) factory_girl_rails haml + lighthouse-api mongoid (~> 2.0.0.rc.7) nokogiri rails (= 3.0.5) diff --git a/README.md b/README.md index 6557b7c..1213b01 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,11 @@ for you. Checkout [Hoptoad](http://hoptoadapp.com) from the guys over at 4. Enjoy! +Lighthouseapp integration +------------------------- + +* 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) + TODO ---- diff --git a/app/controllers/apps_controller.rb b/app/controllers/apps_controller.rb index 7b32f3b..e874380 100644 --- a/app/controllers/apps_controller.rb +++ b/app/controllers/apps_controller.rb @@ -26,6 +26,7 @@ class AppsController < ApplicationController def edit @app.watchers.build if @app.watchers.none? + @app.issue_trackers.build if @app.issue_trackers.none? end def create diff --git a/app/models/app.rb b/app/models/app.rb index 073be68..b300438 100644 --- a/app/models/app.rb +++ b/app/models/app.rb @@ -16,6 +16,7 @@ class App embeds_many :watchers embeds_many :deploys + embeds_many :issue_trackers references_many :errs, :dependent => :destroy before_validation :generate_api_key, :on => :create @@ -24,9 +25,12 @@ class App validates_uniqueness_of :name, :allow_blank => true validates_uniqueness_of :api_key, :allow_blank => true validates_associated :watchers + validate :check_issue_trackers accepts_nested_attributes_for :watchers, :allow_destroy => true, :reject_if => proc { |attrs| attrs[:user_id].blank? && attrs[:email].blank? } + accepts_nested_attributes_for :issue_trackers, :allow_destroy => true, + :reject_if => proc { |attrs| !%w( lighthouseapp ).include?(attrs[:issue_tracker_type]) } # Mongoid Bug: find(id) on association proxies returns an Enumerator def self.find_by_id!(app_id) @@ -46,5 +50,13 @@ class App def generate_api_key self.api_key ||= ActiveSupport::SecureRandom.hex end - + + def check_issue_trackers + issue_trackers.map(&:valid?) + issue_trackers.each do |tracker| + tracker.errors.full_messages.each do |error| + errors[:base] << error + end if tracker.errors + end + end end diff --git a/app/models/issue_tracker.rb b/app/models/issue_tracker.rb new file mode 100644 index 0000000..ed7a4fe --- /dev/null +++ b/app/models/issue_tracker.rb @@ -0,0 +1,18 @@ +class IssueTracker + include Mongoid::Document + include Mongoid::Timestamps + + validate :check_lighthouseapp_params + + embedded_in :app, :inverse_of => :issue_trackers + + field :account, :type => String + field :api_token, :type => String + field :project_id, :type => String + field :issue_tracker_type, :type => String, :default => 'lighthouseapp' + + protected + def check_lighthouseapp_params + 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? + end +end diff --git a/spec/controllers/apps_controller_spec.rb b/spec/controllers/apps_controller_spec.rb index 81468dd..e9f6c3e 100644 --- a/spec/controllers/apps_controller_spec.rb +++ b/spec/controllers/apps_controller_spec.rb @@ -147,7 +147,6 @@ describe AppsController do describe "PUT /apps/:id" do before do @app = Factory(:app) - App.stub(:find).with(@app.id).and_return(@app) end context "when the update is successful" do @@ -172,11 +171,60 @@ describe AppsController do context "when the update is unsuccessful" do it "should render the edit page" do - @app.should_receive(:update_attributes).and_return(false) - put :update, :id => @app.id, :app => {} + put :update, :id => @app.id, :app => { :name => '' } response.should render_template(:edit) end end + + context "setting up issue tracker", :cur => true do + context "unknown tracker type" do + before(:each) do + put :update, :id => @app.id, :app => { :issue_trackers_attributes => { '0' => { + :issue_tracker_type => 'unknown', :project_id => '1234', :api_token => '123123', :account => 'myapp' + } } } + @app.reload + end + + it "should not create issue tracker" do + @app.issue_trackers.should be_empty + end + end + + context "lighthouseapp" do + it "should save tracker params" do + put :update, :id => @app.id, :app => { :issue_trackers_attributes => { '0' => { + :issue_tracker_type => 'lighthouseapp', :project_id => '1234', :api_token => '123123', :account => 'myapp' + } } } + @app.reload + + tracker = @app.issue_trackers.first + tracker.issue_tracker_type.should == 'lighthouseapp' + tracker.project_id.should == '1234' + tracker.api_token.should == '123123' + tracker.account.should == 'myapp' + end + + it "should show validation notice when sufficient params are not present" do + put :update, :id => @app.id, :app => { :issue_trackers_attributes => { '0' => { + :issue_tracker_type => 'lighthouseapp', :project_id => '1234', :api_token => '123123' + } } } + @app.reload + + @app.issue_trackers.should be_empty + response.body.should match(/You must specify your Lighthouseapp account, token and project id/) + end + + it "should show validation notice when sufficient params are not present" do + put :update, :id => @app.id, :app => { :issue_trackers_attributes => { '0' => { + :issue_tracker_type => 'lighthouseapp', :project_id => '1234', :api_token => '123123' + } } } + @app.reload + + @app.issue_trackers.should be_empty + response.body.should match(/You must specify your Lighthouseapp account, token and project id/) + end + end + end end describe "DELETE /apps/:id" do -- libgit2 0.21.2