11package org .scalasteward .core .forge .github
22
3- import cats .syntax .semigroupk ._
3+ import cats .effect .IO
4+ import cats .syntax .all ._
45import io .circe .literal ._
6+ import io .circe .Json
57import munit .CatsEffectSuite
68import org .http4s .circe ._
79import org .http4s .dsl .Http4sDsl
@@ -66,7 +68,19 @@ class GitHubApiAlgTest extends CatsEffectSuite with Http4sDsl[MockEff] {
6668 }] """
6769 )
6870
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 ) =>
7084 Ok (
7185 json """ {
7286 "html_url": "https://github.com/octocat/Hello-World/pull/1347",
@@ -115,6 +129,14 @@ class GitHubApiAlgTest extends CatsEffectSuite with Http4sDsl[MockEff] {
115129 "title": "new-feature"
116130 } """ )
117131
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+
118140 case POST -> Root / " repos" / " fthomas" / " cant-add-labels" / " pulls" =>
119141 Created (json """ {
120142 "html_url": "https://github.com/octocat/Hello-World/pull/13",
@@ -123,6 +145,14 @@ class GitHubApiAlgTest extends CatsEffectSuite with Http4sDsl[MockEff] {
123145 "title": "can't add labels to me"
124146 } """ )
125147
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+
126156 case POST -> Root / " repos" / " fthomas" / " base.g8" / " issues" / IntVar (_) / " labels" =>
127157 // Response taken from https://docs.github.com/en/rest/reference/issues#labels, is ignored
128158 Created (json """ [
@@ -136,6 +166,9 @@ class GitHubApiAlgTest extends CatsEffectSuite with Http4sDsl[MockEff] {
136166 "default": true
137167 }] """ )
138168
169+ case POST -> Root / " repos" / " fthomas" / " cant-add-labels" / " issues" / " 42" / " labels" =>
170+ BadRequest (" can't add labels" )
171+
139172 case POST -> Root / " repos" / " fthomas" / " cant-add-labels" / " issues" / " 13" / " labels" =>
140173 BadRequest (" can't add labels" )
141174
@@ -155,6 +188,11 @@ class GitHubApiAlgTest extends CatsEffectSuite with Http4sDsl[MockEff] {
155188 / " pulls" / IntVar (_) / " requested_reviewers" =>
156189 BadRequest ()
157190
191+ case PATCH ->
192+ Root / " repos" / " fthomas" / " cant-assign-reviewers"
193+ / " pulls" / IntVar (_) / " requested_reviewers" =>
194+ BadRequest ()
195+
158196 case _ => NotFound ()
159197 }
160198 private val state = MockState .empty.copy(clientResponses = auth <+> httpApp)
@@ -284,6 +322,31 @@ class GitHubApiAlgTest extends CatsEffectSuite with Http4sDsl[MockEff] {
284322 assertIO(pr, pullRequest)
285323 }
286324
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+
287350 test(" createPullRequest with assignees and reviewers" ) {
288351 val data = NewPullRequestData (
289352 title = " new-feature" ,
@@ -298,6 +361,31 @@ class GitHubApiAlgTest extends CatsEffectSuite with Http4sDsl[MockEff] {
298361 assertIO(pr, pullRequest)
299362 }
300363
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+
301389 test(" createPullRequest with assignees and reviewers should not fail if can't assign" ) {
302390 val data = NewPullRequestData (
303391 title = " new-feature" ,
@@ -321,6 +409,34 @@ class GitHubApiAlgTest extends CatsEffectSuite with Http4sDsl[MockEff] {
321409 assertIO(pullRequestOut, expectedPullRequestOut)
322410 }
323411
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+
324440 test(" createPullRequest with labels" ) {
325441 val data = NewPullRequestData (
326442 title = " new-feature" ,
@@ -335,6 +451,31 @@ class GitHubApiAlgTest extends CatsEffectSuite with Http4sDsl[MockEff] {
335451 assertIO(pr, pullRequest)
336452 }
337453
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+
338479 test(" createPullRequest should fail when can't add labels" ) {
339480 val data = NewPullRequestData (
340481 title = " new-feature" ,
@@ -362,6 +503,38 @@ class GitHubApiAlgTest extends CatsEffectSuite with Http4sDsl[MockEff] {
362503 assertIO(error, Right (expectedError))
363504 }
364505
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+
365538 test(" listPullRequests" ) {
366539 val prs = gitHubApiAlg.listPullRequests(repo, " master" , Branch (" master" )).runA(state)
367540 assertIO(prs, List (pullRequest))
0 commit comments