summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/github_import/pull_request_formatter_spec.rb
blob: 44423917944ccbce212e6a37fe50e198413e6f8d (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
require 'spec_helper'

describe Gitlab::GithubImport::PullRequestFormatter, lib: true do
  let(:client) { double }
  let(:project) { create(:project, :repository) }
  let(:source_sha) { create(:commit, project: project).id }
  let(:target_sha) { create(:commit, project: project, git_commit: RepoHelpers.another_sample_commit).id }
  let(:repository) { double(id: 1, fork: false) }
  let(:source_repo) { repository }
  let(:source_branch) { double(ref: 'branch-merged', repo: source_repo, sha: source_sha) }
  let(:forked_source_repo) { double(id: 2, fork: true, name: 'otherproject', full_name: 'company/otherproject') }
  let(:target_repo) { repository }
  let(:target_branch) { double(ref: 'master', repo: target_repo, sha: target_sha) }
  let(:removed_branch) { double(ref: 'removed-branch', repo: source_repo, sha: '2e5d3239642f9161dcbbc4b70a211a68e5e45e2b') }
  let(:forked_branch) { double(ref: 'master', repo: forked_source_repo, sha: '2e5d3239642f9161dcbbc4b70a211a68e5e45e2b') }
  let(:octocat) { double(id: 123456, login: 'octocat', email: 'octocat@example.com') }
  let(:created_at) { DateTime.strptime('2011-01-26T19:01:12Z') }
  let(:updated_at) { DateTime.strptime('2011-01-27T19:01:12Z') }
  let(:base_data) do
    {
      number: 1347,
      milestone: nil,
      state: 'open',
      title: 'New feature',
      body: 'Please pull these awesome changes',
      head: source_branch,
      base: target_branch,
      assignee: nil,
      user: octocat,
      created_at: created_at,
      updated_at: updated_at,
      closed_at: nil,
      merged_at: nil,
      url: 'https://api.github.com/repos/octocat/Hello-World/pulls/1347'
    }
  end

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

  before do
    allow(client).to receive(:user).and_return(octocat)
  end

  shared_examples 'Gitlab::GithubImport::PullRequestFormatter#attributes' do
    context 'when pull request is open' do
      let(:raw_data) { double(base_data.merge(state: 'open')) }

      it 'returns formatted attributes' do
        expected = {
          iid: 1347,
          title: 'New feature',
          description: "*Created by: octocat*\n\nPlease pull these awesome changes",
          source_project: project,
          source_branch: 'branch-merged',
          source_branch_sha: source_sha,
          target_project: project,
          target_branch: 'master',
          target_branch_sha: target_sha,
          state: 'opened',
          milestone: nil,
          author_id: project.creator_id,
          assignee_id: nil,
          created_at: created_at,
          updated_at: updated_at
        }

        expect(pull_request.attributes).to eq(expected)
      end
    end

    context 'when pull request is closed' do
      let(:raw_data) { double(base_data.merge(state: 'closed')) }

      it 'returns formatted attributes' do
        expected = {
          iid: 1347,
          title: 'New feature',
          description: "*Created by: octocat*\n\nPlease pull these awesome changes",
          source_project: project,
          source_branch: 'branch-merged',
          source_branch_sha: source_sha,
          target_project: project,
          target_branch: 'master',
          target_branch_sha: target_sha,
          state: 'closed',
          milestone: nil,
          author_id: project.creator_id,
          assignee_id: nil,
          created_at: created_at,
          updated_at: updated_at
        }

        expect(pull_request.attributes).to eq(expected)
      end
    end

    context 'when pull request is merged' do
      let(:merged_at) { DateTime.strptime('2011-01-28T13:01:12Z') }
      let(:raw_data) { double(base_data.merge(state: 'closed', merged_at: merged_at)) }

      it 'returns formatted attributes' do
        expected = {
          iid: 1347,
          title: 'New feature',
          description: "*Created by: octocat*\n\nPlease pull these awesome changes",
          source_project: project,
          source_branch: 'branch-merged',
          source_branch_sha: source_sha,
          target_project: project,
          target_branch: 'master',
          target_branch_sha: target_sha,
          state: 'merged',
          milestone: nil,
          author_id: project.creator_id,
          assignee_id: nil,
          created_at: created_at,
          updated_at: updated_at
        }

        expect(pull_request.attributes).to eq(expected)
      end
    end

    context 'when it is assigned to someone' do
      let(:raw_data) { double(base_data.merge(assignee: octocat)) }

      it 'returns nil as assignee_id when is not a GitLab user' do
        expect(pull_request.attributes.fetch(:assignee_id)).to be_nil
      end

      it 'returns GitLab user id associated with GitHub id as assignee_id' do
        gl_user = create(:omniauth_user, extern_uid: octocat.id, provider: 'github')

        expect(pull_request.attributes.fetch(:assignee_id)).to eq gl_user.id
      end

      it 'returns GitLab user id associated with GitHub email as assignee_id' do
        gl_user = create(:user, email: octocat.email)

        expect(pull_request.attributes.fetch(:assignee_id)).to eq gl_user.id
      end
    end

    context 'when author is a GitLab user' do
      let(:raw_data) { double(base_data.merge(user: octocat)) }

      it 'returns project creator_id as author_id when is not a GitLab user' do
        expect(pull_request.attributes.fetch(:author_id)).to eq project.creator_id
      end

      it 'returns GitLab user id associated with GitHub id as author_id' do
        gl_user = create(:omniauth_user, extern_uid: octocat.id, provider: 'github')

        expect(pull_request.attributes.fetch(:author_id)).to eq gl_user.id
      end

      it 'returns GitLab user id associated with GitHub email as author_id' do
        gl_user = create(:user, email: octocat.email)

        expect(pull_request.attributes.fetch(:author_id)).to eq gl_user.id
      end

      it 'returns description without created at tag line' do
        create(:omniauth_user, extern_uid: octocat.id, provider: 'github')

        expect(pull_request.attributes.fetch(:description)).to eq('Please pull these awesome changes')
      end
    end

    context 'when it has a milestone' do
      let(:milestone) { double(id: 42, number: 42) }
      let(:raw_data) { double(base_data.merge(milestone: milestone)) }

      it 'returns nil when milestone does not exist' do
        expect(pull_request.attributes.fetch(:milestone)).to be_nil
      end

      it 'returns milestone when it exists' do
        milestone = create(:milestone, project: project, iid: 42)

        expect(pull_request.attributes.fetch(:milestone)).to eq milestone
      end
    end
  end

  shared_examples 'Gitlab::GithubImport::PullRequestFormatter#number' do
    let(:raw_data) { double(base_data) }

    it 'returns pull request number' do
      expect(pull_request.number).to eq 1347
    end
  end

  shared_examples 'Gitlab::GithubImport::PullRequestFormatter#source_branch_name' do
    context 'when source branch exists' do
      let(:raw_data) { double(base_data) }

      it 'returns branch ref' do
        expect(pull_request.source_branch_name).to eq 'branch-merged'
      end
    end

    context 'when source branch does not exist' do
      let(:raw_data) { double(base_data.merge(head: removed_branch)) }

      it 'prefixes branch name with pull request number' do
        expect(pull_request.source_branch_name).to eq 'pull/1347/removed-branch'
      end
    end

    context 'when source branch is from a fork' do
      let(:raw_data) { double(base_data.merge(head: forked_branch)) }

      it 'prefixes branch name with pull request number and project with namespace to avoid collision' do
        expect(pull_request.source_branch_name).to eq 'pull/1347/company/otherproject/master'
      end
    end
  end

  shared_examples 'Gitlab::GithubImport::PullRequestFormatter#target_branch_name' do
    context 'when target branch exists' do
      let(:raw_data) { double(base_data) }

      it 'returns branch ref' do
        expect(pull_request.target_branch_name).to eq 'master'
      end
    end

    context 'when target branch does not exist' do
      let(:raw_data) { double(base_data.merge(base: removed_branch)) }

      it 'prefixes branch name with pull request number' do
        expect(pull_request.target_branch_name).to eq 'pull/1347/removed-branch'
      end
    end
  end

  context 'when importing a GitHub project' do
    it_behaves_like 'Gitlab::GithubImport::PullRequestFormatter#attributes'
    it_behaves_like 'Gitlab::GithubImport::PullRequestFormatter#number'
    it_behaves_like 'Gitlab::GithubImport::PullRequestFormatter#source_branch_name'
    it_behaves_like 'Gitlab::GithubImport::PullRequestFormatter#target_branch_name'
  end

  context 'when importing a Gitea project' do
    before do
      project.update(import_type: 'gitea')
    end

    it_behaves_like 'Gitlab::GithubImport::PullRequestFormatter#attributes'
    it_behaves_like 'Gitlab::GithubImport::PullRequestFormatter#number'
    it_behaves_like 'Gitlab::GithubImport::PullRequestFormatter#source_branch_name'
    it_behaves_like 'Gitlab::GithubImport::PullRequestFormatter#target_branch_name'
  end

  describe '#valid?' do
    context 'when source, and target repos are not a fork' do
      let(:raw_data) { double(base_data) }

      it 'returns true' do
        expect(pull_request.valid?).to eq true
      end
    end

    context 'when source repo is a fork' do
      let(:source_repo) { double(id: 2) }
      let(:raw_data) { double(base_data) }

      it 'returns true' do
        expect(pull_request.valid?).to eq true
      end
    end

    context 'when target repo is a fork' do
      let(:target_repo) { double(id: 2) }
      let(:raw_data) { double(base_data) }

      it 'returns true' do
        expect(pull_request.valid?).to eq true
      end
    end
  end

  describe '#cross_project?' do
    context 'when source and target repositories are different' do
      let(:raw_data) { double(base_data.merge(head: forked_branch)) }

      it 'returns true' do
        expect(pull_request.cross_project?).to eq true
      end
    end

    context 'when source and target repositories are the same' do
      let(:raw_data) { double(base_data.merge(head: source_branch)) }

      it 'returns false' do
        expect(pull_request.cross_project?).to eq false
      end
    end
  end

  describe '#url' do
    let(:raw_data) { double(base_data) }

    it 'return raw url' do
      expect(pull_request.url).to eq 'https://api.github.com/repos/octocat/Hello-World/pulls/1347'
    end
  end

  describe '#opened?' do
    let(:raw_data) { double(base_data.merge(state: 'open')) }

    it 'returns true when state is "open"' do
      expect(pull_request.opened?).to be_truthy
    end
  end
end