Skip to content

Commit 5aef08c

Browse files
Add implementation for updatePullRequest for GitHub forge
1 parent 40f1735 commit 5aef08c

File tree

3 files changed

+228
-4
lines changed

3 files changed

+228
-4
lines changed

modules/core/src/main/scala/org/scalasteward/core/forge/github/GitHubApiAlg.scala

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,24 @@ final class GitHubApiAlg[F[_]](
6969
number: PullRequestNumber,
7070
repo: Repo,
7171
data: NewPullRequestData
72-
): F[PullRequestOut] =
73-
F.raiseError(new NotImplementedError(s"updatePullRequest($number, $repo, $data)"))
72+
): F[PullRequestOut] = {
73+
val payload = UpdatePullRequestPayload.from(data)
74+
75+
val update = client
76+
.patchWithBody[PullRequestOut, UpdatePullRequestPayload](
77+
uri = url.pull(repo, number),
78+
body = payload,
79+
modify = modify(repo)
80+
)
81+
.adaptErr(SecondaryRateLimitExceeded.fromThrowable)
82+
83+
for {
84+
pullRequestOut <- update
85+
_ <- F.whenA(data.labels.nonEmpty)(labelPullRequest(repo, number, data.labels))
86+
_ <- F.whenA(data.assignees.nonEmpty)(addAssignees(repo, number, data.assignees))
87+
_ <- F.whenA(data.reviewers.nonEmpty)(addReviewers(repo, number, data.reviewers))
88+
} yield pullRequestOut
89+
}
7490

7591
/** https://developer.github.com/v3/repos/branches/#get-branch */
7692
override def getBranch(repo: Repo, branch: Branch): F[BranchOut] =
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2018-2023 Scala Steward contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.scalasteward.core.forge.github
18+
19+
import io.circe.Encoder
20+
import io.circe.generic.semiauto.deriveEncoder
21+
import org.scalasteward.core.forge.data.NewPullRequestData
22+
23+
final case class UpdatePullRequestPayload(
24+
title: String,
25+
body: String
26+
)
27+
object UpdatePullRequestPayload {
28+
implicit val encoder: Encoder[UpdatePullRequestPayload] = deriveEncoder
29+
30+
def from(newPullRequestData: NewPullRequestData): UpdatePullRequestPayload =
31+
UpdatePullRequestPayload(
32+
title = newPullRequestData.title,
33+
body = newPullRequestData.body
34+
)
35+
}

modules/core/src/test/scala/org/scalasteward/core/forge/github/GitHubApiAlgTest.scala

Lines changed: 175 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package org.scalasteward.core.forge.github
22

3-
import cats.syntax.semigroupk._
3+
import cats.effect.IO
4+
import cats.syntax.all._
45
import io.circe.literal._
6+
import io.circe.Json
57
import munit.CatsEffectSuite
68
import org.http4s.circe._
79
import 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

Comments
 (0)