Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
ThijsBroersen committed Oct 25, 2024
1 parent 562f690 commit 97f3900
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 90 deletions.
28 changes: 14 additions & 14 deletions zio-sbt-ci/src/main/scala/zio/sbt/ZioSbtCiPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ object ZioSbtCiPlugin extends AutoPlugin {
val checkWebsiteBuildProcess = ciCheckWebsiteBuildProcess.value

Seq(
"build" -> JobValue(
Job(
name = "Build",
continueOnError = true,
steps = {
Expand All @@ -127,7 +127,7 @@ object ZioSbtCiPlugin extends AutoPlugin {
val lint = Lint.value

Seq(
"lint" -> JobValue(
Job(
name = "Lint",
steps = (if (swapSizeGB > 0) Seq(setSwapSpace) else Seq.empty) ++
Seq(checkout, SetupLibuv, SetupJava(javaVersion), CacheDependencies) ++ checkGithubWorkflow.flatMap(
Expand Down Expand Up @@ -156,7 +156,7 @@ object ZioSbtCiPlugin extends AutoPlugin {
versions.contains(scalaVersion)
}.map(e => e._1 + "/test").mkString(" ")}"

"test" -> JobValue(
Job(
name = "Test",
strategy = Some(
Strategy(
Expand Down Expand Up @@ -218,7 +218,7 @@ object ZioSbtCiPlugin extends AutoPlugin {
}

val FlattenTests =
"test" -> JobValue(
Job(
name = "Test",
strategy = Some(
Strategy(
Expand Down Expand Up @@ -290,7 +290,7 @@ object ZioSbtCiPlugin extends AutoPlugin {
)

val DefaultTestStrategy =
"test" -> JobValue(
Job(
name = "Test",
strategy = Some(
Strategy(
Expand Down Expand Up @@ -322,7 +322,7 @@ object ZioSbtCiPlugin extends AutoPlugin {
val pullRequestApprovalJobs = ciPullRequestApprovalJobs.value

Seq(
"ci" -> JobValue(
Job(
name = "ci",
needs = Some(pullRequestApprovalJobs),
steps = Seq(
Expand All @@ -344,7 +344,7 @@ object ZioSbtCiPlugin extends AutoPlugin {
val generateReadme = GenerateReadme.value

Seq(
"update-readme" -> JobValue(
Job(
name = "Update README",
`if` = updateReadmeCondition orElse Some(Condition.Expression("github.event_name == 'push'")),
steps = (if (swapSizeGB > 0) Seq(setSwapSpace) else Seq.empty) ++
Expand Down Expand Up @@ -429,7 +429,7 @@ object ZioSbtCiPlugin extends AutoPlugin {
val jobs = ciReleaseApprovalJobs.value

Seq(
"release" -> JobValue(
Job(
name = "Release",
needs = Some(jobs),
`if` = Some(Condition.Expression("github.event_name != 'pull_request'")),
Expand All @@ -453,7 +453,7 @@ object ZioSbtCiPlugin extends AutoPlugin {
val publishToNpmRegistry = PublishToNpmRegistry.value

Seq(
"release-docs" -> JobValue(
Job(
name = "Release Docs",
needs = Some(Seq("release")),
`if` = Some(
Expand All @@ -472,7 +472,7 @@ object ZioSbtCiPlugin extends AutoPlugin {
publishToNpmRegistry
)
),
"notify-docs-release" -> JobValue(
Job(
name = "Notify Docs Release",
needs = Some(Seq("release-docs")),
`if` = Some(
Expand Down Expand Up @@ -542,10 +542,10 @@ object ZioSbtCiPlugin extends AutoPlugin {
pullRequest = Some(Trigger.PullRequest(branchesIgnore = Some(Seq(Branch.Named("gh-pages")))))
)
),
jobs = ListMap.empty[
String,
JobValue
] ++ buildJobs ++ lintJobs ++ testJobs ++ updateReadmeJobs ++ reportSuccessful ++ releaseJobs ++ postReleaseJobs
jobs = ListMap(
(buildJobs ++ lintJobs ++ testJobs ++ updateReadmeJobs ++ reportSuccessful ++ releaseJobs ++ postReleaseJobs)
.map(job => job.id -> job): _*
)
)

val yaml: String = workflow.toJsonAST.flatMap(_.toYaml(yamlOptions).left.map(_.getMessage())) match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,8 @@ case class Job(

object Job {
implicit class JobOps(private val job: Job) extends AnyVal {
def toNative: ghnative.Job = (
job.id,
ghnative.JobValue(
def toNative: ghnative.Job =
ghnative.Job(
name = job.name,
runsOn = job.runsOn,
timeoutMinutes = Some(job.timeoutMinutes),
Expand All @@ -294,7 +293,7 @@ object Job {
services = Some(job.services.map(_.toNative)).filter(_.nonEmpty),
`if` = job.condition.map(_.toNative)
)
)

}
}

Expand Down Expand Up @@ -335,7 +334,7 @@ object Workflow {
)
).filter(_ => triggers.nonEmpty)
},
jobs = ListMap(workflow.jobs.map(_.toNative): _*)
jobs = ListMap(workflow.jobs.map(_.toNative).map(job => job.id -> job): _*)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ object ScalaWorkflow {
operatingSystems: Seq[OS] = Seq(OS.UbuntuLatest),
javaVersions: Seq[JavaVersion] = Seq(JDK11)
): Job =
job._1 -> job._2.copy(
job.copy(
strategy = Some(
Strategy(
matrix = Map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ object Service {
}

@jsonMemberNames(KebabCase)
case class JobValue(
case class Job(
name: String,
runsOn: String = "ubuntu-latest",
timeoutMinutes: Option[Int] = None,
Expand All @@ -283,22 +283,43 @@ case class JobValue(
`if`: Option[Condition] = None,
steps: Seq[Step.SingleStep] = Seq.empty
) {
def withStrategy(strategy: Strategy): JobValue =

def id: String = name.toLowerCase().replace(" ", "-")

def withStrategy(strategy: Strategy): Job =
copy(strategy = Some(strategy))

def withSteps(steps: Step*): JobValue = steps match {
def withSteps(steps: Step*): Job = steps match {
case steps: Step.StepSequence =>
copy(steps = steps.flatten)
case step: Step.SingleStep =>
copy(steps = step :: Nil)
}

def withServices(services: Service*): JobValue =
def withServices(services: Service*): Job =
copy(services = Some(services))

def withRunsOn(runsOn: String): Job =
copy(runsOn = runsOn)

def withName(name: String): Job =
copy(name = name)

def withTimeoutMinutes(timeoutMinutes: Option[Int]): Job =
copy(timeoutMinutes = timeoutMinutes)

def withContinueOnError(continueOnError: Boolean): Job =
copy(continueOnError = continueOnError)

def withStrategy(strategy: Option[Strategy]): Job =
copy(strategy = strategy)

def withNeeds(needs: Option[Seq[String]]): Job =
copy(needs = needs)
}

object JobValue {
implicit val codec: JsonCodec[JobValue] = DeriveJsonCodec.gen[JobValue]
object Job {
implicit val codec: JsonCodec[Job] = DeriveJsonCodec.gen[Job]
}

@jsonMemberNames(KebabCase)
Expand All @@ -318,18 +339,18 @@ case class Workflow(
concurrency: Concurrency = Concurrency(
"${{ github.workflow }}-${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch) && github.run_id || github.ref }}"
),
jobs: ListMap[String, JobValue] = ListMap.empty
jobs: ListMap[String, Job] = ListMap.empty
) {
def withOn(on: Triggers): Workflow =
copy(on = Some(on))

def withJobs(jobs: (String, JobValue)*): Workflow =
def withJobs(jobs: (String, Job)*): Workflow =
copy(jobs = ListMap(jobs: _*))

def addJob(job: (String, JobValue)): Workflow =
def addJob(job: (String, Job)): Workflow =
copy(jobs = jobs + job)

def addJobs(newJobs: (String, JobValue)*): Workflow =
def addJobs(newJobs: (String, Job)*): Workflow =
copy(jobs = jobs ++ newJobs)
}

Expand Down

This file was deleted.

0 comments on commit 97f3900

Please sign in to comment.