Allow users to apply group labels on Issues/MRs

This commit is contained in:
Douglas Barbosa Alexandre 2016-09-19 17:21:39 -03:00
parent d5a595b597
commit 398ab263fd
11 changed files with 60 additions and 20 deletions

View file

@ -1,6 +1,9 @@
class Dashboard::LabelsController < Dashboard::ApplicationController
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|
format.json { render json: labels }

View file

@ -25,8 +25,7 @@ class Projects::IssuesController < Projects::ApplicationController
def index
@issues = issues_collection
@issues = @issues.page(params[:page])
@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|
format.html
@ -45,11 +44,15 @@ class Projects::IssuesController < Projects::ApplicationController
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)
end
def edit
@labels = LabelsFinder.new(current_user, project_id: @project.id).execute
respond_with(@issue)
end

View file

@ -17,7 +17,7 @@ class Projects::LabelsController < Projects::ApplicationController
respond_to do |format|
format.html
format.json do
render json: @project.labels
render json: LabelsFinder.new(current_user, project_id: @project.id).execute
end
end
end

View file

@ -40,7 +40,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController
@merge_requests = @merge_requests.page(params[:page])
@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|
format.html
@ -263,6 +263,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController
@source_project = @merge_request.source_project
@target_project = @merge_request.target_project
@target_branches = @merge_request.target_project.repository.branch_names
@labels = LabelsFinder.new(current_user, project_id: @project.id).execute
end
def update
@ -575,6 +576,8 @@ class Projects::MergeRequestsController < Projects::ApplicationController
@note_counts = Note.where(commit_id: @commits.map(&:id)).
group(:commit_id).count
@labels = LabelsFinder.new(current_user, project_id: @project.id).execute
define_pipelines_vars
end

View file

@ -274,8 +274,10 @@ class IssuableFinder
items = items.without_label
else
items = items.with_label(label_names, params[:sort])
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

View file

@ -23,7 +23,7 @@ class Label < ActiveRecord::Base
has_many :merge_requests, through: :label_links, source: :target, source_type: 'MergeRequest'
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
validates :title,
@ -127,6 +127,10 @@ class Label < ActiveRecord::Base
private
def project_label?
type.blank? && !template?
end
def label_format_reference(format = :id)
raise StandardError, 'Unknown format' unless [:id, :name].include?(format)

View file

@ -80,17 +80,18 @@ class IssuableBaseService < BaseService
def filter_labels_in_param(key)
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
def find_or_create_label_ids
labels = params.delete(:labels)
return unless labels
params[:label_ids] = labels.split(",").map do |label_name|
project.labels.create_with(color: Label::DEFAULT_COLOR)
.find_or_create_by(title: label_name.strip)
.id
params[:label_ids] = labels.split(',').map do |label_name|
label = available_labels.find_by(title: title).select(:id)
label ||= project.labels.create(title: label_name.strip, color: Label::DEFAULT_COLOR)
label.id
end
end
@ -111,6 +112,10 @@ class IssuableBaseService < BaseService
new_label_ids
end
def available_labels
LabelsFinder.new(current_user, project_id: @project.id).execute
end
def merge_slash_commands_into_params!(issuable)
description, command_params =
SlashCommands::InterpretService.new(project, current_user).

View file

@ -13,7 +13,7 @@ module Projects
end
def labels
@project.labels.select([:title, :color])
LabelsFinder.new(current_user, project_id: project.id).execute.select([:title, :color])
end
def commands(noteable, type)

View file

@ -77,11 +77,10 @@
= hidden_field_tag :state_event, params[:state_event]
.filter-item.inline
= button_tag "Update #{type.to_s.humanize(capitalize: false)}", class: "btn update_selected_issues btn-save"
- if !@labels.nil?
.row-content-block.second-block.filtered-labels{ class: ("hidden" if !@labels.any?) }
- if @labels.any?
= render "shared/labels_row", labels: @labels
- has_labels = @labels && @labels.any?
.row-content-block.second-block.filtered-labels{ class: ("hidden" unless has_labels) }
- if has_labels
= render 'shared/labels_row', labels: @labels
:javascript
new UsersSelect();

View file

@ -95,7 +95,7 @@
.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"
.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.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}" }

View file

@ -67,6 +67,27 @@ describe Issues::CreateService, services: true do
expect(Todo.where(attributes).count).to eq 1
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
let(:label) { create(:label) }