Fix template labels

This commit is contained in:
Felipe Artur 2019-02-04 10:52:19 +00:00 committed by Sean McGivern
parent fa3a2f9e98
commit d3f5fd33cf
6 changed files with 71 additions and 3 deletions

View file

@ -1068,7 +1068,7 @@ class Project < ActiveRecord::Base
# rubocop: disable CodeReuse/ServiceClass
def create_labels
Label.templates.each do |label|
params = label.attributes.except('id', 'template', 'created_at', 'updated_at')
params = label.attributes.except('id', 'template', 'created_at', 'updated_at', 'type')
Labels::FindOrCreateService.new(nil, self, params).execute(skip_authorization: true)
end
end

View file

@ -0,0 +1,5 @@
---
title: Fix template labels not being created on new projects
merge_request: 24803
author:
type: fixed

View file

@ -0,0 +1,23 @@
# frozen_string_literal: true
class FixNullTypeLabels < ActiveRecord::Migration[5.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
update_column_in_batches(:labels, :type, 'ProjectLabel') do |table, query|
query.where(
table[:project_id].not_eq(nil)
.and(table[:template].eq(false))
.and(table[:type].eq(nil))
)
end
end
def down
# no action
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20190124200344) do
ActiveRecord::Schema.define(version: 20190131122559) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

View file

@ -0,0 +1,36 @@
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20190131122559_fix_null_type_labels')
describe FixNullTypeLabels, :migration do
let(:migration) { described_class.new }
let(:projects) { table(:projects) }
let(:namespaces) { table(:namespaces) }
let(:labels) { table(:labels) }
before do
group = namespaces.create(name: 'labels-test-project', path: 'labels-test-project', type: 'Group')
project = projects.create!(namespace_id: group.id, name: 'labels-test-group', path: 'labels-test-group')
@template_label = labels.create(title: 'template', template: true)
@project_label = labels.create(title: 'project label', project_id: project.id, type: 'ProjectLabel')
@group_label = labels.create(title: 'group_label', group_id: group.id, type: 'GroupLabel')
@broken_label_1 = labels.create(title: 'broken 1', project_id: project.id)
@broken_label_2 = labels.create(title: 'broken 2', project_id: project.id)
end
describe '#up' do
it 'fix labels with type missing' do
migration.up
# Labels that requires type change
expect(@broken_label_1.reload.type).to eq('ProjectLabel')
expect(@broken_label_2.reload.type).to eq('ProjectLabel')
# Labels out of scope
expect(@template_label.reload.type).to be_nil
expect(@project_label.reload.type).to eq('ProjectLabel')
expect(@group_label.reload.type).to eq('GroupLabel')
end
end
end

View file

@ -16,7 +16,11 @@ describe Projects::CreateService, '#execute' do
Label.create(title: "bug", template: true)
project = create_project(user, opts)
expect(project.labels).not_to be_empty
created_label = project.reload.labels.last
expect(created_label.type).to eq('ProjectLabel')
expect(created_label.project_id).to eq(project.id)
expect(created_label.title).to eq('bug')
end
context 'user namespace' do