Commit 3a22631dd347333036b4eec62e96be1196c1ee27

Authored by Dmitriy Zaporozhets
1 parent 576ad445

Make service code more abstract

app/controllers/services_controller.rb
1 1 class ServicesController < ProjectResourceController
2 2 # Authorize
3 3 before_filter :authorize_admin_project!
  4 + before_filter :service, only: [:edit, :update, :test]
4 5  
5 6 respond_to :html
6 7  
7 8 def index
8   - @gitlab_ci_service = @project.gitlab_ci_service
  9 + @project.build_missing_services
  10 + @services = @project.services
9 11 end
10 12  
11 13 def edit
12   - @service = @project.gitlab_ci_service
13   -
14   - # Create if missing
15   - @service = @project.create_gitlab_ci_service unless @service
16 14 end
17 15  
18 16 def update
19   - @service = @project.gitlab_ci_service
20   -
21 17 if @service.update_attributes(params[:service])
22   - redirect_to edit_project_service_path(@project, :gitlab_ci)
  18 + redirect_to edit_project_service_path(@project, @service.to_param)
23 19 else
24 20 render 'edit'
25 21 end
... ... @@ -28,9 +24,14 @@ class ServicesController &lt; ProjectResourceController
28 24 def test
29 25 data = GitPushService.new.sample_data(project, current_user)
30 26  
31   - @service = project.gitlab_ci_service
32 27 @service.execute(data)
33 28  
34 29 redirect_to :back
35 30 end
  31 +
  32 + private
  33 +
  34 + def service
  35 + @service ||= @project.services.find { |service| service.to_param == params[:id] }
  36 + end
36 37 end
... ...
app/models/gitlab_ci_service.rb
... ... @@ -54,4 +54,23 @@ class GitlabCiService &lt; Service
54 54 def status_img_path
55 55 project_url + "/status.png?ref=" + project.default_branch
56 56 end
  57 +
  58 + def title
  59 + 'GitLab CI'
  60 + end
  61 +
  62 + def description
  63 + 'Continuous integration server from GitLab'
  64 + end
  65 +
  66 + def to_param
  67 + 'gitlab_ci'
  68 + end
  69 +
  70 + def fields
  71 + [
  72 + { type: 'text', name: 'token', placeholder: 'GitLab CI project specific token' },
  73 + { type: 'text', name: 'project_url', placeholder: 'http://ci.gitlabhq.com/projects/3'}
  74 + ]
  75 + end
57 76 end
... ...
app/models/project.rb
... ... @@ -48,6 +48,7 @@ class Project &lt; ActiveRecord::Base
48 48 has_one :forked_project_link, dependent: :destroy, foreign_key: "forked_to_project_id"
49 49 has_one :forked_from_project, through: :forked_project_link
50 50  
  51 + has_many :services, dependent: :destroy
51 52 has_many :events, dependent: :destroy
52 53 has_many :merge_requests, dependent: :destroy
53 54 has_many :issues, dependent: :destroy, order: "state DESC, created_at DESC"
... ... @@ -223,8 +224,18 @@ class Project &lt; ActiveRecord::Base
223 224 self.issues_enabled && !self.used_default_issues_tracker?
224 225 end
225 226  
226   - def services
227   - [gitlab_ci_service].compact
  227 + def build_missing_services
  228 + available_services_names.each do |service_name|
  229 + service = services.find { |service| service.to_param == service_name }
  230 +
  231 + # If service is available but missing in db
  232 + # we should create an instance. Ex `create_gitlab_ci_service`
  233 + service = self.send :"create_#{service_name}_service" if service.nil?
  234 + end
  235 + end
  236 +
  237 + def available_services_names
  238 + %w(gitlab_ci)
228 239 end
229 240  
230 241 def gitlab_ci?
... ...
app/models/service.rb
... ... @@ -13,6 +13,8 @@
13 13 # project_url :string(255)
14 14 #
15 15  
  16 +# To add new service you should build a class inherited from Service
  17 +# and implement a set of methods
16 18 class Service < ActiveRecord::Base
17 19 attr_accessible :title, :token, :type, :active
18 20  
... ... @@ -24,4 +26,17 @@ class Service &lt; ActiveRecord::Base
24 26 def activated?
25 27 active
26 28 end
  29 +
  30 + def title
  31 + end
  32 +
  33 + def description
  34 + end
  35 +
  36 + def to_param
  37 + end
  38 +
  39 + def fields
  40 + []
  41 + end
