Avoid conflict with admin labels when importing GitHub labels

parent 4bdcbc85
...@@ -133,8 +133,7 @@ module Gitlab ...@@ -133,8 +133,7 @@ module Gitlab
if issue.labels.count > 0 if issue.labels.count > 0
label_ids = issue.labels label_ids = issue.labels
.map { |raw| LabelFormatter.new(project, raw).attributes } .map { |attrs| project.labels.find_by(title: attrs.name).try(:id) }
.map { |attrs| Label.find_by(attrs).try(:id) }
.compact .compact
issuable.update_attribute(:label_ids, label_ids) issuable.update_attribute(:label_ids, label_ids)
......
...@@ -13,6 +13,12 @@ module Gitlab ...@@ -13,6 +13,12 @@ module Gitlab
Label Label
end end
def create!
project.labels.find_or_create_by!(title: title) do |label|
label.color = color
end
end
private private
def color def color
......
...@@ -13,7 +13,7 @@ describe Gitlab::GithubImport::Importer, lib: true do ...@@ -13,7 +13,7 @@ describe Gitlab::GithubImport::Importer, lib: true do
let(:target_sha) { create(:commit, project: project, git_commit: RepoHelpers.another_sample_commit).id } let(:target_sha) { create(:commit, project: project, git_commit: RepoHelpers.another_sample_commit).id }
let(:target_branch) { double(ref: 'master', repo: repository, sha: target_sha) } let(:target_branch) { double(ref: 'master', repo: repository, sha: target_sha) }
let(:label) do let(:label1) do
double( double(
name: 'Bug', name: 'Bug',
color: 'ff0000', color: 'ff0000',
...@@ -21,6 +21,14 @@ describe Gitlab::GithubImport::Importer, lib: true do ...@@ -21,6 +21,14 @@ describe Gitlab::GithubImport::Importer, lib: true do
) )
end end
let(:label2) do
double(
name: nil,
color: 'ff0000',
url: 'https://api.github.com/repos/octocat/Hello-World/labels/bug'
)
end
let(:milestone) do let(:milestone) do
double( double(
number: 1347, number: 1347,
...@@ -93,7 +101,7 @@ describe Gitlab::GithubImport::Importer, lib: true do ...@@ -93,7 +101,7 @@ describe Gitlab::GithubImport::Importer, lib: true do
before do before do
allow(project).to receive(:import_data).and_return(double.as_null_object) allow(project).to receive(:import_data).and_return(double.as_null_object)
allow_any_instance_of(Octokit::Client).to receive(:rate_limit!).and_raise(Octokit::NotFound) allow_any_instance_of(Octokit::Client).to receive(:rate_limit!).and_raise(Octokit::NotFound)
allow_any_instance_of(Octokit::Client).to receive(:labels).and_return([label, label]) allow_any_instance_of(Octokit::Client).to receive(:labels).and_return([label1, label2])
allow_any_instance_of(Octokit::Client).to receive(:milestones).and_return([milestone, milestone]) allow_any_instance_of(Octokit::Client).to receive(:milestones).and_return([milestone, milestone])
allow_any_instance_of(Octokit::Client).to receive(:issues).and_return([issue1, issue2]) allow_any_instance_of(Octokit::Client).to receive(:issues).and_return([issue1, issue2])
allow_any_instance_of(Octokit::Client).to receive(:pull_requests).and_return([pull_request, pull_request]) allow_any_instance_of(Octokit::Client).to receive(:pull_requests).and_return([pull_request, pull_request])
...@@ -113,7 +121,7 @@ describe Gitlab::GithubImport::Importer, lib: true do ...@@ -113,7 +121,7 @@ describe Gitlab::GithubImport::Importer, lib: true do
error = { error = {
message: 'The remote data could not be fully imported.', message: 'The remote data could not be fully imported.',
errors: [ errors: [
{ type: :label, url: "https://api.github.com/repos/octocat/Hello-World/labels/bug", errors: "Validation failed: Title has already been taken" }, { type: :label, url: "https://api.github.com/repos/octocat/Hello-World/labels/bug", errors: "Validation failed: Title can't be blank, Title is invalid" },
{ type: :milestone, url: "https://api.github.com/repos/octocat/Hello-World/milestones/1", errors: "Validation failed: Title has already been taken" }, { type: :milestone, url: "https://api.github.com/repos/octocat/Hello-World/milestones/1", errors: "Validation failed: Title has already been taken" },
{ type: :issue, url: "https://api.github.com/repos/octocat/Hello-World/issues/1347", errors: "Invalid Repository. Use user/repo format." }, { type: :issue, url: "https://api.github.com/repos/octocat/Hello-World/issues/1347", errors: "Invalid Repository. Use user/repo format." },
{ type: :issue, url: "https://api.github.com/repos/octocat/Hello-World/issues/1348", errors: "Validation failed: Title can't be blank, Title is too short (minimum is 0 characters)" }, { type: :issue, url: "https://api.github.com/repos/octocat/Hello-World/issues/1348", errors: "Validation failed: Title can't be blank, Title is too short (minimum is 0 characters)" },
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::GithubImport::LabelFormatter, lib: true do describe Gitlab::GithubImport::LabelFormatter, lib: true do
describe '#attributes' do let(:project) { create(:project) }
it 'returns formatted attributes' do let(:raw) { double(name: 'improvements', color: 'e6e6e6') }
project = create(:project)
raw = double(name: 'improvements', color: 'e6e6e6')
formatter = described_class.new(project, raw) subject { described_class.new(project, raw) }
expect(formatter.attributes).to eq({ describe '#attributes' do
it 'returns formatted attributes' do
expect(subject.attributes).to eq({
project: project, project: project,
title: 'improvements', title: 'improvements',
color: '#e6e6e6' color: '#e6e6e6'
}) })
end end
end end
describe '#create!' do
context 'when label does not exist' do
it 'creates a new label' do
expect { subject.create! }.to change(Label, :count).by(1)
end
end
context 'when label exists' do
it 'does not create a new label' do
project.labels.create(name: raw.name)
expect { subject.create! }.not_to change(Label, :count)
end
end
end
end end
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