-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor dashboard reporter and providers
- Loading branch information
1 parent
8a9b407
commit 90a5e19
Showing
12 changed files
with
160 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package stryker4s.env | ||
|
||
trait Environment { | ||
def getEnvVariable(key: String): Option[String] | ||
} | ||
|
||
object SystemEnvironment extends Environment { | ||
override def getEnvVariable(key: String): Option[String] = sys.env.get(key) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
core/src/main/scala/stryker4s/report/dashboard/DashboardConfigProvider.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package stryker4s.report.dashboard | ||
import stryker4s.report.model.DashboardConfig | ||
import stryker4s.config.Config | ||
import stryker4s.report.dashboard.Providers._ | ||
import stryker4s.env.Environment | ||
|
||
class DashboardConfigProvider(env: Environment)(implicit config: Config) { | ||
def resolveConfig(): Either[String, DashboardConfig] = | ||
for { | ||
apiKey <- resolveapiKey() | ||
project <- resolveproject() | ||
version <- resolveversion() | ||
baseUrl = config.dashboard.baseUrl | ||
reportType = config.dashboard.reportType | ||
module = config.dashboard.module | ||
} yield DashboardConfig( | ||
apiKey = apiKey, | ||
project = project, | ||
version = version, | ||
baseUrl = baseUrl, | ||
reportType = reportType, | ||
module = module | ||
) | ||
|
||
private def resolveapiKey() = | ||
env | ||
.getEnvVariable("STRYKER_DASHBOARD_API_KEY") | ||
.toRight("STRYKER_DASHBOARD_API_KEY") | ||
|
||
private def resolveproject() = | ||
config.dashboard.project | ||
.orElse(byCiProvider(_.determineProject())) | ||
.toRight("dashboard.project") | ||
|
||
private def resolveversion() = | ||
config.dashboard.version | ||
.orElse(byCiProvider(_.determineVersion())) | ||
.toRight("dashboard.version") | ||
|
||
private def byCiProvider[T](f: CiProvider => Option[T])() = Providers.determineCiProvider(env).flatMap(f) | ||
} | ||
|
||
final case class DashboardConfigError(message: String) |
46 changes: 25 additions & 21 deletions
46
core/src/main/scala/stryker4s/report/dashboard/Providers.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,41 @@ | ||
package stryker4s.report.dashboard | ||
import grizzled.slf4j.Logging | ||
import stryker4s.env.Environment | ||
|
||
object Providers extends Logging { | ||
def determineCiProvider(env: Environment): Option[CiProvider] = | ||
if (env.getEnvVariable("TRAVIS").isDefined) { | ||
Some(new TravisProvider(env)) | ||
} else if (env.getEnvVariable("CIRCLECI").isDefined) { | ||
Some(new CircleProvider(env)) | ||
} else { | ||
None | ||
} | ||
|
||
trait CiProvider { | ||
def isPullRequest: Boolean | ||
def determineProject(): Option[String] | ||
def determineVersion(): Option[String] | ||
def determineApiKey(): Option[String] = readEnvironmentVariableOrLog("STRYKER_DASHBOARD_API_KEY") | ||
|
||
protected def readEnvironmentVariableOrLog(name: String): Option[String] = { | ||
val environmentOption = sys.env.get(name).filter(_.nonEmpty) | ||
if (environmentOption.isEmpty) { | ||
warn( | ||
s"Missing environment variable $name, not initializing ${this.getClass.getSimpleName} for dashboard reporter." | ||
) | ||
} | ||
environmentOption | ||
} | ||
} | ||
|
||
object TravisProvider extends CiProvider { | ||
override def isPullRequest: Boolean = !readEnvironmentVariableOrLog("TRAVIS_PULL_REQUEST").forall(_ == "false") | ||
override def determineProject(): Option[String] = readEnvironmentVariableOrLog("TRAVIS_REPO_SLUG") | ||
override def determineVersion(): Option[String] = readEnvironmentVariableOrLog("TRAVIS_BRANCH") | ||
private def readEnvironmentVariable(name: String, env: Environment): Option[String] = | ||
env.getEnvVariable(name).filter(_.nonEmpty) | ||
|
||
class TravisProvider(env: Environment) extends CiProvider { | ||
override def determineProject(): Option[String] = | ||
readEnvironmentVariable("TRAVIS_REPO_SLUG", env) | ||
|
||
override def determineVersion(): Option[String] = | ||
readEnvironmentVariable("TRAVIS_BRANCH", env) | ||
} | ||
|
||
object CircleProvider extends CiProvider { | ||
override def isPullRequest: Boolean = !readEnvironmentVariableOrLog("CIRCLE_PULL_REQUEST").forall(_ == "false") | ||
class CircleProvider(env: Environment) extends CiProvider { | ||
override def determineProject(): Option[String] = | ||
for { | ||
username <- readEnvironmentVariableOrLog("CIRCLE_PROJECT_USERNAME") | ||
repoName <- readEnvironmentVariableOrLog("CIRCLE_PROJECT_REPONAME") | ||
username <- readEnvironmentVariable("CIRCLE_PROJECT_USERNAME", env) | ||
repoName <- readEnvironmentVariable("CIRCLE_PROJECT_REPONAME", env) | ||
} yield s"$username/$repoName" | ||
override def determineVersion(): Option[String] = readEnvironmentVariableOrLog("CIRCLE_BRANCH") | ||
|
||
override def determineVersion(): Option[String] = | ||
readEnvironmentVariable("CIRCLE_BRANCH", env) | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
core/src/main/scala/stryker4s/report/model/DashboardConfig.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package stryker4s.report.model | ||
|
||
import stryker4s.config.DashboardReportType | ||
|
||
case class DashboardConfig( | ||
apiKey: String, | ||
baseUrl: String, | ||
reportType: DashboardReportType, | ||
project: String, | ||
version: String, | ||
module: Option[String] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package stryker4s.report.model | ||
|
||
case class DashboardPutResult(href: String) |
3 changes: 3 additions & 0 deletions
3
core/src/main/scala/stryker4s/report/model/ScoreOnlyReport.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package stryker4s.report.model | ||
|
||
case class ScoreOnlyReport(mutationScore: Double) |
22 changes: 0 additions & 22 deletions
22
core/src/main/scala/stryker4s/report/model/StrykerDashboardReport.scala
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters