Skip to content
Snippets Groups Projects
Commit 1d571cad authored by Tetiana Zavediuk's avatar Tetiana Zavediuk
Browse files

[issue-354239] Import github 'renamed' issue events

During project import from GitHub, for each imported issue, check if
any events of type renamed exist for that issue and create corresponding
events in GitLab.

Resolves: #354239

Changelog: added
parent 28aa14c6
No related branches found
No related tags found
1 merge request!89851Import github 'renamed' issue events
# frozen_string_literal: true
module Gitlab
module GithubImport
module Importer
module Events
class Renamed
def initialize(project, user_id)
@project = project
@user_id = user_id
end
# issue_event - An instance of `Gitlab::GithubImport::Representation::IssueEvent`
def execute(issue_event)
Note.create!(note_params(issue_event))
end
private
attr_reader :project, :user_id
def note_params(issue_event)
{
noteable_id: issue_event.issue_db_id,
noteable_type: Issue.name,
project_id: project.id,
author_id: user_id,
note: parse_body(issue_event),
system: true,
created_at: issue_event.created_at,
updated_at: issue_event.created_at,
system_note_metadata: SystemNoteMetadata.new(
{
action: "title",
created_at: issue_event.created_at,
updated_at: issue_event.created_at
}
)
}
end
def parse_body(issue_event)
old_diffs, new_diffs = Gitlab::Diff::InlineDiff.new(
issue_event.old_title, issue_event.new_title
).inline_diffs
marked_old_title = Gitlab::Diff::InlineDiffMarkdownMarker.new(issue_event.old_title).mark(old_diffs)
marked_new_title = Gitlab::Diff::InlineDiffMarkdownMarker.new(issue_event.new_title).mark(new_diffs)
"changed title from **#{marked_old_title}** to **#{marked_new_title}**"
end
end
end
end
end
end
......@@ -27,6 +27,9 @@ def execute
when 'labeled', 'unlabeled'
Gitlab::GithubImport::Importer::Events::ChangedLabel.new(project, author_id)
.execute(issue_event)
when 'renamed'
Gitlab::GithubImport::Importer::Events::Renamed.new(project, author_id)
.execute(issue_event)
else
Gitlab::GithubImport::Logger.debug(
message: 'UNSUPPORTED_EVENT_TYPE',
......
......@@ -9,7 +9,7 @@ class IssueEvent
attr_reader :attributes
expose_attribute :id, :actor, :event, :commit_id, :label_title, :created_at
expose_attribute :id, :actor, :event, :commit_id, :label_title, :old_title, :new_title, :created_at
expose_attribute :issue_db_id # set in SingleEndpointIssueEventsImporter#each_associated
# Builds a event from a GitHub API response.
......@@ -22,6 +22,8 @@ def self.from_api_response(event)
event: event.event,
commit_id: event.commit_id,
label_title: event.label && event.label[:name],
old_title: event.rename && event.rename[:from],
new_title: event.rename && event.rename[:to],
issue_db_id: event.issue_db_id,
created_at: event.created_at
)
......@@ -30,7 +32,7 @@ def self.from_api_response(event)
# Builds a event using a Hash that was built from a JSON payload.
def self.from_json_hash(raw_hash)
hash = Representation.symbolize_hash(raw_hash)
hash[:actor] = Representation::User.from_json_hash(hash[:actor]) if hash[:actor]
hash[:actor] &&= Representation::User.from_json_hash(hash[:actor])
new(hash)
end
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::GithubImport::Importer::Events::Renamed do
subject(:importer) { described_class.new(project, user.id) }
let_it_be(:project) { create(:project, :repository) }
let_it_be(:user) { create(:user) }
let(:issue) { create(:issue, project: project) }
let(:issue_event) do
Gitlab::GithubImport::Representation::IssueEvent.from_json_hash(
'id' => 6501124486,
'actor' => { 'id' => 4, 'login' => 'alice' },
'event' => 'renamed',
'commit_id' => nil,
'created_at' => '2022-04-26 18:30:53 UTC',
'old_title' => 'old title',
'new_title' => 'new title',
'issue_db_id' => issue.id
)
end
let(:expected_note_attrs) do
{
noteable_id: issue.id,
noteable_type: Issue.name,
project_id: project.id,
author_id: user.id,
note: "changed title from **{-old-} title** to **{+new+} title**",
system: true,
created_at: issue_event.created_at,
updated_at: issue_event.created_at
}.stringify_keys
end
let(:expected_system_note_metadata_attrs) do
{
action: "title",
created_at: issue_event.created_at,
updated_at: issue_event.created_at
}.stringify_keys
end
describe '#execute' do
it 'creates expected note' do
expect { importer.execute(issue_event) }.to change { issue.notes.count }
.from(0).to(1)
expect(issue.notes.last)
.to have_attributes(expected_note_attrs)
end
it 'creates expected system note metadata' do
expect { importer.execute(issue_event) }.to change { SystemNoteMetadata.count }
.from(0).to(1)
expect(SystemNoteMetadata.last)
.to have_attributes(
expected_system_note_metadata_attrs.merge(
note_id: Note.last.id
)
)
end
end
end
......@@ -80,6 +80,13 @@
Gitlab::GithubImport::Importer::Events::ChangedLabel
end
context "when it's renamed issue event" do
let(:event_name) { 'renamed' }
it_behaves_like 'triggers specific event importer',
Gitlab::GithubImport::Importer::Events::Renamed
end
context "when it's unknown issue event" do
let(:event_name) { 'fake' }
......
......@@ -57,6 +57,22 @@
end
end
context 'when rename field is present' do
it 'includes the old_title and new_title fields' do
expect(issue_event.old_title).to eq('old title')
expect(issue_event.new_title).to eq('new title')
end
end
context 'when rename field is empty' do
let(:with_rename) { false }
it 'does not return such info' do
expect(issue_event.old_title).to eq nil
expect(issue_event.new_title).to eq nil
end
end
it 'includes the created timestamp' do
expect(issue_event.created_at).to eq('2022-04-26 18:30:53 UTC')
end
......@@ -73,7 +89,7 @@
let(:response) do
event_resource = Struct.new(
:id, :node_id, :url, :actor, :event, :commit_id, :commit_url, :label,
:issue_db_id, :created_at, :performed_via_github_app,
:rename, :issue_db_id, :created_at, :performed_via_github_app,
keyword_init: true
)
user_resource = Struct.new(:id, :login, keyword_init: true)
......@@ -86,6 +102,7 @@
commit_id: '570e7b2abdd848b95f2f578043fc23bd6f6fd24d',
commit_url: 'https://api.github.com/repos/octocat/Hello-World/commits'\
'/570e7b2abdd848b95f2f578043fc23bd6f6fd24d',
rename: with_rename ? { from: 'old title', to: 'new title' } : nil,
issue_db_id: 100500,
label: with_label ? { name: 'label title' } : nil,
created_at: '2022-04-26 18:30:53 UTC',
......@@ -95,6 +112,7 @@
let(:with_actor) { true }
let(:with_label) { true }
let(:with_rename) { true }
it_behaves_like 'an IssueEvent' do
let(:issue_event) { described_class.from_api_response(response) }
......@@ -114,6 +132,8 @@
'commit_url' =>
'https://api.github.com/repos/octocat/Hello-World/commits/570e7b2abdd848b95f2f578043fc23bd6f6fd24d',
'label_title' => (with_label ? 'label title' : nil),
'old_title' => with_rename ? 'old title' : nil,
'new_title' => with_rename ? 'new title' : nil,
"issue_db_id" => 100500,
'created_at' => '2022-04-26 18:30:53 UTC',
'performed_via_github_app' => nil
......@@ -122,6 +142,7 @@
let(:with_actor) { true }
let(:with_label) { true }
let(:with_rename) { true }
let(:issue_event) { described_class.from_json_hash(hash) }
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment