Skip to content

Commit

Permalink
Paths for Push and PullRequest events
Browse files Browse the repository at this point in the history
  • Loading branch information
987Nabil committed Jun 8, 2021
1 parent 99c15d6 commit 40c53ac
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 28 deletions.
4 changes: 3 additions & 1 deletion src/main/scala/sbtghactions/GenerativePlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -645,11 +645,13 @@ ${indent(workflow.jobs.map(compileJob(_, sbt)).mkString("\n\n"), 1)}"""
List(
WebhookEvent.Push(
githubWorkflowTargetBranches.value.toList,
githubWorkflowTargetTags.value.toList
githubWorkflowTargetTags.value.toList,
Nil
),
WebhookEvent.PullRequest(
githubWorkflowTargetBranches.value.toList,
Nil,
Nil,
githubWorkflowPREventTypes.value.toList
)
),
Expand Down
20 changes: 13 additions & 7 deletions src/main/scala/sbtghactions/TriggerEvent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,19 @@ object WebhookEvent {

case object Public extends PlainNameEvent

final case class PullRequest(branches: Seq[String], tags: Seq[String], types: Seq[PREventType])
final case class PullRequest(
branches: Seq[String],
tags: Seq[String],
paths: Seq[String],
types: Seq[PREventType])
extends WebhookEvent {

override def render: String =
s"$name:" +
indentOnce { renderBranches(branches) + renderTags + renderTypes }
indentOnce { renderBranches(branches) + renderTags + renderPaths + renderTypes }

private def renderTags =
renderParamWithList("tags", tags)
private def renderTags = renderParamWithList("tags", tags)
private def renderPaths: String = if (tags.isEmpty) "" else renderParamWithList("paths", paths)

private def renderTypes =
if (types == PREventType.Defaults) ""
Expand All @@ -137,13 +141,15 @@ object WebhookEvent {

final case class PullRequestTarget(types: Seq[PRTargetEventType]) extends TypedEvent

final case class Push(branches: Seq[String], tags: Seq[String]) extends WebhookEvent {
final case class Push(branches: Seq[String], tags: Seq[String], paths: Seq[String])
extends WebhookEvent {

override def render: String =
s"$name:" +
indentOnce { renderBranches(branches) + renderTags }
indentOnce { renderBranches(branches) + renderTags + renderPaths }

def renderTags: String = if (tags.isEmpty) "" else renderParamWithList("tags", tags)
def renderTags: String = if (tags.isEmpty) "" else renderParamWithList("tags", tags)
def renderPaths: String = if (tags.isEmpty) "" else renderParamWithList("paths", paths)

}

Expand Down
27 changes: 14 additions & 13 deletions src/test/scala/sbtghactions/GenerativePluginSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ class GenerativePluginSpec extends Specification {
Workflow(
"test",
List(
WebhookEvent.PullRequest(List("main"), Nil, PREventType.Defaults),
WebhookEvent.Push(List("main"), Nil)
WebhookEvent.PullRequest(List("main"), Nil, Nil, PREventType.Defaults),
WebhookEvent.Push(List("main"), Nil, Nil)
),
Nil,
Map(),
Expand All @@ -76,8 +76,8 @@ class GenerativePluginSpec extends Specification {
Workflow(
"test",
List(
WebhookEvent.PullRequest(List("main"), Nil, PREventType.Defaults),
WebhookEvent.Push(List("main"), List("howdy"))
WebhookEvent.PullRequest(List("main"), Nil, Nil, PREventType.Defaults),
WebhookEvent.Push(List("main"), List("howdy"), Nil)
),
Nil,
Map(),
Expand Down Expand Up @@ -106,8 +106,9 @@ class GenerativePluginSpec extends Specification {
WebhookEvent.PullRequest(
List("main"),
Nil,
Nil,
List(PREventType.ReadyForReview, PREventType.ReviewRequested, PREventType.Opened)),
WebhookEvent.Push(List("main"), Nil)),
WebhookEvent.Push(List("main"), Nil, Nil)),
Nil,
Map(),
),
Expand Down Expand Up @@ -143,8 +144,8 @@ class GenerativePluginSpec extends Specification {
Workflow(
"test2",
List(
WebhookEvent.PullRequest(List("main", "backport/v*"), Nil, PREventType.Defaults),
WebhookEvent.Push(List("main", "backport/v*"), Nil)
WebhookEvent.PullRequest(List("main", "backport/v*"), Nil, Nil, PREventType.Defaults),
WebhookEvent.Push(List("main", "backport/v*"), Nil, Nil)
),
List(
WorkflowJob(
Expand Down Expand Up @@ -195,8 +196,8 @@ class GenerativePluginSpec extends Specification {
Workflow(
"test3",
List(
WebhookEvent.PullRequest(List("main"), Nil, PREventType.Defaults),
WebhookEvent.Push(List("main"), Nil)
WebhookEvent.PullRequest(List("main"), Nil, Nil, PREventType.Defaults),
WebhookEvent.Push(List("main"), Nil, Nil)
),
List(
WorkflowJob(
Expand Down Expand Up @@ -240,8 +241,8 @@ class GenerativePluginSpec extends Specification {
Workflow(
"test4",
List(
WebhookEvent.PullRequest(List("main"), Nil, PREventType.Defaults),
WebhookEvent.Push(List("main"), Nil)
WebhookEvent.PullRequest(List("main"), Nil, Nil, PREventType.Defaults),
WebhookEvent.Push(List("main"), Nil, Nil)
),
List(
WorkflowJob(
Expand Down Expand Up @@ -292,8 +293,8 @@ class GenerativePluginSpec extends Specification {
Workflow(
"test4",
List(
WebhookEvent.PullRequest(List("main"), Nil, PREventType.Defaults),
WebhookEvent.Push(List("main"), Nil)
WebhookEvent.PullRequest(List("main"), Nil, Nil, PREventType.Defaults),
WebhookEvent.Push(List("main"), Nil, Nil)
),
List(
WorkflowJob(
Expand Down
22 changes: 15 additions & 7 deletions src/test/scala/sbtghactions/TriggerEventSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -127,31 +127,39 @@ class TriggerEventSpec extends Specification with AllExpectations {
}

"push" should {
"render without branches, tags or types" in {
"render without branches, tags or paths" in {
val expected = "push:"
WebhookEvent.Push(Nil, Nil).render mustEqual expected
WebhookEvent.Push(Nil, Nil, Nil).render mustEqual expected
}

"render without branches, but with tags and types" in {
"render without branches, but with tags and paths" in {
val expected =
"""|push:
| tags: [v1*, v2*]""".stripMargin
WebhookEvent.Push(Nil, List("v1*", "v2*")).render mustEqual expected
| tags: [v1*, v2*]
| paths: ['scr/main/**']""".stripMargin
WebhookEvent.Push(Nil, List("v1*", "v2*"), List("src/main/**")).render mustEqual expected
}

"render only with paths" in {
val expected =
"""|push:
| paths: ['scr/main/**']""".stripMargin
WebhookEvent.Push(Nil, Nil, List("src/main/**")).render mustEqual expected
}

"render with branches and tags" in {
val expected =
"""|push:
| branches: [master]
| tags: [v1*, v2*]""".stripMargin
WebhookEvent.Push(List("master"), List("v1*", "v2*")).render mustEqual expected
WebhookEvent.Push(List("master"), List("v1*", "v2*"), Nil).render mustEqual expected
}

"render without tags, but with branches" in {
val expected =
"""|push:
| branches: [master]""".stripMargin
WebhookEvent.Push(List("master"), Nil).render mustEqual expected
WebhookEvent.Push(List("master"), Nil, Nil).render mustEqual expected
}
}

Expand Down

0 comments on commit 40c53ac

Please sign in to comment.