Skip to content
This repository has been archived by the owner on Dec 10, 2018. It is now read-only.

Commit

Permalink
iss #22: tmp ...
Browse files Browse the repository at this point in the history
  • Loading branch information
maizy committed Jul 11, 2014
1 parent f33c8d6 commit b9292c6
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 21 deletions.
5 changes: 1 addition & 4 deletions app/controllers/Config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package controllers
import play.api.mvc.{Action, Controller}
import play.api.libs.json.Json

import hedgehog.models.Sources



/**
* Copyright (c) Nikita Kovaliov, maizy.ru, 2013
Expand All @@ -16,7 +13,7 @@ object Config extends Controller {
def list = Action { implicit request =>
import hedgehog.models.AccountSettings.accountSettingWrites
Ok(Json.obj(
"sources" -> Sources.accountsSettings
"sources" -> hedgehog.Config.playAppInstance.sources.accountsSettings
))
}

Expand Down
24 changes: 24 additions & 0 deletions app/controllers/Github.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package controllers

import play.api.mvc.{Action, Controller}
import play.api.libs.json.Json
import play.api.libs.concurrent.Execution.Implicits._
import hedgehog.models.{GithubOrg, Repo, GithubUser}
import hedgehog.clients.github.RepositoriesFetcher


/**
* Copyright (c) Nikita Kovaliov, maizy.ru, 2014
* See LICENSE.txt for details.
*/
object Github extends Controller {

def forceUpdate = Action {
// val config = hedgehog.Config.playAppInstance
// RepositoriesFetcher.playAppInstance.reload(config.sources.accountsSettings).map {
// Ok(Json.obj("success" -> false))
// }
Ok("1")
}

}
23 changes: 16 additions & 7 deletions app/controllers/Repositories.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,29 @@ package controllers

import play.api.mvc.{Action, Controller}
import play.api.libs.json.Json
import hedgehog.models.{Repo, ConfigError}
import hedgehog.models.Repo
import hedgehog.clients.github.RepositoriesFetcher

