-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathtest_bitbucket_cloud_adapter.py
More file actions
455 lines (404 loc) · 17 KB
/
Copy pathtest_bitbucket_cloud_adapter.py
File metadata and controls
455 lines (404 loc) · 17 KB
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
import json
import unittest
from unittest import TestCase
from unittest.mock import MagicMock
import requests
from dateutil import parser
from jf_agent.git import StandardizedShortRepository
from jf_agent.git.bitbucket_cloud_adapter import BitbucketCloudAdapter
TEST_INPUT_FILE_PATH = 'tests/test_data/bitbucket_cloud/'
class TestBitbucketCloudAdapter(TestCase):
def setUp(self):
self.mock_config = MagicMock()
self.mock_config.git_include_repos = None
self.mock_config.git_exclude_repos = None
self.mock_config.git_strip_text_content = False
self.mock_config.git_redact_names_and_urls = False
self.mock_client = MagicMock()
self.outdir = "test"
self.adapter = BitbucketCloudAdapter(self.mock_config, self.outdir, False, self.mock_client)
def test_get_users(self):
# Act
users = self.adapter.get_users()
# Assert
self.assertEqual(users, [], "Should be an empty list")
def test_get_users(self):
# Arrange
input_projects = ['test_project_1', 'test_project_2']
self.mock_config.git_include_projects = input_projects
# Act
resulting_projects = self.adapter.get_projects()
# Assert
self.assertEqual(
len(resulting_projects),
len(input_projects),
f"Should be project list of size {len(input_projects)}",
)
for idx, resulting_project in enumerate(resulting_projects):
self.assertEqual(
resulting_project.id,
input_projects[idx],
"Project id should be the same as the input project name",
)
self.assertEqual(
resulting_project.name,
input_projects[idx],
"Project name should be the same as the input project name",
)
self.assertEqual(
resulting_project.login,
input_projects[idx],
"Project login should be the same as the input project name",
)
self.assertIsNone(resulting_project.url)
def test_get_repos(self):
# Arrange
test_repos = _get_test_data('test_repos.json')
test_branches = _get_test_data('test_branches.json')
mock_standardized_project = MagicMock()
mock_standardized_projects = [mock_standardized_project]
self.mock_client.get_all_repos.return_value = test_repos
self.mock_client.get_branches.return_value = test_branches
# Act
resulting_repos = self.adapter.get_repos(mock_standardized_projects)
# Assert
self.assertEqual(
len(resulting_repos),
len(test_repos),
f"Resulting repos should be a list of size {len(test_repos)}",
)
resulting_repo = resulting_repos[0]
input_repo = test_repos[0]
self.assertEqual(
resulting_repo.id, input_repo['uuid'], "Resulting repo id should be the UUID"
)
self.assertEqual(
resulting_repo.name, input_repo['name'], "Resulting repo name does not match input"
)
self.assertEqual(
resulting_repo.full_name,
input_repo['full_name'],
"Resulting repo full_name does not match input",
)
self.assertEqual(
resulting_repo.url,
input_repo['links']['self']['href'],
"Resulting repo url does not match input",
)
self.assertFalse(resulting_repo.is_fork)
self.assertEqual(
resulting_repo.default_branch_name,
input_repo['mainbranch']['name'],
"Resulting repo default_branch_name does not match input",
)
self.assertEqual(
resulting_repo.project,
mock_standardized_project,
"Resulting repo project does not match input",
)
self.assertEqual(
len(resulting_repo.branches),
len(test_branches),
f"Resulting repo should have {len(test_branches)} branch",
)
resulting_branch = resulting_repo.branches[0]
input_branch = test_branches[0]
self.assertEqual(
resulting_branch.name,
input_branch['name'],
"Resulting branch name does not match input",
)
self.assertEqual(
resulting_branch.sha,
input_branch['target']['hash'],
"Resulting branch name does not match input",
)
def test_get_branch_commits(self):
# Arrange
test_commits = _get_test_data('test_commits.json')
mock_standardized_repo = MagicMock()
mock_standardized_repo.default_branch_name = 'default_branch_name'
test_short_repo = StandardizedShortRepository(
id='test_id', name='test_name', url='test_url'
)
mock_standardized_repo.short.return_value = test_short_repo
mock_standardized_repos = [mock_standardized_repo]
self.mock_client.get_commits.return_value = test_commits
# Set pull_from to very far in the past to ensure fake timestamps in test commits are after this date.
test_git_instance_info = {'pull_from': '1900-07-23', 'repos_dict_v2': {}}
# Act
resulting_commits = list(
self.adapter.get_commits_for_included_branches(
mock_standardized_repos, {'test_repo': ['test_branch_name']}, test_git_instance_info
)
)
# Assert
self.assertEqual(
len(resulting_commits),
len(test_commits),
f"Resulting commits should be a list of size {len(test_commits)}",
)
resulting_commit = resulting_commits[0]
input_commit = test_commits[0]
self.assertEqual(
resulting_commit.hash,
input_commit['hash'],
"Resulting commit hash does not match input",
)
self.assertEqual(
resulting_commit.author.name,
input_commit['author']['user']['display_name'],
"Resulting commit author name does not match input",
)
self.assertEqual(
resulting_commit.url,
input_commit['links']['html']['href'],
"Resulting commit url does not match input",
)
self.assertEqual(
resulting_commit.commit_date,
parser.parse(input_commit['date']),
"Resulting commit date does not match input",
)
self.assertEqual(
resulting_commit.message,
input_commit['message'],
"Resulting commit message does not match input",
)
self.assertEqual(
resulting_commit.repo.id,
test_short_repo.id,
"Resulting commit's repo id does not match input",
)
self.assertEqual(
resulting_commit.repo.name,
test_short_repo.name,
"Resulting commit's repo name does not match input",
)
self.assertEqual(
resulting_commit.repo.url,
test_short_repo.url,
"Resulting commit's repo url does not match input",
)
self.assertFalse(resulting_commit.is_merge)
self.assertIsNone(resulting_commit.author_date)
def test_get_prs(self):
# Arrange
test_prs = _get_test_data('test_prs.json')
test_commits = _get_test_data('test_commits.json')
mock_standardized_repo = MagicMock()
mock_standardized_repo.default_branch_name = 'default_branch_name'
mock_standardized_repos = [mock_standardized_repo]
self.mock_client.get_pullrequests.return_value = test_prs
self.mock_client.pr_diff.return_value = ""
self.mock_client.pr_comments.return_value = []
self.mock_client.pr_activity.return_value = []
self.mock_client.pr_commits.return_value = test_commits
self.mock_client.get_commit.return_value = test_commits[0]
# Set pull_from to very far in the past to ensure fake timestamps in test commits are after this date.
test_git_instance_info = {'pull_from': '1900-07-23', 'repos_dict_v2': {}}
# Act
resulting_prs = list(
self.adapter.get_pull_requests(mock_standardized_repos, test_git_instance_info)
)
# Assert
self.assertEqual(
len(resulting_prs),
len(test_prs),
f"Resulting prs should be a list of size {len(test_prs)}",
)
resulting_pr = resulting_prs[0]
input_pr = test_prs[0]
self.assertEqual(resulting_pr.id, input_pr['id'], "Resulting pr id does not match input")
self.assertEqual(
resulting_pr.title, input_pr['title'], "Resulting pr title does not match input"
)
self.assertEqual(
resulting_pr.body,
input_pr['description'],
"Resulting pr description does not match input",
)
self.assertEqual(
resulting_pr.url,
input_pr['links']['html']['href'],
"Resulting pr url does not match input",
)
self.assertEqual(
resulting_pr.base_branch,
input_pr['destination']['branch']['name'],
"Resulting pr base branch does not match input",
)
self.assertEqual(
resulting_pr.head_branch,
input_pr['source']['branch']['name'],
"Resulting pr head branch does not match input",
)
self.assertEqual(
resulting_pr.author.name,
input_pr['author']['display_name'],
"Resulting pr author name does not match input",
)
self.assertEqual(
len(resulting_pr.commits),
len(test_commits),
f"Resulting pr should have {len(test_commits)} commit",
)
self.assertEqual(
resulting_pr.commits[0].hash,
test_commits[0]['hash'],
"Resulting pr should have hash matching input commit",
)
self.assertFalse(resulting_pr.is_closed)
self.assertFalse(resulting_pr.is_merged)
def test_get_prs_merge_commit_uses_destination_repo(self):
# Arrange: a MERGED PR where source repo uuid differs from destination repo slug.
# This verifies the fix for the bug where get_commit was called with the source
# repo uuid instead of the destination repo id.
test_commits = _get_test_data('test_commits.json')
dest_slug = 'destination_repo_slug'
source_uuid = '{aaaaaaaa-0000-0000-0000-bbbbbbbbbbbb}'
merged_pr = {
'id': 99,
'title': 'Merged PR',
'description': '',
'state': 'MERGED',
'merge_commit': {'hash': 'abc123merge'},
'created_on': '2020-01-01T00:00:00+00:00',
'updated_on': '2020-01-02T00:00:00+00:00',
'author': {
'display_name': 'test_user',
'uuid': '{1cd06601-cd0e-4fce-be03-e9ac226978b7}',
'links': {'html': {'href': 'https://bitbucket.org/test_user/'}},
},
'source': {
'branch': {'name': 'feature'},
'repository': {
'full_name': 'some-fork/destination_repo_slug',
'name': 'fork_repo',
'type': 'repository',
'uuid': source_uuid,
'links': {
'self': {
'href': 'https://api.bitbucket.org/2.0/repositories/some-fork/fork'
}
},
},
},
'destination': {
'branch': {'name': 'master'},
'repository': {
'full_name': f'test_project/{dest_slug}',
'name': dest_slug,
'type': 'repository',
'uuid': '{cccccccc-0000-0000-0000-dddddddddddd}',
'links': {
'self': {
'href': f'https://api.bitbucket.org/2.0/repositories/test_project/{dest_slug}'
}
},
},
},
'links': {'html': {'href': 'https://bitbucket.org/test_project/pull-requests/99'}},
}
mock_standardized_repo = MagicMock()
mock_standardized_repo.id = '{cccccccc-0000-0000-0000-dddddddddddd}'
mock_standardized_repo.full_name = f'test_project/{dest_slug}'
mock_standardized_repo.project.id = 'test_project'
mock_standardized_repos = [mock_standardized_repo]
self.mock_client.get_pullrequests.return_value = [merged_pr]
self.mock_client.pr_diff.return_value = ""
self.mock_client.pr_comments.return_value = []
self.mock_client.pr_activity.return_value = []
self.mock_client.pr_commits.return_value = test_commits
self.mock_client.get_commit.return_value = test_commits[0]
test_git_instance_info = {'pull_from': '1900-07-23', 'repos_dict_v2': {}}
# Call the adapter method
list(self.adapter.get_pull_requests(mock_standardized_repos, test_git_instance_info))
# Assert: get_commit must be called with the destination repo slug, not the source uuid
self.mock_client.get_commit.assert_called_once_with(
'test_project', dest_slug, 'abc123merge'
)
def test_get_prs_merge_commit_404_does_not_skip_pr(self) -> None:
# Regression test for OJ-54730: when Bitbucket Cloud has garbage-collected a PR's
# merge commit, get_commit raises HTTPError(404). The PR must still be returned,
# just with merge_commit left unset.
test_commits = _get_test_data('test_commits.json')
dest_slug = 'destination_repo_slug'
merged_pr = {
'id': 2394,
'title': 'Merged PR with stale merge commit',
'description': '',
'state': 'MERGED',
'merge_commit': {'hash': 'stalemergehash'},
'created_on': '2020-01-01T00:00:00+00:00',
'updated_on': '2020-01-02T00:00:00+00:00',
'author': {
'display_name': 'test_user',
'uuid': '{1cd06601-cd0e-4fce-be03-e9ac226978b7}',
'links': {'html': {'href': 'https://bitbucket.org/test_user/'}},
},
'source': {
'branch': {'name': 'feature'},
'repository': {
'full_name': f'test_project/{dest_slug}',
'name': dest_slug,
'type': 'repository',
'uuid': '{cccccccc-0000-0000-0000-dddddddddddd}',
'links': {
'self': {
'href': f'https://api.bitbucket.org/2.0/repositories/test_project/{dest_slug}'
}
},
},
},
'destination': {
'branch': {'name': 'master'},
'repository': {
'full_name': f'test_project/{dest_slug}',
'name': dest_slug,
'type': 'repository',
'uuid': '{cccccccc-0000-0000-0000-dddddddddddd}',
'links': {
'self': {
'href': f'https://api.bitbucket.org/2.0/repositories/test_project/{dest_slug}'
}
},
},
},
'links': {'html': {'href': 'https://bitbucket.org/test_project/pull-requests/2394'}},
}
mock_standardized_repo = MagicMock()
mock_standardized_repo.id = '{cccccccc-0000-0000-0000-dddddddddddd}'
mock_standardized_repo.full_name = f'test_project/{dest_slug}'
mock_standardized_repo.project.id = 'test_project'
mock_standardized_repos = [mock_standardized_repo]
mock_response = MagicMock()
mock_response.status_code = 404
http_error = requests.exceptions.HTTPError(
'404 Client Error: Not Found', response=mock_response
)
self.mock_client.get_pullrequests.return_value = [merged_pr]
self.mock_client.pr_diff.return_value = ""
self.mock_client.pr_comments.return_value = []
self.mock_client.pr_activity.return_value = []
self.mock_client.pr_commits.return_value = test_commits
self.mock_client.get_commit.side_effect = http_error
test_git_instance_info = {'pull_from': '1900-07-23', 'repos_dict_v2': {}}
resulting_prs = list(
self.adapter.get_pull_requests(mock_standardized_repos, test_git_instance_info)
)
self.assertEqual(
len(resulting_prs), 1, "PR must still be ingested despite 404 on merge commit"
)
resulting_pr = resulting_prs[0]
self.assertEqual(resulting_pr.id, 2394)
self.assertTrue(resulting_pr.is_merged)
self.assertIsNone(
resulting_pr.merge_commit,
"merge_commit should be None when Bitbucket returns 404 for the merge commit hash",
)
def _get_test_data(file_name):
with open(f'{TEST_INPUT_FILE_PATH}{file_name}', 'r') as f:
return json.loads(f.read())
if __name__ == "__main__":
unittest.main()