Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from guardian/upgrade-to-scala-v2.13-and-play-…
…v2.8 Upgrade to Scala v2.13 and Play v2.8
- Loading branch information
Showing
61 changed files
with
1,046 additions
and
921 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 |
---|---|---|
|
@@ -20,6 +20,7 @@ dist | |
### Scala ### | ||
*.class | ||
*.log | ||
.bsp | ||
|
||
# sbt specific | ||
.cache/ | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package configuration | ||
|
||
import play.api.ApplicationLoader.Context | ||
import play.api.{Application, ApplicationLoader, LoggerConfigurator} | ||
|
||
class AppLoader extends ApplicationLoader { | ||
|
||
override def load(context: Context): Application = { | ||
LoggerConfigurator(context.environment.classLoader).foreach { | ||
_.configure(context.environment) | ||
} | ||
new ApplicationComponents(context).application | ||
} | ||
} |
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,58 @@ | ||
package configuration | ||
|
||
import com.madgag.scalagithub.model.User | ||
import com.madgag.scalagithub.{GitHub, GitHubCredentials} | ||
import com.softwaremill.macwire._ | ||
import controllers.{Application, _} | ||
import lib.actions.Actions | ||
import lib.sentry.SentryApiClient | ||
import lib.{Bot, CheckpointSnapshoter, Delayer, Droid, PRSnapshot, PRUpdater, RepoSnapshot, RepoUpdater, ScanScheduler} | ||
import play.api.routing.Router | ||
import play.api.{ApplicationLoader, BuiltInComponentsFromContext, Logging} | ||
import router.Routes | ||
|
||
import java.nio.file.Path | ||
import scala.concurrent.Await | ||
import scala.concurrent.duration._ | ||
|
||
class ApplicationComponents(context: ApplicationLoader.Context) | ||
extends BuiltInComponentsFromContext(context) with ReasonableHttpFilters | ||
with AssetsComponents with Logging { | ||
|
||
implicit val checkpointSnapshoter: CheckpointSnapshoter = CheckpointSnapshoter | ||
|
||
val workingDir: Path = Path.of("/tmp", "bot", "working-dir") | ||
|
||
implicit val bot: Bot = Bot.forAccessToken(configuration.get[String]("github.botAccessToken")) | ||
|
||
implicit val github: GitHub = bot.github | ||
|
||
implicit val authClient: com.madgag.playgithub.auth.Client = com.madgag.playgithub.auth.Client( | ||
id = configuration.get[String]("github.clientId"), | ||
secret = configuration.get[String]("github.clientSecret") | ||
) | ||
|
||
val delayer: Delayer = wire[Delayer] | ||
val repoSnapshotFactory: RepoSnapshot.Factory = wire[RepoSnapshot.Factory] | ||
|
||
implicit val sentryApiClient: Option[SentryApiClient] = SentryApiClient.instanceOptFrom(configuration) | ||
val repoUpdater: RepoUpdater = wire[RepoUpdater] | ||
val prUpdater: PRUpdater = wire[PRUpdater] | ||
val droid: Droid = wire[Droid] | ||
val scanSchedulerFactory: ScanScheduler.Factory = wire[ScanScheduler.Factory] | ||
val repoAcceptListService: RepoAcceptListService = wire[RepoAcceptListService] | ||
|
||
val actions: Actions = wire[Actions] | ||
val controllerAppComponents: ControllerAppComponents = wire[ControllerAppComponents] | ||
|
||
val apiController: Api = wire[Api] | ||
val appController: Application = wire[Application] | ||
val authController: Auth = wire[_root_.controllers.Auth] | ||
|
||
val router: Router = { | ||
// add the prefix string in local scope for the Routes constructor | ||
val prefix: String = "/" | ||
wire[Routes] | ||
} | ||
|
||
} |
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,15 @@ | ||
package configuration | ||
|
||
import play.api.mvc.EssentialFilter | ||
import play.filters.csrf.CSRFComponents | ||
import play.filters.headers.SecurityHeadersComponents | ||
|
||
/* | ||
This is based off the original Play class HttpFiltersComponents, | ||
with allowedHostsFilter removed so we can use with randomly-named | ||
autoscaled EC2 boxes, or whatever Heroku does. | ||
*/ | ||
trait ReasonableHttpFilters extends CSRFComponents with SecurityHeadersComponents { | ||
|
||
def httpFilters: Seq[EssentialFilter] = Seq(csrfFilter, securityHeadersFilter) | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package controllers | ||
|
||
import com.madgag.playgithub.auth.GHRequest | ||
import com.madgag.scalagithub.model.RepoId | ||
import lib.actions.Actions | ||
import play.api.Logging | ||
import play.api.http.FileMimeTypes | ||
import play.api.i18n.{Langs, MessagesApi} | ||
import play.api.mvc._ | ||
|
||
import scala.concurrent.ExecutionContext | ||
|
||
case class ControllerAppComponents( | ||
actions: Actions, | ||
actionBuilder: DefaultActionBuilder, | ||
parsers: PlayBodyParsers, | ||
messagesApi: MessagesApi, | ||
langs: Langs, | ||
fileMimeTypes: FileMimeTypes, | ||
executionContext: scala.concurrent.ExecutionContext | ||
) extends ControllerComponents | ||
|
||
trait BaseAppController extends BaseController with Logging { | ||
|
||
val controllerAppComponents: ControllerAppComponents | ||
|
||
override val controllerComponents = controllerAppComponents | ||
|
||
implicit val ec: ExecutionContext = controllerAppComponents.executionContext // Controversial? https://www.playframework.com/documentation/2.6.x/ThreadPools | ||
|
||
def repoAuthenticated(repoId: RepoId): ActionBuilder[GHRequest, AnyContent] = | ||
controllerAppComponents.actions.repoAuthenticated(repoId) | ||
|
||
} | ||
|
||
abstract class AbstractAppController( | ||
val controllerAppComponents: ControllerAppComponents | ||
) extends BaseAppController |
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,50 @@ | ||
package controllers | ||
|
||
import akka.actor.ActorSystem | ||
import akka.stream.Materializer | ||
import com.madgag.github.Implicits._ | ||
import com.madgag.scalagithub.GitHub | ||
import com.madgag.scalagithub.model.{Repo, RepoId} | ||
import com.typesafe.scalalogging.LazyLogging | ||
import lib.ConfigFinder.ProutConfigFileName | ||
import lib.gitgithub._ | ||
|
||
import java.util.concurrent.atomic.AtomicReference | ||
import scala.concurrent.ExecutionContext.Implicits.global | ||
import scala.concurrent.Future | ||
import scala.concurrent.duration._ | ||
|
||
case class RepoAcceptList(allKnownRepos: Set[RepoId], publicRepos: Set[RepoId]) | ||
|
||
class RepoAcceptListService( | ||
actorSystem: ActorSystem | ||
) (implicit | ||
github: GitHub, | ||
mat: Materializer | ||
) extends LazyLogging { | ||
|
||
lazy val repoAcceptList = new AtomicReference[Future[RepoAcceptList]](getAllKnownRepos) | ||
|
||
def acceptList(): Future[RepoAcceptList] = repoAcceptList.get() | ||
|
||
def hasProutConfigFile(repo: Repo): Future[Boolean] = for { | ||
treeT <- repo.trees2.getRecursively(s"heads/${repo.default_branch}").trying | ||
} yield treeT.map(_.tree.exists(_.path.endsWith(ProutConfigFileName))).getOrElse(false) | ||
|
||
private def getAllKnownRepos: Future[RepoAcceptList] = for { // check this to see if it always expends quota... | ||
allRepos <- github.listRepos(sort="pushed", direction = "desc").take(6).all() | ||
proutRepos <- Future.traverse(allRepos.filter(_.permissions.exists(_.push))) { repo => | ||
hasProutConfigFile(repo).map(hasConfig => Option.when(hasConfig)(repo)) | ||
}.map(_.flatten.toSet) | ||
} yield RepoAcceptList(proutRepos.map(_.repoId), proutRepos.filterNot(_.`private`).map(_.repoId)) | ||
|
||
|
||
def start(): Unit = { | ||
logger.info("Starting background repo fetch") | ||
actorSystem.scheduler.scheduleWithFixedDelay(1.second, 60.seconds) { () => | ||
repoAcceptList.set(getAllKnownRepos) | ||
github.checkRateLimit().foreach(status => logger.info(status.summary)) | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.