diff options
author | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2016-09-12 19:43:04 -0300 |
---|---|---|
committer | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2016-09-16 10:56:48 -0300 |
commit | 3d985faa2c5e8298d5946df3e3995a3e1860b096 (patch) | |
tree | 54ec2b43a95b6ce91f265ce4cad781fb302e1a1f | |
parent | d2283aa1597a5588f2d4f10fe3982463ba63db2f (diff) | |
download | gitlab-ce-3d985faa2c5e8298d5946df3e3995a3e1860b096.tar.gz |
Add service to generate default labels for a group/project
-rw-r--r-- | app/controllers/groups/labels_controller.rb | 2 | ||||
-rw-r--r-- | app/controllers/projects/labels_controller.rb | 2 | ||||
-rw-r--r-- | app/services/labels/generate_service.rb | 35 | ||||
-rw-r--r-- | lib/gitlab/issues_labels.rb | 27 | ||||
-rw-r--r-- | spec/services/labels/generate_service_spec.rb | 24 |
5 files changed, 62 insertions, 28 deletions
diff --git a/app/controllers/groups/labels_controller.rb b/app/controllers/groups/labels_controller.rb index 51605a9586c..1f67e3e198a 100644 --- a/app/controllers/groups/labels_controller.rb +++ b/app/controllers/groups/labels_controller.rb @@ -36,6 +36,8 @@ class Groups::LabelsController < Groups::ApplicationController end def generate + Labels::GenerateService.new(@group, current_user).execute + redirect_to group_labels_path(@group) end diff --git a/app/controllers/projects/labels_controller.rb b/app/controllers/projects/labels_controller.rb index 8d1935eff59..648d5de2e0f 100644 --- a/app/controllers/projects/labels_controller.rb +++ b/app/controllers/projects/labels_controller.rb @@ -48,7 +48,7 @@ class Projects::LabelsController < Projects::ApplicationController end def generate - Gitlab::IssuesLabels.generate(@project) + Labels::GenerateService.new(@project, current_user).execute if params[:redirect] == 'issues' redirect_to namespace_project_issues_path(@project.namespace, @project) diff --git a/app/services/labels/generate_service.rb b/app/services/labels/generate_service.rb new file mode 100644 index 00000000000..8f6a045f46d --- /dev/null +++ b/app/services/labels/generate_service.rb @@ -0,0 +1,35 @@ +module Labels + class GenerateService + def initialize(subject, user) + @subject, @user = subject, user + end + + def execute + label_params.each do |params| + Labels::CreateService.new(subject, user, params).execute + end + end + + private + + attr_reader :subject, :user + + def label_params + red = '#d9534f' + yellow = '#f0ad4e' + blue = '#428bca' + green = '#5cb85c' + + [ + { title: 'bug', color: red }, + { title: 'critical', color: red }, + { title: 'confirmed', color: red }, + { title: 'documentation', color: yellow }, + { title: 'support', color: yellow }, + { title: 'discussion', color: blue }, + { title: 'suggestion', color: blue }, + { title: 'enhancement', color: green } + ] + end + end +end diff --git a/lib/gitlab/issues_labels.rb b/lib/gitlab/issues_labels.rb deleted file mode 100644 index 1bec6088292..00000000000 --- a/lib/gitlab/issues_labels.rb +++ /dev/null @@ -1,27 +0,0 @@ -module Gitlab - class IssuesLabels - class << self - def generate(project) - red = '#d9534f' - yellow = '#f0ad4e' - blue = '#428bca' - green = '#5cb85c' - - labels = [ - { title: "bug", color: red }, - { title: "critical", color: red }, - { title: "confirmed", color: red }, - { title: "documentation", color: yellow }, - { title: "support", color: yellow }, - { title: "discussion", color: blue }, - { title: "suggestion", color: blue }, - { title: "enhancement", color: green } - ] - - labels.each do |label| - project.labels.create(label) - end - end - end - end -end diff --git a/spec/services/labels/generate_service_spec.rb b/spec/services/labels/generate_service_spec.rb new file mode 100644 index 00000000000..e6c27fbe4e3 --- /dev/null +++ b/spec/services/labels/generate_service_spec.rb @@ -0,0 +1,24 @@ +require 'spec_helper' + +describe Labels::GenerateService, services: true do + describe '#execute' do + let(:project) { create(:empty_project) } + + subject(:service) { described_class.new(project, double) } + + context 'when project labels is empty' do + it 'creates the default labels' do + expect { service.execute }.to change(project.labels, :count).by(8) + end + end + + context 'when project labels contains some of default labels' do + it 'creates the missing labels' do + create(:label, subject: project, name: 'bug') + create(:label, subject: project, name: 'critical') + + expect { service.execute }.to change(project.labels, :count).by(6) + end + end + end +end |