summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/github_import/importer/pull_requests_reviews_importer_spec.rb
blob: 08be350f0f93eab1f34f078b78c87b275de3d141 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::GithubImport::Importer::PullRequestsReviewsImporter do
  let(:client) { double }
  let(:project) { create(:project, import_source: 'github/repo') }

  subject { described_class.new(project, client) }

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

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

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

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

  describe '#id_for_already_imported_cache' do
    it { expect(subject.id_for_already_imported_cache(double(id: 1))).to eq(1) }
  end

  describe '#each_object_to_import', :clean_gitlab_redis_cache do
    context 'when github_review_importer_query_only_unimported_merge_requests is enabled' do
      before do
        stub_feature_flags(github_review_importer_query_only_unimported_merge_requests: true)
      end

      let(:merge_request) do
        create(
          :merged_merge_request,
          iid: 999,
          source_project: project,
          target_project: project
        )
      end

      let(:review) { double(id: 1) }

      it 'fetches the pull requests reviews data' do
        page = double(objects: [review], number: 1)

        expect(review)
          .to receive(:merge_request_id=)
          .with(merge_request.id)

        expect(client)
          .to receive(:each_page)
          .exactly(:once) # ensure to be cached on the second call
          .with(:pull_request_reviews, 'github/repo', merge_request.iid, page: 1)
          .and_yield(page)

        expect { |b| subject.each_object_to_import(&b) }
          .to yield_with_args(review)

        subject.each_object_to_import {}
      end

      it 'skips cached pages' do
        Gitlab::GithubImport::PageCounter
          .new(project, "merge_request/#{merge_request.id}/pull_request_reviews")
          .set(2)

        expect(review).not_to receive(:merge_request_id=)

        expect(client)
          .to receive(:each_page)
          .exactly(:once) # ensure to be cached on the second call
          .with(:pull_request_reviews, 'github/repo', merge_request.iid, page: 2)

        subject.each_object_to_import {}
      end

      it 'skips cached merge requests' do
        Gitlab::Cache::Import::Caching.set_add(
          "github-importer/merge_request/already-imported/#{project.id}",
          merge_request.id
        )

        expect(review).not_to receive(:merge_request_id=)

        expect(client).not_to receive(:each_page)

        subject.each_object_to_import {}
      end
    end

    context 'when github_review_importer_query_only_unimported_merge_requests is disabled' do
      before do
        stub_feature_flags(github_review_importer_query_only_unimported_merge_requests: false)
      end

      it 'fetchs the merged pull requests data' do
        merge_request = create(
          :merged_merge_request,
          iid: 999,
          source_project: project,
          target_project: project
        )

        review = double

        expect(review)
          .to receive(:merge_request_id=)
          .with(merge_request.id)

        allow(client)
          .to receive(:pull_request_reviews)
          .exactly(:once) # ensure to be cached on the second call
          .with('github/repo', merge_request.iid)
          .and_return([review])

        expect { |b| subject.each_object_to_import(&b) }
          .to yield_with_args(review)

        subject.each_object_to_import {}
      end
    end
  end
end