Commit c73db5e2 authored by Gabriel Mazetto's avatar Gabriel Mazetto

Merge branch 'kassio/github-importer-more-details-to-logs' into 'master'

Github Importer: Add more details to logs

See merge request gitlab-org/gitlab!61188
parents 874ce133 c4184d1f
...@@ -27,15 +27,19 @@ module Gitlab ...@@ -27,15 +27,19 @@ module Gitlab
# client - An instance of `Gitlab::GithubImport::Client` # client - An instance of `Gitlab::GithubImport::Client`
# hash - A Hash containing the details of the object to import. # hash - A Hash containing the details of the object to import.
def import(project, client, hash) def import(project, client, hash)
object = representation_class.from_json_hash(hash)
# To better express in the logs what object is being imported.
self.github_id = object.attributes.fetch(:github_id)
info(project.id, message: 'starting importer') info(project.id, message: 'starting importer')
object = representation_class.from_json_hash(hash)
importer_class.new(object, project, client).execute importer_class.new(object, project, client).execute
counter.increment counter.increment
info(project.id, message: 'importer finished') info(project.id, message: 'importer finished')
rescue StandardError => e rescue StandardError => e
error(project.id, e) error(project.id, e, hash)
end end
def counter def counter
...@@ -65,16 +69,19 @@ module Gitlab ...@@ -65,16 +69,19 @@ module Gitlab
private private
attr_accessor :github_id
def info(project_id, extra = {}) def info(project_id, extra = {})
logger.info(log_attributes(project_id, extra)) logger.info(log_attributes(project_id, extra))
end end
def error(project_id, exception) def error(project_id, exception, data = {})
logger.error( logger.error(
log_attributes( log_attributes(
project_id, project_id,
message: 'importer failed', message: 'importer failed',
'error.message': exception.message 'error.message': exception.message,
'github.data': data
) )
) )
...@@ -88,7 +95,8 @@ module Gitlab ...@@ -88,7 +95,8 @@ module Gitlab
extra.merge( extra.merge(
import_source: :github, import_source: :github,
project_id: project_id, project_id: project_id,
importer: importer_class.name importer: importer_class.name,
github_id: github_id
) )
end end
end end
......
...@@ -25,6 +25,7 @@ module Gitlab ...@@ -25,6 +25,7 @@ module Gitlab
hash = { hash = {
iid: issue.number, iid: issue.number,
github_id: issue.number,
title: issue.title, title: issue.title,
description: issue.body, description: issue.body,
milestone_number: issue.milestone&.number, milestone_number: issue.milestone&.number,
......
...@@ -13,7 +13,12 @@ module Gitlab ...@@ -13,7 +13,12 @@ module Gitlab
# Builds a lfs_object # Builds a lfs_object
def self.from_api_response(lfs_object) def self.from_api_response(lfs_object)
new({ oid: lfs_object.oid, link: lfs_object.link, size: lfs_object.size }) new(
oid: lfs_object.oid,
link: lfs_object.link,
size: lfs_object.size,
github_id: lfs_object.oid
)
end end
# Builds a new lfs_object using a Hash that was built from a JSON payload. # Builds a new lfs_object using a Hash that was built from a JSON payload.
......
...@@ -25,6 +25,7 @@ module Gitlab ...@@ -25,6 +25,7 @@ module Gitlab
hash = { hash = {
iid: pr.number, iid: pr.number,
github_id: pr.number,
title: pr.title, title: pr.title,
description: pr.body, description: pr.body,
source_branch: pr.head.ref, source_branch: pr.head.ref,
......
...@@ -15,7 +15,11 @@ module Gitlab ...@@ -15,7 +15,11 @@ module Gitlab
# #
# user - An instance of `Sawyer::Resource` containing the user details. # user - An instance of `Sawyer::Resource` containing the user details.
def self.from_api_response(user) def self.from_api_response(user)
new(id: user.id, login: user.login) new(
id: user.id,
github_id: user.id,
login: user.login
)
end end
# Builds a user using a Hash that was built from a JSON payload. # Builds a user using a Hash that was built from a JSON payload.
......
...@@ -18,39 +18,49 @@ RSpec.describe Gitlab::GithubImport::ObjectImporter do ...@@ -18,39 +18,49 @@ RSpec.describe Gitlab::GithubImport::ObjectImporter do
def counter_description def counter_description
'This is a counter' 'This is a counter'
end end
def representation_class
MockRepresantation
end
end.new end.new
end end
before do
stub_const('MockRepresantation', Class.new do
include Gitlab::GithubImport::Representation::ToHash
include Gitlab::GithubImport::Representation::ExposeAttribute
def self.from_json_hash(raw_hash)
new(Gitlab::GithubImport::Representation.symbolize_hash(raw_hash))
end
attr_reader :attributes
def initialize(attributes)
@attributes = attributes
end
end)
end
describe '#import' do describe '#import' do
let(:representation_class) { double(:representation_class) }
let(:importer_class) { double(:importer_class, name: 'klass_name') } let(:importer_class) { double(:importer_class, name: 'klass_name') }
let(:importer_instance) { double(:importer_instance) } let(:importer_instance) { double(:importer_instance) }
let(:representation) { double(:representation) }
let(:project) { double(:project, full_path: 'foo/bar', id: 1) } let(:project) { double(:project, full_path: 'foo/bar', id: 1) }
let(:client) { double(:client) } let(:client) { double(:client) }
before do before do
expect(worker)
.to receive(:representation_class)
.and_return(representation_class)
expect(worker) expect(worker)
.to receive(:importer_class) .to receive(:importer_class)
.at_least(:once) .at_least(:once)
.and_return(importer_class) .and_return(importer_class)
end
expect(representation_class) it 'imports the object' do
.to receive(:from_json_hash)
.with(an_instance_of(Hash))
.and_return(representation)
expect(importer_class) expect(importer_class)
.to receive(:new) .to receive(:new)
.with(representation, project, client) .with(instance_of(MockRepresantation), project, client)
.and_return(importer_instance) .and_return(importer_instance)
end
it 'imports the object' do
expect(importer_instance) expect(importer_instance)
.to receive(:execute) .to receive(:execute)
...@@ -62,6 +72,7 @@ RSpec.describe Gitlab::GithubImport::ObjectImporter do ...@@ -62,6 +72,7 @@ RSpec.describe Gitlab::GithubImport::ObjectImporter do
expect(logger) expect(logger)
.to receive(:info) .to receive(:info)
.with( .with(
github_id: 1,
message: 'starting importer', message: 'starting importer',
import_source: :github, import_source: :github,
project_id: 1, project_id: 1,
...@@ -70,6 +81,7 @@ RSpec.describe Gitlab::GithubImport::ObjectImporter do ...@@ -70,6 +81,7 @@ RSpec.describe Gitlab::GithubImport::ObjectImporter do
expect(logger) expect(logger)
.to receive(:info) .to receive(:info)
.with( .with(
github_id: 1,
message: 'importer finished', message: 'importer finished',
import_source: :github, import_source: :github,
project_id: 1, project_id: 1,
...@@ -77,10 +89,15 @@ RSpec.describe Gitlab::GithubImport::ObjectImporter do ...@@ -77,10 +89,15 @@ RSpec.describe Gitlab::GithubImport::ObjectImporter do
) )
end end
worker.import(project, client, { 'number' => 10 }) worker.import(project, client, { 'number' => 10, 'github_id' => 1 })
end end
it 'logs error when the import fails' do it 'logs error when the import fails' do
expect(importer_class)
.to receive(:new)
.with(instance_of(MockRepresantation), project, client)
.and_return(importer_instance)
exception = StandardError.new('some error') exception = StandardError.new('some error')
expect(importer_instance) expect(importer_instance)
.to receive(:execute) .to receive(:execute)
...@@ -90,6 +107,7 @@ RSpec.describe Gitlab::GithubImport::ObjectImporter do ...@@ -90,6 +107,7 @@ RSpec.describe Gitlab::GithubImport::ObjectImporter do
expect(logger) expect(logger)
.to receive(:info) .to receive(:info)
.with( .with(
github_id: 1,
message: 'starting importer', message: 'starting importer',
import_source: :github, import_source: :github,
project_id: project.id, project_id: project.id,
...@@ -98,20 +116,64 @@ RSpec.describe Gitlab::GithubImport::ObjectImporter do ...@@ -98,20 +116,64 @@ RSpec.describe Gitlab::GithubImport::ObjectImporter do
expect(logger) expect(logger)
.to receive(:error) .to receive(:error)
.with( .with(
github_id: 1,
message: 'importer failed', message: 'importer failed',
import_source: :github, import_source: :github,
project_id: project.id, project_id: project.id,
importer: 'klass_name', importer: 'klass_name',
'error.message': 'some error' 'error.message': 'some error',
'github.data': {
'github_id' => 1,
'number' => 10
}
) )
end end
expect(Gitlab::ErrorTracking) expect(Gitlab::ErrorTracking)
.to receive(:track_and_raise_exception) .to receive(:track_and_raise_exception)
.with(exception, import_source: :github, project_id: 1, importer: 'klass_name') .with(
.and_call_original exception,
import_source: :github,
github_id: 1,
project_id: 1,
importer: 'klass_name'
).and_call_original
expect { worker.import(project, client, { 'number' => 10, 'github_id' => 1 }) }
.to raise_error(exception)
end
it 'logs error when representation does not have a github_id' do
expect(importer_class).not_to receive(:new)
expect_next_instance_of(Gitlab::Import::Logger) do |logger|
expect(logger)
.to receive(:error)
.with(
github_id: nil,
message: 'importer failed',
import_source: :github,
project_id: project.id,
importer: 'klass_name',
'error.message': 'key not found: :github_id',
'github.data': {
'number' => 10
}
)
end
expect { worker.import(project, client, { 'number' => 10 }) }.to raise_error(exception) expect(Gitlab::ErrorTracking)
.to receive(:track_and_raise_exception)
.with(
an_instance_of(KeyError),
import_source: :github,
github_id: nil,
project_id: 1,
importer: 'klass_name'
).and_call_original
expect { worker.import(project, client, { 'number' => 10 }) }
.to raise_error(KeyError, 'key not found: :github_id')
end end
end end
......
...@@ -12,6 +12,7 @@ RSpec.describe Gitlab::GithubImport::ImportDiffNoteWorker do ...@@ -12,6 +12,7 @@ RSpec.describe Gitlab::GithubImport::ImportDiffNoteWorker do
importer = double(:importer) importer = double(:importer)
hash = { hash = {
'noteable_id' => 42, 'noteable_id' => 42,
'github_id' => 42,
'path' => 'README.md', 'path' => 'README.md',
'commit_id' => '123abc', 'commit_id' => '123abc',
'diff_hunk' => "@@ -1 +1 @@\n-Hello\n+Hello world", 'diff_hunk' => "@@ -1 +1 @@\n-Hello\n+Hello world",
......
...@@ -12,6 +12,7 @@ RSpec.describe Gitlab::GithubImport::ImportIssueWorker do ...@@ -12,6 +12,7 @@ RSpec.describe Gitlab::GithubImport::ImportIssueWorker do
importer = double(:importer) importer = double(:importer)
hash = { hash = {
'iid' => 42, 'iid' => 42,
'github_id' => 42,
'title' => 'My Issue', 'title' => 'My Issue',
'description' => 'This is my issue', 'description' => 'This is my issue',
'milestone_number' => 4, 'milestone_number' => 4,
......
...@@ -12,6 +12,7 @@ RSpec.describe Gitlab::GithubImport::ImportNoteWorker do ...@@ -12,6 +12,7 @@ RSpec.describe Gitlab::GithubImport::ImportNoteWorker do
importer = double(:importer) importer = double(:importer)
hash = { hash = {
'noteable_id' => 42, 'noteable_id' => 42,
'github_id' => 42,
'noteable_type' => 'issues', 'noteable_type' => 'issues',
'user' => { 'id' => 4, 'login' => 'alice' }, 'user' => { 'id' => 4, 'login' => 'alice' },
'note' => 'Hello world', 'note' => 'Hello world',
......
...@@ -12,6 +12,7 @@ RSpec.describe Gitlab::GithubImport::ImportPullRequestWorker do ...@@ -12,6 +12,7 @@ RSpec.describe Gitlab::GithubImport::ImportPullRequestWorker do
importer = double(:importer) importer = double(:importer)
hash = { hash = {
'iid' => 42, 'iid' => 42,
'github_id' => 42,
'title' => 'My Pull Request', 'title' => 'My Pull Request',
'description' => 'This is my pull request', 'description' => 'This is my pull request',
'source_branch' => 'my-feature', 'source_branch' => 'my-feature',
......
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