27 42 end
... ...
app/views/services/_form.html.haml 0 → 100644
... ... @@ -0,0 +1,48 @@
  1 +%h3.page_title
  2 + - if @service.activated?
  3 + %span.cgreen
  4 + %i.icon-circle
  5 + - else
  6 + %span.cgray
  7 + %i.icon-circle-blank
  8 + = @service.title
  9 +
  10 +%p= @service.description
  11 +
  12 +.back_link
  13 + = link_to project_services_path(@project) do
  14 + &larr; to services
  15 +
  16 +%hr
  17 +
  18 += form_for(@service, as: :service, url: project_service_path(@project, @service.to_param), method: :put) do |f|
  19 + - if @service.errors.any?
  20 + .alert.alert-error
  21 + %ul
  22 + - @service.errors.full_messages.each do |msg|
  23 + %li= msg
  24 +
  25 +
  26 + .control-group
  27 + = f.label :active, "Active", class: "control-label"
  28 + .controls
  29 + = f.check_box :active
  30 +
  31 + - @service.fields.each do |field|
  32 + - name = field[:name]
  33 + - type = field[:type]
  34 + - placeholder = field[:placeholder]
  35 +
  36 + .control-group
  37 + = f.label name, class: "control-label"
  38 + .controls
  39 + - if type == 'text'
  40 + = f.text_field name, class: "input-xlarge", placeholder: placeholder
  41 + - elsif type == 'checkbox'
  42 + = f.check_box name
  43 +
  44 + .form-actions
  45 + = f.submit 'Save', class: 'btn btn-save'
  46 + &nbsp;
  47 + - if @service.valid? && @service.activated?
  48 + = link_to 'Test settings', test_project_service_path(@project, @service.to_param), class: 'btn btn-small'
... ...
app/views/services/_gitlab_ci.html.haml
... ... @@ -1,46 +0,0 @@
1   -%h3.page_title
2   - GitLab CI
3   - %small Continuous integration server from GitLab
4   - .pull-right
5   - - if @service.active
6   - %small.cgreen Enabled
7   - - else
8   - %small.cgray Disabled
9   -
10   -
11   -
12   -.back_link
13   - = link_to project_services_path(@project) do
14   - &larr; to services
15   -
16   -%hr
17   -= form_for(@service, :as => :service, :url => project_service_path(@project, :gitlab_ci), :method => :put) do |f|
18   - - if @service.errors.any?
19   - .alert.alert-error
20   - %ul
21   - - @service.errors.full_messages.each do |msg|
22   - %li= msg
23   -
24   -
25   - .control-group
26   - = f.label :active, "Active", class: "control-label"
27   - .controls
28   - = f.check_box :active
29   -
30   - .control-group
31   - = f.label :project_url, "Project URL", class: "control-label"
32   - .controls
33   - = f.text_field :project_url, class: "input-xlarge", placeholder: "http://ci.gitlabhq.com/projects/3"
34   -
35   - .control-group
36   - = f.label :token, class: "control-label" do
37   - CI Project token
38   - .controls
39   - = f.text_field :token, class: "input-xlarge", placeholder: "GitLab CI project specific token"
40   -
41   -
42   - .form-actions
43   - = f.submit 'Save', class: 'btn btn-save'
44   - &nbsp;
45   - - if @service.valid? && @service.active
46   - = link_to 'Test settings', test_project_service_path(@project), class: 'btn btn-small'
app/views/services/edit.html.haml
1 1 = render "projects/settings_nav"
2 2  
3   -= render 'gitlab_ci'
  3 += render 'form'
... ...
app/views/services/index.html.haml
... ... @@ -3,30 +3,16 @@
3 3 %h3.page_title Services
4 4 %br
5 5  
6   -%ul.ui-box.well-list
7   - %li
8   - %h4.cgreen
9   - = link_to edit_project_service_path(@project, :gitlab_ci) do
10   - GitLab CI
11   - %small Continuous integration server from GitLab
12   - .pull-right
13   - - if @gitlab_ci_service.try(:active)
14   - %small.cgreen
15   - %i.icon-ok
16   - Enabled
  6 +%ul.bordered-list
  7 + - @services.each do |service|
  8 + %li
  9 + %h4
  10 + - if service.activated?
  11 + %span.cgreen
  12 + %i.icon-circle
17 13 - else
18   - %small.cgray
19   - %i.icon-off
20   - Disabled
21   - %li.disabled
22   - %h4
23   - Jenkins CI
24   - %small An extendable open source continuous integration server
25   - .pull-right
26   - %small Not implemented yet
27   - %li.disabled
28   - %h4
29   - Campfire
30   - %small Web-based group chat tool
31   - .pull-right
32   - %small Not implemented yet
  14 + %span.cgray
  15 + %i.icon-circle-blank
  16 + = link_to edit_project_service_path(@project, service.to_param) do
  17 + = service.title
  18 + %p= service.description
... ...