Commit 84e406c8c4f3db3a10769f5b09d644d9c2c12e2a

Authored by Arthur Neves
1 parent ad85849b
Exists in master and in 1 other branch production

Replace issue creation interactor with Issue model

app/controllers/problems_controller.rb
... ... @@ -62,9 +62,10 @@ class ProblemsController < ApplicationController
62 62 end
63 63  
64 64 def create_issue
65   - issue_creation = IssueCreation.new(problem, current_user, params[:tracker], request)
66   -
67   - unless issue_creation.execute
  65 + body = "" # TODO render_issue_body
  66 + title = "" # TODO generate_title
  67 + issue = Issue.new(problem: problem, user: current_user, title: title, body: body)
  68 + unless issue.save
68 69 flash[:error] = issue_creation.errors.full_messages.join(', ')
69 70 end
70 71  
... ...
app/interactors/issue_creation.rb
... ... @@ -1,55 +0,0 @@
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, request)
9   - @problem = problem
10   - @user = user
11   - @tracker_name = tracker_name
12   - IssueTracker.update_url_options(request)
13   - end
14   -
15   - def execute
16   - tracker.create_issue(problem, user) if tracker
17   - errors.empty?
18   - rescue => ex
19   - Rails.logger.error "Error during issue creation: " << ex.message
20   - errors.add :base, "There was an error during issue creation: #{ex.message}"
21   - false
22   - end
23   -
24   - private
25   -
26   - def tracker
27   - return @tracker if @tracker
28   -
29   - # Create an issue on GitHub using user's github token
30   - if tracker_name == 'user_github'
31   - if !app.github_repo?
32   - errors.add :base, "This app doesn't have a GitHub repo set up."
33   - elsif !user.github_account?
34   - errors.add :base, "You haven't linked your Github account."
35   - else
36   - @tracker = ErrbitGithubPlugin::IssueTracker.new(
37   - app, {
38   - :username => user.github_login,
39   - :oauth_token => user.github_oauth_token
40   - }
41   - )
42   - end
43   -
44   - # Or, create an issue using the App's issue tracker
45   - elsif app.issue_tracker_configured?
46   - @tracker = app.issue_tracker
47   -
48   - # Otherwise, display error about missing tracker configuration.
49   - else
50   - errors.add :base, "This app has no issue tracker setup."
51   - end
52   -
53   - @tracker
54   - end
55   -end
app/models/issue.rb 0 → 100644
... ... @@ -0,0 +1,24 @@
  1 +class Issue
  2 + include ActiveModel::Model
  3 + attr_accessor :problem, :user, :title, :body
  4 +
  5 + def intialize(problem: nil, user: nil, title: nil, body: nil)
  6 + @problem, @user, @title, @body = problem, user, title, body
  7 + end
  8 +
  9 + def save
  10 + if tracker
  11 + tracker.create_issue(title, body, user.as_document)
  12 + else
  13 + errors.add :base, "This app has no issue tracker setup."
  14 + end
  15 + errors.empty?
  16 + rescue => ex
  17 + errors.add :base, "There was an error during issue creation: #{ex.message}"
  18 + false
  19 + end
  20 +
  21 + def tracker
  22 + problem.app.issue_tracker
  23 + end
  24 +end
... ...