1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::GithubImport::Representation::DiffNotes::DiscussionId, :clean_gitlab_redis_cache,
feature_category: :importers do
describe '#discussion_id' do
let(:hunk) do
'@@ -1 +1 @@
-Hello
+Hello world'
end
let(:note_id) { 1 }
let(:html_url) { 'https://github.com/foo/project_name/pull/42' }
let(:note) do
{
id: note_id,
html_url: html_url,
path: 'README.md',
commit_id: '123abc',
original_commit_id: 'original123abc',
side: 'RIGHT',
user: { id: 4, login: 'alice' },
diff_hunk: hunk,
body: 'Hello world',
created_at: Time.new(2017, 1, 1, 12, 10).utc,
updated_at: Time.new(2017, 1, 1, 12, 15).utc,
line: 23,
start_line: nil,
in_reply_to_id: nil
}
end
context 'when the note is not a reply to a discussion' do
subject(:discussion_id) { described_class.new(note).find_or_generate }
it 'generates and caches new discussion_id' do
expect(Discussion)
.to receive(:discussion_id)
.and_return('FIRST_DISCUSSION_ID')
expect(Gitlab::Cache::Import::Caching).to receive(:write).with(
"github-importer/discussion-id-map/project_name/42/#{note_id}",
'FIRST_DISCUSSION_ID'
).and_return('FIRST_DISCUSSION_ID')
expect(discussion_id).to eq('FIRST_DISCUSSION_ID')
end
end
context 'when the note is a reply to a discussion' do
let(:reply_note) do
{
note_id: note_id + 1,
in_reply_to_id: note_id,
html_url: html_url
}
end
subject(:discussion_id) { described_class.new(reply_note).find_or_generate }
it 'uses the cached value as the discussion_id' do
expect(Discussion)
.to receive(:discussion_id)
.and_return('FIRST_DISCUSSION_ID')
described_class.new(note).find_or_generate
expect(discussion_id).to eq('FIRST_DISCUSSION_ID')
end
context 'when cached value does not exist' do
it 'falls back to generating a new discussion_id' do
expect(Discussion)
.to receive(:discussion_id)
.and_return('NEW_DISCUSSION_ID')
expect(discussion_id).to eq('NEW_DISCUSSION_ID')
end
end
end
end
end
|