summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/github_import/importer/events/cross_referenced_spec.rb
blob: 68e001c736445ba7c69bec3b5c163dcf6bb254e6 (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::GithubImport::Importer::Events::CrossReferenced, :clean_gitlab_redis_cache do
  subject(:importer) { described_class.new(project, client) }

  let_it_be(:project) { create(:project, :repository) }
  let_it_be(:user) { create(:user) }

  let(:client) { instance_double('Gitlab::GithubImport::Client') }

  let(:issue_iid) { 999 }
  let(:issue) { create(:issue, project: project, iid: issue_iid) }
  let(:referenced_in) { build_stubbed(:issue, project: project, iid: issue_iid + 1) }
  let(:commit_id) { nil }

  let(:issue_event) do
    Gitlab::GithubImport::Representation::IssueEvent.from_json_hash(
      'id' => 6501124486,
      'node_id' => 'CE_lADOHK9fA85If7x0zwAAAAGDf0mG',
      'url' => 'https://api.github.com/repos/elhowm/test-import/issues/events/6501124486',
      'actor' => { 'id' => user.id, 'login' => user.username },
      'event' => 'cross-referenced',
      'source' => {
        'type' => 'issue',
        'issue' => {
          'number' => referenced_in.iid,
          'pull_request' => pull_request_resource
        }
      },
      'created_at' => '2022-04-26 18:30:53 UTC',
      'issue' => { 'number' => issue.iid }
    )
  end

  let(:pull_request_resource) { nil }
  let(:expected_note_attrs) do
    {
      system: true,
      noteable_type: Issue.name,
      noteable_id: issue.id,
      project_id: project.id,
      author_id: user.id,
      note: expected_note_body,
      created_at: issue_event.created_at
    }.stringify_keys
  end

  context 'when referenced in other issue' do
    let(:expected_note_body) { "mentioned in issue ##{referenced_in.iid}" }

    before do
      allow_next_instance_of(Gitlab::GithubImport::IssuableFinder) do |finder|
        allow(finder).to receive(:database_id).and_return(referenced_in.iid)
        allow(finder).to receive(:database_id).and_return(issue.id)
      end
      allow_next_instance_of(Gitlab::GithubImport::UserFinder) do |finder|
        allow(finder).to receive(:find).with(user.id, user.username).and_return(user.id)
      end
    end

    it 'creates expected note' do
      importer.execute(issue_event)

      expect(issue.notes.count).to eq 1
      expect(issue.notes[0]).to have_attributes expected_note_attrs
      expect(issue.notes[0].system_note_metadata.action).to eq 'cross_reference'
    end
  end

  context 'when referenced in pull request' do
    let(:referenced_in) { build_stubbed(:merge_request, project: project) }
    let(:pull_request_resource) { { 'id' => referenced_in.iid } }

    let(:expected_note_body) { "mentioned in merge request !#{referenced_in.iid}" }

    before do
      allow_next_instance_of(Gitlab::GithubImport::IssuableFinder) do |finder|
        allow(finder).to receive(:database_id).and_return(referenced_in.iid)
        allow(finder).to receive(:database_id).and_return(issue.id)
      end
      allow_next_instance_of(Gitlab::GithubImport::UserFinder) do |finder|
        allow(finder).to receive(:find).with(user.id, user.username).and_return(user.id)
      end
    end

    it 'creates expected note' do
      importer.execute(issue_event)

      expect(issue.notes.count).to eq 1
      expect(issue.notes[0]).to have_attributes expected_note_attrs
      expect(issue.notes[0].system_note_metadata.action).to eq 'cross_reference'
    end
  end

  context 'when referenced in out of project issue/pull_request' do
    it 'does not create expected note' do
      importer.execute(issue_event)

      expect(issue.notes.count).to eq 0
    end
  end
end