-
Notifications
You must be signed in to change notification settings - Fork 475
/
github_spec.rb
666 lines (523 loc) · 27.8 KB
/
github_spec.rb
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
# coding: utf-8
require "danger/request_sources/github/github"
require "danger/ci_source/circle"
require "danger/ci_source/travis"
require "danger/danger_core/messages/violation"
RSpec.describe Danger::RequestSources::GitHub, host: :github do
describe "#inspect" do
it "masks access_token and bearer_token on inspect" do
gh_env = { "DANGER_GITHUB_API_TOKEN" => "hi", "DANGER_GITHUB_BEARER_TOKEN" => "ho" }
inspected = Danger::RequestSources::GitHub.new(stub_ci, gh_env).inspect
expect(inspected).to include(%(@access_token="********"))
expect(inspected).to include(%(@bearer_token="********"))
end
end
describe "the github host" do
it "sets a default GitHub host" do
gh_env = { "DANGER_GITHUB_API_TOKEN" => "hi" }
result = Danger::RequestSources::GitHub.new(stub_ci, gh_env).host
expect(result).to eq("github.com")
end
it "allows the GitHub host to be overridden" do
gh_env = { "DANGER_GITHUB_API_TOKEN" => "hi", "DANGER_GITHUB_HOST" => "git.club-mateusa.com" }
result = Danger::RequestSources::GitHub.new(stub_ci, gh_env).host
expect(result).to eq("git.club-mateusa.com")
end
describe "#api_url" do
it "allows the GitHub API host to be overridden with `DANGER_GITHUB_API_BASE_URL`" do
api_endpoint = "https://git.club-mateusa.com/api/v3/"
gh_env = { "DANGER_GITHUB_API_TOKEN" => "hi", "DANGER_GITHUB_API_BASE_URL" => api_endpoint }
result = Danger::RequestSources::GitHub.new(stub_ci, gh_env).api_url
expect(result).to eq api_endpoint
end
it "allows the GitHub API host to be overridden with `DANGER_GITHUB_API_HOST` for backwards compatibility" do
api_endpoint = "https://git.club-mateusa.com/api/v4/"
gh_env = { "DANGER_GITHUB_API_TOKEN" => "hi", "DANGER_GITHUB_API_HOST" => api_endpoint }
result = Danger::RequestSources::GitHub.new(stub_ci, gh_env).api_url
expect(result).to eq api_endpoint
end
end
end
describe "bearer token" do
context "with DANGER_GITHUB_BEARER_TOKEN" do
let(:client) { double('client') }
it 'creates Octokit client with bearer token' do
allow(Octokit::Client).to receive(:new).and_return(client)
gh_env = { "DANGER_GITHUB_BEARER_TOKEN" => "hi" }
expect(Octokit::Client).to receive(:new).with(hash_including(:bearer_token))
Danger::RequestSources::GitHub.new(stub_ci, gh_env).client
end
end
end
describe "ssl verification" do
it "sets ssl verification environment variable to false" do
gh_env = { "DANGER_GITHUB_API_TOKEN" => "hi", "DANGER_GITHUB_HOST" => "git.club-mateusa.com", "DANGER_OCTOKIT_VERIFY_SSL" => "false" }
result = Danger::RequestSources::GitHub.new(stub_ci, gh_env).verify_ssl
expect(result).to be_falsey
end
it "sets ssl verification environment variable to true" do
gh_env = { "DANGER_GITHUB_API_TOKEN" => "hi", "DANGER_GITHUB_HOST" => "git.club-mateusa.com", "DANGER_OCTOKIT_VERIFY_SSL" => "true" }
result = Danger::RequestSources::GitHub.new(stub_ci, gh_env).verify_ssl
expect(result).to be_truthy
end
it "sets ssl verification environment variable to wrong input" do
gh_env = { "DANGER_GITHUB_API_TOKEN" => "hi", "DANGER_GITHUB_HOST" => "git.club-mateusa.com", "DANGER_OCTOKIT_VERIFY_SSL" => "wronginput" }
result = Danger::RequestSources::GitHub.new(stub_ci, gh_env).verify_ssl
expect(result).to be_truthy
end
it "unsets ssl verification environment variable" do
gh_env = { "DANGER_GITHUB_API_TOKEN" => "hi", "DANGER_GITHUB_HOST" => "git.club-mateusa.com" }
result = Danger::RequestSources::GitHub.new(stub_ci, gh_env).verify_ssl
expect(result).to be_truthy
end
end
describe "valid server response" do
before do
gh_env = { "DANGER_GITHUB_API_TOKEN" => "hi" }
@g = Danger::RequestSources::GitHub.new(stub_ci, gh_env)
pr_response = JSON.parse(fixture("github_api/pr_response"))
allow(@g.client).to receive(:pull_request).with("artsy/eigen", "800").and_return(pr_response)
issue_response = JSON.parse(fixture("github_api/issue_response"))
allow(@g.client).to receive(:get).with("https://api.github.com/repos/artsy/eigen/issues/800").and_return(issue_response)
end
it "sets its pr_json" do
@g.fetch_details
expect(@g.pr_json).to be_truthy
end
it "sets its issue_json" do
@g.fetch_details
expect(@g.issue_json).to be_truthy
end
it "raises an exception when the repo was moved from the git remote" do
allow(@g.client).to receive(:pull_request).with("artsy/eigen", "800").and_return({ "message" => "Moved Permanently" })
expect do
@g.fetch_details
end.to raise_error("Repo moved or renamed, make sure to update the git remote".red)
end
it "sets the ignored violations" do
@g.fetch_details
expect(@g.ignored_violations).to eq(
[
"Developer Specific file shouldn't be changed",
"Some warning"
]
)
end
describe "#organisation" do
it "valid value available" do
@g.fetch_details
expect(@g.organisation).to eq("artsy")
end
it "no valid value available doesn't crash" do
@g.issue_json = nil
expect(@g.organisation).to eq(nil)
end
end
describe "#file_url" do
before do
contents_response = JSON.parse(fixture("github_api/contents_response"))
allow(@g.client).to receive(:contents).with("artsy/danger", path: "Dangerfile", ref: nil).and_return(contents_response)
allow(@g.client).to receive(:contents).with("artsy/danger", path: "path/Dangerfile", ref: "master").and_return(contents_response)
allow(@g.client).to receive(:contents).with("teapot/danger", path: "Dangerfile", ref: nil).and_raise(Octokit::NotFound)
end
it "returns a valid URL with the minimum parameters" do
url = @g.file_url(organisation: "artsy", repository: "danger", path: "Dangerfile")
expect(url).to eq("https://raw.githubusercontent.com/artsy/danger/master/path/Dangerfile")
end
it "returns a valid URL with more parameters" do
url = @g.file_url(repository: "danger", organisation: "artsy", branch: "master", path: "path/Dangerfile")
expect(url).to eq("https://raw.githubusercontent.com/artsy/danger/master/path/Dangerfile")
end
it "returns a valid fallback URL" do
url = @g.file_url(repository: "danger", organisation: "teapot", path: "Dangerfile")
expect(url).to eq("https://raw.githubusercontent.com/teapot/danger/master/Dangerfile")
end
end
describe "review" do
context "when already asked for review" do
before do
allow(@g.client).to receive(:pull_request_reviews).with("artsy/eigen", "800").and_return([])
@created_review = @g.review
end
it "returns the same review" do
expect(@g.review).to eq(@created_review)
end
end
context "when ask for review first time" do
context "when there are no danger review for PR" do
before do
allow(@g.client).to receive(:pull_request_reviews).with("artsy/eigen", "800").and_return([])
end
it "returns a newly created review" do
@review = @g.review
expect(@review.review_json).to be_nil
end
end
context "when there are danger review for PR" do
before do
pr_reviews_response = JSON.parse(fixture("github_api/pr_reviews_response"))
allow(@g.client).to receive(:pull_request_reviews).with("artsy/eigen", "800").and_return(pr_reviews_response)
end
it "returns the last review from danger" do
@review = @g.review
expect(@review.review_json).to_not be_nil
expect(@review.id).to eq(16_237_194)
end
end
end
context "when running against github enterprise which doesn't support reviews" do
it "returns an unsupported review instance" do
allow(@g.client).to receive(:pull_request_reviews).with("artsy/eigen", "800").and_raise(Octokit::NotFound)
review = @g.review
expect(review).to respond_to(
:id, :body, :status, :review_json, :start, :submit, :message, :warn,
:fail, :markdown
)
end
end
end
describe "status message" do
it "Shows a success message when no errors/warnings" do
message = @g.generate_description(warnings: [], errors: [])
expect(message).to start_with("All green.")
end
it "Shows an error messages when there are errors" do
message = @g.generate_description(warnings: violations_factory([1, 2, 3]), errors: [])
expect(message).to eq("⚠️ 3 Warnings. Don't worry, everything is fixable.")
end
it "Shows an error message when errors and warnings" do
message = @g.generate_description(warnings: violations_factory([1, 2]), errors: violations_factory([1, 2, 3]))
expect(message).to eq("⚠️ 3 Errors. 2 Warnings. Don't worry, everything is fixable.")
end
it "Deals with singualars in messages when errors and warnings" do
message = @g.generate_description(warnings: violations_factory([1]), errors: violations_factory([1]))
expect(message).to eq("⚠️ 1 Error. 1 Warning. Don't worry, everything is fixable.")
end
end
describe "commit status update" do
before do
stub_request(:post, "https://git.club-mateusa.com/api/v3/repos/artsy/eigen/statuses/").to_return status: 200
end
it "fails when no head commit is set" do
@g.pr_json = { "base" => { "sha" => "" }, "head" => { "sha" => "" } }
expect do
@g.submit_pull_request_status!
end.to raise_error("Couldn't find a commit to update its status".red)
end
it "uses danger_id as context of status" do
options = hash_including(context: "danger/special_context")
expect(@g.client).to receive(:create_status).with(any_args, options).and_return({})
@g.pr_json = { "head" => { "sha" => "pr_commit_ref" } }
@g.submit_pull_request_status!(danger_id: "special_context")
end
it "aborts when access to setting the status was denied but there were errors" do
stub_request(:post, "https://api.github.com/repos/artsy/eigen/statuses/pr_commit_ref").to_return(status: 404)
@g.pr_json = { "head" => { "sha" => "pr_commit_ref" }, "base" => { "repo" => { "private" => true } } }
expect do
@g.submit_pull_request_status!(errors: violations_factory(["error"]))
end.to raise_error.and output(/Danger has failed this build/).to_stderr
end
it "warns when access to setting the status was denied but no errors were reported" do
stub_request(:post, "https://api.github.com/repos/artsy/eigen/statuses/pr_commit_ref").to_return(status: 404)
@g.pr_json = { "head" => { "sha" => "pr_commit_ref" }, "base" => { "repo" => { "private" => true } } }
expect do
@g.submit_pull_request_status!(warnings: violations_factory(["error"]))
end.to output(/warning.*not have write access/im).to_stdout
end
end
describe "issue creation" do
before do
@g.pr_json = { "base" => { "sha" => "" }, "head" => { "sha" => "" } }
allow(@g).to receive(:submit_pull_request_status!).and_return(true)
end
it "creates a comment if no danger comments exist" do
comments = []
allow(@g.client).to receive(:issue_comments).with("artsy/eigen", "800").and_return(comments)
body = @g.generate_comment(warnings: violations_factory(["hi"]), errors: [], messages: [])
expect(@g.client).to receive(:add_comment).with("artsy/eigen", "800", body).and_return({})
@g.update_pull_request!(warnings: violations_factory(["hi"]), errors: [], messages: [])
end
it "updates the issue if no danger comments exist" do
comments = [{ "body" => '"generated_by_danger"', "id" => "12" }]
allow(@g.client).to receive(:issue_comments).with("artsy/eigen", "800").and_return(comments)
body = @g.generate_comment(warnings: violations_factory(["hi"]), errors: [], messages: [])
expect(@g.client).to receive(:update_comment).with("artsy/eigen", "12", body).and_return({})
@g.update_pull_request!(warnings: violations_factory(["hi"]), errors: [], messages: [])
end
it "creates a new comment instead of updating the issue if --new-comment is provided" do
comments = [{ "body" => '"generated_by_danger"', "id" => "12" }]
allow(@g.client).to receive(:issue_comments).with("artsy/eigen", "800").and_return(comments)
body = @g.generate_comment(warnings: violations_factory(["hi"]), errors: [], messages: [])
expect(@g.client).to receive(:add_comment).with("artsy/eigen", "800", body).and_return({})
@g.update_pull_request!(warnings: violations_factory(["hi"]), errors: [], messages: [], new_comment: true)
end
it "updates the issue if no danger comments exist and a custom danger_id is provided" do
comments = [{ "body" => '"generated_by_another_danger"', "id" => "12" }]
allow(@g.client).to receive(:issue_comments).with("artsy/eigen", "800").and_return(comments)
body = @g.generate_comment(warnings: violations_factory(["hi"]), errors: [], messages: [], danger_id: "another_danger")
expect(@g.client).to receive(:update_comment).with("artsy/eigen", "12", body).and_return({})
@g.update_pull_request!(warnings: violations_factory(["hi"]), errors: [], messages: [], danger_id: "another_danger")
end
it "deletes existing comments if danger doesnt need to say anything" do
comments = [{ "body" => '"generated_by_danger"', "id" => "12" }]
allow(@g.client).to receive(:issue_comments).with("artsy/eigen", "800").and_return(comments)
allow(@g.client).to receive(:delete_comment).with("artsy/eigen", main_issue_id)
expect(@g.client).to receive(:delete_comment).with("artsy/eigen", "12").and_return({})
@g.update_pull_request!(warnings: [], errors: [], messages: [])
end
it "deletes existing comments if danger doesnt need to say anything and a custom danger_id is provided" do
comments = [{ "body" => '"generated_by_another_danger"', "id" => "12" }]
allow(@g.client).to receive(:issue_comments).with("artsy/eigen", "800").and_return(comments)
expect(@g.client).to receive(:delete_comment).with("artsy/eigen", "12").and_return({})
allow(@g.client).to receive(:delete_comment).with("artsy/eigen", main_issue_id)
@g.update_pull_request!(warnings: [], errors: [], messages: [], danger_id: "another_danger")
end
# it "updates the comment if danger doesnt need to say anything but there are sticky violations" do
# comments = [{ body: "generated_by_danger", id: "12" }]
# allow(@g).to receive(:parse_comment).and_return({ errors: ["an error"] })
# allow(@g.client).to receive(:issue_comments).with("artsy/eigen", "800").and_return(comments)
# expect(@g.client).to receive(:update_comment).with("artsy/eigen", "12", any_args).and_return({})
# @g.update_pull_request!(warnings: [], errors: [], messages: [])
# end
end
describe "#parse_message_from_row" do
it "handles pulling out links that include the file / line when in the main Danger comment" do
body = '<a href="https://github.com/artsy/eigen/blob/8e5d0bab431839a7046b2f7d5cd5ccb91677fe23/CHANGELOG.md#L1">CHANGELOG.md#L1</a> - Testing inline docs'
v = @g.parse_message_from_row(body)
expect(v.file).to eq("CHANGELOG.md")
expect(v.line).to eq(1)
expect(v.message).to include("- Testing inline docs")
end
it "handles pulling out file info from an inline Danger comment" do
body = '<span data-href="https://github.com/artsy/eigen/blob/8e5d0bab431839a7046b2f7d5cd5ccb91677fe23/CHANGELOG.md#L1"/>Testing inline docs'
v = @g.parse_message_from_row(body)
expect(v.file).to eq("CHANGELOG.md")
expect(v.line).to eq(1)
expect(v.message).to include("Testing inline docs")
end
end
let(:main_issue_id) { 76_535_362 }
let(:inline_issue_id_1) { 76_537_315 }
let(:inline_issue_id_2) { 76_537_316 }
describe "inline issues" do
before do
issues = JSON.parse(fixture("github_api/inline_comments"))
allow(@g.client).to receive(:issue_comments).with("artsy/eigen", "800").and_return(issues)
diff = diff_fixture("github_api/inline_comments_pr_diff")
allow(@g.client).to receive(:pull_request).with("artsy/eigen", "800", { accept: "application/vnd.github.v3.diff" }).and_return(diff)
@g.fetch_details
allow(@g).to receive(:submit_pull_request_status!)
end
it "deletes all inline comments if there are no violations at all" do
allow(@g.client).to receive(:delete_comment).with("artsy/eigen", main_issue_id)
allow(@g.client).to receive(:delete_comment).with("artsy/eigen", inline_issue_id_1)
allow(@g.client).to receive(:delete_comment).with("artsy/eigen", inline_issue_id_2)
allow(@g).to receive(:submit_pull_request_status!)
@g.update_pull_request!(warnings: [], errors: [], messages: [])
end
it "adds new comments inline" do
allow(@g.client).to receive(:pull_request_comments).with("artsy/eigen", "800").and_return([])
expect(@g.client).to receive(:create_pull_request_comment).with("artsy/eigen", "800", anything, "561827e46167077b5e53515b4b7349b8ae04610b", "CHANGELOG.md", 4)
expect(@g.client).to receive(:delete_comment).with("artsy/eigen", inline_issue_id_1).and_return({})
expect(@g.client).to receive(:delete_comment).with("artsy/eigen", inline_issue_id_2).and_return({})
expect(@g.client).to receive(:delete_comment).with("artsy/eigen", main_issue_id).and_return({})
v = Danger::Violation.new("Sure thing", true, "CHANGELOG.md", 4)
@g.update_pull_request!(warnings: [], errors: [], messages: [v])
end
it "correctly handles trailing tabs in diff" do
allow(@g.client).to receive(:pull_request_comments).with("artsy/eigen", "800").and_return([])
expect(@g.client).to receive(:create_pull_request_comment).with("artsy/eigen", "800", anything, "561827e46167077b5e53515b4b7349b8ae04610b", "ios/Example/App Delegates/README.md", 4)
expect(@g.client).to receive(:delete_comment).with("artsy/eigen", inline_issue_id_1).and_return({})
expect(@g.client).to receive(:delete_comment).with("artsy/eigen", inline_issue_id_2).and_return({})
expect(@g.client).to receive(:delete_comment).with("artsy/eigen", main_issue_id).and_return({})
v = Danger::Violation.new("Sure thing", true, "ios/Example/App Delegates/README.md", 4)
@g.update_pull_request!(warnings: [], errors: [], messages: [v])
end
it "adds main comment when inline out of range" do
allow(@g.client).to receive(:pull_request_comments).with("artsy/eigen", "800").and_return([])
allow(@g.client).to receive(:issue_comments).with("artsy/eigen", "800").and_return([])
v = Danger::Violation.new("Sure thing", true, "CHANGELOG.md", 10)
body = @g.generate_comment(warnings: [], errors: [], messages: [v])
expect(@g.client).not_to receive(:create_pull_request_comment).with("artsy/eigen", "800", anything, "561827e46167077b5e53515b4b7349b8ae04610b", "CHANGELOG.md", 10)
expect(@g.client).to receive(:add_comment).with("artsy/eigen", "800", body).and_return({})
@g.update_pull_request!(warnings: [], errors: [], messages: [v])
end
it "ingores out of range inline comments when in dismiss mode" do
allow(@g.client).to receive(:pull_request_comments).with("artsy/eigen", "800").and_return([])
expect(@g.client).not_to receive(:create_pull_request_comment).with("artsy/eigen", "800", anything, "561827e46167077b5e53515b4b7349b8ae04610b", "CHANGELOG.md", 10)
expect(@g.client).not_to receive(:add_comment).with("artsy/eigen", "800", anything)
expect(@g.client).to receive(:delete_comment).with("artsy/eigen", inline_issue_id_1).and_return({})
expect(@g.client).to receive(:delete_comment).with("artsy/eigen", inline_issue_id_2).and_return({})
expect(@g.client).to receive(:delete_comment).with("artsy/eigen", main_issue_id).and_return({})
v = Danger::Violation.new("Sure thing", true, "CHANGELOG.md", 10)
@g.dismiss_out_of_range_messages = true
@g.update_pull_request!(warnings: [], errors: [], messages: [v])
end
it "ingores out of range inline comments when in dismiss mode per kind" do
allow(@g.client).to receive(:pull_request_comments).with("artsy/eigen", "800").and_return([])
expect(@g.client).to receive(:create_pull_request_comment).with("artsy/eigen", "800", anything, "561827e46167077b5e53515b4b7349b8ae04610b", "CHANGELOG.md", 4)
expect(@g.client).to receive(:delete_comment).with("artsy/eigen", inline_issue_id_1).and_return({})
expect(@g.client).to receive(:delete_comment).with("artsy/eigen", inline_issue_id_2).and_return({})
expect(@g.client).to receive(:delete_comment).with("artsy/eigen", main_issue_id).and_return({})
v = Danger::Violation.new("Sure thing", true, "CHANGELOG.md", 4)
m = Danger::Markdown.new("Sure thing", "CHANGELOG.md", 4)
@g.dismiss_out_of_range_messages = {
warning: false,
error: false,
message: false,
markdown: true
}
@g.update_pull_request!(warnings: [], errors: [], messages: [v], markdowns: [m])
end
it "crosses out sticky comments" do
allow(@g.client).to receive(:pull_request_comments).with("artsy/eigen", "800").and_return([])
expect(@g.client).to receive(:create_pull_request_comment).with("artsy/eigen", "800", anything, "561827e46167077b5e53515b4b7349b8ae04610b", "CHANGELOG.md", 4)
expect(@g.client).to receive(:delete_comment).with("artsy/eigen", inline_issue_id_1).and_return({})
expect(@g.client).to receive(:delete_comment).with("artsy/eigen", inline_issue_id_2).and_return({})
expect(@g.client).to receive(:delete_comment).with("artsy/eigen", main_issue_id).and_return({})
m = Danger::Markdown.new("Sure thing", "CHANGELOG.md", 4)
@g.update_pull_request!(warnings: [], errors: [], messages: [], markdowns: [m])
end
it "removes inline comments if they are not included" do
issues = [{ "body" => "'generated_by_another_danger'", "id" => "12" }]
allow(@g.client).to receive(:pull_request_comments).with("artsy/eigen", "800").and_return(issues)
allow(@g.client).to receive(:create_pull_request_comment).with("artsy/eigen", "800", anything, "561827e46167077b5e53515b4b7349b8ae04610b", "CHANGELOG.md", 4)
# Main
allow(@g.client).to receive(:delete_comment).with("artsy/eigen", main_issue_id)
# Inline Issues
allow(@g.client).to receive(:delete_comment).with("artsy/eigen", inline_issue_id_1)
allow(@g.client).to receive(:delete_comment).with("artsy/eigen", inline_issue_id_2)
allow(@g).to receive(:submit_pull_request_status!)
v = Danger::Violation.new("Sure thing", true, "CHANGELOG.md", 4)
@g.update_pull_request!(warnings: [], errors: [], messages: [v])
end
it "keeps initial messages order" do
allow(@g.client).to receive(:pull_request_comments).with("artsy/eigen", "800").and_return([])
allow(@g.client).to receive(:issue_comments).with("artsy/eigen", "800").and_return([])
allow(@g.client).to receive(:add_comment).and_return({})
allow(@g).to receive(:submit_pull_request_status!).and_return(true)
allow(@g.client).to receive(:create_pull_request_comment).and_return({})
allow(@g.client).to receive(:delete_comment).and_return(true)
allow(@g.client).to receive(:delete_comment).and_return(true)
messages = [
Danger::Violation.new("1", false, nil, nil),
Danger::Violation.new("2", false, nil, nil),
Danger::Violation.new("3", false, nil, nil),
Danger::Violation.new("4", false, nil, nil),
Danger::Violation.new("5", false, nil, nil),
Danger::Violation.new("6", false, nil, nil),
Danger::Violation.new("7", false, nil, nil),
Danger::Violation.new("8", false, nil, nil),
Danger::Violation.new("9", false, nil, nil),
Danger::Violation.new("10", false, nil, nil)
]
expect(@g).to receive(:generate_comment).with(
template: "github",
danger_id: "danger",
previous_violations: {},
warnings: [],
errors: [],
messages: messages,
markdowns: []
)
@g.update_pull_request!(messages: messages)
end
end
describe "branch setup" do
it "setups the danger branches" do
@g.fetch_details
expect(@g.scm).to receive(:exec)
.with("rev-parse --quiet --verify 704dc55988c6996f69b6873c2424be7d1de67bbe^{commit}")
.and_return("345e74fabb2fecea93091e8925b1a7a208b48ba6")
expect(@g.scm).to receive(:exec)
.with("branch danger_base 704dc55988c6996f69b6873c2424be7d1de67bbe")
expect(@g.scm).to receive(:exec)
.with("rev-parse --quiet --verify 561827e46167077b5e53515b4b7349b8ae04610b^{commit}")
.and_return("561827e46167077b5e53515b4b7349b8ae04610b")
expect(@g.scm).to receive(:exec)
.with("branch danger_head 561827e46167077b5e53515b4b7349b8ae04610b")
@g.setup_danger_branches
end
it "fetches when the branches are not in the local store" do
# not in history
expect(@g.scm).to receive(:exec).
with("rev-parse --quiet --verify 704dc55988c6996f69b6873c2424be7d1de67bbe^{commit}").
and_return("")
[20, 74, 222, 625].each do |depth|
# fetch it
expect(@g.scm).to receive(:exec).with("fetch --depth=#{depth} --prune origin +refs/heads/master:refs/remotes/origin/master")
# still not in history
expect(@g.scm).to receive(:exec).
with("rev-parse --quiet --verify 704dc55988c6996f69b6873c2424be7d1de67bbe^{commit}").
and_return("")
end
# fetch it
expect(@g.scm).to receive(:exec).with("fetch --depth 1000000")
# still not in history
expect(@g.scm).to receive(:exec).
with("rev-parse --quiet --verify 704dc55988c6996f69b6873c2424be7d1de67bbe^{commit}").
and_return("")
expect do
@g.fetch_details
@g.setup_danger_branches
end.to raise_error(RuntimeError, /Commit (\w+|\b[0-9a-f]{5,40}\b) doesn't exist/)
end
end
end
describe "#find_position_in_diff" do
subject do
github.find_position_in_diff(diff_lines, message, kind)
end
let(:diff_lines) do
<<-DIFF.each_line.to_a
diff --git a/#{file_path} b/#{file_path}
index 0000000..0000001 100644
--- a/#{file_path}
+++ b/#{file_path}
@@ -1 +1,2 @@
-foo
+bar
+baz
DIFF
end
let(:file_path) do
"dummy"
end
let(:github) do
described_class.new(stub_ci, "DANGER_GITHUB_API_TOKEN" => "hi")
end
let(:kind) do
:dummy
end
let(:message) do
double(
file: file_path,
line: 1,
)
end
context "when the lines count for the original file is omitted" do
it "returns correct position" do
is_expected.to eq(2)
end
end
context "when diff contain `No newline` annotation before added lines" do
let(:diff_lines) do
<<-DIFF.each_line.to_a
diff --git a/#{file_path} b/#{file_path}
index 0000000..0000001 100644
--- a/#{file_path}
+++ b/#{file_path}
@@ -1 +1,3 @@
-foo
\\ No newline at end of file
+foo
+bar
+baz
DIFF
end
it "returns correct position" do
is_expected.to eq(3)
end
end
end
end