Welcome to mirror list, hosted at ThFree Co, Russian Federation.

single_endpoint_issue_events_importer_spec.rb « importer « github_import « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4ed01fd7e0b364b39edd7c55fdf72d6d1628121a (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::GithubImport::Importer::SingleEndpointIssueEventsImporter do
  let(:client) { double }

  let_it_be(:project) { create(:project, :import_started, import_source: 'http://somegithub.com') }

  let!(:issuable) { create(:issue, project: project) }

  subject { described_class.new(project, client, parallel: parallel) }

  let(:parallel) { true }

  it { is_expected.to include_module(Gitlab::GithubImport::ParallelScheduling) }

  describe '#importer_class' do
    it { expect(subject.importer_class).to eq(Gitlab::GithubImport::Importer::IssueEventImporter) }
  end

  describe '#representation_class' do
    it { expect(subject.representation_class).to eq(Gitlab::GithubImport::Representation::IssueEvent) }
  end

  describe '#sidekiq_worker_class' do
    it { expect(subject.sidekiq_worker_class).to eq(Gitlab::GithubImport::ImportIssueEventWorker) }
  end

  describe '#object_type' do
    it { expect(subject.object_type).to eq(:issue_event) }
  end

  describe '#collection_method' do
    it { expect(subject.collection_method).to eq(:issue_timeline) }
  end

  describe '#page_counter_id' do
    it { expect(subject.page_counter_id(issuable)).to eq("issues/#{issuable.iid}/issue_timeline") }
  end

  describe '#id_for_already_imported_cache' do
    let(:event) { instance_double('Event', id: 1) }

    it { expect(subject.id_for_already_imported_cache(event)).to eq(1) }
  end

  describe '#collection_options' do
    it do
      expect(subject.collection_options)
        .to eq({ state: 'all', sort: 'created', direction: 'asc' })
    end
  end

  describe '#compose_associated_id!' do
    let(:issuable) { build_stubbed(:issue, iid: 99) }
    let(:event_resource) { Struct.new(:id, :event, :source, keyword_init: true) }

    context 'when event type is cross-referenced' do
      let(:event) do
        source_resource = Struct.new(:issue, keyword_init: true)
        issue_resource = Struct.new(:id, keyword_init: true)
        event_resource.new(
          id: nil,
          event: 'cross-referenced',
          source: source_resource.new(issue: issue_resource.new(id: '100500'))
        )
      end

      it 'assigns event id' do
        subject.compose_associated_id!(issuable, event)

        expect(event.id).to eq 'cross-reference#99-in-100500'
      end
    end

    context "when event type isn't cross-referenced" do
      let(:event) { event_resource.new(id: nil, event: 'labeled') }

      it "doesn't assign event id" do
        subject.compose_associated_id!(issuable, event)

        expect(event.id).to eq nil
      end
    end
  end

  describe '#each_object_to_import', :clean_gitlab_redis_cache do
    let(:issue_event) do
      struct = Struct.new(:id, :event, :created_at, :issue, keyword_init: true)
      struct.new(id: rand(10), event: 'closed', created_at: '2022-04-26 18:30:53 UTC')
    end

    let(:page) do
      instance_double(
        Gitlab::GithubImport::Client::Page,
        number: 1, objects: [issue_event]
      )
    end

    let(:page_counter) { instance_double(Gitlab::GithubImport::PageCounter) }

    before do
      allow(client).to receive(:each_page)
        .once
        .with(
          :issue_timeline,
          project.import_source,
          issuable.iid,
          { state: 'all', sort: 'created', direction: 'asc', page: 1 }
        ).and_yield(page)
    end

    context 'with issues' do
      it 'imports each issue event page by page' do
        counter = 0
        subject.each_object_to_import do |object|
          expect(object).to eq issue_event
          expect(issue_event.issue['number']).to eq issuable.iid
          expect(issue_event.issue['pull_request']).to eq false
          counter += 1
        end
        expect(counter).to eq 1
      end
    end

    context 'with merge requests' do
      let!(:issuable) { create(:merge_request, source_project: project, target_project: project) }

      it 'imports each merge request event page by page' do
        counter = 0
        subject.each_object_to_import do |object|
          expect(object).to eq issue_event
          expect(issue_event.issue['number']).to eq issuable.iid
          expect(issue_event.issue['pull_request']).to eq true
          counter += 1
        end
        expect(counter).to eq 1
      end
    end

    it 'triggers page number increment' do
      expect(Gitlab::GithubImport::PageCounter)
        .to receive(:new).with(project, 'issues/1/issue_timeline')
        .and_return(page_counter)
      expect(page_counter).to receive(:current).and_return(1)
      expect(page_counter)
        .to receive(:set).with(page.number).and_return(true)

      counter = 0
      subject.each_object_to_import { counter += 1 }
      expect(counter).to eq 1
    end

    context 'when page is already processed' do
      before do
        page_counter = Gitlab::GithubImport::PageCounter.new(
          project, subject.page_counter_id(issuable)
        )
        page_counter.set(page.number)
      end

      it "doesn't process this page" do
        counter = 0
        subject.each_object_to_import { counter += 1 }
        expect(counter).to eq 0
      end
    end

    context 'when event is already processed' do
      it "doesn't process this event" do
        subject.mark_as_imported(issue_event)

        counter = 0
        subject.each_object_to_import { counter += 1 }
        expect(counter).to eq 0
      end
    end
  end
end