1
1
package org .scalasteward .core .forge .github
2
2
3
- import cats .syntax .semigroupk ._
3
+ import cats .effect .IO
4
+ import cats .syntax .all ._
4
5
import io .circe .literal ._
6
+ import io .circe .Json
5
7
import munit .CatsEffectSuite
6
8
import org .http4s .circe ._
7
9
import org .http4s .dsl .Http4sDsl
@@ -66,7 +68,19 @@ class GitHubApiAlgTest extends CatsEffectSuite with Http4sDsl[MockEff] {
66
68
}] """
67
69
)
68
70
69
- case PATCH -> Root / " repos" / " fthomas" / " base.g8" / " pulls" / IntVar (_) =>
71
+ case req @ PATCH -> Root / " repos" / " fthomas" / " base.g8" / " pulls" / IntVar (42 ) =>
72
+ req.as[Json ].flatMapF(_.hcursor.get[String ](" title" ).liftTo[IO ]).flatMap { title =>
73
+ Ok (
74
+ json """ {
75
+ "html_url": "https://github.com/octocat/Hello-World/pull/42",
76
+ "state": "open",
77
+ "number": 42,
78
+ "title": $title
79
+ } """
80
+ )
81
+ }
82
+
83
+ case PATCH -> Root / " repos" / " fthomas" / " base.g8" / " pulls" / IntVar (1347 ) =>
70
84
Ok (
71
85
json """ {
72
86
"html_url": "https://github.com/octocat/Hello-World/pull/1347",
@@ -115,6 +129,14 @@ class GitHubApiAlgTest extends CatsEffectSuite with Http4sDsl[MockEff] {
115
129
"title": "new-feature"
116
130
} """ )
117
131
132
+ case PATCH -> Root / " repos" / " fthomas" / " cant-assign-reviewers" / " pulls" / " 42" =>
133
+ Created (json """ {
134
+ "html_url": "https://github.com/fthomas/cant-assign-reviewers/pull/42",
135
+ "state": "open",
136
+ "number": 42,
137
+ "title": "updated-title"
138
+ } """ )
139
+
118
140
case POST -> Root / " repos" / " fthomas" / " cant-add-labels" / " pulls" =>
119
141
Created (json """ {
120
142
"html_url": "https://github.com/octocat/Hello-World/pull/13",
@@ -123,6 +145,14 @@ class GitHubApiAlgTest extends CatsEffectSuite with Http4sDsl[MockEff] {
123
145
"title": "can't add labels to me"
124
146
} """ )
125
147
148
+ case PATCH -> Root / " repos" / " fthomas" / " cant-add-labels" / " pulls" / " 42" =>
149
+ Created (json """ {
150
+ "html_url": "https://github.com/fthomas/cant-add-labels/pull/42",
151
+ "state": "open",
152
+ "number": 42,
153
+ "title": "can't add labels to me"
154
+ } """ )
155
+
126
156
case POST -> Root / " repos" / " fthomas" / " base.g8" / " issues" / IntVar (_) / " labels" =>
127
157
// Response taken from https://docs.github.com/en/rest/reference/issues#labels, is ignored
128
158
Created (json """ [
@@ -136,6 +166,9 @@ class GitHubApiAlgTest extends CatsEffectSuite with Http4sDsl[MockEff] {
136
166
"default": true
137
167
}] """ )
138
168
169
+ case POST -> Root / " repos" / " fthomas" / " cant-add-labels" / " issues" / " 42" / " labels" =>
170
+ BadRequest (" can't add labels" )
171
+
139
172
case POST -> Root / " repos" / " fthomas" / " cant-add-labels" / " issues" / " 13" / " labels" =>
140
173
BadRequest (" can't add labels" )
141
174
@@ -155,6 +188,11 @@ class GitHubApiAlgTest extends CatsEffectSuite with Http4sDsl[MockEff] {
155
188
/ " pulls" / IntVar (_) / " requested_reviewers" =>
156
189
BadRequest ()
157
190
191
+ case PATCH ->
192
+ Root / " repos" / " fthomas" / " cant-assign-reviewers"
193
+ / " pulls" / IntVar (_) / " requested_reviewers" =>
194
+ BadRequest ()
195
+
158
196
case _ => NotFound ()
159
197
}
160
198
private val state = MockState .empty.copy(clientResponses = auth <+> httpApp)
@@ -284,6 +322,31 @@ class GitHubApiAlgTest extends CatsEffectSuite with Http4sDsl[MockEff] {
284
322
assertIO(pr, pullRequest)
285
323
}
286
324
325
+ test(" updatePullRequest" ) {
326
+ val data = NewPullRequestData (
327
+ title = " updated-title" ,
328
+ body = " body" ,
329
+ head = " aaa" ,
330
+ base = Branch (" master" ),
331
+ labels = Nil ,
332
+ assignees = Nil ,
333
+ reviewers = Nil
334
+ )
335
+
336
+ val number = PullRequestNumber (42 )
337
+
338
+ val pr = gitHubApiAlg.updatePullRequest(number, repo, data).runA(state)
339
+
340
+ val expected = PullRequestOut (
341
+ uri " https://github.com/octocat/Hello-World/pull/42 " ,
342
+ PullRequestState .Open ,
343
+ number,
344
+ " updated-title"
345
+ )
346
+
347
+ assertIO(pr, expected)
348
+ }
349
+
287
350
test(" createPullRequest with assignees and reviewers" ) {
288
351
val data = NewPullRequestData (
289
352
title = " new-feature" ,
@@ -298,6 +361,31 @@ class GitHubApiAlgTest extends CatsEffectSuite with Http4sDsl[MockEff] {
298
361
assertIO(pr, pullRequest)
299
362
}
300
363
364
+ test(" updatePullRequest with assignees and reviewers" ) {
365
+ val data = NewPullRequestData (
366
+ title = " updated-title" ,
367
+ body = " body" ,
368
+ head = " aaa" ,
369
+ base = Branch (" master" ),
370
+ labels = Nil ,
371
+ assignees = List (" foo" ),
372
+ reviewers = List (" bar" )
373
+ )
374
+
375
+ val number = PullRequestNumber (42 )
376
+
377
+ val pr = gitHubApiAlg.updatePullRequest(number, repo, data).runA(state)
378
+
379
+ val expected = PullRequestOut (
380
+ uri " https://github.com/octocat/Hello-World/pull/42 " ,
381
+ PullRequestState .Open ,
382
+ number,
383
+ " updated-title"
384
+ )
385
+
386
+ assertIO(pr, expected)
387
+ }
388
+
301
389
test(" createPullRequest with assignees and reviewers should not fail if can't assign" ) {
302
390
val data = NewPullRequestData (
303
391
title = " new-feature" ,
@@ -321,6 +409,34 @@ class GitHubApiAlgTest extends CatsEffectSuite with Http4sDsl[MockEff] {
321
409
assertIO(pullRequestOut, expectedPullRequestOut)
322
410
}
323
411
412
+ test(" updatePullRequest with assignees and reviewers should not fail if can't assign" ) {
413
+ val data = NewPullRequestData (
414
+ title = " updated-title" ,
415
+ body = " body" ,
416
+ head = " aaa" ,
417
+ base = Branch (" master" ),
418
+ labels = Nil ,
419
+ assignees = List (" foo" ),
420
+ reviewers = List (" bar" )
421
+ )
422
+
423
+ val number = PullRequestNumber (42 )
424
+
425
+ val pr =
426
+ gitHubApiAlg
427
+ .updatePullRequest(number, repo.copy(repo = " cant-assign-reviewers" ), data)
428
+ .runA(state)
429
+
430
+ val expected = PullRequestOut (
431
+ uri " https://github.com/fthomas/cant-assign-reviewers/pull/42 " ,
432
+ PullRequestState .Open ,
433
+ number,
434
+ " updated-title"
435
+ )
436
+
437
+ assertIO(pr, expected)
438
+ }
439
+
324
440
test(" createPullRequest with labels" ) {
325
441
val data = NewPullRequestData (
326
442
title = " new-feature" ,
@@ -335,6 +451,31 @@ class GitHubApiAlgTest extends CatsEffectSuite with Http4sDsl[MockEff] {
335
451
assertIO(pr, pullRequest)
336
452
}
337
453
454
+ test(" updatePullRequest with labels" ) {
455
+ val data = NewPullRequestData (
456
+ title = " updated-title" ,
457
+ body = " body" ,
458
+ head = " aaa" ,
459
+ base = Branch (" master" ),
460
+ labels = List (" foo" , " bar" ),
461
+ assignees = Nil ,
462
+ reviewers = Nil
463
+ )
464
+
465
+ val number = PullRequestNumber (42 )
466
+
467
+ val pr = gitHubApiAlg.updatePullRequest(number, repo, data).runA(state)
468
+
469
+ val expected = PullRequestOut (
470
+ uri " https://github.com/octocat/Hello-World/pull/42 " ,
471
+ PullRequestState .Open ,
472
+ number,
473
+ " updated-title"
474
+ )
475
+
476
+ assertIO(pr, expected)
477
+ }
478
+
338
479
test(" createPullRequest should fail when can't add labels" ) {
339
480
val data = NewPullRequestData (
340
481
title = " new-feature" ,
@@ -362,6 +503,38 @@ class GitHubApiAlgTest extends CatsEffectSuite with Http4sDsl[MockEff] {
362
503
assertIO(error, Right (expectedError))
363
504
}
364
505
506
+ test(" updatePullRequest should fail when can't add labels" ) {
507
+ val data = NewPullRequestData (
508
+ title = " updated-title" ,
509
+ body = " body" ,
510
+ head = " aaa" ,
511
+ base = Branch (" master" ),
512
+ labels = List (" foo" , " bar" ),
513
+ assignees = Nil ,
514
+ reviewers = Nil
515
+ )
516
+
517
+ val number = PullRequestNumber (42 )
518
+
519
+ val error =
520
+ gitHubApiAlg
521
+ .updatePullRequest(number, repo.copy(repo = " cant-add-labels" ), data)
522
+ .runA(state)
523
+ .attempt
524
+ .map(_.swap.map(_.getMessage))
525
+
526
+ val expectedError =
527
+ """ |uri: http://example.com/repos/fthomas/cant-add-labels/issues/42/labels
528
+ |method: POST
529
+ |status: 400 Bad Request
530
+ |headers:
531
+ | Content-Type: text/plain; charset=UTF-8
532
+ | Content-Length: 16
533
+ |body: can't add labels""" .stripMargin
534
+
535
+ assertIO(error, Right (expectedError))
536
+ }
537
+
365
538
test(" listPullRequests" ) {
366
539
val prs = gitHubApiAlg.listPullRequests(repo, " master" , Branch (" master" )).runA(state)
367
540
assertIO(prs, List (pullRequest))
0 commit comments