diff --git a/app/controllers/problems_controller.rb b/app/controllers/problems_controller.rb index 5c4f296..7933b9f 100644 --- a/app/controllers/problems_controller.rb +++ b/app/controllers/problems_controller.rb @@ -35,36 +35,10 @@ class ProblemsController < ApplicationController end def create_issue - # Create an issue on GitHub using user's github token - if params[:tracker] == 'user_github' - if !@app.github_repo? - flash[:error] = "This app doesn't have a GitHub repo set up." - elsif !current_user.github_account? - flash[:error] = "You haven't linked your Github account." - else - @tracker = GithubIssuesTracker.new( - :app => @app, - :username => current_user.github_login, - :oauth_token => current_user.github_oauth_token - ) - end + issue_creation = IssueCreation.new(@problem, current_user, params[:tracker]) - # Or, create an issue using the App's issue tracker - elsif @app.issue_tracker_configured? - @tracker = @app.issue_tracker - - # Otherwise, display error about missing tracker configuration. - else - flash[:error] = "This app has no issue tracker setup." - end - - if flash[:error].blank? && @tracker - begin - @tracker.create_issue @problem, current_user - rescue Exception => ex - Rails.logger.error "Error during issue creation: " << ex.message - flash[:error] = "There was an error during issue creation: #{ex.message}" - end + unless issue_creation.execute + flash[:error] = issue_creation.errors[:base].first end redirect_to app_problem_path(@app, @problem) diff --git a/app/interactors/issue_creation.rb b/app/interactors/issue_creation.rb new file mode 100644 index 0000000..8c074f8 --- /dev/null +++ b/app/interactors/issue_creation.rb @@ -0,0 +1,55 @@ +class IssueCreation + include ActiveModel::Validations + + attr_reader :problem, :user, :tracker_name + + delegate :app, :to => :problem + + def initialize(problem, user, tracker_name) + @problem = problem + @user = user + @tracker_name = tracker_name + end + + def tracker + return @tracker if @tracker + + # Create an issue on GitHub using user's github token + if tracker_name == 'user_github' + if !app.github_repo? + errors.add :base, "This app doesn't have a GitHub repo set up." + elsif !user.github_account? + errors.add :base, "You haven't linked your Github account." + else + @tracker = GithubIssuesTracker.new( + :app => app, + :username => user.github_login, + :oauth_token => user.github_oauth_token + ) + end + + # Or, create an issue using the App's issue tracker + elsif app.issue_tracker_configured? + @tracker = app.issue_tracker + + # Otherwise, display error about missing tracker configuration. + else + errors.add :base, "This app has no issue tracker setup." + end + + @tracker + end + + def execute + if tracker + begin + tracker.create_issue problem, user + rescue => ex + Rails.logger.error "Error during issue creation: " << ex.message + errors.add :base, "There was an error during issue creation: #{ex.message}" + end + end + + errors.empty? + end +end diff --git a/spec/interactors/issue_creation_spec.rb b/spec/interactors/issue_creation_spec.rb new file mode 100644 index 0000000..16fb3ac --- /dev/null +++ b/spec/interactors/issue_creation_spec.rb @@ -0,0 +1,53 @@ +require 'spec_helper' + +describe IssueCreation do + subject(:issue_creation) { IssueCreation.new(problem, user, tracker_name) } + + let(:problem) { notice.problem } + let(:notice) { Fabricate(:notice) } + let(:user) { Fabricate(:admin) } + let(:errors) { issue_creation.errors[:base] } + let(:tracker_name) { nil } + + it "adds the error when issue tracker isn't configured" do + issue_creation.execute + expect(errors).to include("This app has no issue tracker setup.") + end + + it 'creates an issue if issue tracker is configured' do + tracker = Fabricate(:lighthouse_tracker, :app => notice.app) + tracker.should_receive(:create_issue) + issue_creation.execute + expect(errors).to be_empty + end + + context "with user's github" do + let(:tracker_name) { 'user_github' } + + it "adds the error when repo isn't set up" do + issue_creation.execute + expect(errors).to include("This app doesn't have a GitHub repo set up.") + end + + context 'with repo set up' do + before do + notice.app.update_attribute(:github_repo, 'errbit/errbit') + end + + it "adds the error when github account isn't linked" do + issue_creation.execute + expect(errors).to include("You haven't linked your Github account.") + end + + it 'creates an issue if github account is linked' do + user.github_login = 'admin' + user.github_oauth_token = 'oauthtoken' + user.save! + + GithubIssuesTracker.any_instance.should_receive(:create_issue) + issue_creation.execute + expect(errors).to be_empty + end + end + end +end -- libgit2 0.21.2