Commit 357dba32 authored by Sean McGivern's avatar Sean McGivern

Merge branch 'issue_55744' into 'master'

Fix template labels

Closes #55744

See merge request gitlab-org/gitlab-ce!24803
parents fa3a2f9e d3f5fd33
...@@ -1068,7 +1068,7 @@ class Project < ActiveRecord::Base ...@@ -1068,7 +1068,7 @@ class Project < ActiveRecord::Base
# rubocop: disable CodeReuse/ServiceClass # rubocop: disable CodeReuse/ServiceClass
def create_labels def create_labels
Label.templates.each do |label| 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) Labels::FindOrCreateService.new(nil, self, params).execute(skip_authorization: true)
end end
end end
......
---
title: Fix template labels not being created on new projects
merge_request: 24803
author:
type: fixed
# 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
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # 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 # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
......
# 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
...@@ -16,7 +16,11 @@ describe Projects::CreateService, '#execute' do ...@@ -16,7 +16,11 @@ describe Projects::CreateService, '#execute' do
Label.create(title: "bug", template: true) Label.create(title: "bug", template: true)
project = create_project(user, opts) 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 end
context 'user namespace' do context 'user namespace' do
......
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