Commit aecbe534 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Removes legacy GlobalLabel class

Removes the last usage of GlobalLabel. This changes the dashboard labels
endpoint to get a unique list of label titles using SQL.

GlobalLabel used to be how we implemented group labels where we grouped
project labels by title. For this dashboard endpoint, we're not showing
group labels but we're only returning labels that have unique titles
because these are used by the filter bar and we filter by title so
duplicates are redundant.
parent be95dbcb
...@@ -11,8 +11,8 @@ class Dashboard::LabelsController < Dashboard::ApplicationController ...@@ -11,8 +11,8 @@ class Dashboard::LabelsController < Dashboard::ApplicationController
def labels def labels
finder_params = { project_ids: projects.select(:id) } finder_params = { project_ids: projects.select(:id) }
labels = LabelsFinder.new(current_user, finder_params).execute
GlobalLabel.build_collection(labels) LabelsFinder.new(current_user, finder_params).execute
.select('DISTINCT ON (labels.title) labels.*')
end end
end end
# frozen_string_literal: true
class GlobalLabel
include Presentable
attr_accessor :title, :labels
alias_attribute :name, :title
delegate :color, :text_color, :description, :scoped_label?, to: :@first_label
def for_display
@first_label
end
def self.build_collection(labels)
labels = labels.group_by(&:title)
labels.map do |title, labels|
new(title, labels)
end
end
def initialize(title, labels)
@title = title
@labels = labels
@first_label = labels.find { |lbl| lbl.description.present? } || labels.first
end
def present(attributes)
super(attributes.merge(presenter_class: ::LabelPresenter))
end
end
# frozen_string_literal: true # frozen_string_literal: true
class LabelEntity < Grape::Entity class LabelEntity < Grape::Entity
expose :id, if: ->(label, _) { !label.is_a?(GlobalLabel) } expose :id
expose :title expose :title
expose :color expose :color
expose :description expose :description
expose :group_id expose :group_id
expose :project_id, if: ->(label, _) { !label.is_a?(GlobalLabel) } expose :project_id
expose :template expose :template
expose :text_color expose :text_color
expose :created_at expose :created_at
......
...@@ -3,27 +3,32 @@ ...@@ -3,27 +3,32 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Dashboard::LabelsController do RSpec.describe Dashboard::LabelsController do
let(:project) { create(:project) } let_it_be(:user) { create(:user) }
let(:user) { create(:user) } let_it_be(:project) { create(:project) }
let!(:label) { create(:label, project: project) } let_it_be(:project_2) { create(:project) }
let_it_be(:label) { create(:label, project: project, title: 'some_label') }
let_it_be(:label_with_same_title) { create(:label, project: project_2, title: 'some_label') }
let_it_be(:unrelated_label) { create(:label, project: create(:project, :public)) }
before_all do
project.add_reporter(user)
project_2.add_reporter(user)
end
before do before do
sign_in(user) sign_in(user)
project.add_reporter(user)
end end
describe "#index" do describe "#index" do
let!(:unrelated_label) { create(:label, project: create(:project, :public)) }
subject { get :index, format: :json } subject { get :index, format: :json }
it 'returns global labels for projects the user has a relationship with' do it 'returns labels with unique titles for projects the user has a relationship with' do
subject subject
expect(json_response).to be_kind_of(Array) expect(json_response).to be_kind_of(Array)
expect(json_response.size).to eq(1) expect(json_response.size).to eq(1)
expect(json_response[0]["id"]).to be_nil expect(json_response[0]['title']).to eq(label.title)
expect(json_response[0]["title"]).to eq(label.title)
end end
it_behaves_like 'disabled when using an external authorization service' it_behaves_like 'disabled when using an external authorization service'
......
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