├── .github ├── CODEOWNERS ├── dependabot.yml └── workflows │ └── build.yml ├── project ├── build.properties └── plugins.sbt ├── src └── main │ ├── scala │ ├── gitbucket │ │ └── gist │ │ │ ├── model │ │ │ ├── GistUser.scala │ │ │ ├── GistInfo.scala │ │ │ ├── GistProfile.scala │ │ │ ├── Mode.scala │ │ │ ├── GistComment.scala │ │ │ └── Gist.scala │ │ │ ├── util │ │ │ ├── Configurations.scala │ │ │ ├── GistAuthenticator.scala │ │ │ └── GistUtils.scala │ │ │ ├── service │ │ │ ├── GistCommentService.scala │ │ │ └── GistService.scala │ │ │ └── controller │ │ │ └── GistController.scala │ └── Plugin.scala │ ├── resources │ ├── update │ │ ├── gitbucket-gist_4.2.xml │ │ └── gitbucket-gist_2.0.xml │ └── gitbucket │ │ └── gist │ │ └── assets │ │ └── style.css │ └── twirl │ └── gitbucket │ └── gist │ ├── commentform.scala.html │ ├── forks.scala.html │ ├── profile.scala.html │ ├── commentpreview.scala.html │ ├── commentedit.scala.html │ ├── header.scala.html │ ├── detail.scala.html │ ├── editor.scala.html │ ├── detail.scala.js │ ├── commentlist.scala.html │ ├── list.scala.html │ ├── menu.scala.html │ ├── revisions.scala.html │ └── edit.scala.html ├── .gitignore ├── README.md ├── etc └── icons.svg └── LICENSE /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @takezoe @xuwei-k 2 | -------------------------------------------------------------------------------- /project/build.properties: -------------------------------------------------------------------------------- 1 | sbt.version = 1.11.7 2 | -------------------------------------------------------------------------------- /project/plugins.sbt: -------------------------------------------------------------------------------- 1 | addSbtPlugin("io.github.gitbucket" % "sbt-gitbucket-plugin" % "1.6.0") -------------------------------------------------------------------------------- /src/main/scala/gitbucket/gist/model/GistUser.scala: -------------------------------------------------------------------------------- 1 | package gitbucket.gist.model 2 | 3 | case class GistUser(userName: String, fullName: String) -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: "github-actions" 4 | directory: "/" 5 | schedule: 6 | interval: "weekly" 7 | -------------------------------------------------------------------------------- /src/main/scala/gitbucket/gist/model/GistInfo.scala: -------------------------------------------------------------------------------- 1 | package gitbucket.gist.model 2 | 3 | case class GistInfo(fileName: String, source: String, fileCount: Int, forkedCount: Int, commentCount: Int) -------------------------------------------------------------------------------- /src/main/scala/gitbucket/gist/model/GistProfile.scala: -------------------------------------------------------------------------------- 1 | package gitbucket.gist.model 2 | 3 | import gitbucket.core.model._ 4 | 5 | object Profile extends CoreProfile 6 | with GistComponent 7 | with GistCommentComponent 8 | -------------------------------------------------------------------------------- /src/main/scala/gitbucket/gist/util/Configurations.scala: -------------------------------------------------------------------------------- 1 | package gitbucket.gist.util 2 | 3 | object Configurations { 4 | lazy val GistRepoDir = s"${gitbucket.core.util.Directory.GitBucketHome}/gist" 5 | lazy val Limit = 10 6 | } 7 | -------------------------------------------------------------------------------- /src/main/resources/update/gitbucket-gist_4.2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | UPDATE GIST SET MODE='SECRET' WHERE PRIVATE = TRUE 7 | 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | *.log 3 | 4 | # sbt specific 5 | dist/* 6 | target/ 7 | lib_managed/ 8 | src_managed/ 9 | project/boot/ 10 | project/plugins/project/ 11 | .bsp/ 12 | 13 | # Scala-IDE specific 14 | .scala_dependencies 15 | .classpath 16 | .project 17 | .cache 18 | .settings 19 | 20 | # IntelliJ specific 21 | .idea/ 22 | .idea_modules/ 23 | 24 | # Ensime 25 | .ensime 26 | .ensime_cache/ 27 | 28 | # Metals 29 | .bloop/ 30 | .metals/ 31 | .vscode/ 32 | **/metals.sbt 33 | 34 | -------------------------------------------------------------------------------- /src/main/scala/gitbucket/gist/model/Mode.scala: -------------------------------------------------------------------------------- 1 | package gitbucket.gist.model 2 | 3 | sealed trait Mode { 4 | val code: String 5 | } 6 | 7 | object Mode { 8 | 9 | def from(code: String): Mode = { 10 | code match { 11 | case Public.code => Public 12 | case Secret.code => Secret 13 | case Private.code => Private 14 | } 15 | } 16 | 17 | case object Public extends Mode { 18 | val code = "PUBLIC" 19 | } 20 | 21 | case object Secret extends Mode { 22 | val code = "SECRET" 23 | } 24 | 25 | case object Private extends Mode { 26 | val code = "PRIVATE" 27 | } 28 | 29 | } -------------------------------------------------------------------------------- /src/main/resources/gitbucket/gist/assets/style.css: -------------------------------------------------------------------------------- 1 | div.head { 2 | padding-top: 15px; 3 | padding-left: 15px; 4 | padding-right: 15px; 5 | } 6 | 7 | div.list-markup { 8 | border: 1px solid rgba(0, 0, 0, 0.15); 9 | padding-left: 16px; 10 | padding-right: 16px; 11 | margin-bottom: 20px; 12 | padding-left: 16px; 13 | padding-right: 16px; 14 | } 15 | 16 | pre.list-code { 17 | padding-left: 25px; 18 | background-color: transparent; 19 | border: 1px solid rgba(0, 0, 0, 0.15); 20 | padding-left: 25px; 21 | } 22 | 23 | @media (min-width: 767px) { 24 | div.gist-content { 25 | width: 980px; 26 | margin: auto; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/scala/gitbucket/gist/util/GistAuthenticator.scala: -------------------------------------------------------------------------------- 1 | package gitbucket.gist.util 2 | 3 | import gitbucket.core.controller.ControllerBase 4 | import gitbucket.core.service.AccountService 5 | import gitbucket.core.util.Implicits._ 6 | 7 | /** 8 | * Allows only editor of the accessed snippet. 9 | */ 10 | trait GistEditorAuthenticator { self: ControllerBase with AccountService => 11 | protected def editorOnly(action: => Any) = { authenticate(action) } 12 | protected def editorOnly[T](action: T => Any) = (form: T) => { authenticate(action(form)) } 13 | 14 | private def authenticate(action: => Any) = { 15 | { 16 | val paths = request.paths 17 | if(context.loginAccount.map { loginAccount => 18 | loginAccount.isAdmin || loginAccount.userName == paths(1) || getGroupsByUserName(loginAccount.userName).contains(paths(1)) 19 | }.getOrElse(false)){ 20 | action 21 | } else { 22 | Unauthorized() 23 | } 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /src/main/twirl/gitbucket/gist/commentform.scala.html: -------------------------------------------------------------------------------- 1 | @(gist: gitbucket.gist.model.Gist)(implicit context: gitbucket.core.controller.Context) 2 | @if(context.loginAccount.isDefined){ 3 |

