Skip to content
This repository has been archived by the owner on Jul 26, 2021. It is now read-only.

Commit

Permalink
Merge pull request #51 from guardian/make-github-label-setting-option…
Browse files Browse the repository at this point in the history
…al-and-fire-and-forget

Make Github label setting optional and fire-and-forget
  • Loading branch information
Mario Galic committed Sep 8, 2018
2 parents 95d71bc + a665a1b commit 7f4000f
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 24 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ How to verify the most important user journeys are not broken without writing a
val tipConfig = TipConfig(
owner = "guardian",
repo = "identity",
personalAccessToken = config.Tip.personalAccessToken, // set to empty string "" if you do not need GitHub label functionality
label = "Verified in PROD",
personalAccessToken = config.Tip.personalAccessToken, // remove if you do not need GitHub label functionality
label = "Verified in PROD", // remove if you do not need GitHub label functionality
boardSha = BuildInfo.GitHeadSha
)

Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/com/gu/tip/Configuration.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import scala.io.Source

case class TipConfig(owner: String,
repo: String,
personalAccessToken: String,
label: String,
personalAccessToken: String = "",
label: String = "",
boardSha: String = "")

class TipConfigurationException(
Expand Down
35 changes: 21 additions & 14 deletions src/main/scala/com/gu/tip/Tip.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ case object LabelSet extends TipResponse
case object TipFinished extends TipResponse
case class PathsActorResponse(msg: PathsActorMessage) extends TipResponse
case object CloudPathVerified extends TipResponse
case object AllTestsInProductionPassed extends TipResponse

// NOK
case object FailedToSetLabel extends TipResponse
Expand All @@ -42,22 +43,28 @@ trait Tip extends TipIf with LazyLogging {
system.registerOnTermination(
logger.info("Successfully terminated actor system"))

private def fireAndForgetSetLabelOnLatestMergedPr() =
if (configuration.tipConfig.personalAccessToken.nonEmpty) {
setLabelOnLatestMergedPr().run.attempt
.map({
case Left(error) =>
logger.error("Failed to set label on PR!", error)
FailedToSetLabel

case Right((logs, result)) =>
logs.foreach(log => logger.info(log.toString))
LabelSet
})
.unsafeRunSync()
}

private def inMemoryVerify(pathName: String): Future[TipResponse] = {
pathsActor ? Verify(pathName) map {
case AllPathsVerified =>
setLabelOnLatestMergedPr.run.attempt
.map({
case Left(error) =>
logger.error("Failed to set label on PR!", error)
FailedToSetLabel

case Right((logs, result)) =>
logs.foreach(log => logger.info(log.toString))
logger.info("Successfully verified all paths!")
pathsActor ? Stop
LabelSet
})
.unsafeRunSync()
fireAndForgetSetLabelOnLatestMergedPr()
logger.info("All tests in production passed.")
pathsActor ? Stop
AllTestsInProductionPassed

case PathDoesNotExist(pathname) =>
logger.error(s"Unrecognized path name: $pathname")
Expand All @@ -81,7 +88,7 @@ trait Tip extends TipIf with LazyLogging {
inMemoryResult: TipResponse): Future[TipResponse] =
Future {
inMemoryResult match {
case PathsActorResponse(PathIsVerified(_)) | LabelSet | FailedToSetLabel
case PathsActorResponse(PathIsVerified(_)) | AllTestsInProductionPassed
if configuration.cloudEnabled =>
verifyPath(configuration.tipConfig.boardSha, pathname).run.attempt
.map({
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/application.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
tip {
owner = "mario-galic"
repo = "sandbox"
personalAccessToken = ""
personalAccessToken = "testtoken"
label = "Verified in PROD"
}
10 changes: 5 additions & 5 deletions src/test/scala/com/gu/tip/TipTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class TipTest extends AsyncFlatSpec with MustMatchers {
} yield result mustBe UnclassifiedError
}

it should "handle failure to set the label" in {
it should "not affect verification result when setting of PR label fails" in {
trait MockNotifier extends NotifierIf { this: GitHubApiIf =>
override def setLabelOnLatestMergedPr: WriterT[IO, List[Log], String] =
WriterT(
Expand All @@ -114,7 +114,7 @@ class TipTest extends AsyncFlatSpec with MustMatchers {
for {
_ <- Tip.verify("Name A")
result <- Tip.verify("Name B")
} yield result mustBe FailedToSetLabel
} yield result mustBe AllTestsInProductionPassed
}

behavior of "happy Tip"
Expand All @@ -125,7 +125,7 @@ class TipTest extends AsyncFlatSpec with MustMatchers {
.map(_ mustBe PathsActorResponse(PathIsVerified("Name A")))
}

it should "set the label when all paths are verified" in {
it should "return AllTestsInProductionPassed when all paths have been verified" in {
trait MockNotifier extends NotifierIf { this: GitHubApiIf =>
override def setLabelOnLatestMergedPr(): WriterT[IO, List[Log], String] =
mockOkResponse
Expand All @@ -142,10 +142,10 @@ class TipTest extends AsyncFlatSpec with MustMatchers {
for {
_ <- Tip.verify("Name A")
result <- Tip.verify("Name B")
} yield result mustBe LabelSet
} yield result mustBe AllTestsInProductionPassed
}

it should "not set the label if the same path is verified multiple times concurrently (no race conditions)" in {
it should "not return AllTestsInProductionPassed if the same path is verified multiple times concurrently (no race conditions)" in {
trait MockNotifier extends NotifierIf { this: GitHubApiIf =>
override def setLabelOnLatestMergedPr: WriterT[IO, List[Log], String] =
mockOkResponse
Expand Down

0 comments on commit 7f4000f

Please sign in to comment.