Allow users to apply group labels on Issues/MRs

parent d5a595b5
class Dashboard::LabelsController < Dashboard::ApplicationController class Dashboard::LabelsController < Dashboard::ApplicationController
def index def index
labels = Label.where(project_id: projects).select(:id, :title, :color).uniq(:title) labels = LabelsFinder.new(current_user, project_id: projects)
.execute
.select(:id, :title, :color)
.uniq(:title)
respond_to do |format| respond_to do |format|
format.json { render json: labels } format.json { render json: labels }
......
...@@ -25,8 +25,7 @@ class Projects::IssuesController < Projects::ApplicationController ...@@ -25,8 +25,7 @@ class Projects::IssuesController < Projects::ApplicationController
def index def index
@issues = issues_collection @issues = issues_collection
@issues = @issues.page(params[:page]) @issues = @issues.page(params[:page])
@labels = LabelsFinder.new(current_user, project_id: @project.id, title: params[:label_name]).execute if params[:label_name].presence
@labels = @project.labels.where(title: params[:label_name])
respond_to do |format| respond_to do |format|
format.html format.html
...@@ -45,11 +44,15 @@ class Projects::IssuesController < Projects::ApplicationController ...@@ -45,11 +44,15 @@ class Projects::IssuesController < Projects::ApplicationController
assignee_id: "" assignee_id: ""
) )
@issue = @noteable = @project.issues.new(issue_params) @issue = @noteable = @project.issues.new(issue_params)
@labels = LabelsFinder.new(current_user, project_id: @project.id).execute
respond_with(@issue) respond_with(@issue)
end end
def edit def edit
@labels = LabelsFinder.new(current_user, project_id: @project.id).execute
respond_with(@issue) respond_with(@issue)
end end
......
...@@ -17,7 +17,7 @@ class Projects::LabelsController < Projects::ApplicationController ...@@ -17,7 +17,7 @@ class Projects::LabelsController < Projects::ApplicationController
respond_to do |format| respond_to do |format|
format.html format.html
format.json do format.json do
render json: @project.labels render json: LabelsFinder.new(current_user, project_id: @project.id).execute
end end
end end
end end
......
...@@ -40,7 +40,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController ...@@ -40,7 +40,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController
@merge_requests = @merge_requests.page(params[:page]) @merge_requests = @merge_requests.page(params[:page])
@merge_requests = @merge_requests.preload(:target_project) @merge_requests = @merge_requests.preload(:target_project)
@labels = @project.labels.where(title: params[:label_name]) @labels = LabelsFinder.new(current_user, project_id: @project.id, title: params[:label_name]).execute if params[:label_name].presence
respond_to do |format| respond_to do |format|
format.html format.html
...@@ -263,6 +263,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController ...@@ -263,6 +263,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController
@source_project = @merge_request.source_project @source_project = @merge_request.source_project
@target_project = @merge_request.target_project @target_project = @merge_request.target_project
@target_branches = @merge_request.target_project.repository.branch_names @target_branches = @merge_request.target_project.repository.branch_names
@labels = LabelsFinder.new(current_user, project_id: @project.id).execute
end end
def update def update
...@@ -575,6 +576,8 @@ class Projects::MergeRequestsController < Projects::ApplicationController ...@@ -575,6 +576,8 @@ class Projects::MergeRequestsController < Projects::ApplicationController
@note_counts = Note.where(commit_id: @commits.map(&:id)). @note_counts = Note.where(commit_id: @commits.map(&:id)).
group(:commit_id).count group(:commit_id).count
@labels = LabelsFinder.new(current_user, project_id: @project.id).execute
define_pipelines_vars define_pipelines_vars
end end
......
...@@ -274,8 +274,10 @@ class IssuableFinder ...@@ -274,8 +274,10 @@ class IssuableFinder
items = items.without_label items = items.without_label
else else
items = items.with_label(label_names, params[:sort]) items = items.with_label(label_names, params[:sort])
if projects if projects
items = items.where(labels: { project_id: projects }) label_ids = LabelsFinder.new(current_user, project_id: projects).execute.select(:id)
items = items.where(labels: { id: label_ids })
end end
end end
end end
......
...@@ -23,7 +23,7 @@ class Label < ActiveRecord::Base ...@@ -23,7 +23,7 @@ class Label < ActiveRecord::Base
has_many :merge_requests, through: :label_links, source: :target, source_type: 'MergeRequest' has_many :merge_requests, through: :label_links, source: :target, source_type: 'MergeRequest'
validates :color, color: true, allow_blank: false validates :color, color: true, allow_blank: false
validates :project, presence: true, unless: Proc.new { |service| service.template? } validates :project, presence: true, if: :project_label?
# Don't allow ',' for label titles # Don't allow ',' for label titles
validates :title, validates :title,
...@@ -127,6 +127,10 @@ class Label < ActiveRecord::Base ...@@ -127,6 +127,10 @@ class Label < ActiveRecord::Base
private private
def project_label?
type.blank? && !template?
end
def label_format_reference(format = :id) def label_format_reference(format = :id)
raise StandardError, 'Unknown format' unless [:id, :name].include?(format) raise StandardError, 'Unknown format' unless [:id, :name].include?(format)
......
...@@ -80,17 +80,18 @@ class IssuableBaseService < BaseService ...@@ -80,17 +80,18 @@ class IssuableBaseService < BaseService
def filter_labels_in_param(key) def filter_labels_in_param(key)
return if params[key].to_a.empty? return if params[key].to_a.empty?
params[key] = project.labels.where(id: params[key]).pluck(:id) params[key] = available_labels.where(id: params[key]).pluck(:id)
end end
def find_or_create_label_ids def find_or_create_label_ids
labels = params.delete(:labels) labels = params.delete(:labels)
return unless labels return unless labels
params[:label_ids] = labels.split(",").map do |label_name| params[:label_ids] = labels.split(',').map do |label_name|
project.labels.create_with(color: Label::DEFAULT_COLOR) label = available_labels.find_by(title: title).select(:id)
.find_or_create_by(title: label_name.strip) label ||= project.labels.create(title: label_name.strip, color: Label::DEFAULT_COLOR)
.id
label.id
end end
end end
...@@ -111,6 +112,10 @@ class IssuableBaseService < BaseService ...@@ -111,6 +112,10 @@ class IssuableBaseService < BaseService
new_label_ids new_label_ids
end end
def available_labels
LabelsFinder.new(current_user, project_id: @project.id).execute
end
def merge_slash_commands_into_params!(issuable) def merge_slash_commands_into_params!(issuable)
description, command_params = description, command_params =
SlashCommands::InterpretService.new(project, current_user). SlashCommands::InterpretService.new(project, current_user).
......
...@@ -13,7 +13,7 @@ module Projects ...@@ -13,7 +13,7 @@ module Projects
end end
def labels def labels
@project.labels.select([:title, :color]) LabelsFinder.new(current_user, project_id: project.id).execute.select([:title, :color])
end end
def commands(noteable, type) def commands(noteable, type)
......
...@@ -77,11 +77,10 @@ ...@@ -77,11 +77,10 @@
= hidden_field_tag :state_event, params[:state_event] = hidden_field_tag :state_event, params[:state_event]
.filter-item.inline .filter-item.inline
= button_tag "Update #{type.to_s.humanize(capitalize: false)}", class: "btn update_selected_issues btn-save" = button_tag "Update #{type.to_s.humanize(capitalize: false)}", class: "btn update_selected_issues btn-save"
- has_labels = @labels && @labels.any?
- if !@labels.nil? .row-content-block.second-block.filtered-labels{ class: ("hidden" unless has_labels) }
.row-content-block.second-block.filtered-labels{ class: ("hidden" if !@labels.any?) } - if has_labels
- if @labels.any? = render 'shared/labels_row', labels: @labels
= render "shared/labels_row", labels: @labels
:javascript :javascript
new UsersSelect(); new UsersSelect();
......
...@@ -95,7 +95,7 @@ ...@@ -95,7 +95,7 @@
.issuable-form-select-holder .issuable-form-select-holder
= render "shared/issuable/milestone_dropdown", selected: issuable.milestone, name: "#{issuable.class.model_name.param_key}[milestone_id]", show_any: false, show_menu_above: true, show_upcoming: false, extra_class: "js-issuable-form-dropdown js-dropdown-keep-input", dropdown_title: "Select milestone" = render "shared/issuable/milestone_dropdown", selected: issuable.milestone, name: "#{issuable.class.model_name.param_key}[milestone_id]", show_any: false, show_menu_above: true, show_upcoming: false, extra_class: "js-issuable-form-dropdown js-dropdown-keep-input", dropdown_title: "Select milestone"
.form-group .form-group
- has_labels = issuable.project.labels.any? - has_labels = @labels && @labels.any?
= f.label :label_ids, "Labels", class: "control-label #{"col-lg-4" if has_due_date}" = f.label :label_ids, "Labels", class: "control-label #{"col-lg-4" if has_due_date}"
= f.hidden_field :label_ids, multiple: true, value: '' = f.hidden_field :label_ids, multiple: true, value: ''
.col-sm-10{ class: "#{"col-lg-8" if has_due_date} #{'issuable-form-padding-top' if !has_labels}" } .col-sm-10{ class: "#{"col-lg-8" if has_due_date} #{'issuable-form-padding-top' if !has_labels}" }
......
...@@ -67,6 +67,27 @@ describe Issues::CreateService, services: true do ...@@ -67,6 +67,27 @@ describe Issues::CreateService, services: true do
expect(Todo.where(attributes).count).to eq 1 expect(Todo.where(attributes).count).to eq 1
end end
context 'when label belongs to project group' do
let(:group) { create(:group) }
let(:group_labels) { create_pair(:group_label, group: group) }
let(:opts) do
{
title: 'Title',
description: 'Description',
label_ids: group_labels.map(&:id)
}
end
before do
project.update(group: group)
end
it 'assigns group labels' do
expect(issue.labels).to match_array group_labels
end
end
context 'when label belongs to different project' do context 'when label belongs to different project' do
let(:label) { create(:label) } let(:label) { create(:label) }
......
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