Commit d9d8d3b7 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Fix old api compatibility and tests

Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent 70f868b7
...@@ -5,13 +5,7 @@ module LabelsHelper ...@@ -5,13 +5,7 @@ module LabelsHelper
def render_colored_label(label) def render_colored_label(label)
label_color = label.color || "#428bca" label_color = label.color || "#428bca"
r, g, b = label_color.slice(1,7).scan(/.{2}/).map(&:hex) text_color = text_color_for_bg(label_color)
if (r + g + b) > 500
text_color = "#333"
else
text_color = "#FFF"
end
content_tag :span, class: 'label color-label', style: "background:#{label_color};color:#{text_color}" do content_tag :span, class: 'label color-label', style: "background:#{label_color};color:#{text_color}" do
label.name label.name
...@@ -30,4 +24,14 @@ module LabelsHelper ...@@ -30,4 +24,14 @@ module LabelsHelper
'#FFECDB' '#FFECDB'
] ]
end 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 end
...@@ -133,4 +133,15 @@ module Issuable ...@@ -133,4 +133,15 @@ module Issuable
object_attributes: self.attributes object_attributes: self.attributes
} }
end 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 end
...@@ -126,7 +126,7 @@ module API ...@@ -126,7 +126,7 @@ module API
end end
class Issue < ProjectEntity class Issue < ProjectEntity
expose :label_list, as: :labels expose :label_names, as: :labels
expose :milestone, using: Entities::Milestone expose :milestone, using: Entities::Milestone
expose :assignee, :author, using: Entities::UserBasic expose :assignee, :author, using: Entities::UserBasic
end end
...@@ -135,7 +135,7 @@ module API ...@@ -135,7 +135,7 @@ module API
expose :target_branch, :source_branch, :upvotes, :downvotes expose :target_branch, :source_branch, :upvotes, :downvotes
expose :author, :assignee, using: Entities::UserBasic expose :author, :assignee, using: Entities::UserBasic
expose :source_project_id, :target_project_id expose :source_project_id, :target_project_id
expose :label_list, as: :labels expose :label_names, as: :labels
end end
class SSHKey < Grape::Entity class SSHKey < Grape::Entity
......
...@@ -50,10 +50,15 @@ module API ...@@ -50,10 +50,15 @@ module API
post ":id/issues" do post ":id/issues" do
required_attributes! [:title] required_attributes! [:title]
attrs = attributes_for_keys [:title, :description, :assignee_id, :milestone_id] 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 issue = ::Issues::CreateService.new(user_project, current_user, attrs).execute
if issue.valid? 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 present issue, with: Entities::Issue
else else
not_found! not_found!
...@@ -76,13 +81,16 @@ module API ...@@ -76,13 +81,16 @@ module API
put ":id/issues/:issue_id" do put ":id/issues/:issue_id" do
issue = user_project.issues.find(params[:issue_id]) issue = user_project.issues.find(params[:issue_id])
authorize! :modify_issue, issue authorize! :modify_issue, issue
attrs = attributes_for_keys [:title, :description, :assignee_id, :milestone_id, :state_event] 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) issue = ::Issues::UpdateService.new(user_project, current_user, attrs).execute(issue)
if issue.valid? 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 present issue, with: Entities::Issue
else else
not_found! not_found!
......
...@@ -76,10 +76,14 @@ module API ...@@ -76,10 +76,14 @@ module API
authorize! :write_merge_request, user_project authorize! :write_merge_request, user_project
required_attributes! [:source_branch, :target_branch, :title] required_attributes! [:source_branch, :target_branch, :title]
attrs = attributes_for_keys [:source_branch, :target_branch, :assignee_id, :title, :target_project_id, :description] 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 merge_request = ::MergeRequests::CreateService.new(user_project, current_user, attrs).execute
if merge_request.valid? 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 present merge_request, with: Entities::MergeRequest
else else
handle_merge_request_errors! merge_request.errors handle_merge_request_errors! merge_request.errors
...@@ -103,12 +107,16 @@ module API ...@@ -103,12 +107,16 @@ module API
# #
put ":id/merge_request/:merge_request_id" do put ":id/merge_request/:merge_request_id" do
attrs = attributes_for_keys [:source_branch, :target_branch, :assignee_id, :title, :state_event, :description] 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]) merge_request = user_project.merge_requests.find(params[:merge_request_id])
authorize! :modify_merge_request, merge_request authorize! :modify_merge_request, merge_request
merge_request = ::MergeRequests::UpdateService.new(user_project, current_user, attrs).execute(merge_request) merge_request = ::MergeRequests::UpdateService.new(user_project, current_user, attrs).execute(merge_request)
if merge_request.valid? 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 present merge_request, with: Entities::MergeRequest
else else
handle_merge_request_errors! merge_request.errors handle_merge_request_errors! merge_request.errors
......
require 'spec_helper' require 'spec_helper'
describe LabelsHelper do describe LabelsHelper do
describe '#label_css_class' do it { expect(text_color_for_bg('#EEEEEE')).to eq('#333') }
it 'returns label-danger when given Bug as param' do it { expect(text_color_for_bg('#222E2E')).to eq('#FFF') }
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
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment