Permalink
Browse files

Add repos info page

  • Loading branch information...
1 parent 826cae6 commit e85b6eda69608078f8c69adefe18e418e476745c @Dinduks committed Sep 13, 2012
@@ -5,6 +5,8 @@ import play.api.mvc._
import play.api.libs.ws._
import play.api.libs.json._
+import models._
+
object Application extends Controller {
def index = Action {
Ok(views.html.index())
@@ -21,5 +23,44 @@ object Application extends Controller {
}
}
}
+
+ def repoInfo(username: String, repositoryName: String) = Action {
+ val githubApiUrl = Play.current.configuration.getString("github.apiUrl").get
+
+ Async {
+ WS.url(githubApiUrl + "/repos/" + username + "/" + repositoryName + "/commits").get().map { response =>
+ val commits = (response.json)
+ val contributorsList = getSortedCommittersList(commits)
+ Ok(views.html.showRepoInfo(username, repositoryName, contributorsList))
+ }
+ }
+ }
+
+ def getSortedCommittersList(commits: JsValue): Map[String, Contributor] = {
+ val commitsList = commits.as[List[JsValue]]
+ var committersList: Map[String, Contributor] = Map()
+
+ for(commit <- commitsList) {
+ committersList.get((commit \ "commit" \ "author" \ "email").toString) match {
+ case Some(committer) => {
+ committer.contributionsCounter = committer.contributionsCounter + 1
+ }
+ case None => {
+ val commitAuthor = commit \ "commit" \ "author"
+ val author = commit \ "author"
+ val contributor = Contributor(
+ Some((author \ "login").toString),
+ Some((commitAuthor \ "name").toString),
+ Some((commitAuthor \ "email").toString),
+ Some((author \ "avatar_url").toString),
+ 1
+ )
+ committersList += ((commitAuthor \ "email").toString -> contributor)
+ }
+ }
+ }
+
+ committersList
+ }
}
@@ -0,0 +1,38 @@
+package models
+
+import revontulet.Util
+
+class Contributor(
+ username: Option[String] = None,
+ name: Option[String] = None,
+ email: Option[String] = None,
+ url: Option[String] = None,
+ avatarUrl: Option[String] = None,
+ var contributionsCounter: Int = 0
+) extends User(
+ username,
+ name,
+ email,
+ url,
+ avatarUrl
+) {
+}
+
+object Contributor {
+ def apply(
+ username: Option[String],
+ name: Option[String],
+ email: Option[String],
+ avatarUrl: Option[String],
+ contributionsCounter: Int
+ ) = {
+ new Contributor(
+ username,
+ name,
+ email,
+ Some("https://github.com/" + Util.trim(username.get, '"')),
+ (avatarUrl),
+ contributionsCounter
+ )
+ }
+}
View
@@ -0,0 +1,10 @@
+package models
+
+case class User(
+ username: Option[String] = None,
+ name: Option[String] = None,
+ email: Option[String] = None,
+ url: Option[String] = None,
+ avatarUrl: Option[String] = None
+)
+
View
@@ -0,0 +1,13 @@
+package revontulet
+
+import scala.util.matching.Regex
+
+import java.util.regex.Pattern
+
+object Util {
+ def trim(string: String, char: Char) = {
+ val pattern = new Regex("^" + Pattern.quote(char.toString) + "+|" + Pattern.quote(char.toString) + "+$", "trimmedString")
+ pattern.replaceAllIn(string, "")
+ }
+}
+
@@ -0,0 +1,11 @@
+@(contributor: Contributor)
+
+@import revontulet.Util
+
+<a href="@contributor.url.getOrElse("https://github.com")">
+ @Util.trim(contributor.username.getOrElse(contributor.name.get), '"')
+ -
+ @contributor.contributionsCounter commit among 30
+
+ (@((contributor.contributionsCounter.toFloat / 30 * 100).ceil)%)
+</a>
@@ -1,6 +1,8 @@
@(username: play.api.libs.json.JsValue, repositoryName: play.api.libs.json.JsValue)
-@defining(username.toString.replaceAll("\"", "") + "/" + repositoryName.toString.replaceAll("\"", "")) { repositoryFullName =>
+@import revontulet.Util
+
+@defining(Util.trim(username.toString, '"') + "/" + Util.trim(repositoryName.toString, '"')) { repositoryFullName =>
<a href="/@repositoryFullName">
@repositoryFullName
</a>
@@ -0,0 +1,19 @@
+@(username : String,
+ repositoryName : String,
+ contributorsList: Map[String, Contributor])
+
+@import views.html.helper._
+
+@main(username + "/" + repositoryName + " - Repository info") {
+ <h2>Contributors</h2>
+ <div>
+ <ul>
+ @for((username, contributor) <- contributorsList) {
+ <li>
+ @contributorInfo(contributor)
+ </li>
+ }
+ </ul>
+ </div>
+}
+
View
@@ -47,3 +47,4 @@ logger.application=DEBUG
github.apiUrl = "https://api.github.com"
github.searchReposPath = "/legacy/repos/search/"
+github.getRepoCommitsPath = "/repos/:user/:repo/commits"
View
@@ -1,3 +1,4 @@
GET / controllers.Application.index
GET /search/:keyword controllers.Application.search(keyword: String)
+GET /:username/:repositoryName controllers.Application.repoInfo(username: String, repositoryName: String)
GET /assets/*file controllers.Assets.at(path="/public", file)
View
@@ -0,0 +1,15 @@
+import org.specs2.mutable._
+
+import play.api.test._
+import play.api.test.Helpers._
+
+import revontulet.Util
+
+class UtilSpec extends Specification {
+ "trim" should {
+ "trim a character of a specific string" in {
+ Util.trim(".hello.", '.') must beEqualTo("hello")
+ Util.trim("!!hello!!!!!!", '!') must beEqualTo("hello")
+ }
+ }
+}

0 comments on commit e85b6ed

Please sign in to comment.