4 |
5 |
6 |
7 | @gitbucket.gist.html.commentpreview( 8 | gist = gist, 9 | content = "", 10 | style = "height: 100px; max-height: 150px;", 11 | elastic = true 12 | ) 13 |
14 | 15 |
16 |
17 |
18 |
19 | } 20 | 27 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: build 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | timeout-minutes: 20 9 | strategy: 10 | matrix: 11 | java: [11] 12 | steps: 13 | - uses: actions/checkout@v6 14 | - name: Cache 15 | uses: actions/cache@v5 16 | env: 17 | cache-name: cache-sbt-libs 18 | with: 19 | path: | 20 | ~/.ivy2/cache 21 | ~/.sbt 22 | ~/.coursier 23 | key: build-${{ env.cache-name }}-${{ hashFiles('build.sbt') }} 24 | - name: Set up JDK 25 | uses: actions/setup-java@v5 26 | with: 27 | distribution: temurin 28 | java-version: ${{ matrix.java }} 29 | - uses: sbt/setup-sbt@v1 30 | - name: Run tests 31 | run: | 32 | git clone https://github.com/gitbucket/gitbucket.git 33 | cd gitbucket 34 | sbt publishLocal 35 | cd ../ 36 | sbt test 37 | - name: Assembly 38 | run: sbt assembly 39 | - name: Upload artifacts 40 | uses: actions/upload-artifact@v6 41 | with: 42 | name: gitbucket-gist-plugin-java${{ matrix.java }}-${{ github.sha }} 43 | path: ./target/scala-2.13/*.jar 44 | 45 | 46 | -------------------------------------------------------------------------------- /src/main/scala/gitbucket/gist/model/GistComment.scala: -------------------------------------------------------------------------------- 1 | package gitbucket.gist.model 2 | 3 | trait GistCommentComponent { self: gitbucket.core.model.Profile => 4 | import profile.api._ 5 | import self._ 6 | 7 | lazy val GistComments = new TableQuery(tag => new GistComments(tag)){ 8 | def autoInc = this returning this.map(_.commentId) 9 | } 10 | 11 | class GistComments(tag: Tag) extends Table[GistComment](tag, "GIST_COMMENT") { 12 | val userName = column[String]("USER_NAME") 13 | val repositoryName = column[String]("REPOSITORY_NAME") 14 | val commentId = column[Int]("COMMENT_ID", O AutoInc) 15 | val commentedUserName = column[String]("COMMENTED_USER_NAME") 16 | val content = column[String]("CONTENT") 17 | val registeredDate = column[java.util.Date]("REGISTERED_DATE") 18 | val updatedDate = column[java.util.Date]("UPDATED_DATE") 19 | def * = (userName, repositoryName, commentId, commentedUserName, content, registeredDate, updatedDate).<>(GistComment.tupled, GistComment.unapply) 20 | } 21 | } 22 | 23 | case class GistComment( 24 | userName: String, 25 | repositoryName: String, 26 | commentId: Int = 0, 27 | commentedUserName: String, 28 | content: String, 29 | registeredDate: java.util.Date, 30 | updatedDate: java.util.Date 31 | ) 32 | -------------------------------------------------------------------------------- /src/main/twirl/gitbucket/gist/forks.scala.html: -------------------------------------------------------------------------------- 1 | @(gist: gitbucket.gist.model.Gist, 2 | forkedCount: Int, 3 | repositoryUrl: gitbucket.gist.util.GistUtils.GistRepositoryURL, 4 | forkedGists: Seq[gitbucket.gist.model.Gist], 5 | editable: Boolean)(implicit context: gitbucket.core.controller.Context) 6 | @import gitbucket.core.view.helpers 7 | @gitbucket.core.html.main("Snippets"){ 8 | 9 |
10 |
11 | @gitbucket.gist.html.header(gist, forkedCount, editable) 12 |
13 | @gitbucket.gist.html.menu("forks", gist, repositoryUrl) 14 |
15 | @forkedGists.map { forkedGist => 16 |
17 | @helpers.avatar(forkedGist.userName, 20) 18 | @forkedGist.userName 19 |
20 | View Fork 21 |
22 |
23 | } 24 |
25 |
26 |
27 |
28 | } -------------------------------------------------------------------------------- /src/main/twirl/gitbucket/gist/profile.scala.html: -------------------------------------------------------------------------------- 1 | @(account: gitbucket.core.model.Account, groupNames: List[String], extraMailAddresses: List[String], 2 | gists: Seq[gitbucket.gist.model.Gist], createSnippet: Boolean)(implicit context: gitbucket.core.controller.Context) 3 | @import gitbucket.gist.model.Mode 4 | @gitbucket.core.account.html.main(account, groupNames, "snippets", extraMailAddresses){ 5 | @if(createSnippet){ 6 |
7 | Create snippet 8 |
9 | } 10 | @if(gists.isEmpty){ 11 | No snippets 12 | } else { 13 | @gists.map { gist => 14 |
15 |
16 | 17 |
18 |
19 |
20 | @gist.title 21 | @if(gist.mode == Mode.Secret.code){ 22 | Secret 23 | } 24 | @if(gist.mode == Mode.Private.code){ 25 | Private 26 | } 27 |
28 |
@gist.description
29 |
Updated @gitbucket.core.helper.html.datetimeago(gist.updatedDate)
30 |
31 |
32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /src/main/twirl/gitbucket/gist/commentpreview.scala.html: -------------------------------------------------------------------------------- 1 | @(gist: gitbucket.gist.model.Gist, 2 | content: String, 3 | placeholder: String = "Leave a comment", 4 | style: String = "", 5 | elastic: Boolean = false, 6 | uid: Long = new java.util.Date().getTime())(implicit context: gitbucket.core.controller.Context) 7 | @import gitbucket.core.view.helpers 8 |
9 | 13 |
14 |
15 | 16 | 17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 | 41 | -------------------------------------------------------------------------------- /src/main/scala/gitbucket/gist/model/Gist.scala: -------------------------------------------------------------------------------- 1 | package gitbucket.gist.model 2 | 3 | import gitbucket.core.issues.milestones.html.milestone 4 | 5 | trait GistComponent { self: gitbucket.core.model.Profile => 6 | import profile.api._ 7 | import self._ 8 | 9 | lazy val Gists = TableQuery[Gists] 10 | 11 | class Gists(tag: Tag) extends Table[Gist](tag, "GIST") { 12 | val userName = column[String]("USER_NAME") 13 | val repositoryName = column[String]("REPOSITORY_NAME") 14 | val title = column[String]("TITLE") 15 | val description = column[String]("DESCRIPTION") 16 | val registeredDate = column[java.util.Date]("REGISTERED_DATE") 17 | val updatedDate = column[java.util.Date]("UPDATED_DATE") 18 | val originUserName = column[String]("ORIGIN_USER_NAME") 19 | val originRepositoryName = column[String]("ORIGIN_REPOSITORY_NAME") 20 | val mode = column[String]("MODE") 21 | def * = (userName, repositoryName, title, description, registeredDate, updatedDate, originUserName.?, originRepositoryName.?, mode).<>(Gist.tupled, Gist.unapply) 22 | } 23 | } 24 | 25 | case class Gist( 26 | userName: String, 27 | repositoryName: String, 28 | title: String, 29 | description: String, 30 | registeredDate: java.util.Date, 31 | updatedDate: java.util.Date, 32 | originUserName: Option[String], 33 | originRepositoryName: Option[String], 34 | mode: String 35 | ){ 36 | def toRepositoryInfo = { 37 | gitbucket.core.service.RepositoryService.RepositoryInfo( 38 | owner = userName, 39 | name = repositoryName, 40 | repository = null, 41 | issueCount = 0, 42 | pullCount = 0, 43 | forkedCount = 0, 44 | milestoneCount = 0, 45 | branchList = Nil, 46 | tags = Nil, 47 | managers = Nil 48 | ) 49 | } 50 | } 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /src/main/twirl/gitbucket/gist/commentedit.scala.html: -------------------------------------------------------------------------------- 1 | @(gist: gitbucket.gist.model.Gist, content: String, commentId: Int)(implicit context: gitbucket.core.controller.Context) 2 | 3 | @gitbucket.gist.html.commentpreview( 4 | gist = gist, 5 | content = content, 6 | style = "height: 100px; max-height: 150px;", 7 | elastic = true, 8 | uid = commentId 9 | ) 10 |
11 | 12 | 13 |
14 | 46 | -------------------------------------------------------------------------------- /src/main/twirl/gitbucket/gist/header.scala.html: -------------------------------------------------------------------------------- 1 | @(gist: gitbucket.gist.model.Gist, 2 | forkedCount: Int, 3 | editable: Boolean)(implicit context: gitbucket.core.controller.Context) 4 | @import gitbucket.gist.model.Mode 5 | @import gitbucket.core.view.helpers 6 |
7 | @helpers.avatar(gist.userName, 24) 8 | @gist.userName / 9 | @gist.title 10 | @if(gist.mode == Mode.Secret.code){ 11 | Secret 12 | } 13 | @if(gist.mode == Mode.Private.code){ 14 | Private 15 | } 16 |
17 | @if(editable){ 18 | Edit 19 | Delete 20 | } 21 | @if(gist.originUserName.isEmpty){ 22 | @if(context.loginAccount.isEmpty){ 23 | Fork @forkedCount 24 | } else { 25 | Fork @forkedCount 26 | } 27 | @if(context.loginAccount.isDefined){ 28 |
29 |
30 | } 31 | } 32 |
33 |
34 | Created at @gist.registeredDate 35 | @if(gist.originUserName.isDefined){ 36 | - forked from @gist.originUserName/@gist.originRepositoryName 37 | } 38 |
39 |
40 | -------------------------------------------------------------------------------- /src/main/twirl/gitbucket/gist/detail.scala.html: -------------------------------------------------------------------------------- 1 | @(gist: gitbucket.gist.model.Gist, 2 | forkedCount: Int, 3 | repositoryUrl: gitbucket.gist.util.GistUtils.GistRepositoryURL, 4 | revision: String, 5 | files: Seq[(String, String)], 6 | comments: Seq[gitbucket.gist.model.GistComment], 7 | editable: Boolean)(implicit context: gitbucket.core.controller.Context) 8 | @import gitbucket.core.view.helpers 9 | @gitbucket.core.html.main(if(gist.description.isEmpty) gist.repositoryName else gist.description){ 10 | 11 |
12 |
13 | @gitbucket.gist.html.header(gist, forkedCount, editable) 14 |
15 | @gitbucket.gist.html.menu("code", gist, repositoryUrl) 16 |
17 |
18 | @gist.description 19 |
20 | @files.map { case (fileName, content) => 21 |
22 |
23 | @fileName 24 |
25 | Raw 26 |
27 |
28 | @if(helpers.isRenderable(fileName)){ 29 |
30 | @helpers.renderMarkup(List(fileName), content, "master", gist.toRepositoryInfo, false, false, true) 31 |
32 | } else { 33 |
34 |
@content
35 |
36 | } 37 |
38 | } 39 | @gitbucket.gist.html.commentlist(gist, comments, editable, gist.toRepositoryInfo) 40 | @gitbucket.gist.html.commentform(gist) 41 |
42 |
43 |
44 |
45 | 52 | } 53 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gitbucket-gist-plugin [![build](https://github.com/gitbucket/gitbucket-gist-plugin/workflows/build/badge.svg?branch=master)](https://github.com/gitbucket/gitbucket-gist-plugin/actions?query=workflow%3Abuild+branch%3Amaster) 2 | 3 | This is a GitBucket plug-in which provides code snippet repository like Gist. 4 | 5 | | Plugin version | GitBucket version | 6 | |:---------------|:------------------| 7 | | 4.23.x | 4.40.x - | 8 | | 4.22.x | 4.37.x - | 9 | | 4.21.x | 4.36.x - | 10 | | 4.20.x | 4.35.x - | 11 | | 4.19.x | 4.34.x - | 12 | | 4.18.x | 4.32.x - 4.33.x | 13 | | 4.17.x | 4.30.x - 4.31.x | 14 | | 4.16.x | 4.26.x - 4.29.x | 15 | | 4.15.x | 4.25.x | 16 | | 4.13.x, 4.14.x | 4.23.x - 4.24.x | 17 | | 4.12.x | 4.21.x - 4.22.x | 18 | | 4.11.x | 4.19.x - 4.20.x | 19 | | 4.10.x | 4.15.x - 4.18.x | 20 | | 4.9.x | 4.14.x | 21 | | 4.8.x | 4.11.x - 4.13.x | 22 | | 4.7.x | 4.11.x | 23 | | 4.6.x | 4.10.x | 24 | | 4.5.x | 4.9.x | 25 | | 4.4.x | 4.8.x | 26 | | 4.2.x, 4.3.x | 4.2.x - 4.7.x | 27 | | 4.0.x | 4.0.x - 4.1.x | 28 | | 3.13.x | 3.13.x | 29 | | 3.12.x | 3.12.x | 30 | | 3.11.x | 3.11.x | 31 | | 3.10.x | 3.10.x | 32 | | 3.7.x | 3.7.x - 3.9.x | 33 | | 3.6.x | 3.6.x | 34 | 35 | ## Installation 36 | 37 | Download jar file from [Releases page](https://github.com/gitbucket/gitbucket-gist-plugin/releases) and put into `GITBUCKET_HOME/plugins`. 38 | 39 | **Note:** If you had used this plugin with GitBucket 3.x, it does not work after upgrading to GitBucket 4.x. Solution is below: 40 | 41 | 1. `UPDATE VERSIONS SET VERSION='2.0.0' WHERE MODULE_ID='gist';` 42 | 2. restart gitbucket 43 | 3. can open snippets page 44 | 4. `SELECT VERSION FROM VERSIONS WHERE MODULE_ID='gist'` -> `4.2.0` 45 | 46 | See [Connect to H2 database](https://github.com/gitbucket/gitbucket/wiki/Connect-to-H2-database) to know how to execute SQL on the GitBucket database. 47 | 48 | ## Build from source 49 | 50 | Run `sbt assembly` and copy generated `/target/scala-2.13/gitbucket-gist-plugin-x.x.x.jar` to `~/.gitbucket/plugins/` (If the directory does not exist, create it by hand before copying the jar), or just run `sbt install`. 51 | -------------------------------------------------------------------------------- /src/main/scala/gitbucket/gist/service/GistCommentService.scala: -------------------------------------------------------------------------------- 1 | package gitbucket.gist.service 2 | 3 | import scala.language.reflectiveCalls 4 | import gitbucket.gist.model.GistComment 5 | import gitbucket.gist.model.Profile._ 6 | import gitbucket.gist.model.Profile.profile.blockingApi._ 7 | import gitbucket.gist.model.Profile.dateColumnType 8 | 9 | trait GistCommentService { 10 | 11 | def registerGistComment(userName: String, repositoryName: String, content: String, commentedUserName: String) 12 | (implicit s: Session): Int = 13 | GistComments.autoInc insert GistComment( 14 | userName = userName, 15 | repositoryName = repositoryName, 16 | commentedUserName = commentedUserName, 17 | content = content, 18 | registeredDate = currentDate, 19 | updatedDate = currentDate) 20 | 21 | def getGistComments(userName: String, repositoryName: String)(implicit s: Session): Seq[GistComment] = 22 | GistComments.filter { t => 23 | (t.userName === userName.bind) && 24 | (t.repositoryName === repositoryName.bind) 25 | }.sortBy(_.registeredDate.desc).list 26 | 27 | def getGistComment(userName: String, repositoryName: String, commentId: Int)(implicit s: Session): Option[GistComment] = 28 | GistComments.filter { t => 29 | (t.userName === userName.bind) && 30 | (t.repositoryName === repositoryName.bind) && 31 | (t.commentId === commentId.bind) 32 | }.firstOption 33 | 34 | def updateGistComment(userName: String, repositoryName: String, commentId: Int, content: String)(implicit s: Session): Int = 35 | GistComments.filter { t => 36 | (t.userName === userName.bind) && 37 | (t.repositoryName === repositoryName.bind) && 38 | (t.commentId === commentId.bind) 39 | }.map { t => 40 | (t.content, t.updatedDate) 41 | }.update(content, currentDate) 42 | 43 | def deleteGistComment(userName: String, repositoryName: String, commentId: Int)(implicit s: Session): Int = 44 | GistComments.filter { t => 45 | (t.userName === userName.bind) && 46 | (t.repositoryName === repositoryName.bind) && 47 | (t.commentId === commentId.bind) 48 | }.delete 49 | 50 | def getCommentCount(userName: String, repositoryName: String)(implicit s: Session): Int = 51 | Query(GistComments.filter { t => 52 | (t.userName === userName.bind) && 53 | (t.repositoryName === repositoryName.bind) 54 | }.length).first 55 | 56 | } 57 | -------------------------------------------------------------------------------- /src/main/twirl/gitbucket/gist/editor.scala.html: -------------------------------------------------------------------------------- 1 | @(i: Int, 2 | fileName: String, 3 | content: gitbucket.core.util.JGitUtil.ContentInfo)(implicit context: gitbucket.core.controller.Context) 4 | @import gitbucket.core.view.helpers 5 |
6 |
7 |
8 | 14 |
15 | 16 | 17 |
18 |
19 |
20 |
21 |
22 | 23 | 24 | 25 | 26 | 61 | -------------------------------------------------------------------------------- /src/main/scala/gitbucket/gist/util/GistUtils.scala: -------------------------------------------------------------------------------- 1 | package gitbucket.gist.util 2 | 3 | import gitbucket.core.controller.Context 4 | import gitbucket.core.model.Account 5 | import gitbucket.core.service.SystemSettingsService 6 | import gitbucket.core.service.SystemSettingsService.SystemSettings 7 | import gitbucket.core.util.JGitUtil 8 | import gitbucket.gist.model.Gist 9 | import org.eclipse.jgit.api.Git 10 | import org.eclipse.jgit.dircache.DirCache 11 | import org.eclipse.jgit.lib.{FileMode, Constants, ObjectId} 12 | 13 | object GistUtils { 14 | 15 | def isEditable(userName: String, groupNames: Seq[String])(implicit context: Context): Boolean = { 16 | context.loginAccount.map { loginAccount => 17 | loginAccount.isAdmin || loginAccount.userName == userName || groupNames.contains(userName) 18 | }.getOrElse(false) 19 | } 20 | 21 | def commitFiles(git: Git, loginAccount: Account, message: String, files: Seq[(String, String)]): ObjectId = { 22 | val builder = DirCache.newInCore.builder() 23 | val inserter = git.getRepository.newObjectInserter() 24 | val headId = git.getRepository.resolve(Constants.HEAD + "^{commit}") 25 | 26 | files.foreach { case (fileName, content) => 27 | builder.add(JGitUtil.createDirCacheEntry(fileName, FileMode.REGULAR_FILE, 28 | inserter.insert(Constants.OBJ_BLOB, content.getBytes("UTF-8")))) 29 | } 30 | builder.finish() 31 | 32 | val commitId = JGitUtil.createNewCommit(git, inserter, headId, builder.getDirCache.writeTree(inserter), 33 | Constants.HEAD, loginAccount.fullName, loginAccount.mailAddress, message) 34 | 35 | inserter.flush() 36 | inserter.close() 37 | 38 | commitId 39 | } 40 | 41 | def getLines(fileName: String, source: String): String = { 42 | val lines = source.split("\n").map(_.trim).take(10) 43 | 44 | (if((fileName.endsWith(".md") || fileName.endsWith(".markdown")) && lines.count(_ == "```") % 2 != 0) { 45 | lines :+ "```" 46 | } else { 47 | lines 48 | }).mkString("\n") 49 | } 50 | 51 | def isGistFile(fileName: String): Boolean = fileName.matches("gistfile[0-9]+\\.txt") 52 | 53 | def getTitle(fileName: String, repoName: String): String = if(isGistFile(fileName)) repoName else fileName 54 | 55 | case class GistRepositoryURL(gist: Gist, baseUrl: String, settings: SystemSettings){ 56 | 57 | def httpUrl: String = s"${baseUrl}/git/gist/${gist.userName}/${gist.repositoryName}.git" 58 | 59 | def embedUrl: String = s"${baseUrl}/gist/${gist.userName}/${gist.repositoryName}.js" 60 | 61 | def sshUrl: Option[String] = { 62 | settings.sshUrl.map { sshUrl => 63 | s"${sshUrl}/gist/${gist.userName}/${gist.repositoryName}.git" 64 | } 65 | } 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /src/main/resources/update/gitbucket-gist_2.0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /src/main/twirl/gitbucket/gist/detail.scala.js: -------------------------------------------------------------------------------- 1 | @(gist: gitbucket.gist.model.Gist, 2 | repositoryUrl: gitbucket.gist.util.GistUtils.GistRepositoryURL, 3 | revision: String, 4 | files: Seq[(String, String)] 5 | )(implicit context: gitbucket.core.controller.Context) 6 | @import gitbucket.core.view.helpers 7 | jqueryScript = document.createElement('script'); 8 | jqueryScript.src = '@helpers.assets("/vendors/jquery/jquery-1.12.2.min.js")'; 9 | document.head.appendChild(jqueryScript); 10 | prettifyScript = document.createElement('script'); 11 | prettifyScript.src = '@helpers.assets("/vendors/google-code-prettify/prettify.js")'; 12 | prettifyScript.onload = function() { prettyPrint(); $('pre:has(> span.pln)').hide(); }; 13 | document.head.appendChild(prettifyScript); 14 | fireScript = document.createElement('script'); 15 | fireScript.setAttribute('type','text/javascript'); 16 | fireScript.text = '$(document).load(function() { prettyPrint(); });' 17 | 18 | var _html = (function () {/*4f85e035-2513-453b-b435-33f0a12b2339 19 |
20 |
21 |
22 |
23 | @gist.description 24 |
25 | @files.map { case (fileName, content) => 26 |
27 |
28 | @fileName 29 |
30 | Raw 31 |
32 |
33 | @if(helpers.isRenderable(fileName)){ 34 |
35 | @helpers.renderMarkup(List(fileName), content, "master", gist.toRepositoryInfo, false, false, true) 36 |
37 | } else { 38 |
39 |
@content.toString.replaceAll("<","<").replaceAll(">",">")
40 |               
41 | } 42 |
43 | } 44 |
45 |
46 |
47 | 4f85e035-2513-453b-b435-33f0a12b2339*/}).toString().replace(/(\n)/g, '').split('4f85e035-2513-453b-b435-33f0a12b2339')[1]; 48 | 49 | document.write(''); 50 | document.write(''); 51 | document.write(''); 52 | document.write(''); 53 | document.write(''); 54 | document.write(_html.replace(/\\r\\n/g,"\n").replace(/\\/g,"")); 55 | -------------------------------------------------------------------------------- /src/main/scala/gitbucket/gist/service/GistService.scala: -------------------------------------------------------------------------------- 1 | package gitbucket.gist.service 2 | 3 | import gitbucket.core.model.Account 4 | import gitbucket.gist.model.Gist 5 | import gitbucket.gist.model.Mode 6 | import gitbucket.gist.model.Profile._ 7 | import gitbucket.gist.model.Profile.profile.blockingApi._ 8 | import gitbucket.gist.model.Profile.dateColumnType 9 | 10 | trait GistService { 11 | 12 | def getVisibleGists(offset: Int, limit: Int, account: Option[Account])(implicit s: Session): Seq[Gist] = 13 | visibleHistsQuery(account).sortBy(_.registeredDate desc).drop(offset).take(limit).list 14 | 15 | def countVisibleGists(account: Option[Account])(implicit s: Session): Int = 16 | Query(visibleHistsQuery(account).length).first 17 | 18 | private def visibleHistsQuery(account: Option[Account]): Query[Gists, Gists#TableElementType, Seq] = { 19 | account.map { x => 20 | Gists.filter { t => (t.mode === "PUBLIC".bind) || (t.userName === x.userName.bind) } 21 | } getOrElse { 22 | Gists.filter { t => (t.mode === "PUBLIC".bind) } 23 | } 24 | } 25 | 26 | def getUserGists(userName: String, loginUserName: Option[String], offset: Int, limit: Int)(implicit s: Session): Seq[Gist] = 27 | userGistsQuery(userName, loginUserName).sortBy(_.registeredDate desc).drop(offset).take(limit).list 28 | 29 | 30 | def countUserGists(userName: String, loginUserName: Option[String])(implicit s: Session): Int = 31 | Query(userGistsQuery(userName, loginUserName).length).first 32 | 33 | private def userGistsQuery(userName: String, loginUserName: Option[String]): Query[Gists, Gists#TableElementType, Seq] = { 34 | if (loginUserName.isDefined) { 35 | Gists filter (t => (t.userName === userName.bind) && ((t.userName === loginUserName.bind) || (t.mode === "PUBLIC".bind))) 36 | } else { 37 | Gists filter (t => (t.userName === userName.bind) && (t.mode === "PUBLIC".bind)) 38 | } 39 | } 40 | 41 | def getGist(userName: String, repositoryName: String)(implicit s: Session): Option[Gist] = 42 | Gists.filter(t => (t.userName === userName.bind) && (t.repositoryName === repositoryName.bind)).firstOption 43 | 44 | def getForkedCount(userName: String, repositoryName: String)(implicit s: Session): Int = 45 | Query(Gists.filter(t => (t.originUserName === userName.bind) && (t.originRepositoryName === repositoryName.bind)).length).first 46 | 47 | def getForkedGists(userName: String, repositoryName: String)(implicit s: Session): Seq[Gist] = 48 | Gists.filter(t => (t.originUserName === userName.bind) && (t.originRepositoryName === repositoryName.bind)).sortBy(_.userName).list 49 | 50 | def registerGist(userName: String, repositoryName: String, title: String, description: String, mode: Mode, 51 | originUserName: Option[String] = None, originRepositoryName: Option[String] = None)(implicit s: Session): Unit = 52 | Gists.insert(Gist(userName, repositoryName, title, description, new java.util.Date(), new java.util.Date(), 53 | originUserName, originRepositoryName, mode.code)) 54 | 55 | def updateGist(userName: String, repositoryName: String, title: String, description: String, mode: Mode)(implicit s: Session): Unit = 56 | Gists 57 | .filter(t => (t.userName === userName.bind) && (t.repositoryName === repositoryName.bind)) 58 | .map(t => (t.title, t.description, t.updatedDate, t.mode)) 59 | .update(title, description, new java.util.Date(), mode.code) 60 | 61 | def deleteGist(userName: String, repositoryName: String)(implicit s: Session): Unit = { 62 | GistComments.filter(t => (t.userName === userName.bind) && (t.repositoryName === repositoryName.bind)).delete 63 | Gists .filter(t => (t.userName === userName.bind) && (t.repositoryName === repositoryName.bind)).delete 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /src/main/twirl/gitbucket/gist/commentlist.scala.html: -------------------------------------------------------------------------------- 1 | @(gist: gitbucket.gist.model.Gist, 2 | comments: Seq[gitbucket.gist.model.GistComment], 3 | hasWritePermission: Boolean, 4 | repository: gitbucket.core.service.RepositoryService.RepositoryInfo)(implicit context: gitbucket.core.controller.Context) 5 | @import gitbucket.core.view.helpers 6 | @comments.map { comment => 7 |
8 |
9 | @helpers.avatar(comment.commentedUserName, 20) 10 | @helpers.user(comment.commentedUserName, styleClass="username strong") 11 | 12 | commented 13 | @gitbucket.core.helper.html.datetimeago(comment.registeredDate) 14 | 15 | 16 | @if((hasWritePermission || context.loginAccount.map(_.userName == comment.commentedUserName).getOrElse(false))){ 17 |   18 | 19 | } 20 | 21 |
22 |
23 | @helpers.markdown( 24 | markdown = comment.content, 25 | repository = repository, 26 | branch = "master", 27 | enableWikiLink = false, 28 | enableRefsLink = true, 29 | enableLineBreaks = true, 30 | enableTaskList = true, 31 | hasWritePermission = hasWritePermission 32 | ) 33 |
34 |
35 | } 36 | 92 | -------------------------------------------------------------------------------- /src/main/scala/Plugin.scala: -------------------------------------------------------------------------------- 1 | import gitbucket.core.controller.Context 2 | import gitbucket.core.model._ 3 | import gitbucket.core.service.AccountService 4 | import gitbucket.core.service.SystemSettingsService.SystemSettings 5 | import gitbucket.gist.controller.GistController 6 | import gitbucket.core.plugin._ 7 | import io.github.gitbucket.solidbase.migration.LiquibaseMigration 8 | import io.github.gitbucket.solidbase.model.Version 9 | import java.io.File 10 | import javax.servlet.ServletContext 11 | import gitbucket.gist.util.Configurations._ 12 | 13 | class Plugin extends gitbucket.core.plugin.Plugin { 14 | 15 | override val pluginId: String = "gist" 16 | 17 | override val pluginName: String = "Gist Plugin" 18 | 19 | override val description: String = "Provides Gist feature on GitBucket." 20 | 21 | override val versions: List[Version] = List( 22 | new Version("2.0.0", // This is mistake in 4.0.0 but it can't be fixed for migration. 23 | new LiquibaseMigration("update/gitbucket-gist_2.0.xml") 24 | ), 25 | new Version("4.2.0", 26 | new LiquibaseMigration("update/gitbucket-gist_4.2.xml") 27 | ), 28 | new Version("4.4.0"), 29 | new Version("4.5.0"), 30 | new Version("4.6.0"), 31 | new Version("4.7.0"), 32 | new Version("4.8.0"), 33 | new Version("4.9.0"), 34 | new Version("4.9.1"), 35 | new Version("4.10.0"), 36 | new Version("4.11.0"), 37 | new Version("4.12.0"), 38 | new Version("4.13.0"), 39 | new Version("4.14.0"), 40 | new Version("4.15.0"), 41 | new Version("4.16.0"), 42 | new Version("4.17.0"), 43 | new Version("4.18.0"), 44 | new Version("4.19.0"), 45 | new Version("4.20.0"), 46 | new Version("4.21.0"), 47 | new Version("4.22.0"), 48 | new Version("4.23.0") 49 | ) 50 | 51 | override def initialize(registry: PluginRegistry, context: ServletContext, settings: SystemSettings): Unit = { 52 | super.initialize(registry, context, settings) 53 | 54 | // Create gist repository directory 55 | val rootdir = new File(GistRepoDir) 56 | if(!rootdir.exists){ 57 | rootdir.mkdirs() 58 | } 59 | 60 | } 61 | 62 | override val repositoryRoutings = Seq( 63 | GitRepositoryRouting("gist/(.+?)/(.+?)\\.git", "gist/$1/$2", new GistRepositoryFilter()) 64 | ) 65 | 66 | override val controllers = Seq( 67 | "/*" -> new GistController() 68 | ) 69 | 70 | override val globalMenus = Seq( 71 | (context: Context) => Some(Link("snippets", "Snippets", "gist")) 72 | ) 73 | override val profileTabs = Seq( 74 | (account: Account, context: Context) => Some(Link("snippets", "Snippets", s"gist/${account.userName}/_profile")) 75 | ) 76 | override val assetsMappings = Seq("/gist" -> "/gitbucket/gist/assets") 77 | 78 | // override def javaScripts(registry: PluginRegistry, context: ServletContext, settings: SystemSettings): Seq[(String, String)] = { 79 | // // Add Snippet link to the header 80 | // val path = settings.baseUrl.getOrElse(context.getContextPath) 81 | // Seq( 82 | // ".*" -> s""" 83 | // |var accountName = $$('div.account-username').text(); 84 | // |if(accountName != ''){ 85 | // | var active = location.href.endsWith('_profile'); 86 | // | $$('li:has(a:contains(Public Activity))').after( 87 | // | $$('Snippets') 88 | // | ); 89 | // |} 90 | // """.stripMargin 91 | // ) 92 | // } 93 | } 94 | 95 | class GistRepositoryFilter extends GitRepositoryFilter with AccountService { 96 | 97 | override def filter(path: String, userName: Option[String], settings: SystemSettings, isUpdating: Boolean) 98 | (implicit session: Session): Boolean = { 99 | if(isUpdating){ 100 | (for { 101 | userName <- userName 102 | account <- getAccountByUserName(userName) 103 | } yield 104 | path.startsWith("/" + userName + "/") || account.isAdmin 105 | ).getOrElse(false) 106 | } else true 107 | } 108 | 109 | } 110 | -------------------------------------------------------------------------------- /src/main/twirl/gitbucket/gist/list.scala.html: -------------------------------------------------------------------------------- 1 | @(gistUser: Option[gitbucket.gist.model.GistUser], 2 | gists: Seq[(gitbucket.gist.model.Gist, gitbucket.gist.model.GistInfo)], 3 | page: Int, 4 | hasNext: Boolean)(implicit context: gitbucket.core.controller.Context) 5 | @import gitbucket.gist.model.Mode 6 | @import gitbucket.core.view.helpers 7 | @gitbucket.core.html.main(gistUser.map(user => s"${user.userName}'s Snippets").getOrElse("Snippets")){ 8 | 9 |
10 |
11 |
12 | @if(gistUser.isEmpty){ 13 |
14 | New snippet 15 |
16 |

Discover Snippets

17 | } else { 18 | @helpers.avatar(gistUser.get.userName, 24) 19 | @gistUser.get.fullName 20 | 23 |
24 | @gistUser.get.userName 25 |
26 | } 27 |
28 |
29 | @gists.map { case (gist, gistInfo) => 30 |
31 |
32 | @helpers.avatar(gist.userName, 20) 33 | @gist.userName / 34 | @gist.title 35 | Created at @gist.registeredDate 36 | @if(gist.mode == Mode.Secret.code){ 37 | Secret 38 | } 39 | @if(gist.mode == Mode.Private.code){ 40 | Private 41 | } 42 | 55 |
56 |
57 |
@gist.description
58 |
59 | @if(gistInfo.fileName.toLowerCase.endsWith(".md") || gistInfo.fileName.toLowerCase.endsWith(".markdown")){ 60 |
61 | @helpers.markdown(gistInfo.source, gist.toRepositoryInfo, "master", false, false, false) 62 |
63 | } else { 64 |
@gistInfo.source
65 | } 66 |
67 |
68 |
69 | } 70 |
71 | 72 | 73 |
74 |
75 |
76 |
77 | } -------------------------------------------------------------------------------- /src/main/twirl/gitbucket/gist/menu.scala.html: -------------------------------------------------------------------------------- 1 | @(active: String, 2 | gist: gitbucket.gist.model.Gist, 3 | repositoryUrl: gitbucket.gist.util.GistUtils.GistRepositoryURL)(implicit context: gitbucket.core.controller.Context) 4 | @menuitem(name: String, path: String, label: String, count: Int = 0) = { 5 |
  • 6 | 7 | @label 8 | @if(count > 0){ 9 |
    @count
    10 | } 11 |
    12 |
  • 13 | } 14 |
    15 | Download ZIP 16 |
    17 |
    18 |
    19 |
    20 | 23 | 45 |
    46 | @gitbucket.core.helper.html.copy("repository-url", "repository-url-copy", repositoryUrl.httpUrl){ 47 | 48 | } 49 |
    50 |
    51 | 107 | 130 | -------------------------------------------------------------------------------- /etc/icons.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /src/main/twirl/gitbucket/gist/revisions.scala.html: -------------------------------------------------------------------------------- 1 | @(gist: gitbucket.gist.model.Gist, 2 | forkedCount: Int, 3 | repositoryUrl: gitbucket.gist.util.GistUtils.GistRepositoryURL, 4 | editable: Boolean, 5 | revisions: List[(gitbucket.core.util.JGitUtil.CommitInfo, List[gitbucket.core.util.JGitUtil.DiffInfo])])(implicit context: gitbucket.core.controller.Context) 6 | @import gitbucket.core.view.helpers 7 | @import org.eclipse.jgit.diff.DiffEntry.ChangeType 8 | @gitbucket.core.html.main(s"Revisions · ${gist.repositoryName}"){ 9 | 10 |
    11 |
    12 | @gitbucket.gist.html.header(gist, forkedCount, editable) 13 |
    14 | @gitbucket.gist.html.menu("revision", gist, repositoryUrl) 15 |
    16 | @revisions.map { case (revision, diffs) => 17 |
    18 |
    19 | @helpers.avatar(revision, 20) @revision.authorName revised this @gitbucket.core.helper.html.datetimeago(revision.authorTime) 20 |
    21 | @if(diffs.isEmpty){ 22 | No changes. 23 | } 24 | @revision.id.substring(0, 7) 25 |
    26 |
    27 |
    28 | @diffs.zipWithIndex.map { case (diff, i) => 29 | 30 | 31 | 32 | 53 | 54 | 55 | 56 | 57 | 66 | 67 | 68 |
    33 | @diff.changeType match { 34 | case ChangeType.ADD => { 35 | 36 | 37 | @diff.newPath 38 | } 39 | case ChangeType.MODIFY => { 40 | 41 | 42 | @diff.newPath 43 | } 44 | case ChangeType.DELETE => { 45 | 46 | 47 | @diff.oldPath 48 | } 49 | case _ => { 50 | } 51 | } 52 |
    58 | @if(diff.newContent != None || diff.oldContent != None){ 59 |
    60 | 61 | 62 | } else { 63 | Not supported 64 | } 65 |
    69 | } 70 |
    71 |
    72 | } 73 |
    74 |
    75 |
    76 |
    77 | 78 | 79 | 80 | 131 | } -------------------------------------------------------------------------------- /src/main/twirl/gitbucket/gist/edit.scala.html: -------------------------------------------------------------------------------- 1 | @(gist: Option[gitbucket.gist.model.Gist], 2 | files: Seq[(String, gitbucket.core.util.JGitUtil.ContentInfo)], 3 | userName: Option[String])(implicit context: gitbucket.core.controller.Context) 4 | @import gitbucket.gist.model.Mode 5 | @import gitbucket.core.view.helpers 6 | @gitbucket.core.html.main("Snippets"){ 7 | 8 |
    9 |
    10 |
    11 |
    12 | @if(gist.isEmpty){ 13 |

    New snippet

    14 | } else { 15 | @gist.map { x => 16 | @helpers.avatar(gist.get.userName, 24) 17 | Editing 18 | @gist.get.title 19 | @if(gist.get.mode == Mode.Secret.code){ 20 | Secret 21 | } 22 | @if(gist.get.mode == Mode.Private.code){ 23 | Private 24 | } 25 |
    26 | Delete 27 |
    28 |
    29 | Created at @gist.get.registeredDate 30 |
    31 | } 32 | } 33 |
    34 |
    35 |
    36 | 37 |
    38 | @files.zipWithIndex.map { case ((fileName, content), i) => 39 | @gitbucket.gist.html.editor(i, fileName, content) 40 | } 41 |
    42 |
    43 | 44 |
    45 | @if(gist.isDefined){ 46 | Cancel 47 | } 48 |
    49 | 52 | 55 | 58 |
    59 | @if(gist.isDefined){ 60 | 61 | } else { 62 | 63 | } 64 |
    65 |
    66 | @userName.map { userName => 67 | 68 | } 69 | 70 |
    71 |
    72 |
    73 |
    74 | 75 | 76 | 77 | 78 | 79 | 141 | } 142 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /src/main/scala/gitbucket/gist/controller/GistController.scala: -------------------------------------------------------------------------------- 1 | package gitbucket.gist.controller 2 | 3 | import java.io.File 4 | import gitbucket.core.view.helpers 5 | import org.scalatra.forms._ 6 | 7 | import gitbucket.core.controller.ControllerBase 8 | import gitbucket.core.service.AccountService 9 | import gitbucket.core.service.RepositoryService.RepositoryInfo 10 | import gitbucket.core.util._ 11 | import gitbucket.core.util.Implicits._ 12 | import gitbucket.core.view.helpers._ 13 | 14 | import gitbucket.gist.model._ 15 | import gitbucket.gist.service._ 16 | import gitbucket.gist.util._ 17 | import gitbucket.gist.util.GistUtils._ 18 | import gitbucket.gist.util.Configurations._ 19 | import gitbucket.gist.html 20 | import gitbucket.gist.js 21 | 22 | import org.eclipse.jgit.api.Git 23 | import org.eclipse.jgit.lib._ 24 | import org.scalatra.Ok 25 | import play.twirl.api.Html 26 | import play.twirl.api.JavaScript 27 | import scala.util.Using 28 | 29 | class GistController extends GistControllerBase with GistService with GistCommentService with AccountService 30 | with GistEditorAuthenticator with UsersAuthenticator 31 | 32 | trait GistControllerBase extends ControllerBase { 33 | self: GistService with GistCommentService with AccountService with GistEditorAuthenticator with UsersAuthenticator => 34 | 35 | case class CommentForm(content: String) 36 | 37 | val commentForm = mapping( 38 | "content" -> trim(label("Comment", text(required))) 39 | )(CommentForm.apply) 40 | 41 | //////////////////////////////////////////////////////////////////////////////// 42 | // 43 | // Gist Actions 44 | // 45 | //////////////////////////////////////////////////////////////////////////////// 46 | 47 | get("/gist"){ 48 | val page = request.getParameter("page") match { 49 | case ""|null => 1 50 | case s => s.toInt 51 | } 52 | val result = getVisibleGists((page - 1) * Limit, Limit, context.loginAccount) 53 | val count = countVisibleGists(context.loginAccount) 54 | 55 | val gists: Seq[(Gist, GistInfo)] = result.map { gist => 56 | val userName = gist.userName 57 | val repoName = gist.repositoryName 58 | val files = getGistFiles(userName, repoName) 59 | val (fileName, source) = files.head 60 | 61 | (gist, GistInfo(fileName, getLines(fileName, source), files.length, getForkedCount(userName, repoName), getCommentCount(userName, repoName))) 62 | } 63 | 64 | html.list(None, gists, page, page * Limit < count) 65 | } 66 | 67 | get("/gist/:userName/:repoName"){ 68 | _gist(params("userName"), Some(params("repoName"))) 69 | } 70 | 71 | get("/gist/:userName/:repoName.js"){ 72 | val userName = params("userName") 73 | val repoName = params("repoName") 74 | getGist(userName, repoName) match { 75 | case Some(gist) => 76 | _embedJs(gist, userName, repoName, "master") 77 | case None => 78 | NotFound() 79 | } 80 | } 81 | 82 | get("/gist/:userName/:repoName/:revision"){ 83 | _gist(params("userName"), Some(params("repoName")), params("revision")) 84 | } 85 | 86 | get("/gist/:userName/:repoName/edit")(editorOnly { 87 | val userName = params("userName") 88 | val repoName = params("repoName") 89 | val gitdir = new File(GistRepoDir, userName + "/" + repoName) 90 | if(gitdir.exists){ 91 | Using.resource(Git.open(gitdir)){ git => 92 | val files: Seq[(String, JGitUtil.ContentInfo)] = JGitUtil.getFileList(git, "master", ".").map { file => 93 | (if(isGistFile(file.name)) "" else file.name) -> JGitUtil.getContentInfo(git, file.name, file.id, true) 94 | } 95 | html.edit(getGist(userName, repoName), files, None) 96 | } 97 | } 98 | }) 99 | 100 | post("/gist/_new")(usersOnly { 101 | val loginAccount = context.loginAccount.get 102 | val userName = params.getOrElse("userName", loginAccount.userName) 103 | 104 | if(isEditable(userName, loginUserGroups)) { 105 | val files = getFileParameters() 106 | if(files.isEmpty){ 107 | redirect(s"/gist") 108 | 109 | } else { 110 | val mode = Mode.from(params("mode")) 111 | val description = params("description") 112 | 113 | // Create new repository 114 | val repoName = StringUtil.md5(userName + " " + datetime(new java.util.Date())) 115 | val gitdir = new File(GistRepoDir, userName + "/" + repoName) 116 | gitdir.mkdirs() 117 | JGitUtil.initRepository(gitdir, "master") 118 | 119 | // Insert record 120 | registerGist( 121 | userName, 122 | repoName, 123 | getTitle(files.head._1, repoName), 124 | description, 125 | mode 126 | ) 127 | 128 | // Commit files 129 | Using.resource(Git.open(gitdir)){ git => 130 | commitFiles(git, loginAccount, "Initial commit", files) 131 | } 132 | 133 | redirect(s"/gist/${userName}/${repoName}") 134 | } 135 | } else Unauthorized() 136 | }) 137 | 138 | post("/gist/:userName/:repoName/edit")(editorOnly { 139 | val userName = params("userName") 140 | val repoName = params("repoName") 141 | 142 | val loginAccount = context.loginAccount.get 143 | val files = getFileParameters() 144 | val description = params("description") 145 | val mode = Mode.from(params("mode")) 146 | 147 | // Update database 148 | updateGist( 149 | userName, 150 | repoName, 151 | getTitle(files.head._1, repoName), 152 | description, 153 | mode 154 | ) 155 | 156 | // Commit files 157 | val gitdir = new File(GistRepoDir, userName + "/" + repoName) 158 | Using.resource(Git.open(gitdir)){ git => 159 | val commitId = commitFiles(git, loginAccount, "Update", files) 160 | 161 | // update refs 162 | val refUpdate = git.getRepository.updateRef(Constants.HEAD) 163 | refUpdate.setNewObjectId(commitId) 164 | refUpdate.setForceUpdate(false) 165 | refUpdate.setRefLogIdent(new org.eclipse.jgit.lib.PersonIdent(loginAccount.fullName, loginAccount.mailAddress)) 166 | //refUpdate.setRefLogMessage("merged", true) 167 | refUpdate.update() 168 | } 169 | 170 | redirect(s"/gist/${userName}/${repoName}") 171 | }) 172 | 173 | get("/gist/:userName/:repoName/delete")(editorOnly { 174 | val userName = params("userName") 175 | val repoName = params("repoName") 176 | 177 | if(isEditable(userName, loginUserGroups)){ 178 | deleteGist(userName, repoName) 179 | 180 | val gitdir = new File(GistRepoDir, userName + "/" + repoName) 181 | org.apache.commons.io.FileUtils.deleteDirectory(gitdir) 182 | 183 | redirect(s"/gist/${userName}") 184 | } 185 | }) 186 | 187 | get("/gist/:userName/:repoName/revisions"){ 188 | val userName = params("userName") 189 | val repoName = params("repoName") 190 | val gitdir = new File(GistRepoDir, userName + "/" + repoName) 191 | 192 | Using.resource(Git.open(gitdir)){ git => 193 | JGitUtil.getCommitLog(git, "master") match { 194 | case Right((revisions, hasNext)) => { 195 | val commits = revisions.map { revision => 196 | (revision, JGitUtil.getDiffs(git, None, revision.id, true, false)) 197 | } 198 | 199 | val gist = getGist(userName, repoName).get 200 | val originUserName = gist.originUserName.getOrElse(userName) 201 | val originRepoName = gist.originRepositoryName.getOrElse(repoName) 202 | 203 | html.revisions( 204 | gist, 205 | getForkedCount(originUserName, originRepoName), 206 | GistRepositoryURL(gist, baseUrl, context.settings), 207 | isEditable(userName, loginUserGroups), 208 | commits 209 | ) 210 | } 211 | case Left(_) => NotFound() 212 | } 213 | } 214 | } 215 | 216 | get("/gist/:userName/:repoName/raw/:revision/:fileName"){ 217 | val userName = params("userName") 218 | val repoName = params("repoName") 219 | val revision = params("revision") 220 | val fileName = params("fileName") 221 | val gitdir = new File(GistRepoDir, userName + "/" + repoName) 222 | if(gitdir.exists){ 223 | Using.resource(Git.open(gitdir)){ git => 224 | val gist = getGist(userName, repoName).get 225 | 226 | if(gist.mode == "PUBLIC" || context.loginAccount.exists(x => x.isAdmin || x.userName == userName)){ 227 | JGitUtil.getFileList(git, revision, ".").find(_.name == fileName).map { file => 228 | val bytes = JGitUtil.getContentFromId(git, file.id, false).get 229 | RawData(FileUtil.getMimeType(file.name, bytes), bytes) 230 | } getOrElse NotFound() 231 | } else Unauthorized() 232 | } 233 | } else NotFound() 234 | } 235 | 236 | get("/gist/:userName/:repoName/download/*"){ 237 | val format = multiParams("splat").head match { 238 | case name if name.endsWith(".zip") => "zip" 239 | case name if name.endsWith(".tar.gz") => "tar.gz" 240 | } 241 | 242 | val userName = params("userName") 243 | val repoName = params("repoName") 244 | 245 | Using.resource(Git.open(new File(GistRepoDir, userName + "/" + repoName))){ git => 246 | val revCommit = JGitUtil.getRevCommitFromId(git, git.getRepository.resolve("master")) 247 | 248 | contentType = "application/octet-stream" 249 | response.setHeader("Content-Disposition", s"attachment; filename=${repoName}.${format}") 250 | response.setBufferSize(1024 * 1024); 251 | 252 | git.archive 253 | .setFormat(format) 254 | .setTree(revCommit.getTree) 255 | .setOutputStream(response.getOutputStream) 256 | .call() 257 | 258 | () 259 | } 260 | } 261 | 262 | get("/gist/:userName/_profile"){ 263 | val userName = params("userName") 264 | 265 | val result: (Seq[Gist], Int) = ( 266 | getUserGists(userName, context.loginAccount.map(_.userName), 0, Limit), 267 | countUserGists(userName, context.loginAccount.map(_.userName)) 268 | ) 269 | 270 | val createSnippet = context.loginAccount.exists { loginAccount => 271 | loginAccount.userName == userName || getGroupsByUserName(loginAccount.userName).contains(userName) 272 | } 273 | 274 | getAccountByUserName(userName).map { account => 275 | html.profile( 276 | account = account, 277 | groupNames = if(account.isGroupAccount) Nil else getGroupsByUserName(userName), 278 | extraMailAddresses = getAccountExtraMailAddresses(userName), 279 | gists = result._1, 280 | createSnippet = createSnippet 281 | ) 282 | } getOrElse NotFound() 283 | } 284 | 285 | get("/gist/:userName"){ 286 | _gist(params("userName")) 287 | } 288 | 289 | get("/gist/_new")(usersOnly { 290 | val userName = params.get("userName") 291 | 292 | if(isEditable(userName.getOrElse(context.loginAccount.get.userName), loginUserGroups)){ 293 | html.edit(None, Seq(("", JGitUtil.ContentInfo("text", None, None, Some("UTF-8")))), userName) 294 | } else Unauthorized() 295 | }) 296 | 297 | get("/gist/_add"){ 298 | val count = params("count").toInt 299 | html.editor(count, "", JGitUtil.ContentInfo("text", None, None, Some("UTF-8"))) 300 | } 301 | 302 | //////////////////////////////////////////////////////////////////////////////// 303 | // 304 | // Fork Actions 305 | // 306 | //////////////////////////////////////////////////////////////////////////////// 307 | 308 | post("/gist/:userName/:repoName/fork")(usersOnly { 309 | val userName = params("userName") 310 | val repoName = params("repoName") 311 | val loginAccount = context.loginAccount.get 312 | 313 | if(getGist(loginAccount.userName, repoName).isDefined){ 314 | redirect(s"/gist/${userName}/${repoName}") 315 | } else { 316 | getGist(userName, repoName).map { gist => 317 | // Insert to the database at first 318 | val originUserName = gist.originUserName.getOrElse(gist.userName) 319 | val originRepoName = gist.originRepositoryName.getOrElse(gist.repositoryName) 320 | 321 | registerGist(loginAccount.userName, repoName, gist.title, gist.description, Mode.from(gist.mode), 322 | Some(originUserName), Some(originRepoName)) 323 | 324 | // Clone repository 325 | JGitUtil.cloneRepository( 326 | new File(GistRepoDir, userName + "/" + repoName), 327 | new File(GistRepoDir, loginAccount.userName + "/" + repoName) 328 | ) 329 | 330 | redirect(s"/gist/${loginAccount.userName}/${repoName}") 331 | 332 | } getOrElse NotFound() 333 | } 334 | }) 335 | 336 | get("/gist/:userName/:repoName/forks"){ 337 | val userName = params("userName") 338 | val repoName = params("repoName") 339 | 340 | getGist(userName, repoName).map { gist => 341 | html.forks( 342 | gist, 343 | getForkedCount(userName, repoName), 344 | GistRepositoryURL(gist, baseUrl, context.settings), 345 | getForkedGists(userName, repoName), 346 | isEditable(userName, loginUserGroups) 347 | ) 348 | } getOrElse NotFound() 349 | } 350 | 351 | //////////////////////////////////////////////////////////////////////////////// 352 | // 353 | // Comment Actions 354 | // 355 | //////////////////////////////////////////////////////////////////////////////// 356 | 357 | post("/gist/:userName/:repoName/_preview"){ 358 | val userName = params("userName") 359 | val repoName = params("repoName") 360 | 361 | contentType = "text/html" 362 | helpers.markdown( 363 | markdown = params("content"), 364 | repository = RepositoryInfo( 365 | owner = userName, 366 | name = repoName, 367 | repository = null, 368 | issueCount = 0, 369 | pullCount = 0, 370 | forkedCount = 0, 371 | milestoneCount = 0, 372 | branchList = Nil, 373 | tags = Nil, 374 | managers = Nil 375 | ), 376 | branch = "master", 377 | enableWikiLink = false, 378 | enableRefsLink = false, 379 | enableLineBreaks = false, 380 | enableAnchor = false, 381 | enableTaskList = true 382 | ) 383 | } 384 | 385 | post("/gist/:userName/:repoName/_comment", commentForm)(usersOnly { form => 386 | val userName = params("userName") 387 | val repoName = params("repoName") 388 | val loginAccount = context.loginAccount.get 389 | 390 | getGist(userName, repoName).map { gist => 391 | registerGistComment(userName, repoName, form.content, loginAccount.userName) 392 | redirect(s"/gist/${userName}/${repoName}") 393 | } getOrElse NotFound() 394 | }) 395 | 396 | ajaxPost("/gist/:userName/:repoName/_comments/:commentId/_delete")(usersOnly { 397 | val userName = params("userName") 398 | val repoName = params("repoName") 399 | val commentId = params("commentId").toInt 400 | 401 | // TODO Access check 402 | 403 | Ok(deleteGistComment(userName, repoName, commentId)) 404 | }) 405 | 406 | ajaxGet("/gist/:userName/:repoName/_comments/:commentId")(usersOnly { 407 | val userName = params("userName") 408 | val repoName = params("repoName") 409 | val commentId = params("commentId").toInt 410 | 411 | // TODO Access check 412 | getGist(userName, repoName).flatMap { gist => 413 | getGistComment(userName, repoName, commentId).map { comment => 414 | params.get("dataType") collect { 415 | case t if t == "html" => gitbucket.gist.html.commentedit(gist, comment.content, comment.commentId) 416 | } getOrElse { 417 | contentType = formats("json") 418 | org.json4s.jackson.Serialization.write( 419 | Map("content" -> gitbucket.core.view.Markdown.toHtml( 420 | markdown = comment.content, 421 | repository = gist.toRepositoryInfo, 422 | branch = "master", 423 | enableWikiLink = false, 424 | enableRefsLink = true, 425 | enableAnchor = true, 426 | enableLineBreaks = true 427 | )) 428 | ) 429 | } 430 | } 431 | } getOrElse NotFound() 432 | }) 433 | 434 | ajaxPost("/gist/:userName/:repoName/_comments/:commentId/_update", commentForm)(usersOnly { form => 435 | val userName = params("userName") 436 | val repoName = params("repoName") 437 | val commentId = params("commentId").toInt 438 | 439 | // TODO Access check 440 | 441 | updateGistComment(userName, repoName, commentId, form.content) 442 | redirect(s"/gist/${userName}/${repoName}/_comments/${commentId}") 443 | }) 444 | 445 | //////////////////////////////////////////////////////////////////////////////// 446 | // 447 | // Private Methods 448 | // 449 | //////////////////////////////////////////////////////////////////////////////// 450 | 451 | 452 | private def _gist(userName: String, repoName: Option[String] = None, revision: String = "master"): Any = { 453 | repoName match { 454 | case None => { 455 | val page = params.get("page") match { 456 | case Some("")|None => 1 457 | case Some(s) => s.toInt 458 | } 459 | 460 | val result: (Seq[Gist], Int) = ( 461 | getUserGists(userName, context.loginAccount.map(_.userName), (page - 1) * Limit, Limit), 462 | countUserGists(userName, context.loginAccount.map(_.userName)) 463 | ) 464 | 465 | val gists: Seq[(Gist, GistInfo)] = result._1.map { gist => 466 | val repoName = gist.repositoryName 467 | val files = getGistFiles(userName, repoName, revision) 468 | val (fileName, source) = files.head 469 | (gist, GistInfo(fileName, getLines(fileName, source), files.length, getForkedCount(userName, repoName), getCommentCount(userName, repoName))) 470 | } 471 | 472 | val fullName = getAccountByUserName(userName).get.fullName 473 | html.list(Some(GistUser(userName, fullName)), gists, page, page * Limit < result._2) 474 | } 475 | case Some(repoName) => { 476 | getGist(userName, repoName) match { 477 | case Some(gist) => 478 | if(gist.mode == "PRIVATE"){ 479 | context.loginAccount match { 480 | case Some(x) if(x.userName == userName) => _gistDetail(gist, userName, repoName, revision) 481 | case _ => Unauthorized() 482 | } 483 | } else { 484 | _gistDetail(gist, userName, repoName, revision) 485 | } 486 | case None => 487 | NotFound() 488 | } 489 | } 490 | } 491 | } 492 | 493 | private def _embedJs(gist: Gist, userName: String, repoName: String, revision: String): JavaScript = { 494 | val originUserName = gist.originUserName.getOrElse(userName) 495 | val originRepoName = gist.originRepositoryName.getOrElse(repoName) 496 | 497 | js.detail( 498 | gist, 499 | GistRepositoryURL(gist, baseUrl, context.settings), 500 | revision, 501 | getGistFiles(userName, repoName, revision) 502 | ) 503 | } 504 | 505 | private def _gistDetail(gist: Gist, userName: String, repoName: String, revision: String): Html = { 506 | val originUserName = gist.originUserName.getOrElse(userName) 507 | val originRepoName = gist.originRepositoryName.getOrElse(repoName) 508 | 509 | html.detail( 510 | gist, 511 | getForkedCount(originUserName, originRepoName), 512 | GistRepositoryURL(gist, baseUrl, context.settings), 513 | revision, 514 | getGistFiles(userName, repoName, revision), 515 | getGistComments(userName, repoName), 516 | isEditable(userName, loginUserGroups) 517 | ) 518 | } 519 | 520 | private def getGistFiles(userName: String, repoName: String, revision: String = "master"): Seq[(String, String)] = { 521 | val gitdir = new File(GistRepoDir, userName + "/" + repoName) 522 | Using.resource(Git.open(gitdir)){ git => 523 | JGitUtil.getFileList(git, revision, ".").map { file => 524 | file.name -> StringUtil.convertFromByteArray(JGitUtil.getContentFromId(git, file.id, true).get) 525 | } 526 | } 527 | } 528 | 529 | private def getFileParameters(): Seq[(String, String)] = { 530 | val count = params("count").toInt 531 | (0 to count - 1).flatMap { i => 532 | (params.get(s"fileName-${i}"), params.get(s"content-${i}")) match { 533 | case (Some(fileName), Some(content)) if(content.nonEmpty) => Some((if(fileName.isEmpty) s"gistfile${i + 1}.txt" else fileName, content)) 534 | case _ => None 535 | } 536 | } 537 | } 538 | 539 | private def loginUserGroups: Seq[String] = { 540 | context.loginAccount.map { account => 541 | getGroupsByUserName(account.userName) 542 | }.getOrElse(Nil) 543 | } 544 | 545 | } 546 | --------------------------------------------------------------------------------