Skip to content

Commit

Permalink
Merge pull request #320 from Dwolla/mergify-actions
Browse files Browse the repository at this point in the history
Add request_reviews and update Mergify actions
  • Loading branch information
armanbilge authored Jul 5, 2022
2 parents d642490 + 3ad097d commit 42eec29
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 3 deletions.
9 changes: 8 additions & 1 deletion .mergify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
pull_request_rules:
- name: merge scala-steward's PRs
conditions:
- author=scala-steward
- author=typelevel-steward[bot]
- or:
- body~=labels:.*early-semver-patch
- body~=labels:.*early-semver-minor
Expand Down Expand Up @@ -168,3 +168,10 @@ pull_request_rules:
add:
- versioning
remove: []
- name: assign scala-steward's PRs for review
conditions:
- author=typelevel-steward[bot]
actions:
request_reviews:
users:
- armanbilge
11 changes: 10 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,18 @@ ThisBuild / developers := List(
tlGitHubDev("djspiewak", "Daniel Spiewak")
)

ThisBuild / mergifyStewardConfig ~= { _.map(_.copy(mergeMinors = true)) }
ThisBuild / mergifyStewardConfig ~= {
_.map(_.copy(mergeMinors = true, author = "typelevel-steward[bot]"))
}
ThisBuild / mergifySuccessConditions += MergifyCondition.Custom("#approved-reviews-by>=1")
ThisBuild / mergifyLabelPaths += { "docs" -> file("docs") }
ThisBuild / mergifyPrRules += MergifyPrRule(
"assign scala-steward's PRs for review",
List(MergifyCondition.Custom("author=typelevel-steward[bot]")),
List(
MergifyAction.RequestReviews.fromUsers("armanbilge")
)
)

ThisBuild / scalafixDependencies ++= Seq(
"com.github.liancheng" %% "organize-imports" % "0.6.0"
Expand Down
104 changes: 103 additions & 1 deletion mergify/src/main/scala/org/typelevel/sbt/mergify/MergifyAction.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@

package org.typelevel.sbt.mergify

import io.circe.Encoder
import cats.data._
import cats.syntax.all._
import io.circe._
import io.circe.syntax._
import org.typelevel.sbt.mergify.MergifyAction.RequestReviews._
import sbt.librarymanagement.Developer

sealed abstract class MergifyAction {
private[mergify] def name = getClass.getSimpleName.toLowerCase
Expand All @@ -28,6 +32,8 @@ object MergifyAction {
implicit def encoder: Encoder[MergifyAction] = Encoder.instance {
case merge: Merge => merge.asJson
case label: Label => label.asJson
case requestReviews: RequestReviews => requestReviews.asJson
case Update => Update.asJson
case _ => sys.error("should not happen")
}

Expand Down Expand Up @@ -57,6 +63,102 @@ object MergifyAction {
}
}

class RequestReviews private (
private val users: Option[OptionallyWeighted],
private val teams: Option[OptionallyWeighted],
private val usersFromTeams: Option[OptionallyWeighted],
private val randomCount: Option[Int])
extends MergifyAction {
override private[mergify] def name = "request_reviews"

private def copy(
users: Option[OptionallyWeighted] = users,
teams: Option[OptionallyWeighted] = teams,
usersFromTeams: Option[OptionallyWeighted] = usersFromTeams,
randomCount: Option[Int] = randomCount): RequestReviews =
new RequestReviews(users, teams, usersFromTeams, randomCount) {}

def withUsers(user: String, users: String*): RequestReviews =
copy(users = Unweighted(NonEmptyList.of(user, users: _*)).some)

def withUsers(user: (String, Int), users: (String, Int)*): RequestReviews =
copy(users = Weighted(NonEmptyList.of(user, users: _*)).some)

def withTeams(team: String, teams: String*): RequestReviews =
copy(teams = Unweighted(NonEmptyList.of(team, teams: _*)).some)

def withTeams(team: (String, Int), teams: (String, Int)*): RequestReviews =
copy(teams = Weighted(NonEmptyList.of(team, teams: _*)).some)

def withUsersFromTeams(team: String, teams: String*): RequestReviews =
copy(usersFromTeams = Unweighted(NonEmptyList.of(team, teams: _*)).some)

def withUsersFromTeams(team: (String, Int), teams: (String, Int)*): RequestReviews =
copy(usersFromTeams = Weighted(NonEmptyList.of(team, teams: _*)).some)

def withRandomCount(count: Int): RequestReviews =
copy(randomCount = Option(count))

def withDevelopers(developers: List[Developer]): RequestReviews =
copy(users = NonEmptyList.fromList(developers.map(_.id)).map(Unweighted))
}

object RequestReviews {
def fromUsers(user: String, users: String*) =
new RequestReviews(Unweighted(NonEmptyList.of(user, users: _*)).some, None, None, None)
def fromUsers(user: (String, Int), users: (String, Int)*) =
new RequestReviews(Weighted(NonEmptyList.of(user, users: _*)).some, None, None, None)
def fromTeams(team: String, teams: String*) =
new RequestReviews(None, Unweighted(NonEmptyList.of(team, teams: _*)).some, None, None)
def fromTeams(team: (String, Int), teams: (String, Int)*) =
new RequestReviews(None, Weighted(NonEmptyList.of(team, teams: _*)).some, None, None)
def fromUsersOfTeams(team: String, teams: String*) =
new RequestReviews(None, None, Unweighted(NonEmptyList.of(team, teams: _*)).some, None)
def fromUsersOfTeams(team: (String, Int), teams: (String, Int)*) =
new RequestReviews(None, None, Weighted(NonEmptyList.of(team, teams: _*)).some, None)

def apply(developers: List[Developer]) =
new RequestReviews(
Unweighted(
developers
.map(_.id)
.toNel
.getOrElse(throw new RuntimeException("developers must be non-empty"))
).some,
None,
None,
None)

implicit def encoder: Encoder[RequestReviews] =
Encoder.forProduct4("users", "teams", "users_from_teams", "random_count") {
requestReviews =>
(
requestReviews.users,
requestReviews.teams,
requestReviews.usersFromTeams,
requestReviews.randomCount
)
}

private sealed trait OptionallyWeighted
private case class Weighted(value: NonEmptyList[(String, Int)]) extends OptionallyWeighted
private case class Unweighted(value: NonEmptyList[String]) extends OptionallyWeighted

private object OptionallyWeighted {
implicit val encoder: Encoder[OptionallyWeighted] = {
case Weighted(value) => value.asJson
case Unweighted(value) => value.asJson
}
}
}

object Update extends MergifyAction {
override private[mergify] def name = "update"

implicit def encoder: Encoder[Update.type] =
Encoder[JsonObject].contramap(_ => JsonObject.empty)
}

private[this] object Dummy extends MergifyAction

}

0 comments on commit 42eec29

Please sign in to comment.