From d9d8d3b7995c351cb4a7d91718f3f48324bd099a Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Wed, 30 Jul 2014 17:17:29 +0300 Subject: [PATCH] Fix old api compatibility and tests Signed-off-by: Dmitriy Zaporozhets --- app/helpers/labels_helper.rb | 18 +++++++++++------- app/models/concerns/issuable.rb | 11 +++++++++++ lib/api/entities.rb | 4 ++-- lib/api/issues.rb | 14 +++++++++++--- lib/api/merge_requests.rb | 12 ++++++++++-- spec/helpers/labels_helper_spec.rb | 11 ++--------- 6 files changed, 47 insertions(+), 23 deletions(-) diff --git a/app/helpers/labels_helper.rb b/app/helpers/labels_helper.rb index 17c87f5c762..37f3832e54f 100644 --- a/app/helpers/labels_helper.rb +++ b/app/helpers/labels_helper.rb @@ -5,13 +5,7 @@ module LabelsHelper def render_colored_label(label) label_color = label.color || "#428bca" - r, g, b = label_color.slice(1,7).scan(/.{2}/).map(&:hex) - - if (r + g + b) > 500 - text_color = "#333" - else - text_color = "#FFF" - end + text_color = text_color_for_bg(label_color) content_tag :span, class: 'label color-label', style: "background:#{label_color};color:#{text_color}" do label.name @@ -30,4 +24,14 @@ module LabelsHelper '#FFECDB' ] end + + def text_color_for_bg(bg_color) + r, g, b = bg_color.slice(1,7).scan(/.{2}/).map(&:hex) + + if (r + g + b) > 500 + "#333" + else + "#FFF" + end + end end diff --git a/app/models/concerns/issuable.rb b/app/models/concerns/issuable.rb index f260198547e..517e4548624 100644 --- a/app/models/concerns/issuable.rb +++ b/app/models/concerns/issuable.rb @@ -133,4 +133,15 @@ module Issuable object_attributes: self.attributes } end + + def label_names + labels.order('title ASC').pluck(:title) + end + + def add_labels_by_names(label_names) + label_names.each do |label_name| + label = project.labels.find_or_create_by(title: label_name.strip) + self.labels << label + end + end end diff --git a/lib/api/entities.rb b/lib/api/entities.rb index 238416c5379..42715d2be3b 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -126,7 +126,7 @@ module API end class Issue < ProjectEntity - expose :label_list, as: :labels + expose :label_names, as: :labels expose :milestone, using: Entities::Milestone expose :assignee, :author, using: Entities::UserBasic end @@ -135,7 +135,7 @@ module API expose :target_branch, :source_branch, :upvotes, :downvotes expose :author, :assignee, using: Entities::UserBasic expose :source_project_id, :target_project_id - expose :label_list, as: :labels + expose :label_names, as: :labels end class SSHKey < Grape::Entity diff --git a/lib/api/issues.rb b/lib/api/issues.rb index f50be3a815d..b29118b2fd8 100644 --- a/lib/api/issues.rb +++ b/lib/api/issues.rb @@ -50,10 +50,15 @@ module API post ":id/issues" do required_attributes! [:title] attrs = attributes_for_keys [:title, :description, :assignee_id, :milestone_id] - attrs[:label_list] = params[:labels] if params[:labels].present? + issue = ::Issues::CreateService.new(user_project, current_user, attrs).execute if issue.valid? + # Find or create labels and attach to issue + if params[:labels].present? + issue.add_labels_by_names(params[:labels].split(",")) + end + present issue, with: Entities::Issue else not_found! @@ -76,13 +81,16 @@ module API put ":id/issues/:issue_id" do issue = user_project.issues.find(params[:issue_id]) authorize! :modify_issue, issue - attrs = attributes_for_keys [:title, :description, :assignee_id, :milestone_id, :state_event] - attrs[:label_list] = params[:labels] if params[:labels].present? issue = ::Issues::UpdateService.new(user_project, current_user, attrs).execute(issue) if issue.valid? + # Find or create labels and attach to issue + if params[:labels].present? + issue.add_labels_by_names(params[:labels].split(",")) + end + present issue, with: Entities::Issue else not_found! diff --git a/lib/api/merge_requests.rb b/lib/api/merge_requests.rb index fc1f1254a9e..acca7cb6bad 100644 --- a/lib/api/merge_requests.rb +++ b/lib/api/merge_requests.rb @@ -76,10 +76,14 @@ module API authorize! :write_merge_request, user_project required_attributes! [:source_branch, :target_branch, :title] attrs = attributes_for_keys [:source_branch, :target_branch, :assignee_id, :title, :target_project_id, :description] - attrs[:label_list] = params[:labels] if params[:labels].present? merge_request = ::MergeRequests::CreateService.new(user_project, current_user, attrs).execute if merge_request.valid? + # Find or create labels and attach to issue + if params[:labels].present? + merge_request.add_labels_by_names(params[:labels].split(",")) + end + present merge_request, with: Entities::MergeRequest else handle_merge_request_errors! merge_request.errors @@ -103,12 +107,16 @@ module API # put ":id/merge_request/:merge_request_id" do attrs = attributes_for_keys [:source_branch, :target_branch, :assignee_id, :title, :state_event, :description] - attrs[:label_list] = params[:labels] if params[:labels].present? merge_request = user_project.merge_requests.find(params[:merge_request_id]) authorize! :modify_merge_request, merge_request merge_request = ::MergeRequests::UpdateService.new(user_project, current_user, attrs).execute(merge_request) if merge_request.valid? + # Find or create labels and attach to issue + if params[:labels].present? + merge_request.add_labels_by_names(params[:labels].split(",")) + end + present merge_request, with: Entities::MergeRequest else handle_merge_request_errors! merge_request.errors diff --git a/spec/helpers/labels_helper_spec.rb b/spec/helpers/labels_helper_spec.rb index f66a5cc9f5c..1e64a201942 100644 --- a/spec/helpers/labels_helper_spec.rb +++ b/spec/helpers/labels_helper_spec.rb @@ -1,13 +1,6 @@ require 'spec_helper' describe LabelsHelper do - describe '#label_css_class' do - it 'returns label-danger when given Bug as param' do - expect(label_css_class('bug')).to eq('label-danger') - end - - it 'returns label-danger when given Bug as param' do - expect(label_css_class('Bug')).to eq('label-danger') - end - end + it { expect(text_color_for_bg('#EEEEEE')).to eq('#333') } + it { expect(text_color_for_bg('#222E2E')).to eq('#FFF') } end