Commit 3a22631dd347333036b4eec62e96be1196c1ee27
1 parent
576ad445
Exists in
master
and in
4 other branches
Make service code more abstract
Showing
8 changed files
with
118 additions
and
84 deletions
Show diff stats
app/controllers/services_controller.rb
| 1 | class ServicesController < ProjectResourceController | 1 | class ServicesController < ProjectResourceController |
| 2 | # Authorize | 2 | # Authorize |
| 3 | before_filter :authorize_admin_project! | 3 | before_filter :authorize_admin_project! |
| 4 | + before_filter :service, only: [:edit, :update, :test] | ||
| 4 | 5 | ||
| 5 | respond_to :html | 6 | respond_to :html |
| 6 | 7 | ||
| 7 | def index | 8 | def index |
| 8 | - @gitlab_ci_service = @project.gitlab_ci_service | 9 | + @project.build_missing_services |
| 10 | + @services = @project.services | ||
| 9 | end | 11 | end |
| 10 | 12 | ||
| 11 | def edit | 13 | def edit |
| 12 | - @service = @project.gitlab_ci_service | ||
| 13 | - | ||
| 14 | - # Create if missing | ||
| 15 | - @service = @project.create_gitlab_ci_service unless @service | ||
| 16 | end | 14 | end |
| 17 | 15 | ||
| 18 | def update | 16 | def update |
| 19 | - @service = @project.gitlab_ci_service | ||
| 20 | - | ||
| 21 | if @service.update_attributes(params[:service]) | 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 | else | 19 | else |
| 24 | render 'edit' | 20 | render 'edit' |
| 25 | end | 21 | end |
| @@ -28,9 +24,14 @@ class ServicesController < ProjectResourceController | @@ -28,9 +24,14 @@ class ServicesController < ProjectResourceController | ||
| 28 | def test | 24 | def test |
| 29 | data = GitPushService.new.sample_data(project, current_user) | 25 | data = GitPushService.new.sample_data(project, current_user) |
| 30 | 26 | ||
| 31 | - @service = project.gitlab_ci_service | ||
| 32 | @service.execute(data) | 27 | @service.execute(data) |
| 33 | 28 | ||
| 34 | redirect_to :back | 29 | redirect_to :back |
| 35 | end | 30 | end |
| 31 | + | ||
| 32 | + private | ||
| 33 | + | ||
| 34 | + def service | ||
| 35 | + @service ||= @project.services.find { |service| service.to_param == params[:id] } | ||
| 36 | + end | ||
| 36 | end | 37 | end |
app/models/gitlab_ci_service.rb
| @@ -54,4 +54,23 @@ class GitlabCiService < Service | @@ -54,4 +54,23 @@ class GitlabCiService < Service | ||
| 54 | def status_img_path | 54 | def status_img_path |
| 55 | project_url + "/status.png?ref=" + project.default_branch | 55 | project_url + "/status.png?ref=" + project.default_branch |
| 56 | end | 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 | end | 76 | end |
app/models/project.rb
| @@ -48,6 +48,7 @@ class Project < ActiveRecord::Base | @@ -48,6 +48,7 @@ class Project < ActiveRecord::Base | ||
| 48 | has_one :forked_project_link, dependent: :destroy, foreign_key: "forked_to_project_id" | 48 | has_one :forked_project_link, dependent: :destroy, foreign_key: "forked_to_project_id" |
| 49 | has_one :forked_from_project, through: :forked_project_link | 49 | has_one :forked_from_project, through: :forked_project_link |
| 50 | 50 | ||
| 51 | + has_many :services, dependent: :destroy | ||
| 51 | has_many :events, dependent: :destroy | 52 | has_many :events, dependent: :destroy |
| 52 | has_many :merge_requests, dependent: :destroy | 53 | has_many :merge_requests, dependent: :destroy |
| 53 | has_many :issues, dependent: :destroy, order: "state DESC, created_at DESC" | 54 | has_many :issues, dependent: :destroy, order: "state DESC, created_at DESC" |
| @@ -223,8 +224,18 @@ class Project < ActiveRecord::Base | @@ -223,8 +224,18 @@ class Project < ActiveRecord::Base | ||
| 223 | self.issues_enabled && !self.used_default_issues_tracker? | 224 | self.issues_enabled && !self.used_default_issues_tracker? |
| 224 | end | 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 | end | 239 | end |
| 229 | 240 | ||
| 230 | def gitlab_ci? | 241 | def gitlab_ci? |
app/models/service.rb
| @@ -13,6 +13,8 @@ | @@ -13,6 +13,8 @@ | ||
| 13 | # project_url :string(255) | 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 | class Service < ActiveRecord::Base | 18 | class Service < ActiveRecord::Base |
| 17 | attr_accessible :title, :token, :type, :active | 19 | attr_accessible :title, :token, :type, :active |
| 18 | 20 | ||
| @@ -24,4 +26,17 @@ class Service < ActiveRecord::Base | @@ -24,4 +26,17 @@ class Service < ActiveRecord::Base | ||
| 24 | def activated? | 26 | def activated? |
| 25 | active | 27 | active |
| 26 | end | 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 | end | 42 | end |
| @@ -0,0 +1,48 @@ | @@ -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 | + ← 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 | + | ||
| 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,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 | - ← 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 | - | ||
| 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
app/views/services/index.html.haml
| @@ -3,30 +3,16 @@ | @@ -3,30 +3,16 @@ | ||
| 3 | %h3.page_title Services | 3 | %h3.page_title Services |
| 4 | %br | 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 | - else | 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 |