Commit 37496d59 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Merge branch 'georgekoltsov/63408-user-mapping' into 'master'

Add author lines to project import comments

See merge request gitlab-org/gitlab-ce!31579
parents 216acee1 23dfc5d6
---
title: 'Fix missing author line (`Created by: <user>`) in MRs/issues/comments of imported Bitbucket Cloud project'
merge_request: 31579
author:
type: fixed
......@@ -31,12 +31,12 @@ to enable this if not already.
## How it works
When issues/pull requests are being imported, the Bitbucket importer tries to find
the Bitbucket author/assignee in GitLab's database using the Bitbucket ID. For this
to work, the Bitbucket author/assignee should have signed in beforehand in GitLab
and **associated their Bitbucket account**. If the user is not
found in GitLab's database, the project creator (most of the times the current
user that started the import process) is set as the author, but a reference on
the issue about the original Bitbucket author is kept.
the Bitbucket author/assignee in GitLab's database using the Bitbucket `nickname`.
For this to work, the Bitbucket author/assignee should have signed in beforehand in GitLab
and **associated their Bitbucket account**. Their `nickname` must also match their Bitbucket
`username.`. If the user is not found in GitLab's database, the project creator
(most of the times the current user that started the import process) is set as the author,
but a reference on the issue about the original Bitbucket author is kept.
The importer will create any new namespaces (groups) if they don't exist or in
the case the namespace is taken, the repository will be imported under the user's
......
......@@ -4,7 +4,7 @@ module Bitbucket
module Representation
class Comment < Representation::Base
def author
user['username']
user['nickname']
end
def note
......
......@@ -14,7 +14,7 @@ module Bitbucket
end
def author
raw.dig('reporter', 'username')
raw.dig('reporter', 'nickname')
end
def description
......
......@@ -4,7 +4,7 @@ module Bitbucket
module Representation
class PullRequest < Representation::Base
def author
raw.fetch('author', {}).fetch('username', nil)
raw.fetch('author', {}).fetch('nickname', nil)
end
def description
......
......@@ -262,13 +262,19 @@ module Gitlab
def pull_request_comment_attributes(comment)
{
project: project,
note: comment.note,
author_id: gitlab_user_id(project, comment.author),
note: comment_note(comment),
created_at: comment.created_at,
updated_at: comment.updated_at
}
end
def comment_note(comment)
author = @formatter.author_line(comment.author) unless find_user_id(comment.author)
author.to_s + comment.note.to_s
end
def log_error(details)
logger.error(log_base_data.merge(details))
end
......
......@@ -4,7 +4,7 @@ require 'spec_helper'
describe Bitbucket::Representation::Comment do
describe '#author' do
it { expect(described_class.new('user' => { 'username' => 'Ben' }).author).to eq('Ben') }
it { expect(described_class.new('user' => { 'nickname' => 'Ben' }).author).to eq('Ben') }
it { expect(described_class.new({}).author).to be_nil }
end
......
......@@ -17,7 +17,7 @@ describe Bitbucket::Representation::Issue do
end
describe '#author' do
it { expect(described_class.new({ 'reporter' => { 'username' => 'Ben' } }).author).to eq('Ben') }
it { expect(described_class.new({ 'reporter' => { 'nickname' => 'Ben' } }).author).to eq('Ben') }
it { expect(described_class.new({}).author).to be_nil }
end
......
......@@ -8,7 +8,7 @@ describe Bitbucket::Representation::PullRequest do
end
describe '#author' do
it { expect(described_class.new({ 'author' => { 'username' => 'Ben' } }).author).to eq('Ben') }
it { expect(described_class.new({ 'author' => { 'nickname' => 'Ben' } }).author).to eq('Ben') }
it { expect(described_class.new({}).author).to be_nil }
end
......
......@@ -25,12 +25,12 @@ describe Gitlab::BitbucketImport::Importer do
let(:reporters) do
[
nil,
{ "username" => "reporter1" },
{ "nickname" => "reporter1" },
nil,
{ "username" => "reporter2" },
{ "username" => "reporter1" },
{ "nickname" => "reporter2" },
{ "nickname" => "reporter1" },
nil,
{ "username" => "reporter3" }
{ "nickname" => "reporter3" }
]
end
......@@ -115,6 +115,7 @@ describe Gitlab::BitbucketImport::Importer do
created_at: Time.now,
updated_at: Time.now)
end
let(:author_line) { "*Created by: someuser*\n\n" }
before do
allow(subject).to receive(:import_wiki)
......@@ -128,7 +129,7 @@ describe Gitlab::BitbucketImport::Importer do
old_pos: nil,
new_pos: 4,
note: 'Hello world',
author: 'root',
author: 'someuser',
created_at: Time.now,
updated_at: Time.now,
inline?: true,
......@@ -139,7 +140,7 @@ describe Gitlab::BitbucketImport::Importer do
iid: 3,
file_path: '.gitmodules',
note: 'Hello world',
author: 'root',
author: 'someuser',
created_at: Time.now,
updated_at: Time.now,
inline?: true,
......@@ -163,11 +164,33 @@ describe Gitlab::BitbucketImport::Importer do
notes = merge_request.notes.order(:id).to_a
start_note = notes.first
expect(start_note).to be_a(DiffNote)
expect(start_note.note).to eq(@inline_note.note)
expect(start_note.note).to include(@inline_note.note)
expect(start_note.note).to include(author_line)
reply_note = notes.last
expect(reply_note).to be_a(DiffNote)
expect(reply_note.note).to include(@reply.note)
expect(reply_note.note).to include(author_line)
end
context 'when user exists in GitLab' do
let!(:existing_user) { create(:user, username: 'someuser') }
let!(:identity) { create(:identity, provider: 'bitbucket', extern_uid: existing_user.username, user: existing_user) }
it 'does not add author line to comments' do
expect { subject.execute }.to change { MergeRequest.count }.by(1)
merge_request = MergeRequest.first
notes = merge_request.notes.order(:id).to_a
start_note = notes.first
expect(start_note.note).to eq(@inline_note.note)
expect(start_note.note).not_to include(author_line)
reply_note = notes.last
expect(reply_note.note).to eq(@reply.note)
expect(reply_note.note).not_to include(author_line)
end
end
context 'when importing a pull request throws an exception' 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