Commit e64ffaf01350d7dadeec021f87e2451f5fe0e9f0
1 parent
9b21d5b0
Exists in
master
and in
1 other branch
extract IssueCreation class
Showing
3 changed files
with
111 additions
and
29 deletions
Show diff stats
app/controllers/problems_controller.rb
... | ... | @@ -35,36 +35,10 @@ class ProblemsController < ApplicationController |
35 | 35 | end |
36 | 36 | |
37 | 37 | def create_issue |
38 | - # Create an issue on GitHub using user's github token | |
39 | - if params[:tracker] == 'user_github' | |
40 | - if !@app.github_repo? | |
41 | - flash[:error] = "This app doesn't have a GitHub repo set up." | |
42 | - elsif !current_user.github_account? | |
43 | - flash[:error] = "You haven't linked your Github account." | |
44 | - else | |
45 | - @tracker = GithubIssuesTracker.new( | |
46 | - :app => @app, | |
47 | - :username => current_user.github_login, | |
48 | - :oauth_token => current_user.github_oauth_token | |
49 | - ) | |
50 | - end | |
38 | + issue_creation = IssueCreation.new(@problem, current_user, params[:tracker]) | |
51 | 39 | |
52 | - # Or, create an issue using the App's issue tracker | |
53 | - elsif @app.issue_tracker_configured? | |
54 | - @tracker = @app.issue_tracker | |
55 | - | |
56 | - # Otherwise, display error about missing tracker configuration. | |
57 | - else | |
58 | - flash[:error] = "This app has no issue tracker setup." | |
59 | - end | |
60 | - | |
61 | - if flash[:error].blank? && @tracker | |
62 | - begin | |
63 | - @tracker.create_issue @problem, current_user | |
64 | - rescue Exception => ex | |
65 | - Rails.logger.error "Error during issue creation: " << ex.message | |
66 | - flash[:error] = "There was an error during issue creation: #{ex.message}" | |
67 | - end | |
40 | + unless issue_creation.execute | |
41 | + flash[:error] = issue_creation.errors[:base].first | |
68 | 42 | end |
69 | 43 | |
70 | 44 | redirect_to app_problem_path(@app, @problem) | ... | ... |
... | ... | @@ -0,0 +1,55 @@ |
1 | +class IssueCreation | |
2 | + include ActiveModel::Validations | |
3 | + | |
4 | + attr_reader :problem, :user, :tracker_name | |
5 | + | |
6 | + delegate :app, :to => :problem | |
7 | + | |
8 | + def initialize(problem, user, tracker_name) | |
9 | + @problem = problem | |
10 | + @user = user | |
11 | + @tracker_name = tracker_name | |
12 | + end | |
13 | + | |
14 | + def tracker | |
15 | + return @tracker if @tracker | |
16 | + | |
17 | + # Create an issue on GitHub using user's github token | |
18 | + if tracker_name == 'user_github' | |
19 | + if !app.github_repo? | |
20 | + errors.add :base, "This app doesn't have a GitHub repo set up." | |
21 | + elsif !user.github_account? | |
22 | + errors.add :base, "You haven't linked your Github account." | |
23 | + else | |
24 | + @tracker = GithubIssuesTracker.new( | |
25 | + :app => app, | |
26 | + :username => user.github_login, | |
27 | + :oauth_token => user.github_oauth_token | |
28 | + ) | |
29 | + end | |
30 | + | |
31 | + # Or, create an issue using the App's issue tracker | |
32 | + elsif app.issue_tracker_configured? | |
33 | + @tracker = app.issue_tracker | |
34 | + | |
35 | + # Otherwise, display error about missing tracker configuration. | |
36 | + else | |
37 | + errors.add :base, "This app has no issue tracker setup." | |
38 | + end | |
39 | + | |
40 | + @tracker | |
41 | + end | |
42 | + | |
43 | + def execute | |
44 | + if tracker | |
45 | + begin | |
46 | + tracker.create_issue problem, user | |
47 | + rescue => ex | |
48 | + Rails.logger.error "Error during issue creation: " << ex.message | |
49 | + errors.add :base, "There was an error during issue creation: #{ex.message}" | |
50 | + end | |
51 | + end | |
52 | + | |
53 | + errors.empty? | |
54 | + end | |
55 | +end | ... | ... |
... | ... | @@ -0,0 +1,53 @@ |
1 | +require 'spec_helper' | |
2 | + | |
3 | +describe IssueCreation do | |
4 | + subject(:issue_creation) { IssueCreation.new(problem, user, tracker_name) } | |
5 | + | |
6 | + let(:problem) { notice.problem } | |
7 | + let(:notice) { Fabricate(:notice) } | |
8 | + let(:user) { Fabricate(:admin) } | |
9 | + let(:errors) { issue_creation.errors[:base] } | |
10 | + let(:tracker_name) { nil } | |
11 | + | |
12 | + it "adds the error when issue tracker isn't configured" do | |
13 | + issue_creation.execute | |
14 | + expect(errors).to include("This app has no issue tracker setup.") | |
15 | + end | |
16 | + | |
17 | + it 'creates an issue if issue tracker is configured' do | |
18 | + tracker = Fabricate(:lighthouse_tracker, :app => notice.app) | |
19 | + tracker.should_receive(:create_issue) | |
20 | + issue_creation.execute | |
21 | + expect(errors).to be_empty | |
22 | + end | |
23 | + | |
24 | + context "with user's github" do | |
25 | + let(:tracker_name) { 'user_github' } | |
26 | + | |
27 | + it "adds the error when repo isn't set up" do | |
28 | + issue_creation.execute | |
29 | + expect(errors).to include("This app doesn't have a GitHub repo set up.") | |
30 | + end | |
31 | + | |
32 | + context 'with repo set up' do | |
33 | + before do | |
34 | + notice.app.update_attribute(:github_repo, 'errbit/errbit') | |
35 | + end | |
36 | + | |
37 | + it "adds the error when github account isn't linked" do | |
38 | + issue_creation.execute | |
39 | + expect(errors).to include("You haven't linked your Github account.") | |
40 | + end | |
41 | + | |
42 | + it 'creates an issue if github account is linked' do | |
43 | + user.github_login = 'admin' | |
44 | + user.github_oauth_token = 'oauthtoken' | |
45 | + user.save! | |
46 | + | |
47 | + GithubIssuesTracker.any_instance.should_receive(:create_issue) | |
48 | + issue_creation.execute | |
49 | + expect(errors).to be_empty | |
50 | + end | |
51 | + end | |
52 | + end | |
53 | +end | ... | ... |