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
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
......@@ -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
......@@ -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
......
......@@ -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!
......
......@@ -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
......
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
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