Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion app/models/project/Project.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import ore.{Joinable, OreConfig, Visitable}
import play.api.libs.json._
import play.api.libs.functional.syntax._
import play.twirl.api.Html
import _root_.util.GitHubUtil
import _root_.util.StringUtils
import _root_.util.StringUtils._
import models.project.VisibilityTypes.{Public, Visibility}
Expand Down Expand Up @@ -464,7 +465,18 @@ case class Project(override val id: Option[Int] = None,
* @return Project home page
*/
def homePage: Page = Defined {
val page = new Page(this.id.get, Page.HomeName, Page.Template(this.name, Page.HomeMessage), false, -1)
var body = Page.HomeMessage
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should only do it if one is not manually provided, yes? If that is not the behaviour with this PR, it should be.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not quite sure what you're saying. This scrapes the README if a source URL is provided, and if it is from GitHub.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm asking if this will overwrite an existing manually-created README.

Copy link
Contributor Author

@phase phase Oct 31, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a Project is created, it creates a Home Page that has Page.HomeMessage in it. Lines 448 - 451 retrieve the Page from database, passing the page variable as a default. This changes the default to the README it scraped from GitHub. It's basically replacing the text from Page.HomeMessage with the README. So no, it will not replace the Home Page after it has been edited.

val source = this.settings.source
if (source.isDefined && GitHubUtil.isGitHubUrl(source.get)) {
val urlParts = source.get.split("//github.com/")(1).split("/")
val ghUser = urlParts(0)
val ghProject = urlParts(1)
val readme = GitHubUtil.getReadme(ghUser, ghProject)
if (readme != null && readme.isDefined) {
body = readme.get
}
}
val page = new Page(this.id.get, Page.HomeName, Page.Template(this.name, body), false, -1)
this.service.await(page.schema.getOrInsert(page)).get
}

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

import java.io.FileNotFoundException

import play.api.libs.json.{JsValue, Json}

import scala.io.Source

object GitHubUtil {

private val identifier = "A-Za-z0-9-_"
private val gitHubUrlPattern = s"""http(s)?://github.com/[$identifier]+/[$identifier]+(/)?""".r.pattern
private val readmeApi = "https://api.github.com/repos/%s/%s/readme"

def isGitHubUrl(url: String): Boolean = gitHubUrlPattern.matcher(url).matches()

def getReadme(user: String, project: String): Option[String] = {
try {
val readmeApiJson: JsValue = Json.parse(Source.fromURL(readmeApi.format(user, project)).mkString)
val url = (readmeApiJson \ "download_url").get.toString()
val readme = Source.fromURL(url).mkString
Some(readme)
} catch {
case _: FileNotFoundException => None
}
}

}