/**
* Copyright (c) Nikita Kovaliov, maizy.ru, 2013
* Copyright (c) Nikita Kovaliov, maizy.ru, 2013-2014
* See LICENSE.txt for details.
*/
object Repositories extends Controller {
def list = Action { implicit request =>
try {
def list = Action.async { implicit request =>
val fetcher = RepositoriesFetcher.playAppInstance
val config = hedgehog.Config.playAppInstance
import play.api.libs.concurrent.Execution.Implicits._

//TODO: здесь вставить запрос с fetcher'а
//TODO: в force_update убрать вывод реп, нафиг они там
//TODO: для начала игнорить ошибки
//TODO: определять, что обновление уже идёт и как-то вешаться на его Future

fetcher.getRepos(config.sources.accountsSettings).map { repos =>
Ok(Json.obj(
"repositories" -> Repo.getCurrentRepos
//TODO: grouping, params ...
"repositories" -> repos
))
} catch {
case ex: ConfigError => InternalServerError(ex.toString)
}
}
}
11 changes: 9 additions & 2 deletions app/hedgehog/Config.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package hedgehog

import scala.concurrent.ExecutionContext
import play.api.{Configuration, Play}
import hedgehog.clients.github
import hedgehog.models.Sources

/**
* Main project config used for wrapping app.configuration,
Expand All @@ -9,18 +12,22 @@ import play.api.{Configuration, Play}
* Copyright (c) Nikita Kovaliov, maizy.ru, 2014
* See LICENSE.txt for details.
*/
class Config (appConfiguration: Configuration) {
class Config (appConfiguration: Configuration, asyncContext: ExecutionContext) {
val apiBaseUrl = appConfiguration.getString("github.api_url")
.getOrElse("https://api.github.com").stripSuffix("/")
lazy val githubClientConfig = new hedgehog.clients.github.Config(apiBaseUrl)
lazy val githubClient = new github.Client(githubClientConfig, asyncContext) //TODO: is there good place for that?


val version = "0.0.1" //TODO: sync with build.sbt (generate on project builded)
val projectRepoUrl = "https://github.com/maizy/hedGeHog"
val copyrightYears = "2013-2014"
lazy val sources = new Sources(appConfiguration)
}


object Config {
val playAppInstance = new Config(Play.current.configuration)
val playAppInstance = new Config(
Play.current.configuration,
play.api.libs.concurrent.Execution.Implicits.defaultContext)
}
20 changes: 20 additions & 0 deletions app/hedgehog/clients/github/Client.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package hedgehog.clients.github

import scala.concurrent.{Future, Promise}
import hedgehog.models.{AccountSettings, Repo}


/**
* Copyright (c) Nikita Kovaliov, maizy.ru, 2014
* See LICENSE.txt for details.
*/
class Client(val config: Config, implicit val context: scala.concurrent.ExecutionContext) {

def getRepos(accountSettings: AccountSettings): Future[Seq[Repo]] = {
val finish = Promise[Seq[Repo]]()
finish success List(new Repo("a", accountSettings.account),
new Repo("b", accountSettings.account))

finish.future
}
}
45 changes: 45 additions & 0 deletions app/hedgehog/clients/github/RepositoriesFetcher.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package hedgehog.clients.github

import scala.concurrent.{Future, Promise}
import scala.collection.mutable
import com.github.nscala_time.time.Imports.DateTime
import hedgehog.models.{AccountSettings, Repo}


/**
* Copyright (c) Nikita Kovaliov, maizy.ru, 2014
* See LICENSE.txt for details.
*/
class RepositoriesFetcher(client: Client) {
val repositories: mutable.Map[String, Option[Seq[Repo]]] = mutable.Map.empty
val lastFetched: mutable.Map[String, Option[DateTime]] = mutable.Map.empty

implicit val context = client.context

def getRepos(accountsSettings: Seq[AccountSettings]): Future[Seq[Repo]] = {
//TODO: cache
fetch(accountsSettings)
}

private def fetch(accountsSettings: Seq[AccountSettings]): Future[Seq[Repo]] = {
val requests = Future.sequence{
accountsSettings.map(client.getRepos)
}
val future = requests map {
case r => r.flatten
}
future
}

def reload(accountSettings: Seq[AccountSettings]): Future[Seq[(AccountSettings, Boolean)]] = {
//val p = Promise[Seq[(AccountSettings, Boolean)]]
Future {
List()
}
}
}


object RepositoriesFetcher {
lazy val playAppInstance: RepositoriesFetcher = new RepositoriesFetcher(hedgehog.Config.playAppInstance.githubClient)
}
1 change: 1 addition & 0 deletions app/hedgehog/clients/github/errors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ package hedgehog.clients.github
* See LICENSE.txt for details.
*/
class Error(msg: String) extends Exception(msg)
class FetchError(msg: String) extends Exception(msg)
12 changes: 5 additions & 7 deletions app/hedgehog/models/Sources.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ package hedgehog.models

import scala.collection.JavaConversions._

import play.api.Play.current
import play.api.libs.json.{Json, JsValue, Writes}
import play.api.Configuration
import com.typesafe.config.{ConfigValueType, ConfigObject, ConfigList, Config}

/**
* Copyright (c) Nikita Kovaliov, maizy.ru, 2014
* See LICENSE.txt for details.
*/
object Sources {
lazy val accountsSettings: Seq[AccountSettings] = {
current.configuration.getList("github.sources") match {
class Sources(configuration: Configuration) {
lazy val accountsSettings: Seq[AccountSettings] =
configuration.getList("github.sources") match {
case Some(sourcesConfig: ConfigList) =>
sourcesConfig.map {
cfg => cfg.valueType() match {
Expand All @@ -23,14 +23,12 @@ object Sources {
}.flatten.toList
case _ => throw new ConfigError("github.sources config path not defined")
}
}
}


class AccountSettings(
val account: Account,
val includePrivateRepos: Boolean = false) {
}
val includePrivateRepos: Boolean = false)


object AccountSettings {
Expand Down
2 changes: 1 addition & 1 deletion conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ GET /status controllers.Status.index
GET /repositories controllers.Repositories.list

GET /config controllers.Config.list
POST /github/force-update controllers.Github.forceUpdate
GET /github/force-update controllers.Github.forceUpdate

POST /language/set controllers.Language.set

Expand Down

0 comments on commit b9292c6

Please sign in to comment.