Skip to content
This repository has been archived by the owner on Apr 22, 2020. It is now read-only.

Commit

Permalink
Fixes commit files method (#1271)
Browse files Browse the repository at this point in the history
* Fixes commit files method

* Fixes failing tests

Co-authored-by: franciscodr <francisco.d@47deg.com>
  • Loading branch information
fedefernandez and franciscodr authored Mar 19, 2020
1 parent f6e4e7b commit 13f2528
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 115 deletions.
28 changes: 20 additions & 8 deletions core/src/main/scala/sbtorgpolicies/github/GitHubOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class GitHubOps[F[_]: ConcurrentEffect: Timer](
path <- EitherT.right(Sync[F].delay(file.getAbsolutePath()))
content <- EitherT(Sync[F].delay(fileReader.getFileContent(path)))
relativePath <- EitherT.right(relativePath(file))
} yield (content, relativePath)
} yield (relativePath, content)
}

readFileContents
Expand All @@ -105,10 +105,11 @@ class GitHubOps[F[_]: ConcurrentEffect: Timer](
def fetchFileContents(
path: String,
commitSha: String
): EitherT[F, GitHubException, (String, Option[String])] =
run(
): EitherT[F, GitHubException, (String, Option[String])] = {
runOption(
gh.repos.getContents(owner = owner, repo = repo, path = path, ref = Some(commitSha))
).map(res => res.map(content => path -> content.content).head)
).map(res => path -> res.flatMap(_.head.content))
}

filesAndContents.map(_._1).traverse(fetchFileContents(_, commitSha))
}
Expand Down Expand Up @@ -369,8 +370,9 @@ class GitHubOps[F[_]: ConcurrentEffect: Timer](
refs.find(_.ref == s"refs/heads/$branch") match {
case Some(ref) => EitherT.rightT(ref)
case None =>
val e = UnexpectedException(s"Branch $branch not found")
EitherT.leftT(GitHubException(s"GitHub returned an error: ${e.getMessage}", Some(e)))
EitherT.leftT(
GitHubException(s"GitHub returned an error: Branch $branch not found", None)
)
}

run(gh.gitData.getReference(owner, repo, s"heads/$branch")).flatMap(findReference)
Expand All @@ -381,8 +383,18 @@ class GitHubOps[F[_]: ConcurrentEffect: Timer](

def run[A](f: F[GHResponse[A]]): EitherT[F, GitHubException, A] = EitherT(
Sync[F].attempt(f).map {
case Right(Right(r)) => Right(r.result)
case Right(Left(e)) =>
case Right(GHResponse(Right(r), _, _)) => Right(r)
case Right(GHResponse(Left(e), _, _)) =>
Left(GitHubException(s"GitHub returned an error: ${e.getMessage}", Some(e)))
case Left(e) => Left(GitHubException("Error making request to GitHub", Some(e)))
}
)

def runOption[A](f: F[GHResponse[A]]): EitherT[F, GitHubException, Option[A]] = EitherT(
Sync[F].attempt(f).map {
case Right(GHResponse(Right(r), _, _)) => r.some.asRight[GitHubException]
case Right(GHResponse(Left(e), 404, _)) => none[A].asRight[GitHubException]
case Right(GHResponse(Left(e), _, _)) =>
Left(GitHubException(s"GitHub returned an error: ${e.getMessage}", Some(e)))
case Left(e) => Left(GitHubException("Error making request to GitHub", Some(e)))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,12 @@ trait GitHubArbitraries {

def genGHResponse[T](gen: Gen[T]): Gen[GHResponse[T]] =
Gen.oneOf(
genGHException.map(_.asLeft[GHResult[T]]),
gen.map(v => GHResult(v, 200, Map.empty).asRight[GHException])
genGHException.map(e => GHResponse(e.asLeft[T], 500, Map.empty)),
gen.map(v => GHResponse(v.asRight[GHException], 200, Map.empty))
)

val genGHException: Gen[GHException] = {
val message = "Generated Exception"
Gen.oneOf(JsonParsingException(message, """{"val": "value"}"""), UnexpectedException(message))
}
val genGHException: Gen[GHException] =
Gen.const(JsonParsingException("Generated exception", """{"val": "value"}"""))

val genEmail: Gen[String] =
for {
Expand Down Expand Up @@ -109,6 +107,12 @@ trait GitHubArbitraries {
list2 <- Gen.listOfN(n, genGHResponse(genFullUser))
} yield (list1, list2)

val genRefInfo: Gen[RefInfo] =
for {
sha <- Gen.identifier
url <- genURL
} yield RefInfo(sha, url)

val genRefObject: Gen[RefObject] =
for {
refObjectType <- Gen.oneOf("commit", "tag")
Expand Down Expand Up @@ -233,8 +237,8 @@ trait GitHubArbitraries {
url <- genURL
refAuthor <- genRefAuthor
message <- Gen.alphaStr.filter(_ != nonExistingMessage)
tree <- genRefObject
parents <- Gen.listOf(genRefObject)
tree <- genRefInfo
parents <- Gen.listOf(genRefInfo)
} yield RefCommit(sha, url, refAuthor, refAuthor, message, tree, parents)

val genTreeDataResult: Gen[TreeDataResult] =
Expand Down Expand Up @@ -298,7 +302,7 @@ trait GitHubArbitraries {
}

implicit val ghResponseRefObjectArbitrary: Arbitrary[GHResponse[RefInfo]] = Arbitrary {
genGHResponse(genRefObject)
genGHResponse(genRefInfo)
}

}
Expand Down
Loading

0 comments on commit 13f2528

Please sign in to comment.