diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md new file mode 100644 index 0000000..49d184a --- /dev/null +++ b/.github/CONTRIBUTING.md @@ -0,0 +1,6 @@ +# Guideline for Issues + +- At first, See [FAQ](https://github.com/gitbucket/gitbucket/wiki/FAQ) and check issues whether there is a same question or request in the past. +- If you can't find same question and report, send it to [gitter room](https://gitter.im/gitbucket/gitbucket) before raising an issue. +- We can also support in Japaneses other than English at [gitter room for Japanese](https://gitter.im/gitbucket/gitbucket_ja). +- Write an issue in English. At least, write subject in English. diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md new file mode 100644 index 0000000..4642490 --- /dev/null +++ b/.github/ISSUE_TEMPLATE.md @@ -0,0 +1,19 @@ +### Before submitting an issue to Gitbucket I have first: + +- [] read the [contribution guidelines](https://github.com/gitbucket/gitbucket/blob/master/.github/CONTRIBUTING.md) +- [] searched for similar already existing issue +- [] read the documentation and [wiki](https://github.com/gitbucket/gitbucket/wiki) + +*(if you have performed all the above, remove the paragraph and continue describing the issue with template below)* + +## Issue +**Impacted version**: xxxx + +**Deployment mode**: *explain here how you use gitbucket : standalone app, under webcontainer (which one), with an http frontend (nginx, httpd, ...)* + +**Problem description**: +- *be as explicit has you can* +- *describe the problem and its symptoms* +- *explain how to reproduce* +- *attach whatever information that can help understanding the context (screen capture, log files)* +- *do your best to use a correct english (re-read yourself)* diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..981e773 --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,8 @@ +### Before submitting a pull-request to Gitbucket I have first: + +- [] read the [contribution guidelines](https://github.com/gitbucket/gitbucket/blob/master/.github/CONTRIBUTING.md) +- [] rebased my branch over master +- [] verified that project is compiling +- [] verified that tests are passing +- [] squashed my commits as appropriate *(keep several commits if it is relevant to understand the PR)* +- [] [marked as closed using commit message](https://help.github.com/articles/closing-issues-via-commit-messages/) all issue ID that this PR should correct diff --git a/.gitignore b/.gitignore index 51bd3f5..1c2999a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ *.class *.log +.ensime +.ensime_cache # sbt specific dist/* diff --git a/.travis.yml b/.travis.yml index 0498f25..889f1e2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,18 @@ language: scala -sudo: false +sudo: true script: - - . env.sh - sbt test +jdk: + - oraclejdk8 +before_script: + - sudo apt-get install libaio1 + - sudo /etc/init.d/mysql stop + - sudo /etc/init.d/postgresql stop +cache: + directories: + - $HOME/.ivy2/cache + - $HOME/.sbt/boot + - $HOME/.sbt/launchers + - $HOME/.coursier + - $HOME/.embedmysql + - $HOME/.embedpostgresql diff --git a/LICENSE b/LICENSE index d645695..b98f26a 100644 --- a/LICENSE +++ b/LICENSE @@ -187,7 +187,7 @@ same "printed page" as the copyright notice for easier identification within third-party archives. - Copyright [yyyy] [name of copyright owner] + Copyright 2013-2016 GitBucket Team Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/README.md b/README.md index 0efd4d6..4cb606b 100644 --- a/README.md +++ b/README.md @@ -1,86 +1,206 @@ -GitBucket [![Gitter chat](https://badges.gitter.im/takezoe/gitbucket.png)](https://gitter.im/takezoe/gitbucket) [![Build Status](https://travis-ci.org/takezoe/gitbucket.svg?branch=master)](https://travis-ci.org/takezoe/gitbucket) +GitBucket [![Gitter chat](https://badges.gitter.im/gitbucket/gitbucket.png)](https://gitter.im/gitbucket/gitbucket) [![Build Status](https://travis-ci.org/gitbucket/gitbucket.svg?branch=master)](https://travis-ci.org/gitbucket/gitbucket) ========= -GitBucket is the easily installable GitHub clone powered by Scala. - +GitBucket is a Git platform powered by Scala offering: +- Easy installation +- High extensibility by plugins +- API compatibility with GitHub Features -------- The current version of GitBucket provides a basic features below: - Public / Private Git repository (http and ssh access) -- Repository viewer and online file editing -- Repository search (Code and Issues) -- Wiki -- Issues -- Fork / Pull request -- Mail notification -- Activity timeline -- User management (for Administrators) -- Group (like Organization in Github) -- LDAP integration -- Gravatar support +- Repository viewer and online file editor +- Issues, Pull request and Wiki for repositories +- Email notification +- Account and group management with LDAP integration +- Plug-in system -Following features are not implemented, but we will make them in the future release! - -- Network graph -- Statistics -- Watch / Star - -If you want to try the development version of GitBucket, see the documentation for developers at [Wiki](https://github.com/takezoe/gitbucket/wiki). +If you want to try the development version of GitBucket, see the [Developer's Guide](https://github.com/gitbucket/gitbucket/blob/master/doc/how_to_run.md). Installation -------- +GitBucket requires **Java8**. You have to install it if it is not already installed. -1. Download latest **gitbucket.war** from [the release page](https://github.com/takezoe/gitbucket/releases). -2. Deploy it to the Servlet 3.0 container such as Tomcat 7.x, Jetty 8.x, GlassFish 3.x or higher. -3. Access **http://[hostname]:[port]/gitbucket/** using your web browser. +1. Download the latest **gitbucket.war** from [the releases page](https://github.com/gitbucket/gitbucket/releases) and run it by `java -jar gitbucket.war`. +2. Go to `http://[hostname]:8080/` and log in with **root** / **root**. -If you are using Gitbucket behind a webserver please make sure you have increased the **client_max_body_size** (on nignx) +You can specify following options: -The default administrator account is **root** and password is **root**. +- `--port=[NUMBER]` +- `--prefix=[CONTEXTPATH]` +- `--host=[HOSTNAME]` +- `--gitbucket.home=[DATA_DIR]` -or you can start GitBucket by `java -jar gitbucket.war` without servlet container. In this case, GitBucket URL is **http://[hostname]:8080/**. You can specify following options. +You can also deploy gitbucket.war to a servlet container which supports Servlet 3.0 (like Jetty, Tomcat, JBoss, etc) -- --port=[NUMBER] -- --prefix=[CONTEXTPATH] -- --host=[HOSTNAME] -- --gitbucket.home=[DATA_DIR] +For more information about installation on Mac or Windows Server (with IIS), or configuration of Apache or Nginx and also integration with other tools or services such as Jenkins or Slack, see [Wiki](https://github.com/gitbucket/gitbucket/wiki). -To upgrade GitBucket, only replace gitbucket.war. All GitBucket data is stored in HOME/.gitbucket. So if you want to back up GitBucket data, copy this directory to the other disk. +To upgrade GitBucket, replace `gitbucket.war` with the new version, after stopping GitBucket. All GitBucket data is stored in `HOME/.gitbucket` by default. So if you want to back up GitBucket's data, copy this directory to the backup location. -For Installation on Windows Server with IIS see [this wiki page](https://github.com/takezoe/gitbucket/wiki/Installation-on-IIS-and-Helicontech-Zoo) +Plugins +-------- +GitBucket has a plug-in system to allow extensions to GitBucket. We provide some official plug-ins: -### Mac OS X -#### Installing Via Homebrew +- [gitbucket-gist-plugin](https://github.com/gitbucket/gitbucket-gist-plugin) +- [gitbucket-emoji-plugin](https://github.com/gitbucket/gitbucket-emoji-plugin) -``` -$ brew install gitbucket -==> Downloading https://github.com/takezoe/gitbucket/releases/download/1.10/gitbucket.war -######################################################################## 100.0% -==> Caveats -Note: When using launchctl the port will be 8080. +You can find more plugins made by the community at [GitBucket community plugins](http://gitbucket-plugins.github.io/). -To have launchd start gitbucket at login: - ln -sfv /usr/local/opt/gitbucket/*.plist ~/Library/LaunchAgents -Then to load gitbucket now: - launchctl load ~/Library/LaunchAgents/homebrew.mxcl.gitbucket.plist -Or, if you don't want/need launchctl, you can just run: - java -jar /usr/local/opt/gitbucket/libexec/gitbucket.war -==> Summary -/usr/local/Cellar/gitbucket/1.10: 3 files, 42M, built in 11 seconds -``` +Support +-------- -#### Manual Installation -On OS X, copy the [gitbucket.plist](https://raw.github.com/takezoe/gitbucket/master/contrib/macosx/gitbucket.plist) file to `~/Library/LaunchAgents/` - -Run the following commands in `Terminal` to - -- start gitbucket: `launchctl load ~/Library/LaunchAgents/gitbucket.plist` -- stop gitbucket: `launchctl unload ~/Library/LaunchAgents/gitbucket.plist` +- If you have any questions about GitBucket, send it to the [gitter room](https://gitter.im/gitbucket/gitbucket) before opening an issue. +- Make sure check whether there is the same question or request in the past. +- When raise a new issue, write at least the subject in **English**. +- We can also provide support in Japanese at [gitter room for Japanese](https://gitter.im/gitbucket/gitbucket_ja). +- The first priority of GitBucket is easy installation and API compatibility with GitHub, so we might reject if your request is against it. Release Notes --------- +------------- +## 4.8 - 23 Dec 2016 +- Search for repository names from the global header +- Filter repositories on the sidebar of the dashboard +- Search issues and wiki +- Keep pull request comments after new commits are pushed +- New web API to get a single issue +- Performance improvement for the repository viewer + +### 4.7.1 - 28 Nov 2016 +- Bug fix: group repositories are not shown in the your repositories list on the sidebar +- Small performance improvement of the dashboard + +### 4.7 - 26 Nov 2016 +- New permission system +- Dropdown filter for issue labels, milestones and assignees +- Keep sidebar folding status +- Link from milestone label to the issue list + +### 4.6 - 29 Oct 2016 +- Add disable option for forking +- Add History button to wiki page +- Git repository URL redirection for GitHub compatibility +- Get-Content API improvement +- Indicate who is group master in Members tab in group view + +### 4.5 - 29 Sep 2016 +- Attach files by dropping into textarea +- Issues / Pull requests switcher in dashboard +- HikariCP could be configured in `GITBUCKET_HOME/database.conf` +- Improve Cookie security +- Display commit count on the history button +- Improve mobile view + +### 4.4 - 28 Aug 2016 +- Import a SQL dump file to the database +- `go get` support in private repositories +- Sort milestones by due date +- apache-sshd has been updated to 1.2.0 + +### 4.3 - 30 Jul 2016 +- Emoji support by [gitbucket-emoji-plugin](https://github.com/gitbucket/gitbucket-emoji-plugin) +- User name suggestion +- Add new web APIs and basic authentication support for API access +- Root Endpoint + - [List endpoints](https://developer.github.com/v3/#root-endpoint) + - [List Branches](https://developer.github.com/v3/repos/branches/#list-branches) + - [Get contents](https://developer.github.com/v3/repos/contents/#get-contents) + - [Get a Reference](https://developer.github.com/v3/git/refs/#get-a-reference) + - [List Collaborators](https://developer.github.com/v3/repos/collaborators/#list-collaborators) + - [List user repositories](https://developer.github.com/v3/repos/#list-user-repositories) + - [Get a group](https://developer.github.com/v3/orgs/#get-an-organization) + - [List group repositories](https://developer.github.com/v3/repos/#list-organization-repositories) +- Add new extension points + - `assetsMapping` : Supplies resources in plugin classpath as web assets + - `suggestionProvider` : Provides suggestion in the Markdown editing textarea + - `textDecorator` : Decorate text nodes in HTML which is converted from Markdown + +### 4.2.1 - 3 Jul 2016 +- Fix migration bug + +This is hotfix for a critical bug in migration. If you are new installation, use 4.2.0. But if you have an exisiting installation and it had been updated to 4.0 from 3.x, you must update to 4.2.1. + +### 4.2 - 2 Jul 2016 +- New UI based on [AdminLTE](https://github.com/almasaeed2010/AdminLTE) +- git gc +- Issues and Wiki have been possible to be disabled +- SMTP configuration test mail + +### 4.1 - 4 Jun 2016 +- Generic ssh user +- Improve branch protection UI +- Default value of pull request title + +### 4.0 - 30 Apr 2016 +- MySQL and PostgreSQL support +- Data export and import +- Migration system has been switched to [solidbase](https://github.com/gitbucket/solidbase) + +**Note:** You can upgrade to GitBucket 4.0 from 3.14. If your GitBucket is 3.13 or before, you have to upgrade 3.14 at first. + +### 3.14 - 30 Apr 2016 +- File attachment and search for wiki pages +- New extension points to add menus +- Content-Type of webhooks has been choosable + +### 3.13 - 1 Apr 2016 +- Refresh user interface for wide screen +- Add `pull_request` key in list issues API for pull requests +- Add `X-Hub-Signature` security to webhooks +- Provide SHA-256 checksum for `gitbucket.war` + +### 3.12 - 27 Feb 2016 +- New GitHub UI +- Improve mobile view +- Improve printing style +- Individual URL for pull request tabs +- SSH host configuration is separated from HTTP base URL + +### 3.11 - 30 Jan 2016 +- Upgrade Scalatra to 2.4 +- Sidebar and Footer for Wiki +- Branch protection and receive hook extension point for plug-in +- Limit recent updated repositories list +- Issue actions look-alike GitHub +- Web API for labels +- Requires Java 8 + +### 3.10 - 30 Dec 2015 +- Move to Bootstrap3 +- New URL for raw contents (`raw/master/doc/activity.md` instead of `blob/master/doc/activity.md?raw=true`) +- Update xsbt-web-plugin +- Update H2 database + +### 3.9 - 5 Dec 2015 +- GFM inline breaks support in Markdown +- WebHook on create review comment is available +- WebHook event trigger is selectable + +### 3.8 - 31 Oct 2015 +- Moved to GitHub organization +- Omit diff view for large differences +- Repository creation API +- Render url as link in repository description +- Expand attachable file types + +### 3.7 - 3 Oct 2015 +- Markdown processor has been switched to [markedj](https://github.com/gitbucket/markedj) from pegdown +- Clone in desktop button +- Providing MD5 and SHA-1 checksum for `gitbucket.war` has started + +### 3.6 - 30 Aug 2015 +- User interface Improvements: Especially, commit list, issues and pull request have been updated largely. +- Installed plugins list has been available at the system administration console. +- Pages and repository list in the sidebar have been limited and more pages and repositories link is available. +- More reference link notation in Markdown has been supported. + +### 3.5 - 1 Aug 2015 +- Octicons has been applied +- Global header has been enhanced. Now it's further similar to GitHub. +- Default compare / pull request target has been changed to the parent repository +- A lot of updates for [gitbucket-gist-plugin](https://github.com/gitbucket/gitbucket-gist-plugin) + ### 3.4 - 27 Jun 2015 - Declarative style plug-in definition - New extension point to add markup render @@ -293,7 +413,3 @@ ### 1.0 - 04 Jul 2013 - This is a first public release - -Sponsors --------- -[![IntelliJ IDEA](https://www.jetbrains.com/idea/docs/logo_intellij_idea.png)](https://www.jetbrains.com/idea/) diff --git a/build.sbt b/build.sbt new file mode 100644 index 0000000..547d302 --- /dev/null +++ b/build.sbt @@ -0,0 +1,219 @@ +val Organization = "io.github.gitbucket" +val Name = "gitbucket" +val GitBucketVersion = "4.8" +val ScalatraVersion = "2.4.1" +val JettyVersion = "9.3.9.v20160517" + +lazy val root = (project in file(".")).enablePlugins(SbtTwirl, JettyPlugin) + +sourcesInBase := false +organization := Organization +name := Name +version := GitBucketVersion +scalaVersion := "2.11.8" + +// dependency settings +resolvers ++= Seq( + Classpaths.typesafeReleases, + Resolver.jcenterRepo, + "amateras" at "http://amateras.sourceforge.jp/mvn/", + "sonatype-snapshot" at "https://oss.sonatype.org/content/repositories/snapshots/", + "amateras-snapshot" at "http://amateras.sourceforge.jp/mvn-snapshot/" +) +libraryDependencies ++= Seq( + "org.scala-lang.modules" %% "scala-java8-compat" % "0.7.0", + "org.eclipse.jgit" % "org.eclipse.jgit.http.server" % "4.6.0.201612231935-r", + "org.eclipse.jgit" % "org.eclipse.jgit.archive" % "4.6.0.201612231935-r", + "org.scalatra" %% "scalatra" % ScalatraVersion, + "org.scalatra" %% "scalatra-json" % ScalatraVersion, + "org.json4s" %% "json4s-jackson" % "3.3.0", + "io.github.gitbucket" %% "scalatra-forms" % "1.0.0", + "commons-io" % "commons-io" % "2.4", + "io.github.gitbucket" % "solidbase" % "1.0.0", + "io.github.gitbucket" % "markedj" % "1.0.9", + "org.apache.commons" % "commons-compress" % "1.11", + "org.apache.commons" % "commons-email" % "1.4", + "org.apache.httpcomponents" % "httpclient" % "4.5.1", + "org.apache.sshd" % "apache-sshd" % "1.2.0", + "org.apache.tika" % "tika-core" % "1.13", + "com.typesafe.slick" %% "slick" % "2.1.0", + "com.novell.ldap" % "jldap" % "2009-10-07", + "com.h2database" % "h2" % "1.4.192", + "mysql" % "mysql-connector-java" % "5.1.39", + "org.postgresql" % "postgresql" % "9.4.1208", + "ch.qos.logback" % "logback-classic" % "1.1.7", + "com.zaxxer" % "HikariCP" % "2.4.6", + "com.typesafe" % "config" % "1.3.0", + "com.typesafe.akka" %% "akka-actor" % "2.3.15", + "fr.brouillard.oss.security.xhub" % "xhub4j-core" % "1.0.0", + "com.github.bkromhout" % "java-diff-utils" % "2.1.1", + "org.cache2k" % "cache2k-all" % "1.0.0.CR1", + "com.enragedginger" %% "akka-quartz-scheduler" % "1.4.0-akka-2.3.x" exclude("c3p0","c3p0"), + "org.eclipse.jetty" % "jetty-webapp" % JettyVersion % "provided", + "javax.servlet" % "javax.servlet-api" % "3.1.0" % "provided", + "junit" % "junit" % "4.12" % "test", + "org.scalatra" %% "scalatra-scalatest" % ScalatraVersion % "test", + "com.wix" % "wix-embedded-mysql" % "2.1.4" % "test", + "ru.yandex.qatools.embed" % "postgresql-embedded" % "1.14" % "test" +) + +// Compiler settings +scalacOptions := Seq("-deprecation", "-language:postfixOps", "-Ybackend:GenBCode", "-Ydelambdafy:method", "-target:jvm-1.8") +javacOptions in compile ++= Seq("-target", "8", "-source", "8") +javaOptions in Jetty += "-Dlogback.configurationFile=/logback-dev.xml" + +// Test settings +//testOptions in Test += Tests.Argument("-l", "ExternalDBTest") +javaOptions in Test += "-Dgitbucket.home=target/gitbucket_home_for_test" +testOptions in Test += Tests.Setup( () => new java.io.File("target/gitbucket_home_for_test").mkdir() ) +fork in Test := true + +// Packaging options +packageOptions += Package.MainClass("JettyLauncher") + +// Assembly settings +test in assembly := {} +assemblyMergeStrategy in assembly := { + case PathList("META-INF", xs @ _*) => + (xs map {_.toLowerCase}) match { + case ("manifest.mf" :: Nil) => MergeStrategy.discard + case _ => MergeStrategy.discard + } + case x => MergeStrategy.first +} + +// JRebel +Seq(jrebelSettings: _*) + +jrebel.webLinks += (target in webappPrepare).value +jrebel.enabled := System.getenv().get("JREBEL") != null +javaOptions in Jetty ++= Option(System.getenv().get("JREBEL")).toSeq.flatMap { path => + Seq("-noverify", "-XX:+UseConcMarkSweepGC", "-XX:+CMSClassUnloadingEnabled", s"-javaagent:${path}") +} + +// Create executable war file +val executableConfig = config("executable").hide +Keys.ivyConfigurations += executableConfig +libraryDependencies ++= Seq( + "org.eclipse.jetty" % "jetty-security" % JettyVersion % "executable", + "org.eclipse.jetty" % "jetty-webapp" % JettyVersion % "executable", + "org.eclipse.jetty" % "jetty-continuation" % JettyVersion % "executable", + "org.eclipse.jetty" % "jetty-server" % JettyVersion % "executable", + "org.eclipse.jetty" % "jetty-xml" % JettyVersion % "executable", + "org.eclipse.jetty" % "jetty-http" % JettyVersion % "executable", + "org.eclipse.jetty" % "jetty-servlet" % JettyVersion % "executable", + "org.eclipse.jetty" % "jetty-io" % JettyVersion % "executable", + "org.eclipse.jetty" % "jetty-util" % JettyVersion % "executable" +) + +val executableKey = TaskKey[File]("executable") +executableKey := { + import java.util.jar.{ Manifest => JarManifest } + import java.util.jar.Attributes.{ Name => AttrName } + + val workDir = Keys.target.value / "executable" + val warName = Keys.name.value + ".war" + + val log = streams.value.log + log info s"building executable webapp in ${workDir}" + + // initialize temp directory + val temp = workDir / "webapp" + IO delete temp + + // include jetty classes + val jettyJars = Keys.update.value select configurationFilter(name = executableConfig.name) + jettyJars foreach { jar => + IO unzip (jar, temp, (name:String) => + (name startsWith "javax/") || + (name startsWith "org/") + ) + } + + // include original war file + val warFile = (Keys.`package`).value + IO unzip (warFile, temp) + + // include launcher classes + val classDir = (Keys.classDirectory in Compile).value + val launchClasses = Seq("JettyLauncher.class" /*, "HttpsSupportConnector.class" */) + launchClasses foreach { name => + IO copyFile (classDir / name, temp / name) + } + + // zip it up + IO delete (temp / "META-INF" / "MANIFEST.MF") + val contentMappings = (temp.*** --- PathFinder(temp)).get pair relativeTo(temp) + val manifest = new JarManifest + manifest.getMainAttributes put (AttrName.MANIFEST_VERSION, "1.0") + manifest.getMainAttributes put (AttrName.MAIN_CLASS, "JettyLauncher") + val outputFile = workDir / warName + IO jar (contentMappings, outputFile, manifest) + + // generate checksums + Seq( + "md5" -> "MD5", + "sha1" -> "SHA-1", + "sha256" -> "SHA-256" + ) + .foreach { case (extension, algorithm) => + val checksumFile = workDir / (warName + "." + extension) + Checksums generate (outputFile, checksumFile, algorithm) + } + + // done + log info s"built executable webapp ${outputFile}" + outputFile +} +publishTo := { + val nexus = "https://oss.sonatype.org/" + if (version.value.trim.endsWith("SNAPSHOT")) Some("snapshots" at nexus + "content/repositories/snapshots") + else Some("releases" at nexus + "service/local/staging/deploy/maven2") +} +publishMavenStyle := true +pomIncludeRepository := { _ => false } +pomExtra := ( + https://github.com/gitbucket/gitbucket + + + The Apache Software License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + + + + https://github.com/gitbucket/gitbucket + scm:git:https://github.com/gitbucket/gitbucket.git + + + + takezoe + Naoki Takezoe + https://github.com/takezoe + + + shimamoto + Takako Shimamoto + https://github.com/shimamoto + + + tanacasino + Tomofumi Tanaka + https://github.com/tanacasino + + + mrkm4ntr + Shintaro Murakami + https://github.com/mrkm4ntr + + + nazoking + nazoking + https://github.com/nazoking + + + McFoggy + Matthieu Brouillard + https://github.com/McFoggy + + +) diff --git a/contrib/gitbucket.conf b/contrib/gitbucket.conf index ead3331..b500d39 100644 --- a/contrib/gitbucket.conf +++ b/contrib/gitbucket.conf @@ -44,7 +44,7 @@ GITBUCKET_WAR_FILE=$GITBUCKET_WAR_DIR/gitbucket.war # GitBucket version to fetch when installing -GITBUCKET_VERSION=2.1 +GITBUCKET_VERSION=3.5 # # End of configuration section. Ignore this part diff --git a/contrib/install b/contrib/install index 860b02f..97097fb 100755 --- a/contrib/install +++ b/contrib/install @@ -38,7 +38,7 @@ createDir "$GITBUCKET_LOG_DIR" echo "Fetching GitBucket v$GITBUCKET_VERSION and saving as $GITBUCKET_WAR_FILE" -sudo wget -qO "$GITBUCKET_WAR_FILE" https://github.com/takezoe/gitbucket/releases/download/$GITBUCKET_VERSION/gitbucket.war +sudo wget -qO "$GITBUCKET_WAR_FILE" https://github.com/gitbucket/gitbucket/releases/download/$GITBUCKET_VERSION/gitbucket.war sudo rm -f "$GITBUCKET_LOG_DIR/run.log" diff --git a/contrib/linux/redhat/README.md b/contrib/linux/redhat/README.md index ddca44d..5c63178 100644 --- a/contrib/linux/redhat/README.md +++ b/contrib/linux/redhat/README.md @@ -3,6 +3,7 @@ RPM spec file and init script for Red Hat Enterprise Linux 6.x. To create RPM: + 1. Edit `../../gitbucket.conf` to suit. 2. Edit `gitbucket.init` to suit. 3. Edit `gitbucket.spec` to suit. diff --git a/contrib/linux/redhat/gitbucket.spec b/contrib/linux/redhat/gitbucket.spec index d634b1d..1d480cc 100644 --- a/contrib/linux/redhat/gitbucket.spec +++ b/contrib/linux/redhat/gitbucket.spec @@ -3,7 +3,7 @@ Version: 2.6 Release: 1%{?dist} License: Apache -URL: https://github.com/takezoe/gitbucket +URL: https://github.com/gitbucket/gitbucket Group: System/Servers Source0: %{name}.war Source1: %{name}.init diff --git a/doc/authenticator.md b/doc/authenticator.md new file mode 100644 index 0000000..2434767 --- /dev/null +++ b/doc/authenticator.md @@ -0,0 +1,60 @@ +Authentication in Controller +======== +GitBucket provides many [authenticators](https://github.com/gitbucket/gitbucket/blob/master/src/main/scala/gitbucket/core/util/Authenticator.scala) to access controlling in the controller. + +For example, in the case of `RepositoryViwerController`, +it references three authenticators: `ReadableUsersAuthenticator`, `ReferrerAuthenticator` and `CollaboratorsAuthenticator`. + +```scala +class RepositoryViewerController extends RepositoryViewerControllerBase + with RepositoryService with AccountService with ActivityService with IssuesService with WebHookService with CommitsService + with ReadableUsersAuthenticator with ReferrerAuthenticator with CollaboratorsAuthenticator with PullRequestService with CommitStatusService + with WebHookPullRequestService with WebHookPullRequestReviewCommentService + +trait RepositoryViewerControllerBase extends ControllerBase { + self: RepositoryService with AccountService with ActivityService with IssuesService with WebHookService with CommitsService + with ReadableUsersAuthenticator with ReferrerAuthenticator with CollaboratorsAuthenticator with PullRequestService with CommitStatusService + with WebHookPullRequestService with WebHookPullRequestReviewCommentService => + + ... +``` + +Authenticators provides a method to add guard to actions in the controller: + +- `ReadableUsersAuthenticator` provides `readableUsersOnly` method +- `ReferrerAuthenticator` provides `referrersOnly` method +- `CollaboratorsAuthenticator` provides `collaboratorsOnly` method + +These methods are available in each actions as below: + +```scala +// Allows only the repository owner (or manager for group repository) and administrators. +get("/:owner/:repository/tree/*")(referrersOnly { repository => + ... +}) + +// Allows only collaborators and administrators. +get("/:owner/:repository/new/*")(collaboratorsOnly { repository => + ... +}) + +// Allows only signed in users which can access the repository. +post("/:owner/:repository/commit/:id/comment/new", commentForm)(readableUsersOnly { (form, repository) => + ... +}) +``` + +Currently, GitBucket provides below authenticators: + +|Trait | Method | Description | +|--------------------------|-----------------|--------------------------------------------------------------------------------------| +|OneselfAuthenticator |oneselfOnly |Allows only oneself and administrators. | +|OwnerAuthenticator |ownerOnly |Allows only the repository owner and administrators. | +|UsersAuthenticator |usersOnly |Allows only signed in users. | +|AdminAuthenticator |adminOnly |Allows only administrators. | +|CollaboratorsAuthenticator|collaboratorsOnly|Allows only collaborators and administrators. | +|ReferrerAuthenticator |referrersOnly |Allows only the repository owner (or manager for group repository) and administrators.| +|ReadableUsersAuthenticator|readableUsersOnly|Allows only signed in users which can access the repository. | +|GroupManagerAuthenticator |managersOnly |Allows only the group managers. | + +Of course, if you make a new plugin, you can define a your own authenticator according to requirement in your plugin. \ No newline at end of file diff --git a/doc/auto_update.md b/doc/auto_update.md index 83aa711..687b6e9 100644 --- a/doc/auto_update.md +++ b/doc/auto_update.md @@ -1,37 +1,54 @@ Automatic Schema Updating ======== -GitBucket uses H2 database to manage project and account data. GitBucket updates database schema automatically in the first run after the upgrading. +GitBucket updates database schema automatically using [Solidbase](https://github.com/gitbucket/solidbase) in the first run after the upgrading. -To release a new version of GitBucket, add the version definition to the [servlet.AutoUpdate](https://github.com/takezoe/gitbucket/blob/master/src/main/scala/servlet/AutoUpdateListener.scala) at first. +To release a new version of GitBucket, add the version definition to the [gitbucket.core.GitBucketCoreModule](https://github.com/gitbucket/gitbucket/blob/master/src/main/scala/gitbucket/core/GitBucketCoreModule.scala) at first. ```scala -object AutoUpdate { - ... - /** - * The history of versions. A head of this sequence is the current BitBucket version. - */ - val versions = Seq( - Version(1, 0) +object GitBucketCoreModule extends Module("gitbucket-core", + new Version("4.0.0", + new LiquibaseMigration("update/gitbucket-core_4.0.xml"), + new SqlMigration("update/gitbucket-core_4.0.sql") + ), + new Version("4.1.0"), + new Version("4.2.0", + new LiquibaseMigration("update/gitbucket-core_4.2.xml") ) - ... -``` - -Next, add a SQL file which updates database schema into [/src/main/resources/update/](https://github.com/takezoe/gitbucket/tree/master/src/main/resources/update) as ```MAJOR_MINOR.sql```. - -GitBucket stores the current version to ```GITBUCKET_HOME/version``` and checks it at start-up. If the stored version differs from the actual version, it executes differences of SQL files between the stored version and the actual version. And ```GITBUCKET_HOME/version``` is updated by the actual version. - -We can also add any Scala code for upgrade GitBucket which modifies resources other than database. Override ```Version.update``` like below: - -```scala -val versions = Seq( - new Version(1, 3){ - override def update(conn: Connection): Unit = { - super.update(conn) - // Add any code here! - } - }, - Version(1, 2), - Version(1, 1), - Version(1, 0) ) ``` + +Next, add a XML file which updates database schema into [/src/main/resources/update/](https://github.com/gitbucket/gitbucket/tree/master/src/main/resources/update) with a filenane defined in `GitBucketCoreModule`. + +```xml + + + + + + + + + +``` + +Solidbase stores the current version to `VERSIONS` table and checks it at start-up. If the stored version differs from the actual version, it executes differences between the stored version and the actual version. + +We can add the SQL file instead of the XML file using `SqlMigration`. It try to load a SQL file from classpath as following order: + +1. Specified path (if specified) +2. `${moduleId}_${version}_${database}.sql` +3. `${moduleId}_${version}.sql` + +Also we can add any code by extending `Migration`: + +```scala +object GitBucketCoreModule extends Module("gitbucket-core", + new Version("4.0.0", new Migration(){ + override def migrate(moduleId: String, version: String, context: java.util.Map[String, String]): Unit = { + ... + } + }) +) +``` + +See more details [README of Solidbase](https://github.com/gitbucket/solidbase). diff --git a/doc/comment_action.md b/doc/comment_action.md index d5f2d39..d56bd7e 100644 --- a/doc/comment_action.md +++ b/doc/comment_action.md @@ -1,48 +1,56 @@ About Action in Issue Comment ======== After the issue creation at GitBucket, users can add comments or close it. -The details are saved at ```ISSUE_COMMENT``` table. +The details are saved at `ISSUE_COMMENT` table. -To determine if it was any operation, you see the ```ACTION``` column. +To determine if it was any operation, you see the `ACTION` column. +And in the case of some actions, `CONTENT` column value contains additional information. -|ACTION| -|--------| -|comment| -|close_comment| -|reopen_comment| -|close| -|reopen| -|commit| -|merge| -|delete_branch| -|refer| +|ACTION |CONTENT | +|---------------|-----------------| +|comment |comment | +|close_comment |comment | +|reopen_comment |comment | +|close |"Close" | +|reopen |"Reopen" | +|commit |comment commitId | +|merge |comment | +|delete_branch |branchName | +|refer |issueId:title | -#####comment +### comment + This value is saved when users have made a normal comment. -#####close_comment, reopen_comment +### close_comment, reopen_comment + These values are saved when users have reopened or closed the issue with comments. -#####close, reopen +### close, reopen + These values are saved when users have reopened or closed the issue. -At the same time, store the fixed value(i.e. "Close" or "Reopen") to the ```CONTENT``` column. +At the same time, store the fixed value(i.e. "Close" or "Reopen") to the `CONTENT` column. Therefore, this comment is not displayed, and not counted as a comment. -#####commit -This value is saved when users have pushed including the ```#issueId``` to the commit message. -At the same time, store it to the ```CONTENT``` column with its commit id. +### commit + +This value is saved when users have pushed including the `#issueId` to the commit message. +At the same time, store it to the `CONTENT` column with its commit id. This comment is displayed. But it can not be edited by all users, and also not counted as a comment. -#####merge +### merge + This value is saved when users have merged the pull request. -At the same time, store the message to the ```CONTENT``` column. +At the same time, store the message to the `CONTENT` column. This comment is displayed. But it can not be edited by all users, and also not counted as a comment. -#####delete_branch +### delete_branch + This value is saved when users have deleted the branch. Users can delete branch after merging pull request which is requested from the same repository. -At the same time, store it to the ```CONTENT``` column with the deleted branch name. +At the same time, store it to the `CONTENT` column with the deleted branch name. Therefore, this comment is not displayed, and not counted as a comment. -#####refer -This value is saved when other issue or issue comment contains reference to the issue like ```#issueId```. -At the same time, store id and title of the referrer issue as ```id:title```. +### refer + +This value is saved when other issue or issue comment contains reference to the issue like `#issueId`. +At the same time, store id and title of the referrer issue as `id:title`. diff --git a/doc/directory.md b/doc/directory.md index de73fe9..b016698 100644 --- a/doc/directory.md +++ b/doc/directory.md @@ -8,10 +8,10 @@ * /HOME/gitbucket * /repositories * /USER_NAME - * / REPO_NAME.git (substance of repository. GitServlet sees this directory) - * / REPO_NAME + * /REPO_NAME.git (substance of repository. GitServlet sees this directory) + * /REPO_NAME * /issues (files which are attached to issue) - * / REPO_NAME.wiki.git (wiki repository) + * /REPO_NAME.wiki.git (wiki repository) * /data * /USER_NAME * /files diff --git a/doc/how_to_run.md b/doc/how_to_run.md index bb14bbc..ea31909 100644 --- a/doc/how_to_run.md +++ b/doc/how_to_run.md @@ -1,28 +1,18 @@ How to run from the source tree ======== -for Testers +Run for Development -------- If you want to test GitBucket, input following command at the root directory of the source tree. ``` -C:\gitbucket> sbt ~container:start +$ sbt ~jetty:start ``` Then access to `http://localhost:8080/` by your browser. The default administrator account is `root` and password is `root`. -for Developers --------- -If you want to modify source code and confirm it, you can run GitBucket in auto reloading mode as following: - -``` -C:\gitbucket> sbt -... -> container:start -... -> ~ ;copy-resources;aux-compile -``` +Source code modification is detected and reloaded automatically. You can modify logging configuration by editing `src/main/resources/logback-dev.xml`. Build war file -------- @@ -30,9 +20,23 @@ To build war file, run the following command: ``` -C:\gitbucket> sbt package +$ sbt package ``` `gitbucket_2.11-x.x.x.war` is generated into `target/scala-2.11`. -To build executable war file, run Ant at the top of the source tree. It generates executable `gitbucket.war` into `target/scala-2.11`. We release this war file as release artifact. Please note the current build.xml works on Windows only. Replace `sbt.bat` with `sbt.sh` in build.xml if you want to run it on Linux. +To build executable war file, run + +``` +$ sbt executable +``` + +at the top of the source tree. It generates executable `gitbucket.war` into `target/executable`. We release this war file as release artifact. + +Run tests spec +--------- +To run the full serie of tests, run the following command: + +``` +sbt test +``` diff --git a/doc/icons.svg b/doc/icons.svg index 9372a97..f77f5e1 100644 --- a/doc/icons.svg +++ b/doc/icons.svg @@ -379,21 +379,21 @@ - - + + - - + + - - + + - - + + @@ -750,5 +750,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/doc/jrebel.md b/doc/jrebel.md new file mode 100644 index 0000000..9ea1f68 --- /dev/null +++ b/doc/jrebel.md @@ -0,0 +1,148 @@ +JRebel integration (optional) +============================= + +[JRebel](http://zeroturnaround.com/software/jrebel/) is a JVM plugin that makes developing web apps much faster. +JRebel is generally able to eliminate the need for the following slow "app restart" in sbt following a code change: + +``` +> jetty:start +``` + +While JRebel is not open source, it does reload your code faster than the `~;copy-resources;aux-compile` way of doing things using `sbt`. + +It's only used during development, and doesn't change your deployed app in any way. + +JRebel used to be free for Scala developers, but that changed recently, and now there's a cost associated with usage for Scala. There are trial plans and free non-commercial licenses available if you just want to try it out. + +---- + +## 1. Get a JRebel license + +Sign up for a [usage plan](https://my.jrebel.com/). You will need to create an account. + +## 2. Download JRebel + +Download the most recent ["nosetup" JRebel zip](http://zeroturnaround.com/software/jrebel/download/prev-releases/). +Next, unzip the downloaded file. + +## 3. Activate + +Follow the [instructions on the JRebel website](http://zeroturnaround.com/software/jrebel/download/prev-releases/) to activate your downloaded JRebel. + +You can use the default settings for all the configurations. + +You don't need to integrate with your IDE, since we're using sbt to do the servlet deployment. + +## 4. Tell jvm where JRebel is + +Fortunately, the gitbucket project is already set up to use JRebel. +You only need to tell jvm where to find the jrebel jar. + +To do so, edit your shell resource file (usually `~/.bash_profile` on Mac, and `~/.bashrc` on Linux), and add the following line: + +```bash +export JREBEL=/path/to/jrebel/jrebel.jar +``` + +For example, if you unzipped your JRebel download in your home directory, you whould use: + +```bash +export JREBEL=~/jrebel/jrebel.jar +``` + +Now reload your shell: + +``` +$ source ~/.bash_profile # on Mac +$ source ~/.bashrc # on Linux +``` + +## 5. See it in action! + +Now you're ready to use JRebel with the gitbucket. +When you run sbt as normal, you will see a long message from JRebel, indicating it has loaded. +Here's an abbreviated version of what you will see: + +``` +$ ./sbt +[info] Loading project definition from /git/gitbucket/project +[info] Set current project to gitbucket (in build file:/git/gitbucket/) +> +``` + +You will start the servlet container slightly differently now that you're using sbt. + +``` +> jetty:start +: +[info] starting server ... +[success] Total time: 3 s, completed Jan 3, 2016 9:47:55 PM +2016-01-03 21:47:57 JRebel: +2016-01-03 21:47:57 JRebel: A newer version '6.3.1' is available for download +2016-01-03 21:47:57 JRebel: from http://zeroturnaround.com/software/jrebel/download/ +2016-01-03 21:47:57 JRebel: +2016-01-03 21:47:58 JRebel: Contacting myJRebel server .. +2016-01-03 21:47:59 JRebel: Directory '/git/gitbucket/target/scala-2.11/classes' will be monitored for changes. +2016-01-03 21:47:59 JRebel: Directory '/git/gitbucket/target/scala-2.11/test-classes' will be monitored for changes. +2016-01-03 21:47:59 JRebel: Directory '/git/gitbucket/target/webapp' will be monitored for changes. +2016-01-03 21:48:00 JRebel: +2016-01-03 21:48:00 JRebel: ############################################################# +2016-01-03 21:48:00 JRebel: +2016-01-03 21:48:00 JRebel: JRebel Legacy Agent 6.2.5 (201509291538) +2016-01-03 21:48:00 JRebel: (c) Copyright ZeroTurnaround AS, Estonia, Tartu. +2016-01-03 21:48:00 JRebel: +2016-01-03 21:48:00 JRebel: Over the last 30 days JRebel prevented +2016-01-03 21:48:00 JRebel: at least 182 redeploys/restarts saving you about 7.4 hours. +2016-01-03 21:48:00 JRebel: +2016-01-03 21:48:00 JRebel: Over the last 324 days JRebel prevented +2016-01-03 21:48:00 JRebel: at least 1538 redeploys/restarts saving you about 62.4 hours. +2016-01-03 21:48:00 JRebel: +2016-01-03 21:48:00 JRebel: Licensed to nazo king (using myJRebel). +2016-01-03 21:48:00 JRebel: +2016-01-03 21:48:00 JRebel: +2016-01-03 21:48:00 JRebel: ############################################################# +2016-01-03 21:48:00 JRebel: +: + +> ~ copy-resources +[success] Total time: 0 s, completed Jan 3, 2016 9:13:54 PM +1. Waiting for source changes... (press enter to interrupt) +``` + +Finally, change your code. +For example, you can change the title on `src/main/twirl/gitbucket/core/main.scala.html` like this: + +```html +: + + GitBucket + @defining(AutoUpdate.getCurrentVersion){ version => + @version.majorVersion.@version.minorVersion + } + change code !!!!!!!!!!!!!!!! + +: +``` + +If JRebel is doing is correctly installed you will see a notice for you: + +``` +1. Waiting for source changes... (press enter to interrupt) +2016-01-03 21:48:42 JRebel: Reloading class 'gitbucket.core.html.main$'. +[info] Wrote rebel.xml to /git/gitbucket/target/scala-2.11/resource_managed/main/rebel.xml +[info] Compiling 1 Scala source to /git/gitbucket/target/scala-2.11/classes... +[success] Total time: 3 s, completed Jan 3, 2016 9:48:55 PM +2. Waiting for source changes... (press enter to interrupt) +``` + +And you reload browser, JRebel give notice of that it has reloaded classes: + +``` +[success] Total time: 3 s, completed Jan 3, 2016 9:48:55 PM +2. Waiting for source changes... (press enter to interrupt) +2016-01-03 21:49:13 JRebel: Reloading class 'gitbucket.core.html.main$'. +``` + +## 6. Limitations + +JRebel is nearly always able to eliminate the need to explicitly reload your container after a code change. However, if you change any of your routes patterns, there is nothing JRebel can do, you will have to run `jetty:start`. diff --git a/doc/readme.md b/doc/readme.md index bf69b5c..f8b23de 100644 --- a/doc/readme.md +++ b/doc/readme.md @@ -3,9 +3,10 @@ * [How to run from source tree](how_to_run.md) * [Directory Structure](directory.md) * [Mapping and Validation](validation.md) - * Authentication in Controller (not yet) + * [Authentication in Controller](authenticator.md) * [About Action in Issue Comment](comment_action.md) * [Activity Types](activity.md) * [Notification Email](notification.md) * [Automatic Schema Updating](auto_update.md) * [Release Operation](release.md) + * [JRebel integration (optional)](jrebel.md) diff --git a/doc/release.md b/doc/release.md index 186f251..682dc88 100644 --- a/doc/release.md +++ b/doc/release.md @@ -6,24 +6,29 @@ Note to update version number in files below: -### src/main/scala/gitbucket/core/servlet/AutoUpdate.scala +### build.sbt ```scala -object AutoUpdate { - - /** - * The history of versions. A head of this sequence is the current BitBucket version. - */ - val versions = Seq( - new Version(3, 3), // <---- add this line!! - new Version(3, 2), +val Organization = "gitbucket" +val Name = "gitbucket" +val GitBucketVersion = "4.0.0" // <---- update version!! +val ScalatraVersion = "2.4.0" +val JettyVersion = "9.3.6.v20151106" ``` -### env.sh +### src/main/scala/gitbucket/core/GitBucketCoreModule.scala -```bash -#!/bin/sh -export GITBUCKET_VERSION=3.3.0 # <---- update here!! +```scala +object GitBucketCoreModule extends Module("gitbucket-core", + new Version("4.0.0", + new LiquibaseMigration("update/gitbucket-core_4.0.xml"), + new SqlMigration("update/gitbucket-core_4.0.sql") + ), + // add new version definition + new Version("4.1.0", + new LiquibaseMigration("update/gitbucket-core_4.1.xml") + ) +) ``` Generate release files @@ -33,18 +38,18 @@ ### Make release war file -Run `release/make-release-war.sh`. The release war file is generated into `target/scala-2.11/gitbucket.war`. +Run `sbt executable`. The release war file and fingerprint are generated into `target/executable/gitbucket.war`. ```bash -$ cd release -$ ./make-release-war.sh +$ sbt executable ``` ### Deploy assembly jar file -For plug-in development, we have to publish the assembly jar file to the public Maven repository by `release/deploy-assembly-jar.sh`. +For plug-in development, we have to publish the GitBucket jar file to the Maven central repository as well. At first, hit following command to publish artifacts to the sonatype OSS repository: ```bash -$ cd release/ -$ ./deploy-assembly-jar.sh +$ sbt publish-signed ``` + +Then operate release sequence at https://oss.sonatype.org/. diff --git a/embed-jetty/javax.servlet-3.0.0.v201112011016.jar b/embed-jetty/javax.servlet-3.0.0.v201112011016.jar deleted file mode 100644 index b135409..0000000 --- a/embed-jetty/javax.servlet-3.0.0.v201112011016.jar +++ /dev/null Binary files differ diff --git a/embed-jetty/jetty-continuation-8.1.16.v20140903.jar b/embed-jetty/jetty-continuation-8.1.16.v20140903.jar deleted file mode 100644 index ce1acb1..0000000 --- a/embed-jetty/jetty-continuation-8.1.16.v20140903.jar +++ /dev/null Binary files differ diff --git a/embed-jetty/jetty-http-8.1.16.v20140903.jar b/embed-jetty/jetty-http-8.1.16.v20140903.jar deleted file mode 100644 index 30189c7..0000000 --- a/embed-jetty/jetty-http-8.1.16.v20140903.jar +++ /dev/null Binary files differ diff --git a/embed-jetty/jetty-io-8.1.16.v20140903.jar b/embed-jetty/jetty-io-8.1.16.v20140903.jar deleted file mode 100644 index a9afd7c..0000000 --- a/embed-jetty/jetty-io-8.1.16.v20140903.jar +++ /dev/null Binary files differ diff --git a/embed-jetty/jetty-security-8.1.16.v20140903.jar b/embed-jetty/jetty-security-8.1.16.v20140903.jar deleted file mode 100644 index e5bde43..0000000 --- a/embed-jetty/jetty-security-8.1.16.v20140903.jar +++ /dev/null Binary files differ diff --git a/embed-jetty/jetty-server-8.1.16.v20140903.jar b/embed-jetty/jetty-server-8.1.16.v20140903.jar deleted file mode 100644 index ae8ac55..0000000 --- a/embed-jetty/jetty-server-8.1.16.v20140903.jar +++ /dev/null Binary files differ diff --git a/embed-jetty/jetty-servlet-8.1.16.v20140903.jar b/embed-jetty/jetty-servlet-8.1.16.v20140903.jar deleted file mode 100644 index eb2fa57..0000000 --- a/embed-jetty/jetty-servlet-8.1.16.v20140903.jar +++ /dev/null Binary files differ diff --git a/embed-jetty/jetty-util-8.1.16.v20140903.jar b/embed-jetty/jetty-util-8.1.16.v20140903.jar deleted file mode 100644 index 5c3c346..0000000 --- a/embed-jetty/jetty-util-8.1.16.v20140903.jar +++ /dev/null Binary files differ diff --git a/embed-jetty/jetty-webapp-8.1.16.v20140903.jar b/embed-jetty/jetty-webapp-8.1.16.v20140903.jar deleted file mode 100644 index 85fd7e0..0000000 --- a/embed-jetty/jetty-webapp-8.1.16.v20140903.jar +++ /dev/null Binary files differ diff --git a/embed-jetty/jetty-xml-8.1.16.v20140903.jar b/embed-jetty/jetty-xml-8.1.16.v20140903.jar deleted file mode 100644 index 1e485de..0000000 --- a/embed-jetty/jetty-xml-8.1.16.v20140903.jar +++ /dev/null Binary files differ diff --git a/embed-jetty/update.sh b/embed-jetty/update.sh deleted file mode 100755 index 7d1cfd7..0000000 --- a/embed-jetty/update.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash -version=$1 -output_dir=`dirname $0` -git rm -f ${output_dir}/jetty-*.jar -for name in 'io' 'servlet' 'xml' 'continuation' 'security' 'util' 'http' 'server' 'webapp' -do - jar_filename="jetty-${name}-${version}.jar" - wget "http://repo1.maven.org/maven2/org/eclipse/jetty/jetty-${name}/${version}/${jar_filename}" -O ${output_dir}/${jar_filename} -done -git add ${output_dir}/*.jar -git commit diff --git a/env.sh b/env.sh deleted file mode 100644 index d6cd233..0000000 --- a/env.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/sh -export GITBUCKET_VERSION=3.5.0-SNAPSHOT diff --git a/gitbucket-assembly.iml b/gitbucket-assembly.iml deleted file mode 100644 index 3f0a572..0000000 --- a/gitbucket-assembly.iml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/project/Checksums.scala b/project/Checksums.scala new file mode 100644 index 0000000..dc9d849 --- /dev/null +++ b/project/Checksums.scala @@ -0,0 +1,34 @@ +import java.security.MessageDigest; +import scala.annotation._ +import sbt._ +import sbt.Using._ + +object Checksums { + private val bufferSize = 2048 + + def generate(source:File, target:File, algorithm:String):Unit = + IO write (target, compute(source, algorithm)) + + def compute(file:File, algorithm:String):String = + hex(raw(file, algorithm)) + + def raw(file:File, algorithm:String):Array[Byte] = + (Using fileInputStream file) { is => + val md = MessageDigest getInstance algorithm + val buf = new Array[Byte](bufferSize) + md.reset() + @tailrec + def loop() { + val len = is read buf + if (len != -1) { + md update (buf, 0, len) + loop() + } + } + loop() + md.digest() + } + + def hex(bytes:Array[Byte]):String = + bytes map { it => "%02x" format (it.toInt & 0xff) } mkString "" +} diff --git a/project/build.properties b/project/build.properties index a6e117b..35c88ba 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version=0.13.8 +sbt.version=0.13.12 diff --git a/project/build.scala b/project/build.scala deleted file mode 100644 index 7047d25..0000000 --- a/project/build.scala +++ /dev/null @@ -1,80 +0,0 @@ -import sbt._ -import Keys._ -import org.scalatra.sbt._ -import com.typesafe.sbteclipse.plugin.EclipsePlugin.EclipseKeys -import play.twirl.sbt.SbtTwirl -import play.twirl.sbt.Import.TwirlKeys._ -import sbtassembly._ -import sbtassembly.AssemblyKeys._ - -object MyBuild extends Build { - val Organization = "gitbucket" - val Name = "gitbucket" - val Version = System.getenv("GITBUCKET_VERSION") - val ScalaVersion = "2.11.6" - val ScalatraVersion = "2.3.1" - - lazy val project = Project ( - "gitbucket", - file(".") - ) - .settings(ScalatraPlugin.scalatraWithJRebel: _*) - .settings( - test in assembly := {}, - assemblyMergeStrategy in assembly := { - case PathList("META-INF", xs @ _*) => - (xs map {_.toLowerCase}) match { - case ("manifest.mf" :: Nil) => MergeStrategy.discard - case _ => MergeStrategy.discard - } - case x => MergeStrategy.first - } - ) - .settings( - sourcesInBase := false, - organization := Organization, - name := Name, - version := Version, - scalaVersion := ScalaVersion, - resolvers ++= Seq( - Classpaths.typesafeReleases, - "amateras-repo" at "http://amateras.sourceforge.jp/mvn/" - ), - scalacOptions := Seq("-deprecation", "-language:postfixOps"), - libraryDependencies ++= Seq( - "org.eclipse.jgit" % "org.eclipse.jgit.http.server" % "3.4.2.201412180340-r", - "org.eclipse.jgit" % "org.eclipse.jgit.archive" % "3.4.2.201412180340-r", - "org.scalatra" %% "scalatra" % ScalatraVersion, - "org.scalatra" %% "scalatra-specs2" % ScalatraVersion % "test", - "org.scalatra" %% "scalatra-json" % ScalatraVersion, - "org.json4s" %% "json4s-jackson" % "3.2.11", - "jp.sf.amateras" %% "scalatra-forms" % "0.1.0", - "commons-io" % "commons-io" % "2.4", - "org.pegdown" % "pegdown" % "1.4.1", // 1.4.2 has incompatible APi changes - "org.apache.commons" % "commons-compress" % "1.9", - "org.apache.commons" % "commons-email" % "1.3.3", - "org.apache.httpcomponents" % "httpclient" % "4.3.6", - "org.apache.sshd" % "apache-sshd" % "0.11.0", - "com.typesafe.slick" %% "slick" % "2.1.0", - "com.novell.ldap" % "jldap" % "2009-10-07", - "com.h2database" % "h2" % "1.4.180", -// "ch.qos.logback" % "logback-classic" % "1.0.13" % "runtime", - "org.eclipse.jetty" % "jetty-webapp" % "8.1.16.v20140903" % "container;provided", - "org.eclipse.jetty.orbit" % "javax.servlet" % "3.0.0.v201112011016" % "container;provided;test" artifacts Artifact("javax.servlet", "jar", "jar"), - "junit" % "junit" % "4.12" % "test", - "com.mchange" % "c3p0" % "0.9.5", - "com.typesafe" % "config" % "1.2.1", - "com.typesafe.play" %% "twirl-compiler" % "1.0.4", - "com.typesafe.akka" %% "akka-actor" % "2.3.10", - "com.enragedginger" %% "akka-quartz-scheduler" % "1.3.0-akka-2.3.x" - ), - play.twirl.sbt.Import.TwirlKeys.templateImports += "gitbucket.core._", - EclipseKeys.withSource := true, - javacOptions in compile ++= Seq("-target", "7", "-source", "7"), - testOptions in Test += Tests.Argument(TestFrameworks.Specs2, "junitxml", "console"), - javaOptions in Test += "-Dgitbucket.home=target/gitbucket_home_for_test", - testOptions in Test += Tests.Setup( () => new java.io.File("target/gitbucket_home_for_test").mkdir() ), - fork in Test := true, - packageOptions += Package.MainClass("JettyLauncher") - ).enablePlugins(SbtTwirl) -} diff --git a/project/plugins.sbt b/project/plugins.sbt index e19dd70..07a7ebe 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -1,8 +1,8 @@ scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature") -addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.4.0") -addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.6.0") -addSbtPlugin("org.scalatra.sbt" % "scalatra-sbt" % "0.3.5") -addSbtPlugin("com.typesafe.sbt" % "sbt-twirl" % "1.0.4") -addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.1.8") -addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.12.0") +addSbtPlugin("com.typesafe.sbt" % "sbt-twirl" % "1.0.4") +addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.12.0") +addSbtPlugin("com.earldouglas" % "xsbt-web-plugin" % "2.1.0") +addSbtPlugin("fi.gekkio.sbtplugins" % "sbt-jrebel-plugin" % "0.10.0") +addSbtPlugin("com.typesafe.sbt" % "sbt-pgp" % "0.8.3") +addSbtPlugin("io.get-coursier" % "sbt-coursier" % "1.0.0-M15") diff --git a/project/project/plugins.sbt b/project/project/plugins.sbt new file mode 100644 index 0000000..5b48d85 --- /dev/null +++ b/project/project/plugins.sbt @@ -0,0 +1 @@ +addSbtPlugin("io.get-coursier" % "sbt-coursier" % "1.0.0-M15") diff --git a/release/build.xml b/release/build.xml deleted file mode 100644 index 2e6b8da..0000000 --- a/release/build.xml +++ /dev/null @@ -1,62 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/release/deploy-assembly-jar.sh b/release/deploy-assembly-jar.sh deleted file mode 100755 index a1de29b..0000000 --- a/release/deploy-assembly-jar.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/sh -source ../env.sh - -cd ../ -./sbt.sh clean assembly - -cd release -mvn deploy:deploy-file \ - -DgroupId=gitbucket\ - -DartifactId=gitbucket-assembly\ - -Dversion=$GITBUCKET_VERSION\ - -Dpackaging=jar\ - -Dfile=../target/scala-2.11/gitbucket-assembly-$GITBUCKET_VERSION.jar\ - -DrepositoryId=sourceforge.jp\ - -Durl=scp://shell.sourceforge.jp/home/groups/a/am/amateras/htdocs/mvn/ diff --git a/release/make-release-war.sh b/release/make-release-war.sh deleted file mode 100755 index 6245718..0000000 --- a/release/make-release-war.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh -source ../env.sh -ant -f build.xml all diff --git a/release/pom.xml b/release/pom.xml deleted file mode 100644 index 40693f2..0000000 --- a/release/pom.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - 4.0.0 - jp.sf.amateras - gitbucket-assembly - 0.0.1 - - - - org.apache.maven.wagon - wagon-ssh - 1.0-beta-6 - - - - \ No newline at end of file diff --git a/sbt-launch-0.13.12.jar b/sbt-launch-0.13.12.jar new file mode 100644 index 0000000..871dedd --- /dev/null +++ b/sbt-launch-0.13.12.jar Binary files differ diff --git a/sbt-launch-0.13.8.jar b/sbt-launch-0.13.8.jar deleted file mode 100644 index 0d9dd94..0000000 --- a/sbt-launch-0.13.8.jar +++ /dev/null Binary files differ diff --git a/sbt.bat b/sbt.bat index 7e90f12..bfc44ca 100644 --- a/sbt.bat +++ b/sbt.bat @@ -1,2 +1,2 @@ set SCRIPT_DIR=%~dp0 -java -Dsbt.log.noformat=true -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256m -Xmx512M -Xss2M -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005 -jar "%SCRIPT_DIR%\sbt-launch-0.13.8.jar" %* +java %JAVA_OPTS% -Dsbt.log.noformat=true -XX:+CMSClassUnloadingEnabled -Xmx512M -Xss2M -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005 -jar "%SCRIPT_DIR%\sbt-launch-0.13.12.jar" %* diff --git a/sbt.sh b/sbt.sh index 6119129..55d0be1 100755 --- a/sbt.sh +++ b/sbt.sh @@ -1,3 +1,2 @@ #!/bin/sh -source ./env.sh -java -Dsbt.log.noformat=true -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256m -Xmx512M -Xss2M -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005 -jar `dirname $0`/sbt-launch-0.13.8.jar "$@" +java $JAVA_OPTS -Dsbt.log.noformat=true -XX:+CMSClassUnloadingEnabled -Xmx512M -Xss2M -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005 -jar `dirname $0`/sbt-launch-0.13.12.jar "$@" diff --git a/src/main/java/JettyLauncher.java b/src/main/java/JettyLauncher.java index 6300547..01168e0 100644 --- a/src/main/java/JettyLauncher.java +++ b/src/main/java/JettyLauncher.java @@ -1,15 +1,16 @@ import org.eclipse.jetty.server.Server; -import org.eclipse.jetty.server.nio.SelectChannelConnector; import org.eclipse.jetty.webapp.WebAppContext; import java.io.File; import java.net.URL; +import java.net.InetSocketAddress; import java.security.ProtectionDomain; public class JettyLauncher { public static void main(String[] args) throws Exception { String host = null; int port = 8080; + InetSocketAddress address = null; String contextPath = "/"; boolean forceHttps = false; @@ -23,6 +24,9 @@ port = Integer.parseInt(dim[1]); } else if(dim[0].equals("--prefix")) { contextPath = dim[1]; + if(!contextPath.startsWith("/")){ + contextPath = "/" + contextPath; + } } else if(dim[0].equals("--gitbucket.home")){ System.setProperty("gitbucket.home", dim[1]); } @@ -30,24 +34,29 @@ } } - Server server = new Server(); - - SelectChannelConnector connector = new SelectChannelConnector(); if(host != null) { - connector.setHost(host); + address = new InetSocketAddress(host, port); + } else { + address = new InetSocketAddress(port); } - connector.setMaxIdleTime(1000 * 60 * 60); - connector.setSoLingerTime(-1); - connector.setPort(port); - server.addConnector(connector); + + Server server = new Server(address); + +// SelectChannelConnector connector = new SelectChannelConnector(); +// if(host != null) { +// connector.setHost(host); +// } +// connector.setMaxIdleTime(1000 * 60 * 60); +// connector.setSoLingerTime(-1); +// connector.setPort(port); +// server.addConnector(connector); WebAppContext context = new WebAppContext(); File tmpDir = new File(getGitBucketHome(), "tmp"); - if(tmpDir.exists()){ - deleteDirectory(tmpDir); + if(!tmpDir.exists()){ + tmpDir.mkdirs(); } - tmpDir.mkdirs(); context.setTempDirectory(tmpDir); ProtectionDomain domain = JettyLauncher.class.getProtectionDomain(); @@ -62,6 +71,8 @@ } server.setHandler(context); + server.setStopAtShutdown(true); + server.setStopTimeout(7_000); server.start(); server.join(); } diff --git a/src/main/java/org/postgresql/Driver2.java b/src/main/java/org/postgresql/Driver2.java new file mode 100644 index 0000000..5763656 --- /dev/null +++ b/src/main/java/org/postgresql/Driver2.java @@ -0,0 +1,52 @@ +package org.postgresql; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.Properties; + +/** + * Wraps the PostgreSQL JDBC driver to convert the returning column names to lower case. + */ +public class Driver2 extends Driver { + + @Override + public java.sql.Connection connect(String url, Properties info) throws SQLException { + Connection conn = super.connect(url, info); + + Object proxy = Proxy.newProxyInstance( + conn.getClass().getClassLoader(), + new Class[]{ Connection.class }, + new ConnectionProxyHandler(conn) + ); + + return Connection.class.cast(proxy); + } + + + private static class ConnectionProxyHandler implements InvocationHandler { + + private Connection conn; + + public ConnectionProxyHandler(Connection conn){ + this.conn = conn; + } + + @Override + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + if(method.getName().equals("prepareStatement")){ + if(args != null && args.length == 2 && args[1].getClass().isArray()){ + String[] keys = (String[]) args[1]; + for(int i = 0; i < keys.length; i++){ + keys[i] = keys[i].toLowerCase(); + } + } + } + return method.invoke(conn, args); + } + } + +} + diff --git a/src/main/resources/database.conf b/src/main/resources/database.conf deleted file mode 100644 index 0eca733..0000000 --- a/src/main/resources/database.conf +++ /dev/null @@ -1,6 +0,0 @@ -db { - driver = "org.h2.Driver" - url = "jdbc:h2:${DatabaseHome};MVCC=true" - user = "sa" - password = "sa" -} diff --git a/src/main/resources/logback-dev.xml b/src/main/resources/logback-dev.xml new file mode 100644 index 0000000..714265e --- /dev/null +++ b/src/main/resources/logback-dev.xml @@ -0,0 +1,26 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + gitbucket.log + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml index fccd344..68da400 100644 --- a/src/main/resources/logback.xml +++ b/src/main/resources/logback.xml @@ -6,12 +6,23 @@ + + - + + \ No newline at end of file diff --git a/src/main/resources/update/1_0.sql b/src/main/resources/update/1_0.sql deleted file mode 100644 index 5067ada..0000000 --- a/src/main/resources/update/1_0.sql +++ /dev/null @@ -1,135 +0,0 @@ -CREATE TABLE ACCOUNT( - USER_NAME VARCHAR(100) NOT NULL, - MAIL_ADDRESS VARCHAR(100) NOT NULL, - PASSWORD VARCHAR(40) NOT NULL, - ADMINISTRATOR BOOLEAN NOT NULL, - URL VARCHAR(200), - REGISTERED_DATE TIMESTAMP NOT NULL, - UPDATED_DATE TIMESTAMP NOT NULL, - LAST_LOGIN_DATE TIMESTAMP -); - -CREATE TABLE REPOSITORY( - USER_NAME VARCHAR(100) NOT NULL, - REPOSITORY_NAME VARCHAR(100) NOT NULL, - PRIVATE BOOLEAN NOT NULL, - DESCRIPTION TEXT, - DEFAULT_BRANCH VARCHAR(100), - REGISTERED_DATE TIMESTAMP NOT NULL, - UPDATED_DATE TIMESTAMP NOT NULL, - LAST_ACTIVITY_DATE TIMESTAMP NOT NULL -); - -CREATE TABLE COLLABORATOR( - USER_NAME VARCHAR(100) NOT NULL, - REPOSITORY_NAME VARCHAR(100) NOT NULL, - COLLABORATOR_NAME VARCHAR(100) NOT NULL -); - -CREATE TABLE ISSUE( - USER_NAME VARCHAR(100) NOT NULL, - REPOSITORY_NAME VARCHAR(100) NOT NULL, - ISSUE_ID INT NOT NULL, - OPENED_USER_NAME VARCHAR(100) NOT NULL, - MILESTONE_ID INT, - ASSIGNED_USER_NAME VARCHAR(100), - TITLE TEXT NOT NULL, - CONTENT TEXT, - CLOSED BOOLEAN NOT NULL, - REGISTERED_DATE TIMESTAMP NOT NULL, - UPDATED_DATE TIMESTAMP NOT NULL -); - -CREATE TABLE ISSUE_ID( - USER_NAME VARCHAR(100) NOT NULL, - REPOSITORY_NAME VARCHAR(100) NOT NULL, - ISSUE_ID INT NOT NULL -); - -CREATE TABLE ISSUE_COMMENT( - USER_NAME VARCHAR(100) NOT NULL, - REPOSITORY_NAME VARCHAR(100) NOT NULL, - ISSUE_ID INT NOT NULL, - COMMENT_ID INT AUTO_INCREMENT, - ACTION VARCHAR(10), - COMMENTED_USER_NAME VARCHAR(100) NOT NULL, - CONTENT TEXT NOT NULL, - REGISTERED_DATE TIMESTAMP NOT NULL, - UPDATED_DATE TIMESTAMP NOT NULL -); - -CREATE TABLE LABEL( - USER_NAME VARCHAR(100) NOT NULL, - REPOSITORY_NAME VARCHAR(100) NOT NULL, - LABEL_ID INT AUTO_INCREMENT, - LABEL_NAME VARCHAR(100) NOT NULL, - COLOR CHAR(6) NOT NULL -); - -CREATE TABLE ISSUE_LABEL( - USER_NAME VARCHAR(100) NOT NULL, - REPOSITORY_NAME VARCHAR(100) NOT NULL, - ISSUE_ID INT NOT NULL, - LABEL_ID INT NOT NULL -); - -CREATE TABLE MILESTONE( - USER_NAME VARCHAR(100) NOT NULL, - REPOSITORY_NAME VARCHAR(100) NOT NULL, - MILESTONE_ID INT AUTO_INCREMENT, - TITLE VARCHAR(100) NOT NULL, - DESCRIPTION TEXT, - DUE_DATE TIMESTAMP, - CLOSED_DATE TIMESTAMP -); - -ALTER TABLE ACCOUNT ADD CONSTRAINT IDX_ACCOUNT_PK PRIMARY KEY (USER_NAME); -ALTER TABLE ACCOUNT ADD CONSTRAINT IDX_ACCOUNT_1 UNIQUE (MAIL_ADDRESS); - -ALTER TABLE REPOSITORY ADD CONSTRAINT IDX_REPOSITORY_PK PRIMARY KEY (USER_NAME, REPOSITORY_NAME); -ALTER TABLE REPOSITORY ADD CONSTRAINT IDX_REPOSITORY_FK0 FOREIGN KEY (USER_NAME) REFERENCES ACCOUNT (USER_NAME); - -ALTER TABLE COLLABORATOR ADD CONSTRAINT IDX_COLLABORATOR_PK PRIMARY KEY (USER_NAME, REPOSITORY_NAME); -ALTER TABLE COLLABORATOR ADD CONSTRAINT IDX_COLLABORATOR_FK0 FOREIGN KEY (USER_NAME, REPOSITORY_NAME) REFERENCES REPOSITORY (USER_NAME, REPOSITORY_NAME); -ALTER TABLE COLLABORATOR ADD CONSTRAINT IDX_COLLABORATOR_FK1 FOREIGN KEY (COLLABORATOR_NAME) REFERENCES ACCOUNT (USER_NAME); - -ALTER TABLE ISSUE ADD CONSTRAINT IDX_ISSUE_PK PRIMARY KEY (ISSUE_ID, USER_NAME, REPOSITORY_NAME); -ALTER TABLE ISSUE ADD CONSTRAINT IDX_ISSUE_FK0 FOREIGN KEY (USER_NAME, REPOSITORY_NAME) REFERENCES REPOSITORY (USER_NAME, REPOSITORY_NAME); -ALTER TABLE ISSUE ADD CONSTRAINT IDX_ISSUE_FK1 FOREIGN KEY (OPENED_USER_NAME) REFERENCES ACCOUNT (USER_NAME); -ALTER TABLE ISSUE ADD CONSTRAINT IDX_ISSUE_FK2 FOREIGN KEY (MILESTONE_ID) REFERENCES MILESTONE (MILESTONE_ID); - -ALTER TABLE ISSUE_ID ADD CONSTRAINT IDX_ISSUE_ID_PK PRIMARY KEY (USER_NAME, REPOSITORY_NAME); -ALTER TABLE ISSUE_ID ADD CONSTRAINT IDX_ISSUE_ID_FK1 FOREIGN KEY (USER_NAME, REPOSITORY_NAME) REFERENCES REPOSITORY (USER_NAME, REPOSITORY_NAME); - -ALTER TABLE ISSUE_COMMENT ADD CONSTRAINT IDX_ISSUE_COMMENT_PK PRIMARY KEY (COMMENT_ID); -ALTER TABLE ISSUE_COMMENT ADD CONSTRAINT IDX_ISSUE_COMMENT_1 UNIQUE (USER_NAME, REPOSITORY_NAME, ISSUE_ID, COMMENT_ID); -ALTER TABLE ISSUE_COMMENT ADD CONSTRAINT IDX_ISSUE_COMMENT_FK0 FOREIGN KEY (USER_NAME, REPOSITORY_NAME, ISSUE_ID) REFERENCES ISSUE (USER_NAME, REPOSITORY_NAME, ISSUE_ID); - -ALTER TABLE LABEL ADD CONSTRAINT IDX_LABEL_PK PRIMARY KEY (USER_NAME, REPOSITORY_NAME, LABEL_ID); -ALTER TABLE LABEL ADD CONSTRAINT IDX_LABEL_FK0 FOREIGN KEY (USER_NAME, REPOSITORY_NAME) REFERENCES REPOSITORY (USER_NAME, REPOSITORY_NAME); - -ALTER TABLE ISSUE_LABEL ADD CONSTRAINT IDX_ISSUE_LABEL_PK PRIMARY KEY (USER_NAME, REPOSITORY_NAME, ISSUE_ID, LABEL_ID); -ALTER TABLE ISSUE_LABEL ADD CONSTRAINT IDX_ISSUE_LABEL_FK0 FOREIGN KEY (USER_NAME, REPOSITORY_NAME, ISSUE_ID) REFERENCES ISSUE (USER_NAME, REPOSITORY_NAME, ISSUE_ID); - -ALTER TABLE MILESTONE ADD CONSTRAINT IDX_MILESTONE_PK PRIMARY KEY (USER_NAME, REPOSITORY_NAME, MILESTONE_ID); -ALTER TABLE MILESTONE ADD CONSTRAINT IDX_MILESTONE_FK0 FOREIGN KEY (USER_NAME, REPOSITORY_NAME) REFERENCES REPOSITORY (USER_NAME, REPOSITORY_NAME); - -INSERT INTO ACCOUNT ( - USER_NAME, - MAIL_ADDRESS, - PASSWORD, - ADMINISTRATOR, - URL, - REGISTERED_DATE, - UPDATED_DATE, - LAST_LOGIN_DATE -) VALUES ( - 'root', - 'root@localhost', - 'dc76e9f0c0006e8f919e0c515c66dbba3982f785', - true, - 'https://github.com/takezoe/gitbucket', - SYSDATE, - SYSDATE, - NULL -); diff --git a/src/main/resources/update/1_1.sql b/src/main/resources/update/1_1.sql deleted file mode 100644 index 9cfd50a..0000000 --- a/src/main/resources/update/1_1.sql +++ /dev/null @@ -1,8 +0,0 @@ --- Fix COLLABORATOR constraints -ALTER TABLE COLLABORATOR DROP CONSTRAINT IDX_COLLABORATOR_FK1 IF EXISTS; -ALTER TABLE COLLABORATOR DROP CONSTRAINT IDX_COLLABORATOR_FK0 IF EXISTS; -ALTER TABLE COLLABORATOR DROP CONSTRAINT IDX_COLLABORATOR_PK IF EXISTS; - -ALTER TABLE COLLABORATOR ADD CONSTRAINT IDX_COLLABORATOR_PK PRIMARY KEY (USER_NAME, REPOSITORY_NAME, COLLABORATOR_NAME); -ALTER TABLE COLLABORATOR ADD CONSTRAINT IDX_COLLABORATOR_FK0 FOREIGN KEY (USER_NAME, REPOSITORY_NAME) REFERENCES REPOSITORY (USER_NAME, REPOSITORY_NAME); -ALTER TABLE COLLABORATOR ADD CONSTRAINT IDX_COLLABORATOR_FK1 FOREIGN KEY (COLLABORATOR_NAME) REFERENCES ACCOUNT (USER_NAME); diff --git a/src/main/resources/update/1_12.sql b/src/main/resources/update/1_12.sql deleted file mode 100644 index f8658a2..0000000 --- a/src/main/resources/update/1_12.sql +++ /dev/null @@ -1,11 +0,0 @@ -ALTER TABLE GROUP_MEMBER ADD COLUMN MANAGER BOOLEAN DEFAULT FALSE; - -CREATE TABLE SSH_KEY ( - USER_NAME VARCHAR(100) NOT NULL, - SSH_KEY_ID INT AUTO_INCREMENT, - TITLE VARCHAR(100) NOT NULL, - PUBLIC_KEY TEXT NOT NULL -); - -ALTER TABLE SSH_KEY ADD CONSTRAINT IDX_SSH_KEY_PK PRIMARY KEY (USER_NAME, SSH_KEY_ID); -ALTER TABLE SSH_KEY ADD CONSTRAINT IDX_SSH_KEY_FK0 FOREIGN KEY (USER_NAME) REFERENCES ACCOUNT (USER_NAME); diff --git a/src/main/resources/update/1_13.sql b/src/main/resources/update/1_13.sql deleted file mode 100644 index ed26f65..0000000 --- a/src/main/resources/update/1_13.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE COMMIT_LOG; \ No newline at end of file diff --git a/src/main/resources/update/1_2.sql b/src/main/resources/update/1_2.sql deleted file mode 100644 index d2765bc..0000000 --- a/src/main/resources/update/1_2.sql +++ /dev/null @@ -1,24 +0,0 @@ -CREATE TABLE ACTIVITY( - ACTIVITY_ID INT AUTO_INCREMENT, - USER_NAME VARCHAR(100) NOT NULL, - REPOSITORY_NAME VARCHAR(100) NOT NULL, - ACTIVITY_USER_NAME VARCHAR(100) NOT NULL, - ACTIVITY_TYPE VARCHAR(100) NOT NULL, - MESSAGE TEXT NOT NULL, - ADDITIONAL_INFO TEXT, - ACTIVITY_DATE TIMESTAMP NOT NULL -); - -CREATE TABLE COMMIT_LOG ( - USER_NAME VARCHAR(100) NOT NULL, - REPOSITORY_NAME VARCHAR(100) NOT NULL, - COMMIT_ID VARCHAR(40) NOT NULL -); - -ALTER TABLE ACTIVITY ADD CONSTRAINT IDX_ACTIVITY_PK PRIMARY KEY (ACTIVITY_ID); -ALTER TABLE ACTIVITY ADD CONSTRAINT IDX_ACTIVITY_FK0 FOREIGN KEY (USER_NAME, REPOSITORY_NAME) REFERENCES REPOSITORY (USER_NAME, REPOSITORY_NAME); -ALTER TABLE ACTIVITY ADD CONSTRAINT IDX_ACTIVITY_FK1 FOREIGN KEY (ACTIVITY_USER_NAME) REFERENCES ACCOUNT (USER_NAME); - -ALTER TABLE COMMIT_LOG ADD CONSTRAINT IDX_COMMIT_LOG_PK PRIMARY KEY (USER_NAME, REPOSITORY_NAME, COMMIT_ID); -ALTER TABLE COMMIT_LOG ADD CONSTRAINT IDX_COMMIT_LOG_FK0 FOREIGN KEY (USER_NAME, REPOSITORY_NAME) REFERENCES REPOSITORY (USER_NAME, REPOSITORY_NAME); - diff --git a/src/main/resources/update/1_3.sql b/src/main/resources/update/1_3.sql deleted file mode 100644 index 59ab009..0000000 --- a/src/main/resources/update/1_3.sql +++ /dev/null @@ -1,8 +0,0 @@ -ALTER TABLE ACCOUNT ADD COLUMN IMAGE VARCHAR(100); - -UPDATE ISSUE_COMMENT SET ACTION = 'comment' WHERE ACTION IS NULL; - -ALTER TABLE ISSUE_COMMENT ALTER COLUMN ACTION VARCHAR(20) NOT NULL; - -UPDATE ISSUE_COMMENT SET ACTION = 'close_comment' WHERE ACTION = 'close'; -UPDATE ISSUE_COMMENT SET ACTION = 'reopen_comment' WHERE ACTION = 'reopen'; diff --git a/src/main/resources/update/1_4.sql b/src/main/resources/update/1_4.sql deleted file mode 100644 index 2d3c492..0000000 --- a/src/main/resources/update/1_4.sql +++ /dev/null @@ -1,24 +0,0 @@ -CREATE TABLE GROUP_MEMBER( - GROUP_NAME VARCHAR(100) NOT NULL, - USER_NAME VARCHAR(100) NOT NULL -); - -ALTER TABLE GROUP_MEMBER ADD CONSTRAINT IDX_GROUP_MEMBER_PK PRIMARY KEY (GROUP_NAME, USER_NAME); -ALTER TABLE GROUP_MEMBER ADD CONSTRAINT IDX_GROUP_MEMBER_FK0 FOREIGN KEY (GROUP_NAME) REFERENCES ACCOUNT (USER_NAME); -ALTER TABLE GROUP_MEMBER ADD CONSTRAINT IDX_GROUP_MEMBER_FK1 FOREIGN KEY (USER_NAME) REFERENCES ACCOUNT (USER_NAME); - -ALTER TABLE ACCOUNT ADD COLUMN GROUP_ACCOUNT BOOLEAN NOT NULL DEFAULT FALSE; - -CREATE OR REPLACE VIEW ISSUE_OUTLINE_VIEW AS - SELECT - A.USER_NAME, - A.REPOSITORY_NAME, - A.ISSUE_ID, - NVL(B.COMMENT_COUNT, 0) AS COMMENT_COUNT - FROM ISSUE A - LEFT OUTER JOIN ( - SELECT USER_NAME, REPOSITORY_NAME, ISSUE_ID, COUNT(COMMENT_ID) AS COMMENT_COUNT FROM ISSUE_COMMENT - WHERE ACTION IN ('comment', 'close_comment', 'reopen_comment') - GROUP BY USER_NAME, REPOSITORY_NAME, ISSUE_ID - ) B - ON (A.USER_NAME = B.USER_NAME AND A.REPOSITORY_NAME = B.REPOSITORY_NAME AND A.ISSUE_ID = B.ISSUE_ID); diff --git a/src/main/resources/update/1_5.sql b/src/main/resources/update/1_5.sql deleted file mode 100644 index 03fc1bf..0000000 --- a/src/main/resources/update/1_5.sql +++ /dev/null @@ -1,21 +0,0 @@ -ALTER TABLE REPOSITORY ADD COLUMN ORIGIN_USER_NAME VARCHAR(100); -ALTER TABLE REPOSITORY ADD COLUMN ORIGIN_REPOSITORY_NAME VARCHAR(100); -ALTER TABLE REPOSITORY ADD COLUMN PARENT_USER_NAME VARCHAR(100); -ALTER TABLE REPOSITORY ADD COLUMN PARENT_REPOSITORY_NAME VARCHAR(100); - -CREATE TABLE PULL_REQUEST( - USER_NAME VARCHAR(100) NOT NULL, - REPOSITORY_NAME VARCHAR(100) NOT NULL, - ISSUE_ID INT NOT NULL, - BRANCH VARCHAR(100) NOT NULL, - REQUEST_USER_NAME VARCHAR(100) NOT NULL, - REQUEST_REPOSITORY_NAME VARCHAR(100) NOT NULL, - REQUEST_BRANCH VARCHAR(100) NOT NULL, - COMMIT_ID_FROM VARCHAR(40) NOT NULL, - COMMIT_ID_TO VARCHAR(40) NOT NULL -); - -ALTER TABLE PULL_REQUEST ADD CONSTRAINT IDX_PULL_REQUEST_PK PRIMARY KEY (USER_NAME, REPOSITORY_NAME, ISSUE_ID); -ALTER TABLE PULL_REQUEST ADD CONSTRAINT IDX_PULL_REQUEST_FK0 FOREIGN KEY (USER_NAME, REPOSITORY_NAME, ISSUE_ID) REFERENCES ISSUE (USER_NAME, REPOSITORY_NAME, ISSUE_ID); - -ALTER TABLE ISSUE ADD COLUMN PULL_REQUEST BOOLEAN NOT NULL DEFAULT FALSE; diff --git a/src/main/resources/update/1_6.sql b/src/main/resources/update/1_6.sql deleted file mode 100644 index 43eb92d..0000000 --- a/src/main/resources/update/1_6.sql +++ /dev/null @@ -1,8 +0,0 @@ -CREATE TABLE WEB_HOOK ( - USER_NAME VARCHAR(100) NOT NULL, - REPOSITORY_NAME VARCHAR(100) NOT NULL, - URL VARCHAR(200) NOT NULL -); - -ALTER TABLE WEB_HOOK ADD CONSTRAINT IDX_WEB_HOOK_PK PRIMARY KEY (USER_NAME, REPOSITORY_NAME, URL); -ALTER TABLE WEB_HOOK ADD CONSTRAINT IDX_WEB_HOOK_FK0 FOREIGN KEY (USER_NAME, REPOSITORY_NAME) REFERENCES REPOSITORY (USER_NAME, REPOSITORY_NAME); diff --git a/src/main/resources/update/1_7.sql b/src/main/resources/update/1_7.sql deleted file mode 100644 index 9005ff9..0000000 --- a/src/main/resources/update/1_7.sql +++ /dev/null @@ -1,5 +0,0 @@ -ALTER TABLE ACCOUNT ADD COLUMN FULL_NAME VARCHAR(100); - -UPDATE ACCOUNT SET FULL_NAME = USER_NAME WHERE FULL_NAME IS NULL; - -ALTER TABLE ACCOUNT ALTER COLUMN FULL_NAME SET NOT NULL; diff --git a/src/main/resources/update/1_8.sql b/src/main/resources/update/1_8.sql deleted file mode 100644 index 8b50ddf..0000000 --- a/src/main/resources/update/1_8.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER TABLE ACCOUNT ADD COLUMN REMOVED BOOLEAN DEFAULT FALSE; \ No newline at end of file diff --git a/src/main/resources/update/2_3.sql b/src/main/resources/update/2_3.sql deleted file mode 100644 index 7620092..0000000 --- a/src/main/resources/update/2_3.sql +++ /dev/null @@ -1,6 +0,0 @@ -CREATE TABLE PLUGIN ( - PLUGIN_ID VARCHAR(100) NOT NULL, - VERSION VARCHAR(100) NOT NULL -); - -ALTER TABLE PLUGIN ADD CONSTRAINT IDX_PLUGIN_PK PRIMARY KEY (PLUGIN_ID); diff --git a/src/main/resources/update/2_7.sql b/src/main/resources/update/2_7.sql deleted file mode 100644 index 6fa0684..0000000 --- a/src/main/resources/update/2_7.sql +++ /dev/null @@ -1,18 +0,0 @@ -CREATE TABLE COMMIT_COMMENT ( - USER_NAME VARCHAR(100) NOT NULL, - REPOSITORY_NAME VARCHAR(100) NOT NULL, - COMMIT_ID VARCHAR(100) NOT NULL, - COMMENT_ID INT AUTO_INCREMENT, - COMMENTED_USER_NAME VARCHAR(100) NOT NULL, - CONTENT TEXT NOT NULL, - FILE_NAME NVARCHAR(100), - OLD_LINE_NUMBER INT, - NEW_LINE_NUMBER INT, - REGISTERED_DATE TIMESTAMP NOT NULL, - UPDATED_DATE TIMESTAMP NOT NULL, - PULL_REQUEST BOOLEAN NOT NULL -); - -ALTER TABLE COMMIT_COMMENT ADD CONSTRAINT IDX_COMMIT_COMMENT_PK PRIMARY KEY (COMMENT_ID); -ALTER TABLE COMMIT_COMMENT ADD CONSTRAINT IDX_COMMIT_COMMENT_FK0 FOREIGN KEY (USER_NAME, REPOSITORY_NAME) REFERENCES REPOSITORY (USER_NAME, REPOSITORY_NAME); -ALTER TABLE COMMIT_COMMENT ADD CONSTRAINT IDX_COMMIT_COMMENT_1 UNIQUE (USER_NAME, REPOSITORY_NAME, COMMIT_ID, COMMENT_ID); diff --git a/src/main/resources/update/2_8.sql b/src/main/resources/update/2_8.sql deleted file mode 100644 index 38c95d3..0000000 --- a/src/main/resources/update/2_8.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER TABLE COMMIT_COMMENT ALTER COLUMN FILE_NAME NVARCHAR(260); diff --git a/src/main/resources/update/3_1.sql b/src/main/resources/update/3_1.sql deleted file mode 100644 index 3ddc48a..0000000 --- a/src/main/resources/update/3_1.sql +++ /dev/null @@ -1,42 +0,0 @@ -DROP TABLE IF EXISTS ACCESS_TOKEN; - -CREATE TABLE ACCESS_TOKEN ( - ACCESS_TOKEN_ID INT NOT NULL AUTO_INCREMENT, - TOKEN_HASH VARCHAR(40) NOT NULL, - USER_NAME VARCHAR(100) NOT NULL, - NOTE TEXT NOT NULL -); - -ALTER TABLE ACCESS_TOKEN ADD CONSTRAINT IDX_ACCESS_TOKEN_PK PRIMARY KEY (ACCESS_TOKEN_ID); -ALTER TABLE ACCESS_TOKEN ADD CONSTRAINT IDX_ACCESS_TOKEN_FK0 FOREIGN KEY (USER_NAME) REFERENCES ACCOUNT (USER_NAME) - ON DELETE CASCADE ON UPDATE CASCADE; -ALTER TABLE ACCESS_TOKEN ADD CONSTRAINT IDX_ACCESS_TOKEN_TOKEN_HASH UNIQUE(TOKEN_HASH); - - -DROP TABLE IF EXISTS COMMIT_STATUS; -CREATE TABLE COMMIT_STATUS( - COMMIT_STATUS_ID INT AUTO_INCREMENT, - USER_NAME VARCHAR(100) NOT NULL, - REPOSITORY_NAME VARCHAR(100) NOT NULL, - COMMIT_ID VARCHAR(40) NOT NULL, - CONTEXT VARCHAR(255) NOT NULL, -- context is too long (maximum is 255 characters) - STATE VARCHAR(10) NOT NULL, -- pending, success, error, or failure - TARGET_URL VARCHAR(200), - DESCRIPTION TEXT, - CREATOR VARCHAR(100) NOT NULL, - REGISTERED_DATE TIMESTAMP NOT NULL, -- CREATED_AT - UPDATED_DATE TIMESTAMP NOT NULL -- UPDATED_AT -); -ALTER TABLE COMMIT_STATUS ADD CONSTRAINT IDX_COMMIT_STATUS_PK PRIMARY KEY (COMMIT_STATUS_ID); -ALTER TABLE COMMIT_STATUS ADD CONSTRAINT IDX_COMMIT_STATUS_1 - UNIQUE (USER_NAME, REPOSITORY_NAME, COMMIT_ID, CONTEXT); -ALTER TABLE COMMIT_STATUS ADD CONSTRAINT IDX_COMMIT_STATUS_FK1 - FOREIGN KEY (USER_NAME, REPOSITORY_NAME) - REFERENCES REPOSITORY (USER_NAME, REPOSITORY_NAME) - ON DELETE CASCADE ON UPDATE CASCADE; -ALTER TABLE COMMIT_STATUS ADD CONSTRAINT IDX_COMMIT_STATUS_FK2 - FOREIGN KEY (USER_NAME) REFERENCES ACCOUNT (USER_NAME) - ON DELETE CASCADE ON UPDATE CASCADE; -ALTER TABLE COMMIT_STATUS ADD CONSTRAINT IDX_COMMIT_STATUS_FK3 - FOREIGN KEY (CREATOR) REFERENCES ACCOUNT (USER_NAME) - ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/src/main/resources/update/3_5.sql b/src/main/resources/update/3_5.sql deleted file mode 100644 index 99f2594..0000000 --- a/src/main/resources/update/3_5.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER TABLE ACCOUNT ADD COLUMN GROUP_DESCRIPTION TEXT; diff --git a/src/main/resources/update/gitbucket-core_4.0.sql b/src/main/resources/update/gitbucket-core_4.0.sql new file mode 100644 index 0000000..f97bdbc --- /dev/null +++ b/src/main/resources/update/gitbucket-core_4.0.sql @@ -0,0 +1,18 @@ +CREATE OR REPLACE VIEW ISSUE_OUTLINE_VIEW AS + SELECT + A.USER_NAME, + A.REPOSITORY_NAME, + A.ISSUE_ID, + COALESCE(B.COMMENT_COUNT, 0) + COALESCE(C.COMMENT_COUNT, 0) AS COMMENT_COUNT + FROM ISSUE A + LEFT OUTER JOIN ( + SELECT USER_NAME, REPOSITORY_NAME, ISSUE_ID, COUNT(COMMENT_ID) AS COMMENT_COUNT FROM ISSUE_COMMENT + WHERE ACTION IN ('comment', 'close_comment', 'reopen_comment') + GROUP BY USER_NAME, REPOSITORY_NAME, ISSUE_ID + ) B + ON (A.USER_NAME = B.USER_NAME AND A.REPOSITORY_NAME = B.REPOSITORY_NAME AND A.ISSUE_ID = B.ISSUE_ID) + LEFT OUTER JOIN ( + SELECT USER_NAME, REPOSITORY_NAME, ISSUE_ID, COUNT(COMMENT_ID) AS COMMENT_COUNT FROM COMMIT_COMMENT + GROUP BY USER_NAME, REPOSITORY_NAME, ISSUE_ID + ) C + ON (A.USER_NAME = C.USER_NAME AND A.REPOSITORY_NAME = C.REPOSITORY_NAME AND A.ISSUE_ID = C.ISSUE_ID); diff --git a/src/main/resources/update/gitbucket-core_4.0.xml b/src/main/resources/update/gitbucket-core_4.0.xml new file mode 100644 index 0000000..04f33fe --- /dev/null +++ b/src/main/resources/update/gitbucket-core_4.0.xml @@ -0,0 +1,351 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/update/gitbucket-core_4.2.xml b/src/main/resources/update/gitbucket-core_4.2.xml new file mode 100644 index 0000000..78aa748 --- /dev/null +++ b/src/main/resources/update/gitbucket-core_4.2.xml @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/src/main/resources/update/gitbucket-core_4.6.xml b/src/main/resources/update/gitbucket-core_4.6.xml new file mode 100644 index 0000000..6bf4acf --- /dev/null +++ b/src/main/resources/update/gitbucket-core_4.6.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/src/main/resources/update/gitbucket-core_4.7.sql b/src/main/resources/update/gitbucket-core_4.7.sql new file mode 100644 index 0000000..ef13c70 --- /dev/null +++ b/src/main/resources/update/gitbucket-core_4.7.sql @@ -0,0 +1,2 @@ +-- DELETE COLLABORATORS IN GROUP REPOSITORIES +DELETE FROM COLLABORATOR WHERE USER_NAME IN (SELECT USER_NAME FROM ACCOUNT WHERE GROUP_ACCOUNT = TRUE) diff --git a/src/main/resources/update/gitbucket-core_4.7.xml b/src/main/resources/update/gitbucket-core_4.7.xml new file mode 100644 index 0000000..4eb2f03 --- /dev/null +++ b/src/main/resources/update/gitbucket-core_4.7.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + + ENABLE_WIKI = FALSE + + + + ENABLE_WIKI = TRUE AND ALLOW_WIKI_EDITING = FALSE + + + + ENABLE_WIKI = TRUE AND ALLOW_WIKI_EDITING = TRUE + + + + ENABLE_ISSUES = FALSE + + + + ENABLE_ISSUES = TRUE + + + + + diff --git a/src/main/resources/update/gitbucket-core_4.9.sql b/src/main/resources/update/gitbucket-core_4.9.sql new file mode 100644 index 0000000..99f2594 --- /dev/null +++ b/src/main/resources/update/gitbucket-core_4.9.sql @@ -0,0 +1 @@ +ALTER TABLE ACCOUNT ADD COLUMN GROUP_DESCRIPTION TEXT; diff --git a/src/main/scala/ScalatraBootstrap.scala b/src/main/scala/ScalatraBootstrap.scala index 7ba1d02..949bf80 100644 --- a/src/main/scala/ScalatraBootstrap.scala +++ b/src/main/scala/ScalatraBootstrap.scala @@ -1,24 +1,33 @@ -import gitbucket.core.controller._ -import gitbucket.core.plugin.PluginRegistry -import gitbucket.core.servlet.{AccessTokenAuthenticationFilter, BasicAuthenticationFilter, Database, TransactionFilter} -import gitbucket.core.util.Directory - import java.util.EnumSet import javax.servlet._ +import gitbucket.core.controller._ +import gitbucket.core.plugin.PluginRegistry +import gitbucket.core.service.SystemSettingsService +import gitbucket.core.servlet._ +import gitbucket.core.util.Directory import org.scalatra._ -class ScalatraBootstrap extends LifeCycle { +class ScalatraBootstrap extends LifeCycle with SystemSettingsService { override def init(context: ServletContext) { + + val settings = loadSystemSettings() + if(settings.baseUrl.exists(_.startsWith("https://"))) { + context.getSessionCookieConfig.setSecure(true) + } + // Register TransactionFilter and BasicAuthenticationFilter at first context.addFilter("transactionFilter", new TransactionFilter) context.getFilterRegistration("transactionFilter").addMappingForUrlPatterns(EnumSet.allOf(classOf[DispatcherType]), true, "/*") - context.addFilter("basicAuthenticationFilter", new BasicAuthenticationFilter) - context.getFilterRegistration("basicAuthenticationFilter").addMappingForUrlPatterns(EnumSet.allOf(classOf[DispatcherType]), true, "/git/*") - context.addFilter("accessTokenAuthenticationFilter", new AccessTokenAuthenticationFilter) - context.getFilterRegistration("accessTokenAuthenticationFilter").addMappingForUrlPatterns(EnumSet.allOf(classOf[DispatcherType]), true, "/api/v3/*") + context.addFilter("gitAuthenticationFilter", new GitAuthenticationFilter) + context.getFilterRegistration("gitAuthenticationFilter").addMappingForUrlPatterns(EnumSet.allOf(classOf[DispatcherType]), true, "/git/*") + context.addFilter("apiAuthenticationFilter", new ApiAuthenticationFilter) + context.getFilterRegistration("apiAuthenticationFilter").addMappingForUrlPatterns(EnumSet.allOf(classOf[DispatcherType]), true, "/api/v3/*") + context.addFilter("ghCompatRepositoryAccessFilter", new GHCompatRepositoryAccessFilter) + context.getFilterRegistration("ghCompatRepositoryAccessFilter").addMappingForUrlPatterns(EnumSet.allOf(classOf[DispatcherType]), true, "/*") + // Register controllers context.mount(new AnonymousAccessController, "/*") @@ -27,11 +36,10 @@ } context.mount(new IndexController, "/") - context.mount(new SearchController, "/") + context.mount(new ApiController, "/api/v3") context.mount(new FileUploadController, "/upload") + context.mount(new SystemSettingsController, "/admin") context.mount(new DashboardController, "/*") - context.mount(new UserManagementController, "/*") - context.mount(new SystemSettingsController, "/*") context.mount(new AccountController, "/*") context.mount(new RepositoryViewerController, "/*") context.mount(new WikiController, "/*") diff --git a/src/main/scala/gitbucket/core/GitBucketCoreModule.scala b/src/main/scala/gitbucket/core/GitBucketCoreModule.scala new file mode 100644 index 0000000..55d6680 --- /dev/null +++ b/src/main/scala/gitbucket/core/GitBucketCoreModule.scala @@ -0,0 +1,31 @@ +package gitbucket.core + +import io.github.gitbucket.solidbase.migration.{SqlMigration, LiquibaseMigration} +import io.github.gitbucket.solidbase.model.{Version, Module} + +object GitBucketCoreModule extends Module("gitbucket-core", + new Version("4.0.0", + new LiquibaseMigration("update/gitbucket-core_4.0.xml"), + new SqlMigration("update/gitbucket-core_4.0.sql") + ), + new Version("4.1.0"), + new Version("4.2.0", + new LiquibaseMigration("update/gitbucket-core_4.2.xml") + ), + new Version("4.2.1"), + new Version("4.3.0"), + new Version("4.4.0"), + new Version("4.5.0"), + new Version("4.6.0", + new LiquibaseMigration("update/gitbucket-core_4.6.xml") + ), + new Version("4.7.0", + new LiquibaseMigration("update/gitbucket-core_4.7.xml"), + new SqlMigration("update/gitbucket-core_4.7.sql") + ), + new Version("4.7.1"), + new Version("4.8"), + new Version("4.9", + new SqlMigration("update/gitbucket-core_4.9.sql") + ) +) diff --git a/src/main/scala/gitbucket/core/api/ApiBranch.scala b/src/main/scala/gitbucket/core/api/ApiBranch.scala new file mode 100644 index 0000000..0a27850 --- /dev/null +++ b/src/main/scala/gitbucket/core/api/ApiBranch.scala @@ -0,0 +1,23 @@ +package gitbucket.core.api + +import gitbucket.core.util.RepositoryName + +/** + * https://developer.github.com/v3/repos/#get-branch + * https://developer.github.com/v3/repos/#enabling-and-disabling-branch-protection + */ +case class ApiBranch( + name: String, + // commit: ApiBranchCommit, + protection: ApiBranchProtection)(repositoryName:RepositoryName) extends FieldSerializable { + def _links = Map( + "self" -> ApiPath(s"/api/v3/repos/${repositoryName.fullName}/branches/${name}"), + "html" -> ApiPath(s"/${repositoryName.fullName}/tree/${name}")) +} + +case class ApiBranchCommit(sha: String) + +case class ApiBranchForList( + name: String, + commit: ApiBranchCommit +) diff --git a/src/main/scala/gitbucket/core/api/ApiBranchProtection.scala b/src/main/scala/gitbucket/core/api/ApiBranchProtection.scala new file mode 100644 index 0000000..dc452eb --- /dev/null +++ b/src/main/scala/gitbucket/core/api/ApiBranchProtection.scala @@ -0,0 +1,46 @@ +package gitbucket.core.api + +import gitbucket.core.service.ProtectedBranchService +import org.json4s._ + +/** https://developer.github.com/v3/repos/#enabling-and-disabling-branch-protection */ +case class ApiBranchProtection(enabled: Boolean, required_status_checks: Option[ApiBranchProtection.Status]){ + def status: ApiBranchProtection.Status = required_status_checks.getOrElse(ApiBranchProtection.statusNone) +} + +object ApiBranchProtection{ + /** form for enabling-and-disabling-branch-protection */ + case class EnablingAndDisabling(protection: ApiBranchProtection) + + def apply(info: ProtectedBranchService.ProtectedBranchInfo): ApiBranchProtection = ApiBranchProtection( + enabled = info.enabled, + required_status_checks = Some(Status(EnforcementLevel(info.enabled && info.contexts.nonEmpty, info.includeAdministrators), info.contexts))) + val statusNone = Status(Off, Seq.empty) + case class Status(enforcement_level: EnforcementLevel, contexts: Seq[String]) + sealed class EnforcementLevel(val name: String) + case object Off extends EnforcementLevel("off") + case object NonAdmins extends EnforcementLevel("non_admins") + case object Everyone extends EnforcementLevel("everyone") + object EnforcementLevel { + def apply(enabled: Boolean, includeAdministrators: Boolean): EnforcementLevel = if(enabled){ + if(includeAdministrators){ + Everyone + }else{ + NonAdmins + } + }else{ + Off + } + } + + implicit val enforcementLevelSerializer = new CustomSerializer[EnforcementLevel](format => ( + { + case JString("off") => Off + case JString("non_admins") => NonAdmins + case JString("everyone") => Everyone + }, + { + case x: EnforcementLevel => JString(x.name) + } + )) +} diff --git a/src/main/scala/gitbucket/core/api/ApiComment.scala b/src/main/scala/gitbucket/core/api/ApiComment.scala index 47244f2..62bcd3c 100644 --- a/src/main/scala/gitbucket/core/api/ApiComment.scala +++ b/src/main/scala/gitbucket/core/api/ApiComment.scala @@ -14,16 +14,16 @@ user: ApiUser, body: String, created_at: Date, - updated_at: Date)(repositoryName: RepositoryName, issueId: Int){ - val html_url = ApiPath(s"/${repositoryName.fullName}/issues/${issueId}#comment-${id}") + updated_at: Date)(repositoryName: RepositoryName, issueId: Int, isPullRequest: Boolean){ + val html_url = ApiPath(s"/${repositoryName.fullName}/${if(isPullRequest){ "pull" }else{ "issues" }}/${issueId}#comment-${id}") } object ApiComment{ - def apply(comment: IssueComment, repositoryName: RepositoryName, issueId: Int, user: ApiUser): ApiComment = + def apply(comment: IssueComment, repositoryName: RepositoryName, issueId: Int, user: ApiUser, isPullRequest: Boolean): ApiComment = ApiComment( id = comment.commentId, user = user, body = comment.content, created_at = comment.registeredDate, - updated_at = comment.updatedDate)(repositoryName, issueId) + updated_at = comment.updatedDate)(repositoryName, issueId, isPullRequest) } diff --git a/src/main/scala/gitbucket/core/api/ApiCommit.scala b/src/main/scala/gitbucket/core/api/ApiCommit.scala index 15b41d4..9229d0c 100644 --- a/src/main/scala/gitbucket/core/api/ApiCommit.scala +++ b/src/main/scala/gitbucket/core/api/ApiCommit.scala @@ -20,13 +20,21 @@ removed: List[String], modified: List[String], author: ApiPersonIdent, - committer: ApiPersonIdent)(repositoryName:RepositoryName){ - val url = ApiPath(s"/api/v3/${repositoryName.fullName}/commits/${id}") - val html_url = ApiPath(s"/${repositoryName.fullName}/commit/${id}") + committer: ApiPersonIdent)(repositoryName:RepositoryName, urlIsHtmlUrl: Boolean) extends FieldSerializable{ + val url = if(urlIsHtmlUrl){ + ApiPath(s"/${repositoryName.fullName}/commit/${id}") + }else{ + ApiPath(s"/api/v3/${repositoryName.fullName}/commits/${id}") + } + val html_url = if(urlIsHtmlUrl){ + None + }else{ + Some(ApiPath(s"/${repositoryName.fullName}/commit/${id}")) + } } object ApiCommit{ - def apply(git: Git, repositoryName: RepositoryName, commit: CommitInfo): ApiCommit = { + def apply(git: Git, repositoryName: RepositoryName, commit: CommitInfo, urlIsHtmlUrl: Boolean = false): ApiCommit = { val diffs = JGitUtil.getDiffs(git, commit.id, false) ApiCommit( id = commit.id, @@ -43,6 +51,7 @@ }, author = ApiPersonIdent.author(commit), committer = ApiPersonIdent.committer(commit) - )(repositoryName) + )(repositoryName, urlIsHtmlUrl) } + def forPushPayload(git: Git, repositoryName: RepositoryName, commit: CommitInfo): ApiCommit = apply(git, repositoryName, commit, true) } diff --git a/src/main/scala/gitbucket/core/api/ApiContents.scala b/src/main/scala/gitbucket/core/api/ApiContents.scala new file mode 100644 index 0000000..1582370 --- /dev/null +++ b/src/main/scala/gitbucket/core/api/ApiContents.scala @@ -0,0 +1,18 @@ +package gitbucket.core.api + +import gitbucket.core.util.JGitUtil.FileInfo +import org.apache.commons.codec.binary.Base64 + +case class ApiContents(`type`: String, name: String, content: Option[String], encoding: Option[String]) + +object ApiContents{ + def apply(fileInfo: FileInfo, content: Option[Array[Byte]]): ApiContents = { + if(fileInfo.isDirectory) { + ApiContents("dir", fileInfo.name, None, None) + } else { + content.map(arr => + ApiContents("file", fileInfo.name, Some(Base64.encodeBase64String(arr)), Some("base64")) + ).getOrElse(ApiContents("file", fileInfo.name, None, None)) + } + } +} diff --git a/src/main/scala/gitbucket/core/api/ApiEndPoint.scala b/src/main/scala/gitbucket/core/api/ApiEndPoint.scala new file mode 100644 index 0000000..4c66d84 --- /dev/null +++ b/src/main/scala/gitbucket/core/api/ApiEndPoint.scala @@ -0,0 +1,3 @@ +package gitbucket.core.api + +case class ApiEndPoint(rate_limit_url: ApiPath = ApiPath("/api/v3/rate_limit")) \ No newline at end of file diff --git a/src/main/scala/gitbucket/core/api/ApiIssue.scala b/src/main/scala/gitbucket/core/api/ApiIssue.scala index 45b5d62..47374ed 100644 --- a/src/main/scala/gitbucket/core/api/ApiIssue.scala +++ b/src/main/scala/gitbucket/core/api/ApiIssue.scala @@ -17,9 +17,19 @@ state: String, created_at: Date, updated_at: Date, - body: String)(repositoryName: RepositoryName){ + body: String)(repositoryName: RepositoryName, isPullRequest: Boolean){ val comments_url = ApiPath(s"/api/v3/repos/${repositoryName.fullName}/issues/${number}/comments") - val html_url = ApiPath(s"/${repositoryName.fullName}/issues/${number}") + val html_url = ApiPath(s"/${repositoryName.fullName}/${if(isPullRequest){ "pull" }else{ "issues" }}/${number}") + val pull_request = if (isPullRequest) { + Some(Map( + "url" -> ApiPath(s"/api/v3/repos/${repositoryName.fullName}/pulls/${number}"), + "html_url" -> ApiPath(s"/${repositoryName.fullName}/pull/${number}") + // "diff_url" -> ApiPath(s"/${repositoryName.fullName}/pull/${number}.diff"), + // "patch_url" -> ApiPath(s"/${repositoryName.fullName}/pull/${number}.patch") + )) + } else { + None + } } object ApiIssue{ @@ -31,5 +41,5 @@ state = if(issue.closed){ "closed" }else{ "open" }, body = issue.content.getOrElse(""), created_at = issue.registeredDate, - updated_at = issue.updatedDate)(repositoryName) + updated_at = issue.updatedDate)(repositoryName, issue.isPullRequest) } diff --git a/src/main/scala/gitbucket/core/api/ApiLabel.scala b/src/main/scala/gitbucket/core/api/ApiLabel.scala new file mode 100644 index 0000000..2d1842b --- /dev/null +++ b/src/main/scala/gitbucket/core/api/ApiLabel.scala @@ -0,0 +1,21 @@ +package gitbucket.core.api + +import gitbucket.core.model.Label +import gitbucket.core.util.RepositoryName + +/** + * https://developer.github.com/v3/issues/labels/ + */ +case class ApiLabel( + name: String, + color: String)(repositoryName: RepositoryName){ + var url = ApiPath(s"/api/v3/repos/${repositoryName.fullName}/labels/${name}") +} + +object ApiLabel{ + def apply(label:Label, repositoryName: RepositoryName): ApiLabel = + ApiLabel( + name = label.labelName, + color = label.color + )(repositoryName) +} \ No newline at end of file diff --git a/src/main/scala/gitbucket/core/api/ApiPullRequest.scala b/src/main/scala/gitbucket/core/api/ApiPullRequest.scala index 7577525..3ae6ac0 100644 --- a/src/main/scala/gitbucket/core/api/ApiPullRequest.scala +++ b/src/main/scala/gitbucket/core/api/ApiPullRequest.scala @@ -1,7 +1,6 @@ package gitbucket.core.api -import gitbucket.core.model.{Issue, PullRequest} - +import gitbucket.core.model.{Account, Issue, IssueComment, PullRequest} import java.util.Date @@ -15,6 +14,9 @@ head: ApiPullRequest.Commit, base: ApiPullRequest.Commit, mergeable: Option[Boolean], + merged: Boolean, + merged_at: Option[Date], + merged_by: Option[ApiUser], title: String, body: String, user: ApiUser) { @@ -31,7 +33,15 @@ } object ApiPullRequest{ - def apply(issue: Issue, pullRequest: PullRequest, headRepo: ApiRepository, baseRepo: ApiRepository, user: ApiUser): ApiPullRequest = ApiPullRequest( + def apply( + issue: Issue, + pullRequest: PullRequest, + headRepo: ApiRepository, + baseRepo: ApiRepository, + user: ApiUser, + mergedComment: Option[(IssueComment, Account)] + ): ApiPullRequest = + ApiPullRequest( number = issue.issueId, updated_at = issue.updatedDate, created_at = issue.registeredDate, @@ -44,6 +54,9 @@ ref = pullRequest.branch, repo = baseRepo)(issue.userName), mergeable = None, // TODO: need check mergeable. + merged = mergedComment.isDefined, + merged_at = mergedComment.map { case (comment, _) => comment.registeredDate }, + merged_by = mergedComment.map { case (_, account) => ApiUser(account) }, title = issue.title, body = issue.content.getOrElse(""), user = user diff --git a/src/main/scala/gitbucket/core/api/ApiPullRequestReviewComment.scala b/src/main/scala/gitbucket/core/api/ApiPullRequestReviewComment.scala new file mode 100644 index 0000000..55d1fe9 --- /dev/null +++ b/src/main/scala/gitbucket/core/api/ApiPullRequestReviewComment.scala @@ -0,0 +1,61 @@ +package gitbucket.core.api + +import gitbucket.core.util.RepositoryName +import gitbucket.core.model.CommitComment + +import java.util.Date + +/** + * https://developer.github.com/v3/activity/events/types/#pullrequestreviewcommentevent + */ +case class ApiPullRequestReviewComment( + id: Int, // 29724692 + // "diff_hunk": "@@ -1 +1 @@\n-# public-repo", + path: String, // "README.md", + // "position": 1, + // "original_position": 1, + commit_id: String, // "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c", + // "original_commit_id": "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c", + user: ApiUser, + body: String, // "Maybe you should use more emojji on this line.", + created_at: Date, // "2015-05-05T23:40:27Z", + updated_at: Date // "2015-05-05T23:40:27Z", +)(repositoryName:RepositoryName, issueId: Int) extends FieldSerializable { + // "url": "https://api.github.com/repos/baxterthehacker/public-repo/pulls/comments/29724692", + val url = ApiPath(s"/api/v3/repos/${repositoryName.fullName}/pulls/comments/${id}") + // "html_url": "https://github.com/baxterthehacker/public-repo/pull/1#discussion_r29724692", + val html_url = ApiPath(s"/${repositoryName.fullName}/pull/${issueId}#discussion_r${id}") + // "pull_request_url": "https://api.github.com/repos/baxterthehacker/public-repo/pulls/1", + val pull_request_url = ApiPath(s"/api/v3/repos/${repositoryName.fullName}/pulls/${issueId}") + + /* + "_links": { + "self": { + "href": "https://api.github.com/repos/baxterthehacker/public-repo/pulls/comments/29724692" + }, + "html": { + "href": "https://github.com/baxterthehacker/public-repo/pull/1#discussion_r29724692" + }, + "pull_request": { + "href": "https://api.github.com/repos/baxterthehacker/public-repo/pulls/1" + } + } + */ + val _links = Map( + "self" -> Map("href" -> url), + "html" -> Map("href" -> html_url), + "pull_request" -> Map("href" -> pull_request_url)) +} + +object ApiPullRequestReviewComment{ + def apply(comment: CommitComment, commentedUser: ApiUser, repositoryName: RepositoryName, issueId: Int): ApiPullRequestReviewComment = + new ApiPullRequestReviewComment( + id = comment.commentId, + path = comment.fileName.getOrElse(""), + commit_id = comment.commitId, + user = commentedUser, + body = comment.content, + created_at = comment.registeredDate, + updated_at = comment.updatedDate + )(repositoryName, issueId) +} diff --git a/src/main/scala/gitbucket/core/api/ApiPusher.scala b/src/main/scala/gitbucket/core/api/ApiPusher.scala new file mode 100644 index 0000000..a0b714e --- /dev/null +++ b/src/main/scala/gitbucket/core/api/ApiPusher.scala @@ -0,0 +1,11 @@ +package gitbucket.core.api + +import gitbucket.core.model.Account + +case class ApiPusher(name: String, email: String) + +object ApiPusher { + def apply(user: Account): ApiPusher = ApiPusher( + name = user.userName, + email = user.mailAddress) +} \ No newline at end of file diff --git a/src/main/scala/gitbucket/core/api/ApiRef.scala b/src/main/scala/gitbucket/core/api/ApiRef.scala new file mode 100644 index 0000000..01a9785 --- /dev/null +++ b/src/main/scala/gitbucket/core/api/ApiRef.scala @@ -0,0 +1,5 @@ +package gitbucket.core.api + +case class ApiObject(sha: String) + +case class ApiRef(ref: String, `object`: ApiObject) diff --git a/src/main/scala/gitbucket/core/api/ApiRepository.scala b/src/main/scala/gitbucket/core/api/ApiRepository.scala index 0911882..9c7377c 100644 --- a/src/main/scala/gitbucket/core/api/ApiRepository.scala +++ b/src/main/scala/gitbucket/core/api/ApiRepository.scala @@ -13,10 +13,14 @@ forks: Int, `private`: Boolean, default_branch: String, - owner: ApiUser) { - val forks_count = forks - val watchers_coun = watchers - val url = ApiPath(s"/api/v3/repos/${full_name}") + owner: ApiUser)(urlIsHtmlUrl: Boolean) { + val forks_count = forks + val watchers_count = watchers + val url = if(urlIsHtmlUrl){ + ApiPath(s"/${full_name}") + } else { + ApiPath(s"/api/v3/repos/${full_name}") + } val http_url = ApiPath(s"/git/${full_name}.git") val clone_url = ApiPath(s"/git/${full_name}.git") val html_url = ApiPath(s"/${full_name}") @@ -27,17 +31,18 @@ repository: Repository, owner: ApiUser, forkedCount: Int =0, - watchers: Int = 0): ApiRepository = + watchers: Int = 0, + urlIsHtmlUrl: Boolean = false): ApiRepository = ApiRepository( - name = repository.repositoryName, - full_name = s"${repository.userName}/${repository.repositoryName}", - description = repository.description.getOrElse(""), - watchers = 0, - forks = forkedCount, - `private` = repository.isPrivate, + name = repository.repositoryName, + full_name = s"${repository.userName}/${repository.repositoryName}", + description = repository.description.getOrElse(""), + watchers = 0, + forks = forkedCount, + `private` = repository.isPrivate, default_branch = repository.defaultBranch, - owner = owner - ) + owner = owner + )(urlIsHtmlUrl) def apply(repositoryInfo: RepositoryInfo, owner: ApiUser): ApiRepository = ApiRepository(repositoryInfo.repository, owner, forkedCount=repositoryInfo.forkedCount) @@ -45,4 +50,7 @@ def apply(repositoryInfo: RepositoryInfo, owner: Account): ApiRepository = this(repositoryInfo.repository, ApiUser(owner)) + def forPushPayload(repositoryInfo: RepositoryInfo, owner: ApiUser): ApiRepository = + ApiRepository(repositoryInfo.repository, owner, forkedCount=repositoryInfo.forkedCount, urlIsHtmlUrl=true) + } diff --git a/src/main/scala/gitbucket/core/api/ApiUser.scala b/src/main/scala/gitbucket/core/api/ApiUser.scala index fa69900..9b3dc9d 100644 --- a/src/main/scala/gitbucket/core/api/ApiUser.scala +++ b/src/main/scala/gitbucket/core/api/ApiUser.scala @@ -13,6 +13,7 @@ created_at: Date) { val url = ApiPath(s"/api/v3/users/${login}") val html_url = ApiPath(s"/${login}") + val avatar_url = ApiPath(s"/${login}/_avatar") // val followers_url = ApiPath(s"/api/v3/users/${login}/followers") // val following_url = ApiPath(s"/api/v3/users/${login}/following{/other_user}") // val gists_url = ApiPath(s"/api/v3/users/${login}/gists{/gist_id}") @@ -29,7 +30,7 @@ def apply(user: Account): ApiUser = ApiUser( login = user.userName, email = user.mailAddress, - `type` = if(user.isGroupAccount){ "Organization" }else{ "User" }, + `type` = if(user.isGroupAccount){ "Organization" } else { "User" }, site_admin = user.isAdmin, created_at = user.registeredDate ) diff --git a/src/main/scala/gitbucket/core/api/CreateALabel.scala b/src/main/scala/gitbucket/core/api/CreateALabel.scala new file mode 100644 index 0000000..cfd71d1 --- /dev/null +++ b/src/main/scala/gitbucket/core/api/CreateALabel.scala @@ -0,0 +1,18 @@ +package gitbucket.core.api + +/** + * https://developer.github.com/v3/issues/labels/#create-a-label + * api form + */ +case class CreateALabel( + name: String, + color: String +) { + def isValid: Boolean = { + name.length<=100 && + !name.startsWith("_") && + !name.startsWith("-") && + color.length==6 && + color.matches("[a-fA-F0-9+_.]+") + } +} \ No newline at end of file diff --git a/src/main/scala/gitbucket/core/api/CreateARepository.scala b/src/main/scala/gitbucket/core/api/CreateARepository.scala new file mode 100644 index 0000000..7247c9f --- /dev/null +++ b/src/main/scala/gitbucket/core/api/CreateARepository.scala @@ -0,0 +1,19 @@ +package gitbucket.core.api + +/** + * https://developer.github.com/v3/repos/#create + * api form + */ +case class CreateARepository( + name: String, + description: Option[String], + `private`: Boolean = false, + auto_init: Boolean = false +) { + def isValid: Boolean = { + name.length <= 100 && + name.matches("[a-zA-Z0-9\\-\\+_.]+") && + !name.startsWith("_") && + !name.startsWith("-") + } +} diff --git a/src/main/scala/gitbucket/core/api/CreateAnIssue.scala b/src/main/scala/gitbucket/core/api/CreateAnIssue.scala new file mode 100644 index 0000000..cb54652 --- /dev/null +++ b/src/main/scala/gitbucket/core/api/CreateAnIssue.scala @@ -0,0 +1,11 @@ +package gitbucket.core.api + +/** + * https://developer.github.com/v3/issues/#create-an-issue + */ +case class CreateAnIssue( + title: String, + body: Option[String], + assignees: List[String], + milestone: Option[Int], + labels: List[String]) diff --git a/src/main/scala/gitbucket/core/api/FieldSerializable.scala b/src/main/scala/gitbucket/core/api/FieldSerializable.scala new file mode 100644 index 0000000..706bf9c --- /dev/null +++ b/src/main/scala/gitbucket/core/api/FieldSerializable.scala @@ -0,0 +1,4 @@ +package gitbucket.core.api + +/** export fields for json */ +trait FieldSerializable diff --git a/src/main/scala/gitbucket/core/api/JsonFormat.scala b/src/main/scala/gitbucket/core/api/JsonFormat.scala index a14a116..611db3b 100644 --- a/src/main/scala/gitbucket/core/api/JsonFormat.scala +++ b/src/main/scala/gitbucket/core/api/JsonFormat.scala @@ -5,40 +5,50 @@ import org.joda.time.format._ import org.json4s._ import org.json4s.jackson.Serialization - import java.util.Date - import scala.util.Try - object JsonFormat { - case class Context(baseUrl:String) + + case class Context(baseUrl: String) + val parserISO = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'") + val jsonFormats = Serialization.formats(NoTypeHints) + new CustomSerializer[Date](format => ( - { case JString(s) => Try(parserISO.parseDateTime(s)).toOption.map(_.toDate) - .getOrElse(throw new MappingException("Can't convert " + s + " to Date")) }, + { case JString(s) => Try(parserISO.parseDateTime(s)).toOption.map(_.toDate).getOrElse(throw new MappingException("Can't convert " + s + " to Date")) }, { case x: Date => JString(parserISO.print(new DateTime(x).withZone(DateTimeZone.UTC))) } ) - ) + FieldSerializer[ApiUser]() + FieldSerializer[ApiPullRequest]() + FieldSerializer[ApiRepository]() + - FieldSerializer[ApiCommitListItem.Parent]() + FieldSerializer[ApiCommitListItem]() + FieldSerializer[ApiCommitListItem.Commit]() + - FieldSerializer[ApiCommitStatus]() + FieldSerializer[ApiCommit]() + FieldSerializer[ApiCombinedCommitStatus]() + - FieldSerializer[ApiPullRequest.Commit]() + FieldSerializer[ApiIssue]() + FieldSerializer[ApiComment]() - + ) + FieldSerializer[ApiUser]() + + FieldSerializer[ApiPullRequest]() + + FieldSerializer[ApiRepository]() + + FieldSerializer[ApiCommitListItem.Parent]() + + FieldSerializer[ApiCommitListItem]() + + FieldSerializer[ApiCommitListItem.Commit]() + + FieldSerializer[ApiCommitStatus]() + + FieldSerializer[FieldSerializable]() + + FieldSerializer[ApiCombinedCommitStatus]() + + FieldSerializer[ApiPullRequest.Commit]() + + FieldSerializer[ApiIssue]() + + FieldSerializer[ApiComment]() + + FieldSerializer[ApiLabel]() + + ApiBranchProtection.enforcementLevelSerializer def apiPathSerializer(c: Context) = new CustomSerializer[ApiPath](format => - ( - { - case JString(s) if s.startsWith(c.baseUrl) => ApiPath(s.substring(c.baseUrl.length)) - case JString(s) => throw new MappingException("Can't convert " + s + " to ApiPath") - }, - { - case ApiPath(path) => JString(c.baseUrl+path) - } - ) + ( + { + case JString(s) if s.startsWith(c.baseUrl) => ApiPath(s.substring(c.baseUrl.length)) + case JString(s) => throw new MappingException("Can't convert " + s + " to ApiPath") + }, + { + case ApiPath(path) => JString(c.baseUrl + path) + } ) + ) + /** * convert object to json string */ def apply(obj: AnyRef)(implicit c: Context): String = Serialization.write(obj)(jsonFormats + apiPathSerializer(c)) + } diff --git a/src/main/scala/gitbucket/core/controller/AccountController.scala b/src/main/scala/gitbucket/core/controller/AccountController.scala index d49bbe7..7452aac 100644 --- a/src/main/scala/gitbucket/core/controller/AccountController.scala +++ b/src/main/scala/gitbucket/core/controller/AccountController.scala @@ -1,7 +1,6 @@ package gitbucket.core.controller import gitbucket.core.account.html -import gitbucket.core.api._ import gitbucket.core.helper import gitbucket.core.model.GroupMember import gitbucket.core.service._ @@ -12,24 +11,22 @@ import gitbucket.core.util.StringUtil._ import gitbucket.core.util._ -import jp.sf.amateras.scalatra.forms._ +import io.github.gitbucket.scalatra.forms._ import org.apache.commons.io.FileUtils -import org.eclipse.jgit.api.Git -import org.eclipse.jgit.dircache.DirCache -import org.eclipse.jgit.lib.{FileMode, Constants} import org.scalatra.i18n.Messages +import org.scalatra.BadRequest class AccountController extends AccountControllerBase with AccountService with RepositoryService with ActivityService with WikiService with LabelsService with SshKeyService with OneselfAuthenticator with UsersAuthenticator with GroupManagerAuthenticator with ReadableUsersAuthenticator - with AccessTokenService with WebHookService + with AccessTokenService with WebHookService with RepositoryCreationService trait AccountControllerBase extends AccountManagementControllerBase { self: AccountService with RepositoryService with ActivityService with WikiService with LabelsService with SshKeyService with OneselfAuthenticator with UsersAuthenticator with GroupManagerAuthenticator with ReadableUsersAuthenticator - with AccessTokenService with WebHookService => + with AccessTokenService with WebHookService with RepositoryCreationService => case class AccountNewForm(userName: String, password: String, fullName: String, mailAddress: String, url: Option[String], fileId: Option[String]) @@ -42,7 +39,7 @@ case class PersonalTokenForm(note: String) val newForm = mapping( - "userName" -> trim(label("User name" , text(required, maxlength(100), identifier, uniqueUserName))), + "userName" -> trim(label("User name" , text(required, maxlength(100), identifier, uniqueUserName, reservedNames))), "password" -> trim(label("Password" , text(required, maxlength(20)))), "fullName" -> trim(label("Full Name" , text(required, maxlength(100)))), "mailAddress" -> trim(label("Mail Address" , text(required, maxlength(100), uniqueMailAddress()))), @@ -72,7 +69,7 @@ case class EditGroupForm(groupName: String, groupDescription: Option[String], url: Option[String], fileId: Option[String], members: String, clearImage: Boolean) val newGroupForm = mapping( - "groupName" -> trim(label("Group name" ,text(required, maxlength(100), identifier, uniqueUserName))), + "groupName" -> trim(label("Group name" ,text(required, maxlength(100), identifier, uniqueUserName, reservedNames))), "groupDescription" -> trim(label("Group description", optional(text()))), "url" -> trim(label("URL" ,optional(text(maxlength(200))))), "fileId" -> trim(label("File ID" ,optional(text()))), @@ -92,8 +89,8 @@ case class ForkRepositoryForm(owner: String, name: String) val newRepositoryForm = mapping( - "owner" -> trim(label("Owner" , text(required, maxlength(40), identifier, existsAccount))), - "name" -> trim(label("Repository name", text(required, maxlength(40), repository, uniqueRepository))), + "owner" -> trim(label("Owner" , text(required, maxlength(100), identifier, existsAccount))), + "name" -> trim(label("Repository name", text(required, maxlength(100), repository, uniqueRepository))), "description" -> trim(label("Description" , optional(text()))), "isPrivate" -> trim(label("Repository Type", boolean())), "createReadme" -> trim(label("Create README" , boolean())) @@ -126,7 +123,7 @@ // Members case "members" if(account.isGroupAccount) => { val members = getGroupMembers(account.userName) - gitbucket.core.account.html.members(account, members.map(_.userName), + gitbucket.core.account.html.members(account, members, context.loginAccount.exists(x => members.exists { member => member.userName == x.userName && member.isManager })) } @@ -135,11 +132,11 @@ val members = getGroupMembers(account.userName) gitbucket.core.account.html.repositories(account, if(account.isGroupAccount) Nil else getGroupsByUserName(userName), - getVisibleRepositories(context.loginAccount, context.baseUrl, Some(userName)), + getVisibleRepositories(context.loginAccount, Some(userName)), context.loginAccount.exists(x => members.exists { member => member.userName == x.userName && member.isManager })) } } - } getOrElse NotFound + } getOrElse NotFound() } get("/:userName.atom") { @@ -158,30 +155,11 @@ } } - /** - * https://developer.github.com/v3/users/#get-a-single-user - */ - get("/api/v3/users/:userName") { - getAccountByUserName(params("userName")).map { account => - JsonFormat(ApiUser(account)) - } getOrElse NotFound - } - - /** - * https://developer.github.com/v3/users/#get-the-authenticated-user - */ - get("/api/v3/user") { - context.loginAccount.map { account => - JsonFormat(ApiUser(account)) - } getOrElse Unauthorized - } - - get("/:userName/_edit")(oneselfOnly { val userName = params("userName") getAccountByUserName(userName).map { x => - html.edit(x, flash.get("info")) - } getOrElse NotFound + html.edit(x, flash.get("info"), flash.get("error")) + } getOrElse NotFound() }) post("/:userName/_edit", editForm)(oneselfOnly { form => @@ -197,13 +175,17 @@ flash += "info" -> "Account information has been updated." redirect(s"/${userName}/_edit") - } getOrElse NotFound + } getOrElse NotFound() }) get("/:userName/_delete")(oneselfOnly { val userName = params("userName") - getAccountByUserName(userName, true).foreach { account => + getAccountByUserName(userName, true).map { account => + if(isLastAdministrator(account)){ + flash += "error" -> "Account can't be removed because this is last one administrator." + redirect(s"/${userName}/_edit") + } else { // // Remove repositories // getRepositoryNamesOfUser(userName).foreach { repositoryName => // deleteRepository(userName, repositoryName) @@ -212,20 +194,19 @@ // FileUtils.deleteDirectory(getTemporaryDir(userName, repositoryName)) // } // // Remove from GROUP_MEMBER, COLLABORATOR and REPOSITORY -// removeUserRelatedData(userName) - - updateAccount(account.copy(isRemoved = true)) - } - - session.invalidate - redirect("/") + removeUserRelatedData(userName) + updateAccount(account.copy(isRemoved = true)) + session.invalidate + redirect("/") + } + } getOrElse NotFound() }) get("/:userName/_ssh")(oneselfOnly { val userName = params("userName") getAccountByUserName(userName).map { x => html.ssh(x, getPublicKeys(x.userName)) - } getOrElse NotFound + } getOrElse NotFound() }) post("/:userName/_ssh", sshKeyForm)(oneselfOnly { form => @@ -256,7 +237,7 @@ case _ => None } html.application(x, tokens, generatedToken) - } getOrElse NotFound + } getOrElse NotFound() }) post("/:userName/_personalToken", personalTokenForm)(oneselfOnly { form => @@ -282,7 +263,7 @@ } else { html.register() } - } else NotFound + } else NotFound() } post("/register", newForm){ form => @@ -290,7 +271,7 @@ createAccount(form.userName, sha1(form.password), form.fullName, form.mailAddress, false, form.url) updateImage(form.userName, form.fileId, false) redirect("/signin") - } else NotFound + } else NotFound() } get("/groups/new")(usersOnly { @@ -340,18 +321,18 @@ // Update GROUP_MEMBER updateGroupMembers(form.groupName, members) - // Update COLLABORATOR for group repositories - getRepositoryNamesOfUser(form.groupName).foreach { repositoryName => - removeCollaborators(form.groupName, repositoryName) - members.foreach { case (userName, isManager) => - addCollaborator(form.groupName, repositoryName, userName) - } - } +// // Update COLLABORATOR for group repositories +// getRepositoryNamesOfUser(form.groupName).foreach { repositoryName => +// removeCollaborators(form.groupName, repositoryName) +// members.foreach { case (userName, isManager) => +// addCollaborator(form.groupName, repositoryName, userName) +// } +// } updateImage(form.groupName, form.fileId, form.clearImage) redirect(s"/${form.groupName}") - } getOrElse NotFound + } getOrElse NotFound() } }) @@ -367,57 +348,8 @@ */ post("/new", newRepositoryForm)(usersOnly { form => LockUtil.lock(s"${form.owner}/${form.name}"){ - if(getRepository(form.owner, form.name, context.baseUrl).isEmpty){ - val ownerAccount = getAccountByUserName(form.owner).get - val loginAccount = context.loginAccount.get - val loginUserName = loginAccount.userName - - // Insert to the database at first - createRepository(form.name, form.owner, form.description, form.isPrivate) - - // Add collaborators for group repository - if(ownerAccount.isGroupAccount){ - getGroupMembers(form.owner).foreach { member => - addCollaborator(form.owner, form.name, member.userName) - } - } - - // Insert default labels - insertDefaultLabels(form.owner, form.name) - - // Create the actual repository - val gitdir = getRepositoryDir(form.owner, form.name) - JGitUtil.initRepository(gitdir) - - if(form.createReadme){ - using(Git.open(gitdir)){ git => - val builder = DirCache.newInCore.builder() - val inserter = git.getRepository.newObjectInserter() - val headId = git.getRepository.resolve(Constants.HEAD + "^{commit}") - val content = if(form.description.nonEmpty){ - form.name + "\n" + - "===============\n" + - "\n" + - form.description.get - } else { - form.name + "\n" + - "===============\n" - } - - builder.add(JGitUtil.createDirCacheEntry("README.md", FileMode.REGULAR_FILE, - inserter.insert(Constants.OBJ_BLOB, content.getBytes("UTF-8")))) - builder.finish() - - JGitUtil.createNewCommit(git, inserter, headId, builder.getDirCache.writeTree(inserter), - Constants.HEAD, loginAccount.fullName, loginAccount.mailAddress, "Initial commit") - } - } - - // Create Wiki repository - createWikiRepository(loginAccount, form.owner, form.name) - - // Record activity - recordCreateRepositoryActivity(form.owner, form.name, loginUserName) + if(getRepository(form.owner, form.name).isEmpty){ + createRepository(context.loginAccount.get, form.owner, form.name, form.description, form.isPrivate, form.createReadme) } // redirect to the repository @@ -426,87 +358,82 @@ }) get("/:owner/:repository/fork")(readableUsersOnly { repository => - val loginAccount = context.loginAccount.get - val loginUserName = loginAccount.userName - val groups = getGroupsByUserName(loginUserName) - groups match { - case _: List[String] => - val managerPermissions = groups.map { group => - val members = getGroupMembers(group) - context.loginAccount.exists(x => members.exists { member => member.userName == x.userName && member.isManager }) - } - helper.html.forkrepository( - repository, - (groups zip managerPermissions).toMap - ) - case _ => redirect(s"/${loginUserName}") - } + if(repository.repository.options.allowFork){ + val loginAccount = context.loginAccount.get + val loginUserName = loginAccount.userName + val groups = getGroupsByUserName(loginUserName) + groups match { + case _: List[String] => + val managerPermissions = groups.map { group => + val members = getGroupMembers(group) + context.loginAccount.exists(x => members.exists { member => member.userName == x.userName && member.isManager }) + } + helper.html.forkrepository( + repository, + (groups zip managerPermissions).toMap + ) + case _ => redirect(s"/${loginUserName}") + } + } else BadRequest() }) post("/:owner/:repository/fork", accountForm)(readableUsersOnly { (form, repository) => - val loginAccount = context.loginAccount.get - val loginUserName = loginAccount.userName - val accountName = form.accountName + if(repository.repository.options.allowFork){ + val loginAccount = context.loginAccount.get + val loginUserName = loginAccount.userName + val accountName = form.accountName - LockUtil.lock(s"${accountName}/${repository.name}"){ - if(getRepository(accountName, repository.name, baseUrl).isDefined || - (accountName != loginUserName && !getGroupsByUserName(loginUserName).contains(accountName))){ - // redirect to the repository if repository already exists - redirect(s"/${accountName}/${repository.name}") - } else { - // Insert to the database at first - val originUserName = repository.repository.originUserName.getOrElse(repository.owner) - val originRepositoryName = repository.repository.originRepositoryName.getOrElse(repository.name) + LockUtil.lock(s"${accountName}/${repository.name}"){ + if(getRepository(accountName, repository.name).isDefined || + (accountName != loginUserName && !getGroupsByUserName(loginUserName).contains(accountName))){ + // redirect to the repository if repository already exists + redirect(s"/${accountName}/${repository.name}") + } else { + // Insert to the database at first + val originUserName = repository.repository.originUserName.getOrElse(repository.owner) + val originRepositoryName = repository.repository.originRepositoryName.getOrElse(repository.name) - createRepository( - repositoryName = repository.name, - userName = accountName, - description = repository.repository.description, - isPrivate = repository.repository.isPrivate, - originRepositoryName = Some(originRepositoryName), - originUserName = Some(originUserName), - parentRepositoryName = Some(repository.name), - parentUserName = Some(repository.owner) - ) + insertRepository( + repositoryName = repository.name, + userName = accountName, + description = repository.repository.description, + isPrivate = repository.repository.isPrivate, + originRepositoryName = Some(originRepositoryName), + originUserName = Some(originUserName), + parentRepositoryName = Some(repository.name), + parentUserName = Some(repository.owner) + ) - // Add collaborators for group repository - val ownerAccount = getAccountByUserName(accountName).get - if(ownerAccount.isGroupAccount){ - getGroupMembers(accountName).foreach { member => - addCollaborator(accountName, repository.name, member.userName) - } +// // Add collaborators for group repository +// val ownerAccount = getAccountByUserName(accountName).get +// if(ownerAccount.isGroupAccount){ +// getGroupMembers(accountName).foreach { member => +// addCollaborator(accountName, repository.name, member.userName) +// } +// } + + // Insert default labels + insertDefaultLabels(accountName, repository.name) + + // clone repository actually + JGitUtil.cloneRepository( + getRepositoryDir(repository.owner, repository.name), + getRepositoryDir(accountName, repository.name)) + + // Create Wiki repository + JGitUtil.cloneRepository( + getWikiRepositoryDir(repository.owner, repository.name), + getWikiRepositoryDir(accountName, repository.name)) + + // Record activity + recordForkActivity(repository.owner, repository.name, loginUserName, accountName) + // redirect to the repository + redirect(s"/${accountName}/${repository.name}") } - - // Insert default labels - insertDefaultLabels(accountName, repository.name) - - // clone repository actually - JGitUtil.cloneRepository( - getRepositoryDir(repository.owner, repository.name), - getRepositoryDir(accountName, repository.name)) - - // Create Wiki repository - JGitUtil.cloneRepository( - getWikiRepositoryDir(repository.owner, repository.name), - getWikiRepositoryDir(accountName, repository.name)) - - // Record activity - recordForkActivity(repository.owner, repository.name, loginUserName, accountName) - // redirect to the repository - redirect(s"/${accountName}/${repository.name}") } - } + } else BadRequest() }) - private def insertDefaultLabels(userName: String, repositoryName: String): Unit = { - createLabel(userName, repositoryName, "bug", "fc2929") - createLabel(userName, repositoryName, "duplicate", "cccccc") - createLabel(userName, repositoryName, "enhancement", "84b6eb") - createLabel(userName, repositoryName, "invalid", "e6e6e6") - createLabel(userName, repositoryName, "question", "cc317c") - createLabel(userName, repositoryName, "wontfix", "ffffff") - } - private def existsAccount: Constraint = new Constraint(){ override def validate(name: String, value: String, messages: Messages): Option[String] = if(getAccountByUserName(value).isEmpty) Some("User or group does not exist.") else None @@ -529,8 +456,8 @@ private def validPublicKey: Constraint = new Constraint(){ override def validate(name: String, value: String, messages: Messages): Option[String] = SshUtil.str2PublicKey(value) match { - case Some(_) => None - case None => Some("Key is invalid.") + case Some(_) if !getAllKeys().exists(_.publicKey == value) => None + case _ => Some("Key is invalid.") } } diff --git a/src/main/scala/gitbucket/core/controller/ApiController.scala b/src/main/scala/gitbucket/core/controller/ApiController.scala new file mode 100644 index 0000000..5911a3f --- /dev/null +++ b/src/main/scala/gitbucket/core/controller/ApiController.scala @@ -0,0 +1,608 @@ +package gitbucket.core.controller + +import gitbucket.core.api._ +import gitbucket.core.model._ +import gitbucket.core.service.IssuesService.IssueSearchCondition +import gitbucket.core.service.PullRequestService._ +import gitbucket.core.service._ +import gitbucket.core.util.ControlUtil._ +import gitbucket.core.util.Directory._ +import gitbucket.core.util.JGitUtil._ +import gitbucket.core.util._ +import gitbucket.core.util.Implicits._ +import gitbucket.core.view.helpers.{renderMarkup, isRenderable} +import org.eclipse.jgit.api.Git +import org.scalatra.{NoContent, UnprocessableEntity, Created} +import scala.collection.JavaConverters._ + +class ApiController extends ApiControllerBase + with RepositoryService + with AccountService + with ProtectedBranchService + with IssuesService + with LabelsService + with MilestonesService + with PullRequestService + with CommitsService + with CommitStatusService + with RepositoryCreationService + with IssueCreationService + with HandleCommentService + with WebHookService + with WebHookPullRequestService + with WebHookIssueCommentService + with WikiService + with ActivityService + with OwnerAuthenticator + with UsersAuthenticator + with GroupManagerAuthenticator + with ReferrerAuthenticator + with ReadableUsersAuthenticator + with WritableUsersAuthenticator + +trait ApiControllerBase extends ControllerBase { + self: RepositoryService + with AccountService + with ProtectedBranchService + with IssuesService + with LabelsService + with MilestonesService + with PullRequestService + with CommitStatusService + with RepositoryCreationService + with IssueCreationService + with HandleCommentService + with OwnerAuthenticator + with UsersAuthenticator + with GroupManagerAuthenticator + with ReferrerAuthenticator + with ReadableUsersAuthenticator + with WritableUsersAuthenticator => + + /** + * https://developer.github.com/v3/#root-endpoint + */ + get("/api/v3/") { + JsonFormat(ApiEndPoint()) + } + + /** + * https://developer.github.com/v3/orgs/#get-an-organization + */ + get("/api/v3/orgs/:groupName") { + getAccountByUserName(params("groupName")).filter(account => account.isGroupAccount).map { account => + JsonFormat(ApiUser(account)) + } getOrElse NotFound() + } + + /** + * https://developer.github.com/v3/users/#get-a-single-user + */ + get("/api/v3/users/:userName") { + getAccountByUserName(params("userName")).filterNot(account => account.isGroupAccount).map { account => + JsonFormat(ApiUser(account)) + } getOrElse NotFound() + } + + /** + * https://developer.github.com/v3/repos/#list-organization-repositories + */ + get("/api/v3/orgs/:orgName/repos") { + JsonFormat(getVisibleRepositories(context.loginAccount, Some(params("orgName"))).map{ r => ApiRepository(r, getAccountByUserName(r.owner).get)}) + } + /** + * https://developer.github.com/v3/repos/#list-user-repositories + */ + get("/api/v3/users/:userName/repos") { + JsonFormat(getVisibleRepositories(context.loginAccount, Some(params("userName"))).map{ r => ApiRepository(r, getAccountByUserName(r.owner).get)}) + } + + /* + * https://developer.github.com/v3/repos/branches/#list-branches + */ + get ("/api/v3/repos/:owner/:repo/branches")(referrersOnly { repository => + JsonFormat(JGitUtil.getBranches( + owner = repository.owner, + name = repository.name, + defaultBranch = repository.repository.defaultBranch, + origin = repository.repository.originUserName.isEmpty + ).map { br => + ApiBranchForList(br.name, ApiBranchCommit(br.commitId)) + }) + }) + + /* + * https://developer.github.com/v3/repos/contents/#get-contents + */ + get("/api/v3/repos/:owner/:repo/contents/*")(referrersOnly { repository => + def getFileInfo(git: Git, revision: String, pathStr: String): Option[FileInfo] = { + val path = new java.io.File(pathStr) + val dirName = path.getParent match { + case null => "." + case s => s + } + getFileList(git, revision, dirName).find(f => f.name.equals(path.getName)) + } + + val path = multiParams("splat").head match { + case s if s.isEmpty => "." + case s => s + } + val refStr = params.getOrElse("ref", repository.repository.defaultBranch) + + using(Git.open(getRepositoryDir(params("owner"), params("repo")))){ git => + val fileList = getFileList(git, refStr, path) + if (fileList.isEmpty) { // file or NotFound + getFileInfo(git, refStr, path).flatMap(f => { + val largeFile = params.get("large_file").exists(s => s.equals("true")) + val content = getContentFromId(git, f.id, largeFile) + request.getHeader("Accept") match { + case "application/vnd.github.v3.raw" => { + contentType = "application/vnd.github.v3.raw" + content + } + case "application/vnd.github.v3.html" if isRenderable(f.name) => { + contentType = "application/vnd.github.v3.html" + content.map(c => + List( + "
", "
", + renderMarkup(path.split("/").toList, new String(c), refStr, repository, false, false, true).body, + "
", "
" + ).mkString + ) + } + case "application/vnd.github.v3.html" => { + contentType = "application/vnd.github.v3.html" + content.map(c => + List( + "
", "
", "
",
+                  play.twirl.api.HtmlFormat.escape(new String(c)).body,
+                  "
", "
", "
" + ).mkString + ) + } + case _ => + Some(JsonFormat(ApiContents(f, content))) + } + }).getOrElse(NotFound()) + } else { // directory + JsonFormat(fileList.map{f => ApiContents(f, None)}) + } + } + }) + + /* + * https://developer.github.com/v3/git/refs/#get-a-reference + */ + get("/api/v3/repos/:owner/:repo/git/*") (referrersOnly { repository => + val revstr = multiParams("splat").head + using(Git.open(getRepositoryDir(params("owner"), params("repo")))) { git => + //JsonFormat( (revstr, git.getRepository().resolve(revstr)) ) + // getRef is deprecated by jgit-4.2. use exactRef() or findRef() + val sha = git.getRepository().exactRef(revstr).getObjectId().name() + JsonFormat(ApiRef(revstr, ApiObject(sha))) + } + }) + + /** + * https://developer.github.com/v3/repos/collaborators/#list-collaborators + */ + get("/api/v3/repos/:owner/:repo/collaborators") (referrersOnly { repository => + // TODO Should ApiUser take permission? getCollaboratorUserNames does not return owner group members. + JsonFormat(getCollaboratorUserNames(params("owner"), params("repo")).map(u => ApiUser(getAccountByUserName(u).get))) + }) + + /** + * https://developer.github.com/v3/users/#get-the-authenticated-user + */ + get("/api/v3/user") { + context.loginAccount.map { account => + JsonFormat(ApiUser(account)) + } getOrElse Unauthorized() + } + + /** + * List user's own repository + * https://developer.github.com/v3/repos/#list-your-repositories + */ + get("/api/v3/user/repos")(usersOnly{ + JsonFormat(getVisibleRepositories(context.loginAccount, Option(context.loginAccount.get.userName)).map{ + r => ApiRepository(r, getAccountByUserName(r.owner).get) + }) + }) + + /** + * Create user repository + * https://developer.github.com/v3/repos/#create + */ + post("/api/v3/user/repos")(usersOnly { + val owner = context.loginAccount.get.userName + (for { + data <- extractFromJsonBody[CreateARepository] if data.isValid + } yield { + LockUtil.lock(s"${owner}/${data.name}") { + if(getRepository(owner, data.name).isEmpty){ + createRepository(context.loginAccount.get, owner, data.name, data.description, data.`private`, data.auto_init) + val repository = getRepository(owner, data.name).get + JsonFormat(ApiRepository(repository, ApiUser(getAccountByUserName(owner).get))) + } else { + ApiError( + "A repository with this name already exists on this account", + Some("https://developer.github.com/v3/repos/#create") + ) + } + } + }) getOrElse NotFound() + }) + + /** + * Create group repository + * https://developer.github.com/v3/repos/#create + */ + post("/api/v3/orgs/:org/repos")(managersOnly { + val groupName = params("org") + (for { + data <- extractFromJsonBody[CreateARepository] if data.isValid + } yield { + LockUtil.lock(s"${groupName}/${data.name}") { + if(getRepository(groupName, data.name).isEmpty){ + createRepository(context.loginAccount.get, groupName, data.name, data.description, data.`private`, data.auto_init) + val repository = getRepository(groupName, data.name).get + JsonFormat(ApiRepository(repository, ApiUser(getAccountByUserName(groupName).get))) + } else { + ApiError( + "A repository with this name already exists for this group", + Some("https://developer.github.com/v3/repos/#create") + ) + } + } + }) getOrElse NotFound() + }) + + /** + * https://developer.github.com/v3/repos/#enabling-and-disabling-branch-protection + */ + patch("/api/v3/repos/:owner/:repo/branches/:branch")(ownerOnly { repository => + import gitbucket.core.api._ + (for{ + branch <- params.get("branch") if repository.branchList.find(_ == branch).isDefined + protection <- extractFromJsonBody[ApiBranchProtection.EnablingAndDisabling].map(_.protection) + } yield { + if(protection.enabled){ + enableBranchProtection(repository.owner, repository.name, branch, protection.status.enforcement_level == ApiBranchProtection.Everyone, protection.status.contexts) + } else { + disableBranchProtection(repository.owner, repository.name, branch) + } + JsonFormat(ApiBranch(branch, protection)(RepositoryName(repository))) + }) getOrElse NotFound() + }) + + /** + * @see https://developer.github.com/v3/rate_limit/#get-your-current-rate-limit-status + * but not enabled. + */ + get("/api/v3/rate_limit"){ + contentType = formats("json") + // this message is same as github enterprise... + org.scalatra.NotFound(ApiError("Rate limiting is not enabled.")) + } + + /** + * https://developer.github.com/v3/issues/#list-issues-for-a-repository + */ + get("/api/v3/repos/:owner/:repository/issues")(referrersOnly { repository => + val page = IssueSearchCondition.page(request) + // TODO: more api spec condition + val condition = IssueSearchCondition(request) + val baseOwner = getAccountByUserName(repository.owner).get + + val issues: List[(Issue, Account)] = + searchIssueByApi( + condition = condition, + offset = (page - 1) * PullRequestLimit, + limit = PullRequestLimit, + repos = repository.owner -> repository.name + ) + + JsonFormat(issues.map { case (issue, issueUser) => + ApiIssue( + issue = issue, + repositoryName = RepositoryName(repository), + user = ApiUser(issueUser) + ) + }) + }) + + /** + * https://developer.github.com/v3/issues/#get-a-single-issue + */ + get("/api/v3/repos/:owner/:repository/issues/:id")(referrersOnly { repository => + (for{ + issueId <- params("id").toIntOpt + issue <- getIssue(repository.owner, repository.name, issueId.toString) + openedUser <- getAccountByUserName(issue.openedUserName) + } yield { + JsonFormat(ApiIssue(issue, RepositoryName(repository), ApiUser(openedUser))) + }) getOrElse NotFound() + }) + + /** + * https://developer.github.com/v3/issues/#create-an-issue + */ + post("/api/v3/repos/:owner/:repository/issues")(readableUsersOnly { repository => + if(isIssueEditable(repository)){ // TODO Should this check is provided by authenticator? + (for{ + data <- extractFromJsonBody[CreateAnIssue] + loginAccount <- context.loginAccount + } yield { + val milestone = data.milestone.flatMap(getMilestone(repository.owner, repository.name, _)) + val issue = createIssue( + repository, + data.title, + data.body, + data.assignees.headOption, + milestone.map(_.milestoneId), + data.labels, + loginAccount) + JsonFormat(ApiIssue(issue, RepositoryName(repository), ApiUser(loginAccount))) + }) getOrElse NotFound() + } else Unauthorized() + }) + + /** + * https://developer.github.com/v3/issues/comments/#list-comments-on-an-issue + */ + get("/api/v3/repos/:owner/:repository/issues/:id/comments")(referrersOnly { repository => + (for{ + issueId <- params("id").toIntOpt + comments = getCommentsForApi(repository.owner, repository.name, issueId.toInt) + } yield { + JsonFormat(comments.map{ case (issueComment, user, issue) => ApiComment(issueComment, RepositoryName(repository), issueId, ApiUser(user), issue.isPullRequest) }) + }) getOrElse NotFound() + }) + + /** + * https://developer.github.com/v3/issues/comments/#create-a-comment + */ + post("/api/v3/repos/:owner/:repository/issues/:id/comments")(readableUsersOnly { repository => + (for{ + issueId <- params("id").toIntOpt + issue <- getIssue(repository.owner, repository.name, issueId.toString) + body <- extractFromJsonBody[CreateAComment].map(_.body) if ! body.isEmpty + action = params.get("action").filter(_ => isEditable(issue.userName, issue.repositoryName, issue.openedUserName)) + (issue, id) <- handleComment(issue, Some(body), repository, action) + issueComment <- getComment(repository.owner, repository.name, id.toString()) + } yield { + JsonFormat(ApiComment(issueComment, RepositoryName(repository), issueId, ApiUser(context.loginAccount.get), issue.isPullRequest)) + }) getOrElse NotFound() + }) + + /** + * List all labels for this repository + * https://developer.github.com/v3/issues/labels/#list-all-labels-for-this-repository + */ + get("/api/v3/repos/:owner/:repository/labels")(referrersOnly { repository => + JsonFormat(getLabels(repository.owner, repository.name).map { label => + ApiLabel(label, RepositoryName(repository)) + }) + }) + + /** + * Get a single label + * https://developer.github.com/v3/issues/labels/#get-a-single-label + */ + get("/api/v3/repos/:owner/:repository/labels/:labelName")(referrersOnly { repository => + getLabel(repository.owner, repository.name, params("labelName")).map { label => + JsonFormat(ApiLabel(label, RepositoryName(repository))) + } getOrElse NotFound() + }) + + /** + * Create a label + * https://developer.github.com/v3/issues/labels/#create-a-label + */ + post("/api/v3/repos/:owner/:repository/labels")(writableUsersOnly { repository => + (for{ + data <- extractFromJsonBody[CreateALabel] if data.isValid + } yield { + LockUtil.lock(RepositoryName(repository).fullName) { + if (getLabel(repository.owner, repository.name, data.name).isEmpty) { + val labelId = createLabel(repository.owner, repository.name, data.name, data.color) + getLabel(repository.owner, repository.name, labelId).map { label => + Created(JsonFormat(ApiLabel(label, RepositoryName(repository)))) + } getOrElse NotFound() + } else { + // TODO ApiError should support errors field to enhance compatibility of GitHub API + UnprocessableEntity(ApiError( + "Validation Failed", + Some("https://developer.github.com/v3/issues/labels/#create-a-label") + )) + } + } + }) getOrElse NotFound() + }) + + /** + * Update a label + * https://developer.github.com/v3/issues/labels/#update-a-label + */ + patch("/api/v3/repos/:owner/:repository/labels/:labelName")(writableUsersOnly { repository => + (for{ + data <- extractFromJsonBody[CreateALabel] if data.isValid + } yield { + LockUtil.lock(RepositoryName(repository).fullName) { + getLabel(repository.owner, repository.name, params("labelName")).map { label => + if (getLabel(repository.owner, repository.name, data.name).isEmpty) { + updateLabel(repository.owner, repository.name, label.labelId, data.name, data.color) + JsonFormat(ApiLabel( + getLabel(repository.owner, repository.name, label.labelId).get, + RepositoryName(repository) + )) + } else { + // TODO ApiError should support errors field to enhance compatibility of GitHub API + UnprocessableEntity(ApiError( + "Validation Failed", + Some("https://developer.github.com/v3/issues/labels/#create-a-label") + )) + } + } getOrElse NotFound() + } + }) getOrElse NotFound() + }) + + /** + * Delete a label + * https://developer.github.com/v3/issues/labels/#delete-a-label + */ + delete("/api/v3/repos/:owner/:repository/labels/:labelName")(writableUsersOnly { repository => + LockUtil.lock(RepositoryName(repository).fullName) { + getLabel(repository.owner, repository.name, params("labelName")).map { label => + deleteLabel(repository.owner, repository.name, label.labelId) + NoContent() + } getOrElse NotFound() + } + }) + + /** + * https://developer.github.com/v3/pulls/#list-pull-requests + */ + get("/api/v3/repos/:owner/:repository/pulls")(referrersOnly { repository => + val page = IssueSearchCondition.page(request) + // TODO: more api spec condition + val condition = IssueSearchCondition(request) + val baseOwner = getAccountByUserName(repository.owner).get + + val issues: List[(Issue, Account, Int, PullRequest, Repository, Account)] = + searchPullRequestByApi( + condition = condition, + offset = (page - 1) * PullRequestLimit, + limit = PullRequestLimit, + repos = repository.owner -> repository.name + ) + + JsonFormat(issues.map { case (issue, issueUser, commentCount, pullRequest, headRepo, headOwner) => + ApiPullRequest( + issue = issue, + pullRequest = pullRequest, + headRepo = ApiRepository(headRepo, ApiUser(headOwner)), + baseRepo = ApiRepository(repository, ApiUser(baseOwner)), + user = ApiUser(issueUser), + mergedComment = getMergedComment(repository.owner, repository.name, issue.issueId) + ) + }) + }) + + /** + * https://developer.github.com/v3/pulls/#get-a-single-pull-request + */ + get("/api/v3/repos/:owner/:repository/pulls/:id")(referrersOnly { repository => + (for{ + issueId <- params("id").toIntOpt + (issue, pullRequest) <- getPullRequest(repository.owner, repository.name, issueId) + users = getAccountsByUserNames(Set(repository.owner, pullRequest.requestUserName, issue.openedUserName), Set.empty) + baseOwner <- users.get(repository.owner) + headOwner <- users.get(pullRequest.requestUserName) + issueUser <- users.get(issue.openedUserName) + headRepo <- getRepository(pullRequest.requestUserName, pullRequest.requestRepositoryName) + } yield { + JsonFormat(ApiPullRequest( + issue = issue, + pullRequest = pullRequest, + headRepo = ApiRepository(headRepo, ApiUser(headOwner)), + baseRepo = ApiRepository(repository, ApiUser(baseOwner)), + user = ApiUser(issueUser), + mergedComment = getMergedComment(repository.owner, repository.name, issue.issueId) + )) + }) getOrElse NotFound() + }) + + /** + * https://developer.github.com/v3/pulls/#list-commits-on-a-pull-request + */ + get("/api/v3/repos/:owner/:repository/pulls/:id/commits")(referrersOnly { repository => + val owner = repository.owner + val name = repository.name + params("id").toIntOpt.flatMap{ issueId => + getPullRequest(owner, name, issueId) map { case(issue, pullreq) => + using(Git.open(getRepositoryDir(owner, name))){ git => + val oldId = git.getRepository.resolve(pullreq.commitIdFrom) + val newId = git.getRepository.resolve(pullreq.commitIdTo) + val repoFullName = RepositoryName(repository) + val commits = git.log.addRange(oldId, newId).call.iterator.asScala.map { c => ApiCommitListItem(new CommitInfo(c), repoFullName) }.toList + JsonFormat(commits) + } + } + } getOrElse NotFound() + }) + + /** + * https://developer.github.com/v3/repos/#get + */ + get("/api/v3/repos/:owner/:repository")(referrersOnly { repository => + JsonFormat(ApiRepository(repository, ApiUser(getAccountByUserName(repository.owner).get))) + }) + + /** + * https://developer.github.com/v3/repos/statuses/#create-a-status + */ + post("/api/v3/repos/:owner/:repo/statuses/:sha")(writableUsersOnly { repository => + (for{ + ref <- params.get("sha") + sha <- JGitUtil.getShaByRef(repository.owner, repository.name, ref) + data <- extractFromJsonBody[CreateAStatus] if data.isValid + creator <- context.loginAccount + state <- CommitState.valueOf(data.state) + statusId = createCommitStatus(repository.owner, repository.name, sha, data.context.getOrElse("default"), + state, data.target_url, data.description, new java.util.Date(), creator) + status <- getCommitStatus(repository.owner, repository.name, statusId) + } yield { + JsonFormat(ApiCommitStatus(status, ApiUser(creator))) + }) getOrElse NotFound() + }) + + /** + * https://developer.github.com/v3/repos/statuses/#list-statuses-for-a-specific-ref + * + * ref is Ref to list the statuses from. It can be a SHA, a branch name, or a tag name. + */ + val listStatusesRoute = get("/api/v3/repos/:owner/:repo/commits/:ref/statuses")(referrersOnly { repository => + (for{ + ref <- params.get("ref") + sha <- JGitUtil.getShaByRef(repository.owner, repository.name, ref) + } yield { + JsonFormat(getCommitStatuesWithCreator(repository.owner, repository.name, sha).map{ case(status, creator) => + ApiCommitStatus(status, ApiUser(creator)) + }) + }) getOrElse NotFound() + }) + + /** + * https://developer.github.com/v3/repos/statuses/#list-statuses-for-a-specific-ref + * + * legacy route + */ + get("/api/v3/repos/:owner/:repo/statuses/:ref"){ + listStatusesRoute.action() + } + + /** + * https://developer.github.com/v3/repos/statuses/#get-the-combined-status-for-a-specific-ref + * + * ref is Ref to list the statuses from. It can be a SHA, a branch name, or a tag name. + */ + get("/api/v3/repos/:owner/:repo/commits/:ref/status")(referrersOnly { repository => + (for{ + ref <- params.get("ref") + owner <- getAccountByUserName(repository.owner) + sha <- JGitUtil.getShaByRef(repository.owner, repository.name, ref) + } yield { + val statuses = getCommitStatuesWithCreator(repository.owner, repository.name, sha) + JsonFormat(ApiCombinedCommitStatus(sha, statuses, ApiRepository(repository, owner))) + }) getOrElse NotFound() + }) + + private def isEditable(owner: String, repository: String, author: String)(implicit context: Context): Boolean = + hasDeveloperRole(owner, repository, context.loginAccount) || author == context.loginAccount.get.userName + +} + diff --git a/src/main/scala/gitbucket/core/controller/ControllerBase.scala b/src/main/scala/gitbucket/core/controller/ControllerBase.scala index 09cdc85..5db473f 100644 --- a/src/main/scala/gitbucket/core/controller/ControllerBase.scala +++ b/src/main/scala/gitbucket/core/controller/ControllerBase.scala @@ -8,7 +8,7 @@ import gitbucket.core.util.Implicits._ import gitbucket.core.util._ -import jp.sf.amateras.scalatra.forms._ +import io.github.gitbucket.scalatra.forms._ import org.apache.commons.io.FileUtils import org.json4s._ import org.scalatra._ @@ -28,7 +28,11 @@ with ClientSideValidationFormSupport with JacksonJsonSupport with I18nSupport with FlashMapSupport with Validations with SystemSettingsService { - implicit val jsonFormats = DefaultFormats + implicit val jsonFormats = gitbucket.core.api.JsonFormat.jsonFormats + + before("/api/v3/*") { + contentType = formats("json") + } // TODO Scala 2.11 // // Don't set content type via Accept header. @@ -53,7 +57,7 @@ // Redirect to dashboard httpResponse.sendRedirect(baseUrl + "/") } - } else if(path.startsWith("/git/")){ + } else if(path.startsWith("/git/") || path.startsWith("/git-lfs/")){ // Git repository chain.doFilter(request, response) } else { @@ -176,11 +180,18 @@ * Context object for the current request. */ case class Context(settings: SystemSettingsService.SystemSettings, loginAccount: Option[Account], request: HttpServletRequest){ - val path = settings.baseUrl.getOrElse(request.getContextPath) val currentPath = request.getRequestURI.substring(request.getContextPath.length) val baseUrl = settings.baseUrl(request) val host = new java.net.URL(baseUrl).getHost + val platform = request.getHeader("User-Agent") match { + case null => null + case agent if agent.contains("Mac") => "mac" + case agent if agent.contains("Linux") => "linux" + case agent if agent.contains("Win") => "windows" + case _ => null + } + val sidebarCollapse = request.getSession.getAttribute("sidebar-collapse") != null /** * Get object from cache. @@ -234,4 +245,13 @@ .map { _ => "Mail address is already registered." } } + val allReservedNames = Set("git", "admin", "upload", "api") + protected def reservedNames(): Constraint = new Constraint(){ + override def validate(name: String, value: String, messages: Messages): Option[String] = if(allReservedNames.contains(value)){ + Some(s"${value} is reserved") + } else { + None + } + } + } diff --git a/src/main/scala/gitbucket/core/controller/DashboardController.scala b/src/main/scala/gitbucket/core/controller/DashboardController.scala index 713e9c0..81e3085 100644 --- a/src/main/scala/gitbucket/core/controller/DashboardController.scala +++ b/src/main/scala/gitbucket/core/controller/DashboardController.scala @@ -1,13 +1,13 @@ package gitbucket.core.controller import gitbucket.core.dashboard.html -import gitbucket.core.service.{RepositoryService, PullRequestService, AccountService, IssuesService} -import gitbucket.core.util.{StringUtil, Keys, UsersAuthenticator} +import gitbucket.core.service._ +import gitbucket.core.util.{Keys, UsersAuthenticator} import gitbucket.core.util.Implicits._ import gitbucket.core.service.IssuesService._ class DashboardController extends DashboardControllerBase - with IssuesService with PullRequestService with RepositoryService with AccountService + with IssuesService with PullRequestService with RepositoryService with AccountService with CommitsService with UsersAuthenticator trait DashboardControllerBase extends ControllerBase { @@ -15,20 +15,7 @@ with UsersAuthenticator => get("/dashboard/issues")(usersOnly { - val q = request.getParameter("q") - val account = context.loginAccount.get - Option(q).map { q => - val condition = IssueSearchCondition(q, Map[String, Int]()) - q match { - case q if(q.contains("is:pr")) => redirect(s"/dashboard/pulls?q=${StringUtil.urlEncode(q)}") - case q if(q.contains(s"author:${account.userName}")) => redirect(s"/dashboard/issues/created_by${condition.toURL}") - case q if(q.contains(s"assignee:${account.userName}")) => redirect(s"/dashboard/issues/assigned${condition.toURL}") - case q if(q.contains(s"mentions:${account.userName}")) => redirect(s"/dashboard/issues/mentioned${condition.toURL}") - case _ => searchIssues("created_by") - } - } getOrElse { - searchIssues("created_by") - } + searchIssues("created_by") }) get("/dashboard/issues/assigned")(usersOnly { @@ -44,20 +31,7 @@ }) get("/dashboard/pulls")(usersOnly { - val q = request.getParameter("q") - val account = context.loginAccount.get - Option(q).map { q => - val condition = IssueSearchCondition(q, Map[String, Int]()) - q match { - case q if(q.contains("is:issue")) => redirect(s"/dashboard/issues?q=${StringUtil.urlEncode(q)}") - case q if(q.contains(s"author:${account.userName}")) => redirect(s"/dashboard/pulls/created_by${condition.toURL}") - case q if(q.contains(s"assignee:${account.userName}")) => redirect(s"/dashboard/pulls/assigned${condition.toURL}") - case q if(q.contains(s"mentions:${account.userName}")) => redirect(s"/dashboard/pulls/mentioned${condition.toURL}") - case _ => searchPullRequests("created_by") - } - } getOrElse { - searchPullRequests("created_by") - } + searchPullRequests("created_by") }) get("/dashboard/pulls/created_by")(usersOnly { @@ -73,19 +47,12 @@ }) private def getOrCreateCondition(key: String, filter: String, userName: String) = { - val condition = session.putAndGet(key, if(request.hasQueryString){ - val q = request.getParameter("q") - if(q == null){ - IssueSearchCondition(request) - } else { - IssueSearchCondition(q, Map[String, Int]()) - } - } else session.getAs[IssueSearchCondition](key).getOrElse(IssueSearchCondition())) + val condition = IssueSearchCondition(request) filter match { - case "assigned" => condition.copy(assigned = Some(userName), author = None , mentioned = None) - case "mentioned" => condition.copy(assigned = None , author = None , mentioned = Some(userName)) - case _ => condition.copy(assigned = None , author = Some(userName), mentioned = None) + case "assigned" => condition.copy(assigned = Some(Some(userName)), author = None, mentioned = None) + case "mentioned" => condition.copy(assigned = None, author = None, mentioned = Some(userName)) + case _ => condition.copy(assigned = None, author = Some(userName), mentioned = None) } } @@ -94,7 +61,7 @@ val userName = context.loginAccount.get.userName val condition = getOrCreateCondition(Keys.Session.DashboardIssues, filter, userName) - val userRepos = getUserRepositories(userName, context.baseUrl, true).map(repo => repo.owner -> repo.name) + val userRepos = getUserRepositories(userName, true).map(repo => repo.owner -> repo.name) val page = IssueSearchCondition.page(request) html.issues( @@ -103,12 +70,14 @@ countIssue(condition.copy(state = "open" ), false, userRepos: _*), countIssue(condition.copy(state = "closed"), false, userRepos: _*), filter match { - case "assigned" => condition.copy(assigned = Some(userName)) + case "assigned" => condition.copy(assigned = Some(Some(userName))) case "mentioned" => condition.copy(mentioned = Some(userName)) case _ => condition.copy(author = Some(userName)) }, filter, - getGroupNames(userName)) + getGroupNames(userName), + Nil, + getUserRepositories(userName, withoutPhysicalInfo = true)) } private def searchPullRequests(filter: String) = { @@ -126,12 +95,14 @@ countIssue(condition.copy(state = "open" ), true, allRepos: _*), countIssue(condition.copy(state = "closed"), true, allRepos: _*), filter match { - case "assigned" => condition.copy(assigned = Some(userName)) + case "assigned" => condition.copy(assigned = Some(Some(userName))) case "mentioned" => condition.copy(mentioned = Some(userName)) case _ => condition.copy(author = Some(userName)) }, filter, - getGroupNames(userName)) + getGroupNames(userName), + Nil, + getUserRepositories(userName, withoutPhysicalInfo = true)) } diff --git a/src/main/scala/gitbucket/core/controller/FileUploadController.scala b/src/main/scala/gitbucket/core/controller/FileUploadController.scala index c3503ac..3296903 100644 --- a/src/main/scala/gitbucket/core/controller/FileUploadController.scala +++ b/src/main/scala/gitbucket/core/controller/FileUploadController.scala @@ -1,44 +1,108 @@ package gitbucket.core.controller -import gitbucket.core.util.{Keys, FileUtil} +import gitbucket.core.model.Account +import gitbucket.core.service.{AccountService, RepositoryService} +import gitbucket.core.servlet.Database +import gitbucket.core.util._ import gitbucket.core.util.ControlUtil._ import gitbucket.core.util.Directory._ +import gitbucket.core.util.Implicits._ +import org.eclipse.jgit.api.Git +import org.eclipse.jgit.dircache.DirCache +import org.eclipse.jgit.lib.{FileMode, Constants} import org.scalatra._ import org.scalatra.servlet.{MultipartConfig, FileUploadSupport, FileItem} -import org.apache.commons.io.FileUtils +import org.apache.commons.io.{IOUtils, FileUtils} /** * Provides Ajax based file upload functionality. * * This servlet saves uploaded file. */ -class FileUploadController extends ScalatraServlet with FileUploadSupport { +class FileUploadController extends ScalatraServlet with FileUploadSupport with RepositoryService with AccountService { configureMultipartHandling(MultipartConfig(maxFileSize = Some(3 * 1024 * 1024))) post("/image"){ - execute { (file, fileId) => + execute({ (file, fileId) => FileUtils.writeByteArrayToFile(new java.io.File(getTemporaryDir(session.getId), fileId), file.get) session += Keys.Session.Upload(fileId) -> file.name - } + }, FileUtil.isImage) } - post("/image/:owner/:repository"){ - execute { (file, fileId) => + post("/file/:owner/:repository"){ + execute({ (file, fileId) => FileUtils.writeByteArrayToFile(new java.io.File( getAttachedDir(params("owner"), params("repository")), fileId + "." + FileUtil.getExtension(file.getName)), file.get) + }, FileUtil.isUploadableType) + } + + post("/wiki/:owner/:repository"){ + // Don't accept not logged-in users + session.get(Keys.Session.LoginAccount).collect { case loginAccount: Account => + val owner = params("owner") + val repository = params("repository") + + // Check whether logged-in user is collaborator + collaboratorsOnly(owner, repository, loginAccount){ + execute({ (file, fileId) => + val fileName = file.getName + LockUtil.lock(s"${owner}/${repository}/wiki") { + using(Git.open(Directory.getWikiRepositoryDir(owner, repository))) { git => + val builder = DirCache.newInCore.builder() + val inserter = git.getRepository.newObjectInserter() + val headId = git.getRepository.resolve(Constants.HEAD + "^{commit}") + + if(headId != null){ + JGitUtil.processTree(git, headId){ (path, tree) => + if(path != fileName){ + builder.add(JGitUtil.createDirCacheEntry(path, tree.getEntryFileMode, tree.getEntryObjectId)) + } + } + } + + val bytes = IOUtils.toByteArray(file.getInputStream) + builder.add(JGitUtil.createDirCacheEntry(fileName, FileMode.REGULAR_FILE, inserter.insert(Constants.OBJ_BLOB, bytes))) + builder.finish() + + val newHeadId = JGitUtil.createNewCommit(git, inserter, headId, builder.getDirCache.writeTree(inserter), + Constants.HEAD, loginAccount.userName, loginAccount.mailAddress, s"Uploaded ${fileName}") + + fileName + } + } + }, FileUtil.isUploadableType) + } + } getOrElse BadRequest() + } + + post("/import") { + import JDBCUtil._ + session.get(Keys.Session.LoginAccount).collect { case loginAccount: Account if loginAccount.isAdmin => + execute({ (file, fileId) => + request2Session(request).conn.importAsSQL(file.getInputStream) + }, _ => true) + } + redirect("/admin/data") + } + + private def collaboratorsOnly(owner: String, repository: String, loginAccount: Account)(action: => Any): Any = { + implicit val session = Database.getSession(request) + loginAccount match { + case x if(x.isAdmin) => action + case x if(getCollaborators(owner, repository).contains(x.userName)) => action + case _ => BadRequest() } } - private def execute(f: (FileItem, String) => Unit) = fileParams.get("file") match { - case Some(file) if(FileUtil.isImage(file.name)) => + private def execute(f: (FileItem, String) => Unit, mimeTypeChcker: (String) => Boolean) = fileParams.get("file") match { + case Some(file) if(mimeTypeChcker(file.name)) => defining(FileUtil.generateFileId){ fileId => f(file, fileId) - Ok(fileId) } - case _ => BadRequest + case _ => BadRequest() } } diff --git a/src/main/scala/gitbucket/core/controller/IndexController.scala b/src/main/scala/gitbucket/core/controller/IndexController.scala index deb3982..bbe4da6 100644 --- a/src/main/scala/gitbucket/core/controller/IndexController.scala +++ b/src/main/scala/gitbucket/core/controller/IndexController.scala @@ -1,48 +1,46 @@ package gitbucket.core.controller -import gitbucket.core.api._ import gitbucket.core.helper.xml -import gitbucket.core.html import gitbucket.core.model.Account -import gitbucket.core.service.{RepositoryService, ActivityService, AccountService} +import gitbucket.core.service._ import gitbucket.core.util.Implicits._ -import gitbucket.core.util.{LDAPUtil, Keys, UsersAuthenticator} - -import jp.sf.amateras.scalatra.forms._ +import gitbucket.core.util.ControlUtil._ +import gitbucket.core.util.{Keys, LDAPUtil, ReferrerAuthenticator, UsersAuthenticator} +import io.github.gitbucket.scalatra.forms._ +import org.scalatra.Ok class IndexController extends IndexControllerBase - with RepositoryService with ActivityService with AccountService with UsersAuthenticator + with RepositoryService with ActivityService with AccountService with RepositorySearchService with IssuesService + with UsersAuthenticator with ReferrerAuthenticator trait IndexControllerBase extends ControllerBase { - self: RepositoryService with ActivityService with AccountService with UsersAuthenticator => + self: RepositoryService with ActivityService with AccountService with RepositorySearchService + with UsersAuthenticator with ReferrerAuthenticator => case class SignInForm(userName: String, password: String) - val form = mapping( + val signinForm = mapping( "userName" -> trim(label("Username", text(required))), "password" -> trim(label("Password", text(required))) )(SignInForm.apply) - get("/"){ - val loginAccount = context.loginAccount - if(loginAccount.isEmpty) { - html.index(getRecentActivities(), - getVisibleRepositories(loginAccount, context.baseUrl, withoutPhysicalInfo = true), - loginAccount.map{ account => getUserRepositories(account.userName, context.baseUrl, withoutPhysicalInfo = true) }.getOrElse(Nil) - ) - } else { - val loginUserName = loginAccount.get.userName - val loginUserGroups = getGroupsByUserName(loginUserName) - var visibleOwnerSet : Set[String] = Set(loginUserName) - - visibleOwnerSet ++= loginUserGroups + val searchForm = mapping( + "query" -> trim(text(required)), + "owner" -> trim(text(required)), + "repository" -> trim(text(required)) + )(SearchForm.apply) - html.index(getRecentActivitiesByOwners(visibleOwnerSet), - getVisibleRepositories(loginAccount, context.baseUrl, withoutPhysicalInfo = true), - loginAccount.map{ account => getUserRepositories(account.userName, context.baseUrl, withoutPhysicalInfo = true) }.getOrElse(Nil) - ) + case class SearchForm(query: String, owner: String, repository: String) + + + get("/"){ + context.loginAccount.map { account => + val visibleOwnerSet: Set[String] = Set(account.userName) ++ getGroupsByUserName(account.userName) + gitbucket.core.html.index(getRecentActivitiesByOwners(visibleOwnerSet), Nil, getUserRepositories(account.userName, withoutPhysicalInfo = true)) + }.getOrElse { + gitbucket.core.html.index(getRecentActivities(), getVisibleRepositories(None, withoutPhysicalInfo = true), Nil) } } @@ -51,10 +49,10 @@ if(redirect.isDefined && redirect.get.startsWith("/")){ flash += Keys.Flash.Redirect -> redirect.get } - html.signin() + gitbucket.core.html.signin() } - post("/signin", form){ form => + post("/signin", signinForm){ form => authenticate(context.settings, form.userName, form.password) match { case Some(account) => signin(account) case None => redirect("/signin") @@ -71,6 +69,15 @@ xml.feed(getRecentActivities()) } + get("/sidebar-collapse"){ + if(params("collapse") == "true"){ + session.setAttribute("sidebar-collapse", "true") + } else { + session.setAttribute("sidebar-collapse", null) + } + Ok() + } + /** * Set account information into HttpSession and redirect. */ @@ -98,25 +105,68 @@ */ get("/_user/proposals")(usersOnly { contentType = formats("json") + val user = params("user").toBoolean + val group = params("group").toBoolean org.json4s.jackson.Serialization.write( - Map("options" -> getAllUsers(false).filter(!_.isGroupAccount).map(_.userName).toArray) + Map("options" -> ( + getAllUsers(false) + .withFilter { t => (user, group) match { + case (true, true) => true + case (true, false) => !t.isGroupAccount + case (false, true) => t.isGroupAccount + case (false, false) => false + }}.map { t => t.userName } + )) ) }) /** - * JSON APU for checking user existence. + * JSON API for checking user or group existence. + * Returns a single string which is any of "group", "user" or "". */ post("/_user/existence")(usersOnly { - getAccountByUserName(params("userName")).isDefined + getAccountByUserName(params("userName")).map { account => + if(account.isGroupAccount) "group" else "user" + } getOrElse "" }) - /** - * @see https://developer.github.com/v3/rate_limit/#get-your-current-rate-limit-status - * but not enabled. - */ - get("/api/v3/rate_limit"){ - contentType = formats("json") - // this message is same as github enterprise... - org.scalatra.NotFound(ApiError("Rate limiting is not enabled.")) + // TODO Move to RepositoryViwerController? + get("/:owner/:repository/search")(referrersOnly { repository => + defining(params.getOrElse("q", "").trim, params.getOrElse("type", "code")){ case (query, target) => + val page = try { + val i = params.getOrElse("page", "1").toInt + if(i <= 0) 1 else i + } catch { + case e: NumberFormatException => 1 + } + + target.toLowerCase match { + case "issue" => gitbucket.core.search.html.issues( + if(query.nonEmpty) searchIssues(repository.owner, repository.name, query) else Nil, + query, page, repository) + + case "wiki" => gitbucket.core.search.html.wiki( + if(query.nonEmpty) searchWikiPages(repository.owner, repository.name, query) else Nil, + query, page, repository) + + case _ => gitbucket.core.search.html.code( + if(query.nonEmpty) searchFiles(repository.owner, repository.name, query) else Nil, + query, page, repository) + } + } + }) + + get("/search"){ + val query = params.getOrElse("query", "").trim.toLowerCase + val visibleRepositories = getVisibleRepositories(context.loginAccount, None) + val repositories = visibleRepositories.filter { repository => + repository.name.toLowerCase.indexOf(query) >= 0 || repository.owner.toLowerCase.indexOf(query) >= 0 + } + context.loginAccount.map { account => + gitbucket.core.search.html.repositories(query, repositories, Nil, getUserRepositories(account.userName, withoutPhysicalInfo = true)) + }.getOrElse { + gitbucket.core.search.html.repositories(query, repositories, visibleRepositories, Nil) + } } + } diff --git a/src/main/scala/gitbucket/core/controller/IssuesController.scala b/src/main/scala/gitbucket/core/controller/IssuesController.scala index cd3edc3..5969cb4 100644 --- a/src/main/scala/gitbucket/core/controller/IssuesController.scala +++ b/src/main/scala/gitbucket/core/controller/IssuesController.scala @@ -1,8 +1,6 @@ package gitbucket.core.controller -import gitbucket.core.api._ import gitbucket.core.issues.html -import gitbucket.core.model.Issue import gitbucket.core.service.IssuesService._ import gitbucket.core.service._ import gitbucket.core.util.ControlUtil._ @@ -10,18 +8,40 @@ import gitbucket.core.util._ import gitbucket.core.view import gitbucket.core.view.Markdown - -import jp.sf.amateras.scalatra.forms._ -import org.scalatra.Ok +import io.github.gitbucket.scalatra.forms._ +import org.scalatra.{BadRequest, Ok} class IssuesController extends IssuesControllerBase - with IssuesService with RepositoryService with AccountService with LabelsService with MilestonesService with ActivityService - with ReadableUsersAuthenticator with ReferrerAuthenticator with CollaboratorsAuthenticator with PullRequestService with WebHookIssueCommentService + with IssuesService + with RepositoryService + with AccountService + with LabelsService + with MilestonesService + with ActivityService + with HandleCommentService + with IssueCreationService + with ReadableUsersAuthenticator + with ReferrerAuthenticator + with WritableUsersAuthenticator + with PullRequestService + with WebHookIssueCommentService + with CommitsService trait IssuesControllerBase extends ControllerBase { - self: IssuesService with RepositoryService with AccountService with LabelsService with MilestonesService with ActivityService - with ReadableUsersAuthenticator with ReferrerAuthenticator with CollaboratorsAuthenticator with PullRequestService with WebHookIssueCommentService => + self: IssuesService + with RepositoryService + with AccountService + with LabelsService + with MilestonesService + with ActivityService + with HandleCommentService + with IssueCreationService + with ReadableUsersAuthenticator + with ReferrerAuthenticator + with WritableUsersAuthenticator + with PullRequestService + with WebHookIssueCommentService => case class IssueCreateForm(title: String, content: Option[String], assignedUserName: Option[String], milestoneId: Option[Int], labelNames: Option[String]) @@ -69,244 +89,232 @@ _, getComments(owner, name, issueId.toInt), getIssueLabels(owner, name, issueId.toInt), - (getCollaborators(owner, name) ::: (if(getAccountByUserName(owner).get.isGroupAccount) Nil else List(owner))).sorted, + getAssignableUserNames(owner, name), getMilestonesWithIssueCount(owner, name), getLabels(owner, name), - hasWritePermission(owner, name, context.loginAccount), + isIssueEditable(repository), + isIssueManageable(repository), repository) - } getOrElse NotFound + } getOrElse NotFound() } }) - /** - * https://developer.github.com/v3/issues/comments/#list-comments-on-an-issue - */ - get("/api/v3/repos/:owner/:repository/issues/:id/comments")(referrersOnly { repository => - (for{ - issueId <- params("id").toIntOpt - comments = getCommentsForApi(repository.owner, repository.name, issueId.toInt) - } yield { - JsonFormat(comments.map{ case (issueComment, user) => ApiComment(issueComment, RepositoryName(repository), issueId, ApiUser(user)) }) - }).getOrElse(NotFound) - }) - get("/:owner/:repository/issues/new")(readableUsersOnly { repository => - defining(repository.owner, repository.name){ case (owner, name) => - html.create( - (getCollaborators(owner, name) ::: (if(getAccountByUserName(owner).get.isGroupAccount) Nil else List(owner))).sorted, + if(isIssueEditable(repository)){ // TODO Should this check is provided by authenticator? + defining(repository.owner, repository.name){ case (owner, name) => + html.create( + getAssignableUserNames(owner, name), getMilestones(owner, name), getLabels(owner, name), - hasWritePermission(owner, name, context.loginAccount), + isIssueManageable(repository), repository) - } + } + } else Unauthorized() }) post("/:owner/:repository/issues/new", issueCreateForm)(readableUsersOnly { (form, repository) => - defining(repository.owner, repository.name){ case (owner, name) => - val writable = hasWritePermission(owner, name, context.loginAccount) - val userName = context.loginAccount.get.userName + if(isIssueEditable(repository)){ // TODO Should this check is provided by authenticator? + val issue = createIssue( + repository, + form.title, + form.content, + form.assignedUserName, + form.milestoneId, + form.labelNames.toArray.flatMap(_.split(",")), + context.loginAccount.get) - // insert issue - val issueId = createIssue(owner, name, userName, form.title, form.content, - if(writable) form.assignedUserName else None, - if(writable) form.milestoneId else None) - - // insert labels - if(writable){ - form.labelNames.map { value => - val labels = getLabels(owner, name) - value.split(",").foreach { labelName => - labels.find(_.labelName == labelName).map { label => - registerIssueLabel(owner, name, issueId, label.labelId) - } - } - } - } - - // record activity - recordCreateIssueActivity(owner, name, userName, issueId, form.title) - - getIssue(owner, name, issueId.toString).foreach { issue => - // extract references and create refer comment - createReferComment(owner, name, issue, form.title + " " + form.content.getOrElse("")) - - // call web hooks - callIssuesWebHook("opened", repository, issue, context.baseUrl, context.loginAccount.get) - - // notifications - Notifier().toNotify(repository, issue, form.content.getOrElse("")){ - Notifier.msgIssue(s"${context.baseUrl}/${owner}/${name}/issues/${issueId}") - } - } - - redirect(s"/${owner}/${name}/issues/${issueId}") - } + redirect(s"/${issue.userName}/${issue.repositoryName}/issues/${issue.issueId}") + } else Unauthorized() }) ajaxPost("/:owner/:repository/issues/edit_title/:id", issueTitleEditForm)(readableUsersOnly { (title, repository) => defining(repository.owner, repository.name){ case (owner, name) => getIssue(owner, name, params("id")).map { issue => - if(isEditable(owner, name, issue.openedUserName)){ + if(isEditableContent(owner, name, issue.openedUserName)){ // update issue updateIssue(owner, name, issue.issueId, title, issue.content) // extract references and create refer comment - createReferComment(owner, name, issue.copy(title = title), title) + createReferComment(owner, name, issue.copy(title = title), title, context.loginAccount.get) redirect(s"/${owner}/${name}/issues/_data/${issue.issueId}") - } else Unauthorized - } getOrElse NotFound + } else Unauthorized() + } getOrElse NotFound() } }) ajaxPost("/:owner/:repository/issues/edit/:id", issueEditForm)(readableUsersOnly { (content, repository) => defining(repository.owner, repository.name){ case (owner, name) => getIssue(owner, name, params("id")).map { issue => - if(isEditable(owner, name, issue.openedUserName)){ + if(isEditableContent(owner, name, issue.openedUserName)){ // update issue updateIssue(owner, name, issue.issueId, issue.title, content) // extract references and create refer comment - createReferComment(owner, name, issue, content.getOrElse("")) + createReferComment(owner, name, issue, content.getOrElse(""), context.loginAccount.get) redirect(s"/${owner}/${name}/issues/_data/${issue.issueId}") - } else Unauthorized - } getOrElse NotFound + } else Unauthorized() + } getOrElse NotFound() } }) post("/:owner/:repository/issue_comments/new", commentForm)(readableUsersOnly { (form, repository) => - handleComment(form.issueId, Some(form.content), repository)() map { case (issue, id) => - redirect(s"/${repository.owner}/${repository.name}/${ - if(issue.isPullRequest) "pull" else "issues"}/${form.issueId}#comment-${id}") - } getOrElse NotFound - }) - - /** - * https://developer.github.com/v3/issues/comments/#create-a-comment - */ - post("/api/v3/repos/:owner/:repository/issues/:id/comments")(readableUsersOnly { repository => - (for{ - issueId <- params("id").toIntOpt - body <- extractFromJsonBody[CreateAComment].map(_.body) if ! body.isEmpty - (issue, id) <- handleComment(issueId, Some(body), repository)() - issueComment <- getComment(repository.owner, repository.name, id.toString()) - } yield { - JsonFormat(ApiComment(issueComment, RepositoryName(repository), issueId, ApiUser(context.loginAccount.get))) - }) getOrElse NotFound + getIssue(repository.owner, repository.name, form.issueId.toString).flatMap { issue => + val actionOpt = params.get("action").filter(_ => isEditableContent(issue.userName, issue.repositoryName, issue.openedUserName)) + handleComment(issue, Some(form.content), repository, actionOpt) map { case (issue, id) => + redirect(s"/${repository.owner}/${repository.name}/${ + if(issue.isPullRequest) "pull" else "issues"}/${form.issueId}#comment-${id}") + } + } getOrElse NotFound() }) post("/:owner/:repository/issue_comments/state", issueStateForm)(readableUsersOnly { (form, repository) => - handleComment(form.issueId, form.content, repository)() map { case (issue, id) => - redirect(s"/${repository.owner}/${repository.name}/${ - if(issue.isPullRequest) "pull" else "issues"}/${form.issueId}#comment-${id}") - } getOrElse NotFound + getIssue(repository.owner, repository.name, form.issueId.toString).flatMap { issue => + val actionOpt = params.get("action").filter(_ => isEditableContent(issue.userName, issue.repositoryName, issue.openedUserName)) + handleComment(issue, form.content, repository, actionOpt) map { case (issue, id) => + redirect(s"/${repository.owner}/${repository.name}/${ + if(issue.isPullRequest) "pull" else "issues"}/${form.issueId}#comment-${id}") + } + } getOrElse NotFound() }) ajaxPost("/:owner/:repository/issue_comments/edit/:id", commentForm)(readableUsersOnly { (form, repository) => defining(repository.owner, repository.name){ case (owner, name) => getComment(owner, name, params("id")).map { comment => - if(isEditable(owner, name, comment.commentedUserName)){ + if(isEditableContent(owner, name, comment.commentedUserName)){ updateComment(comment.commentId, form.content) redirect(s"/${owner}/${name}/issue_comments/_data/${comment.commentId}") - } else Unauthorized - } getOrElse NotFound + } else Unauthorized() + } getOrElse NotFound() } }) ajaxPost("/:owner/:repository/issue_comments/delete/:id")(readableUsersOnly { repository => defining(repository.owner, repository.name){ case (owner, name) => getComment(owner, name, params("id")).map { comment => - if(isEditable(owner, name, comment.commentedUserName)){ + if(isEditableContent(owner, name, comment.commentedUserName)){ Ok(deleteComment(comment.commentId)) - } else Unauthorized - } getOrElse NotFound + } else Unauthorized() + } getOrElse NotFound() } }) ajaxGet("/:owner/:repository/issues/_data/:id")(readableUsersOnly { repository => getIssue(repository.owner, repository.name, params("id")) map { x => - if(isEditable(x.userName, x.repositoryName, x.openedUserName)){ + if(isEditableContent(x.userName, x.repositoryName, x.openedUserName)){ params.get("dataType") collect { - case t if t == "html" => html.editissue( - x.content, x.issueId, x.userName, x.repositoryName) + case t if t == "html" => html.editissue(x.content, x.issueId, repository) } getOrElse { contentType = formats("json") org.json4s.jackson.Serialization.write( - Map("title" -> x.title, - "content" -> Markdown.toHtml(x.content getOrElse "No description given.", - repository, false, true, true, isEditable(x.userName, x.repositoryName, x.openedUserName)) - )) + Map( + "title" -> x.title, + "content" -> Markdown.toHtml( + markdown = x.content getOrElse "No description given.", + repository = repository, + enableWikiLink = false, + enableRefsLink = true, + enableAnchor = true, + enableLineBreaks = true, + enableTaskList = true, + hasWritePermission = true + ) + ) + ) } - } else Unauthorized - } getOrElse NotFound + } else Unauthorized() + } getOrElse NotFound() }) ajaxGet("/:owner/:repository/issue_comments/_data/:id")(readableUsersOnly { repository => getComment(repository.owner, repository.name, params("id")) map { x => - if(isEditable(x.userName, x.repositoryName, x.commentedUserName)){ + if(isEditableContent(x.userName, x.repositoryName, x.commentedUserName)){ params.get("dataType") collect { - case t if t == "html" => html.editcomment( - x.content, x.commentId, x.userName, x.repositoryName) + case t if t == "html" => html.editcomment(x.content, x.commentId, repository) } getOrElse { contentType = formats("json") org.json4s.jackson.Serialization.write( - Map("content" -> view.Markdown.toHtml(x.content, - repository, false, true, true, isEditable(x.userName, x.repositoryName, x.commentedUserName)) - )) + Map( + "content" -> view.Markdown.toHtml( + markdown = x.content, + repository = repository, + enableWikiLink = false, + enableRefsLink = true, + enableAnchor = true, + enableLineBreaks = true, + enableTaskList = true, + hasWritePermission = true + ) + ) + ) } - } else Unauthorized - } getOrElse NotFound + } else Unauthorized() + } getOrElse NotFound() }) - ajaxPost("/:owner/:repository/issues/:id/label/new")(collaboratorsOnly { repository => + ajaxPost("/:owner/:repository/issues/new/label")(writableUsersOnly { repository => + val labelNames = params("labelNames").split(",") + val labels = getLabels(repository.owner, repository.name).filter(x => labelNames.contains(x.labelName)) + html.labellist(labels) + }) + + ajaxPost("/:owner/:repository/issues/:id/label/new")(writableUsersOnly { repository => defining(params("id").toInt){ issueId => registerIssueLabel(repository.owner, repository.name, issueId, params("labelId").toInt) html.labellist(getIssueLabels(repository.owner, repository.name, issueId)) } }) - ajaxPost("/:owner/:repository/issues/:id/label/delete")(collaboratorsOnly { repository => + ajaxPost("/:owner/:repository/issues/:id/label/delete")(writableUsersOnly { repository => defining(params("id").toInt){ issueId => deleteIssueLabel(repository.owner, repository.name, issueId, params("labelId").toInt) html.labellist(getIssueLabels(repository.owner, repository.name, issueId)) } }) - ajaxPost("/:owner/:repository/issues/:id/assign")(collaboratorsOnly { repository => + ajaxPost("/:owner/:repository/issues/:id/assign")(writableUsersOnly { repository => updateAssignedUserName(repository.owner, repository.name, params("id").toInt, assignedUserName("assignedUserName")) Ok("updated") }) - ajaxPost("/:owner/:repository/issues/:id/milestone")(collaboratorsOnly { repository => + ajaxPost("/:owner/:repository/issues/:id/milestone")(writableUsersOnly { repository => updateMilestoneId(repository.owner, repository.name, params("id").toInt, milestoneId("milestoneId")) milestoneId("milestoneId").map { milestoneId => getMilestonesWithIssueCount(repository.owner, repository.name) .find(_._1.milestoneId == milestoneId).map { case (_, openCount, closeCount) => gitbucket.core.issues.milestones.html.progress(openCount + closeCount, closeCount) - } getOrElse NotFound + } getOrElse NotFound() } getOrElse Ok() }) - post("/:owner/:repository/issues/batchedit/state")(collaboratorsOnly { repository => + post("/:owner/:repository/issues/batchedit/state")(writableUsersOnly { repository => defining(params.get("value")){ action => action match { - case Some("open") => executeBatch(repository) { handleComment(_, None, repository)( _ => Some("reopen")) } - case Some("close") => executeBatch(repository) { handleComment(_, None, repository)( _ => Some("close")) } - case _ => // TODO BadRequest + case Some("open") => executeBatch(repository) { issueId => + getIssue(repository.owner, repository.name, issueId.toString).foreach { issue => + handleComment(issue, None, repository, Some("reopen")) + } + } + case Some("close") => executeBatch(repository) { issueId => + getIssue(repository.owner, repository.name, issueId.toString).foreach { issue => + handleComment(issue, None, repository, Some("close")) + } + } + case _ => BadRequest() } } }) - post("/:owner/:repository/issues/batchedit/label")(collaboratorsOnly { repository => + post("/:owner/:repository/issues/batchedit/label")(writableUsersOnly { repository => params("value").toIntOpt.map{ labelId => executeBatch(repository) { issueId => getIssueLabel(repository.owner, repository.name, issueId, labelId) getOrElse { registerIssueLabel(repository.owner, repository.name, issueId, labelId) } } - } getOrElse NotFound + } getOrElse NotFound() }) - post("/:owner/:repository/issues/batchedit/assign")(collaboratorsOnly { repository => + post("/:owner/:repository/issues/batchedit/assign")(writableUsersOnly { repository => defining(assignedUserName("value")){ value => executeBatch(repository) { updateAssignedUserName(repository.owner, repository.name, _, value) @@ -314,7 +322,7 @@ } }) - post("/:owner/:repository/issues/batchedit/milestone")(collaboratorsOnly { repository => + post("/:owner/:repository/issues/batchedit/milestone")(writableUsersOnly { repository => defining(milestoneId("value")){ value => executeBatch(repository) { updateMilestoneId(repository.owner, repository.name, _, value) @@ -326,18 +334,16 @@ (Directory.getAttachedDir(repository.owner, repository.name) match { case dir if(dir.exists && dir.isDirectory) => dir.listFiles.find(_.getName.startsWith(params("file") + ".")).map { file => + response.setHeader("Content-Disposition", f"""inline; filename=${file.getName}""") RawData(FileUtil.getMimeType(file.getName), file) } case _ => None - }) getOrElse NotFound + }) getOrElse NotFound() }) val assignedUserName = (key: String) => params.get(key) filter (_.trim != "") val milestoneId: String => Option[Int] = (key: String) => params.get(key).flatMap(_.toIntOpt) - private def isEditable(owner: String, repository: String, author: String)(implicit context: Context): Boolean = - hasWritePermission(owner, repository, context.loginAccount) || author == context.loginAccount.get.userName - private def executeBatch(repository: RepositoryService.RepositoryInfo)(execute: Int => Unit) = { params("checked").split(',') map(_.toInt) foreach execute params("from") match { @@ -346,127 +352,33 @@ } } - private def createReferComment(owner: String, repository: String, fromIssue: Issue, message: String) = { - StringUtil.extractIssueId(message).foreach { issueId => - val content = fromIssue.issueId + ":" + fromIssue.title - if(getIssue(owner, repository, issueId).isDefined){ - // Not add if refer comment already exist. - if(!getComments(owner, repository, issueId.toInt).exists { x => x.action == "refer" && x.content == content }) { - createComment(owner, repository, context.loginAccount.get.userName, issueId.toInt, content, "refer") - } - } - } - } - - /** - * @see [[https://github.com/takezoe/gitbucket/wiki/CommentAction]] - */ - private def handleComment(issueId: Int, content: Option[String], repository: RepositoryService.RepositoryInfo) - (getAction: Issue => Option[String] = - p1 => params.get("action").filter(_ => isEditable(p1.userName, p1.repositoryName, p1.openedUserName))) = { - - defining(repository.owner, repository.name){ case (owner, name) => - val userName = context.loginAccount.get.userName - - getIssue(owner, name, issueId.toString) flatMap { issue => - val (action, recordActivity) = - getAction(issue) - .collect { - case "close" if(!issue.closed) => true -> - (Some("close") -> Some(if(issue.isPullRequest) recordClosePullRequestActivity _ else recordCloseIssueActivity _)) - case "reopen" if(issue.closed) => false -> - (Some("reopen") -> Some(recordReopenIssueActivity _)) - } - .map { case (closed, t) => - updateClosed(owner, name, issueId, closed) - t - } - .getOrElse(None -> None) - - val commentId = (content, action) match { - case (None, None) => None - case (None, Some(action)) => Some(createComment(owner, name, userName, issueId, action.capitalize, action)) - case (Some(content), _) => Some(createComment(owner, name, userName, issueId, content, action.map(_+ "_comment").getOrElse("comment"))) - } - - // record comment activity if comment is entered - content foreach { - (if(issue.isPullRequest) recordCommentPullRequestActivity _ else recordCommentIssueActivity _) - (owner, name, userName, issueId, _) - } - recordActivity foreach ( _ (owner, name, userName, issueId, issue.title) ) - - // extract references and create refer comment - content.map { content => - createReferComment(owner, name, issue, content) - } - - // call web hooks - action match { - case None => commentId.map{ commentIdSome => callIssueCommentWebHook(repository, issue, commentIdSome, context.loginAccount.get) } - case Some(act) => val webHookAction = act match { - case "open" => "opened" - case "reopen" => "reopened" - case "close" => "closed" - case _ => act - } - if(issue.isPullRequest){ - callPullRequestWebHook(webHookAction, repository, issue.issueId, context.baseUrl, context.loginAccount.get) - } else { - callIssuesWebHook(webHookAction, repository, issue, context.baseUrl, context.loginAccount.get) - } - } - - // notifications - Notifier() match { - case f => - content foreach { - f.toNotify(repository, issue, _){ - Notifier.msgComment(s"${context.baseUrl}/${owner}/${name}/${ - if(issue.isPullRequest) "pull" else "issues"}/${issueId}#comment-${commentId.get}") - } - } - action foreach { - f.toNotify(repository, issue, _){ - Notifier.msgStatus(s"${context.baseUrl}/${owner}/${name}/issues/${issueId}") - } - } - } - - commentId.map( issue -> _ ) - } - } - } - private def searchIssues(repository: RepositoryService.RepositoryInfo) = { defining(repository.owner, repository.name){ case (owner, repoName) => - val page = IssueSearchCondition.page(request) - val sessionKey = Keys.Session.Issues(owner, repoName) + val page = IssueSearchCondition.page(request) // retrieve search condition - val condition = session.putAndGet(sessionKey, - if(request.hasQueryString){ - val q = request.getParameter("q") - if(q == null || q.trim.isEmpty){ - IssueSearchCondition(request) - } else { - IssueSearchCondition(q, getMilestones(owner, repoName).map(x => (x.title, x.milestoneId)).toMap) - } - } else session.getAs[IssueSearchCondition](sessionKey).getOrElse(IssueSearchCondition()) - ) + val condition = IssueSearchCondition(request) html.list( "issues", searchIssue(condition, false, (page - 1) * IssueLimit, IssueLimit, owner -> repoName), page, - (getCollaborators(owner, repoName) :+ owner).sorted, + getAssignableUserNames(owner, repoName), getMilestones(owner, repoName), getLabels(owner, repoName), countIssue(condition.copy(state = "open" ), false, owner -> repoName), countIssue(condition.copy(state = "closed"), false, owner -> repoName), condition, repository, - hasWritePermission(owner, repoName, context.loginAccount)) + isIssueEditable(repository), + isIssueManageable(repository)) } } + + /** + * Tests whether an issue or a comment is editable by a logged-in user. + */ + private def isEditableContent(owner: String, repository: String, author: String)(implicit context: Context): Boolean = { + hasDeveloperRole(owner, repository, context.loginAccount) || author == context.loginAccount.get.userName + } } diff --git a/src/main/scala/gitbucket/core/controller/LabelsController.scala b/src/main/scala/gitbucket/core/controller/LabelsController.scala index fa6ef7a..08c0aaa 100644 --- a/src/main/scala/gitbucket/core/controller/LabelsController.scala +++ b/src/main/scala/gitbucket/core/controller/LabelsController.scala @@ -2,66 +2,67 @@ import gitbucket.core.issues.labels.html import gitbucket.core.service.{RepositoryService, AccountService, IssuesService, LabelsService} -import gitbucket.core.util.{ReferrerAuthenticator, CollaboratorsAuthenticator} +import gitbucket.core.util.{ReferrerAuthenticator, WritableUsersAuthenticator} import gitbucket.core.util.Implicits._ -import jp.sf.amateras.scalatra.forms._ +import io.github.gitbucket.scalatra.forms._ import org.scalatra.i18n.Messages import org.scalatra.Ok class LabelsController extends LabelsControllerBase with LabelsService with IssuesService with RepositoryService with AccountService -with ReferrerAuthenticator with CollaboratorsAuthenticator +with ReferrerAuthenticator with WritableUsersAuthenticator trait LabelsControllerBase extends ControllerBase { self: LabelsService with IssuesService with RepositoryService - with ReferrerAuthenticator with CollaboratorsAuthenticator => + with ReferrerAuthenticator with WritableUsersAuthenticator => case class LabelForm(labelName: String, color: String) val labelForm = mapping( - "labelName" -> trim(label("Label name", text(required, labelName, maxlength(100)))), + "labelName" -> trim(label("Label name", text(required, labelName, uniqueLabelName, maxlength(100)))), "labelColor" -> trim(label("Color", text(required, color))) )(LabelForm.apply) + get("/:owner/:repository/issues/labels")(referrersOnly { repository => html.list( getLabels(repository.owner, repository.name), countIssueGroupByLabels(repository.owner, repository.name, IssuesService.IssueSearchCondition(), Map.empty), repository, - hasWritePermission(repository.owner, repository.name, context.loginAccount)) + hasDeveloperRole(repository.owner, repository.name, context.loginAccount)) }) - ajaxGet("/:owner/:repository/issues/labels/new")(collaboratorsOnly { repository => + ajaxGet("/:owner/:repository/issues/labels/new")(writableUsersOnly { repository => html.edit(None, repository) }) - ajaxPost("/:owner/:repository/issues/labels/new", labelForm)(collaboratorsOnly { (form, repository) => + ajaxPost("/:owner/:repository/issues/labels/new", labelForm)(writableUsersOnly { (form, repository) => val labelId = createLabel(repository.owner, repository.name, form.labelName, form.color.substring(1)) html.label( getLabel(repository.owner, repository.name, labelId).get, // TODO futility countIssueGroupByLabels(repository.owner, repository.name, IssuesService.IssueSearchCondition(), Map.empty), repository, - hasWritePermission(repository.owner, repository.name, context.loginAccount)) + hasDeveloperRole(repository.owner, repository.name, context.loginAccount)) }) - ajaxGet("/:owner/:repository/issues/labels/:labelId/edit")(collaboratorsOnly { repository => + ajaxGet("/:owner/:repository/issues/labels/:labelId/edit")(writableUsersOnly { repository => getLabel(repository.owner, repository.name, params("labelId").toInt).map { label => html.edit(Some(label), repository) } getOrElse NotFound() }) - ajaxPost("/:owner/:repository/issues/labels/:labelId/edit", labelForm)(collaboratorsOnly { (form, repository) => + ajaxPost("/:owner/:repository/issues/labels/:labelId/edit", labelForm)(writableUsersOnly { (form, repository) => updateLabel(repository.owner, repository.name, params("labelId").toInt, form.labelName, form.color.substring(1)) html.label( getLabel(repository.owner, repository.name, params("labelId").toInt).get, // TODO futility countIssueGroupByLabels(repository.owner, repository.name, IssuesService.IssueSearchCondition(), Map.empty), repository, - hasWritePermission(repository.owner, repository.name, context.loginAccount)) + hasDeveloperRole(repository.owner, repository.name, context.loginAccount)) }) - ajaxPost("/:owner/:repository/issues/labels/:labelId/delete")(collaboratorsOnly { repository => + ajaxPost("/:owner/:repository/issues/labels/:labelId/delete")(writableUsersOnly { repository => deleteLabel(repository.owner, repository.name, params("labelId").toInt) Ok() }) @@ -80,4 +81,16 @@ } } + private def uniqueLabelName: Constraint = new Constraint(){ + override def validate(name: String, value: String, params: Map[String, String], messages: Messages): Option[String] = { + val owner = params("owner") + val repository = params("repository") + params.get("labelId").map { labelId => + getLabel(owner, repository, value).filter(_.labelId != labelId.toInt).map(_ => "Name has already been taken.") + }.getOrElse { + getLabel(owner, repository, value).map(_ => "Name has already been taken.") + } + } + } + } diff --git a/src/main/scala/gitbucket/core/controller/MilestonesController.scala b/src/main/scala/gitbucket/core/controller/MilestonesController.scala index eb4b714..de81c73 100644 --- a/src/main/scala/gitbucket/core/controller/MilestonesController.scala +++ b/src/main/scala/gitbucket/core/controller/MilestonesController.scala @@ -2,17 +2,17 @@ import gitbucket.core.issues.milestones.html import gitbucket.core.service.{RepositoryService, MilestonesService, AccountService} -import gitbucket.core.util.{ReferrerAuthenticator, CollaboratorsAuthenticator} +import gitbucket.core.util.{ReferrerAuthenticator, WritableUsersAuthenticator} import gitbucket.core.util.Implicits._ -import jp.sf.amateras.scalatra.forms._ +import io.github.gitbucket.scalatra.forms._ class MilestonesController extends MilestonesControllerBase with MilestonesService with RepositoryService with AccountService - with ReferrerAuthenticator with CollaboratorsAuthenticator + with ReferrerAuthenticator with WritableUsersAuthenticator trait MilestonesControllerBase extends ControllerBase { self: MilestonesService with RepositoryService - with ReferrerAuthenticator with CollaboratorsAuthenticator => + with ReferrerAuthenticator with WritableUsersAuthenticator => case class MilestoneForm(title: String, description: Option[String], dueDate: Option[java.util.Date]) @@ -27,58 +27,58 @@ params.getOrElse("state", "open"), getMilestonesWithIssueCount(repository.owner, repository.name), repository, - hasWritePermission(repository.owner, repository.name, context.loginAccount)) + hasDeveloperRole(repository.owner, repository.name, context.loginAccount)) }) - get("/:owner/:repository/issues/milestones/new")(collaboratorsOnly { + get("/:owner/:repository/issues/milestones/new")(writableUsersOnly { html.edit(None, _) }) - post("/:owner/:repository/issues/milestones/new", milestoneForm)(collaboratorsOnly { (form, repository) => + post("/:owner/:repository/issues/milestones/new", milestoneForm)(writableUsersOnly { (form, repository) => createMilestone(repository.owner, repository.name, form.title, form.description, form.dueDate) redirect(s"/${repository.owner}/${repository.name}/issues/milestones") }) - get("/:owner/:repository/issues/milestones/:milestoneId/edit")(collaboratorsOnly { repository => + get("/:owner/:repository/issues/milestones/:milestoneId/edit")(writableUsersOnly { repository => params("milestoneId").toIntOpt.map{ milestoneId => html.edit(getMilestone(repository.owner, repository.name, milestoneId), repository) - } getOrElse NotFound + } getOrElse NotFound() }) - post("/:owner/:repository/issues/milestones/:milestoneId/edit", milestoneForm)(collaboratorsOnly { (form, repository) => + post("/:owner/:repository/issues/milestones/:milestoneId/edit", milestoneForm)(writableUsersOnly { (form, repository) => params("milestoneId").toIntOpt.flatMap{ milestoneId => getMilestone(repository.owner, repository.name, milestoneId).map { milestone => updateMilestone(milestone.copy(title = form.title, description = form.description, dueDate = form.dueDate)) redirect(s"/${repository.owner}/${repository.name}/issues/milestones") } - } getOrElse NotFound + } getOrElse NotFound() }) - get("/:owner/:repository/issues/milestones/:milestoneId/close")(collaboratorsOnly { repository => + get("/:owner/:repository/issues/milestones/:milestoneId/close")(writableUsersOnly { repository => params("milestoneId").toIntOpt.flatMap{ milestoneId => getMilestone(repository.owner, repository.name, milestoneId).map { milestone => closeMilestone(milestone) redirect(s"/${repository.owner}/${repository.name}/issues/milestones") } - } getOrElse NotFound + } getOrElse NotFound() }) - get("/:owner/:repository/issues/milestones/:milestoneId/open")(collaboratorsOnly { repository => + get("/:owner/:repository/issues/milestones/:milestoneId/open")(writableUsersOnly { repository => params("milestoneId").toIntOpt.flatMap{ milestoneId => getMilestone(repository.owner, repository.name, milestoneId).map { milestone => openMilestone(milestone) redirect(s"/${repository.owner}/${repository.name}/issues/milestones") } - } getOrElse NotFound + } getOrElse NotFound() }) - get("/:owner/:repository/issues/milestones/:milestoneId/delete")(collaboratorsOnly { repository => + get("/:owner/:repository/issues/milestones/:milestoneId/delete")(writableUsersOnly { repository => params("milestoneId").toIntOpt.flatMap{ milestoneId => getMilestone(repository.owner, repository.name, milestoneId).map { milestone => deleteMilestone(repository.owner, repository.name, milestone.milestoneId) redirect(s"/${repository.owner}/${repository.name}/issues/milestones") } - } getOrElse NotFound + } getOrElse NotFound() }) } diff --git a/src/main/scala/gitbucket/core/controller/PullRequestsController.scala b/src/main/scala/gitbucket/core/controller/PullRequestsController.scala index fcddd27..e33fca1 100644 --- a/src/main/scala/gitbucket/core/controller/PullRequestsController.scala +++ b/src/main/scala/gitbucket/core/controller/PullRequestsController.scala @@ -1,41 +1,36 @@ package gitbucket.core.controller -import gitbucket.core.api._ -import gitbucket.core.model.{Account, CommitState, Repository, PullRequest, Issue} +import gitbucket.core.model.WebHook import gitbucket.core.pulls.html import gitbucket.core.service.CommitStatusService import gitbucket.core.service.MergeService import gitbucket.core.service.IssuesService._ import gitbucket.core.service.PullRequestService._ +import gitbucket.core.service.RepositoryService.RepositoryInfo import gitbucket.core.service._ import gitbucket.core.util.ControlUtil._ import gitbucket.core.util.Directory._ import gitbucket.core.util.Implicits._ -import gitbucket.core.util.JGitUtil._ import gitbucket.core.util._ -import gitbucket.core.view -import gitbucket.core.view.helpers - -import jp.sf.amateras.scalatra.forms._ +import io.github.gitbucket.scalatra.forms._ import org.eclipse.jgit.api.Git import org.eclipse.jgit.lib.PersonIdent -import org.slf4j.LoggerFactory import scala.collection.JavaConverters._ class PullRequestsController extends PullRequestsControllerBase with RepositoryService with AccountService with IssuesService with PullRequestService with MilestonesService with LabelsService - with CommitsService with ActivityService with WebHookPullRequestService with ReferrerAuthenticator with CollaboratorsAuthenticator - with CommitStatusService with MergeService + with CommitsService with ActivityService with WebHookPullRequestService + with ReadableUsersAuthenticator with ReferrerAuthenticator with WritableUsersAuthenticator + with CommitStatusService with MergeService with ProtectedBranchService trait PullRequestsControllerBase extends ControllerBase { self: RepositoryService with AccountService with IssuesService with MilestonesService with LabelsService - with CommitsService with ActivityService with PullRequestService with WebHookPullRequestService with ReferrerAuthenticator with CollaboratorsAuthenticator - with CommitStatusService with MergeService => - - private val logger = LoggerFactory.getLogger(classOf[PullRequestsControllerBase]) + with CommitsService with ActivityService with PullRequestService with WebHookPullRequestService + with ReadableUsersAuthenticator with ReferrerAuthenticator with WritableUsersAuthenticator + with CommitStatusService with MergeService with ProtectedBranchService => val pullRequestForm = mapping( "title" -> trim(label("Title" , text(required, maxlength(100)))), @@ -46,7 +41,10 @@ "requestRepositoryName" -> trim(text(required, maxlength(100))), "requestBranch" -> trim(text(required, maxlength(100))), "commitIdFrom" -> trim(text(required, maxlength(40))), - "commitIdTo" -> trim(text(required, maxlength(40))) + "commitIdTo" -> trim(text(required, maxlength(40))), + "assignedUserName" -> trim(optional(text())), + "milestoneId" -> trim(optional(number())), + "labelNames" -> trim(optional(text())) )(PullRequestForm.apply) val mergeForm = mapping( @@ -62,7 +60,11 @@ requestRepositoryName: String, requestBranch: String, commitIdFrom: String, - commitIdTo: String) + commitIdTo: String, + assignedUserName: Option[String], + milestoneId: Option[Int], + labelNames: Option[String] + ) case class MergeForm(message: String) @@ -75,24 +77,6 @@ } }) - /** - * https://developer.github.com/v3/pulls/#list-pull-requests - */ - get("/api/v3/repos/:owner/:repository/pulls")(referrersOnly { repository => - val page = IssueSearchCondition.page(request) - // TODO: more api spec condition - val condition = IssueSearchCondition(request) - val baseOwner = getAccountByUserName(repository.owner).get - val issues:List[(Issue, Account, Int, PullRequest, Repository, Account)] = searchPullRequestByApi(condition, (page - 1) * PullRequestLimit, PullRequestLimit, repository.owner -> repository.name) - JsonFormat(issues.map{case (issue, issueUser, commentCount, pullRequest, headRepo, headOwner) => - ApiPullRequest( - issue, - pullRequest, - ApiRepository(headRepo, ApiUser(headOwner)), - ApiRepository(repository, ApiUser(baseOwner)), - ApiUser(issueUser)) }) - }) - get("/:owner/:repository/pull/:id")(referrersOnly { repository => params("id").toIntOpt.flatMap{ issueId => val owner = repository.owner @@ -106,82 +90,55 @@ (commits.flatten.map(commit => getCommitComments(owner, name, commit.id, true)).flatten.toList ::: getComments(owner, name, issueId)) .sortWith((a, b) => a.registeredDate before b.registeredDate), getIssueLabels(owner, name, issueId), - (getCollaborators(owner, name) ::: (if(getAccountByUserName(owner).get.isGroupAccount) Nil else List(owner))).sorted, + getAssignableUserNames(owner, name), getMilestonesWithIssueCount(owner, name), getLabels(owner, name), commits, diffs, - hasWritePermission(owner, name, context.loginAccount), - repository) + isEditable(repository), + isManageable(repository), + repository, + flash.toMap.map(f => f._1 -> f._2.toString)) } } - } getOrElse NotFound + } getOrElse NotFound() }) - /** - * https://developer.github.com/v3/pulls/#get-a-single-pull-request - */ - get("/api/v3/repos/:owner/:repository/pulls/:id")(referrersOnly { repository => - (for{ - issueId <- params("id").toIntOpt - (issue, pullRequest) <- getPullRequest(repository.owner, repository.name, issueId) - users = getAccountsByUserNames(Set(repository.owner, pullRequest.requestUserName, issue.openedUserName), Set()) - baseOwner <- users.get(repository.owner) - headOwner <- users.get(pullRequest.requestUserName) - issueUser <- users.get(issue.openedUserName) - headRepo <- getRepository(pullRequest.requestUserName, pullRequest.requestRepositoryName, baseUrl) - } yield { - JsonFormat(ApiPullRequest( - issue, - pullRequest, - ApiRepository(headRepo, ApiUser(headOwner)), - ApiRepository(repository, ApiUser(baseOwner)), - ApiUser(issueUser))) - }).getOrElse(NotFound) - }) - - /** - * https://developer.github.com/v3/pulls/#list-commits-on-a-pull-request - */ - get("/api/v3/repos/:owner/:repository/pulls/:id/commits")(referrersOnly { repository => - val owner = repository.owner - val name = repository.name - params("id").toIntOpt.flatMap{ issueId => - getPullRequest(owner, name, issueId) map { case(issue, pullreq) => - using(Git.open(getRepositoryDir(owner, name))){ git => - val oldId = git.getRepository.resolve(pullreq.commitIdFrom) - val newId = git.getRepository.resolve(pullreq.commitIdTo) - val repoFullName = RepositoryName(repository) - val commits = git.log.addRange(oldId, newId).call.iterator.asScala.map(c => ApiCommitListItem(new CommitInfo(c), repoFullName)).toList - JsonFormat(commits) - } - } - } getOrElse NotFound - }) - - ajaxGet("/:owner/:repository/pull/:id/mergeguide")(collaboratorsOnly { repository => + ajaxGet("/:owner/:repository/pull/:id/mergeguide")(referrersOnly { repository => params("id").toIntOpt.flatMap{ issueId => val owner = repository.owner val name = repository.name getPullRequest(owner, name, issueId) map { case(issue, pullreq) => - val statuses = getCommitStatues(owner, name, pullreq.commitIdTo) - val hasConfrict = LockUtil.lock(s"${owner}/${name}"){ + val hasConflict = LockUtil.lock(s"${owner}/${name}"){ checkConflict(owner, name, pullreq.branch, issueId) } - val hasProblem = hasConfrict || (!statuses.isEmpty && CommitState.combine(statuses.map(_.state).toSet) != CommitState.SUCCESS) + val hasMergePermission = hasDeveloperRole(owner, name, context.loginAccount) + val branchProtection = getProtectedBranchInfo(owner, name, pullreq.branch) + val mergeStatus = PullRequestService.MergeStatus( + hasConflict = hasConflict, + commitStatues = getCommitStatues(owner, name, pullreq.commitIdTo), + branchProtection = branchProtection, + branchIsOutOfDate = JGitUtil.getShaByRef(owner, name, pullreq.branch) != Some(pullreq.commitIdFrom), + needStatusCheck = context.loginAccount.map{ u => + branchProtection.needStatusCheck(u.userName) + }.getOrElse(true), + hasUpdatePermission = hasDeveloperRole(pullreq.requestUserName, pullreq.requestRepositoryName, context.loginAccount) && + context.loginAccount.map{ u => + !getProtectedBranchInfo(pullreq.requestUserName, pullreq.requestRepositoryName, pullreq.requestBranch).needStatusCheck(u.userName) + }.getOrElse(false), + hasMergePermission = hasMergePermission, + commitIdTo = pullreq.commitIdTo) html.mergeguide( - hasConfrict, - hasProblem, + mergeStatus, issue, pullreq, - statuses, repository, - s"${context.baseUrl}/git/${pullreq.requestUserName}/${pullreq.requestRepositoryName}.git") + getRepository(pullreq.requestUserName, pullreq.requestRepositoryName).get) } - } getOrElse NotFound + } getOrElse NotFound() }) - get("/:owner/:repository/pull/:id/delete/*")(collaboratorsOnly { repository => + get("/:owner/:repository/pull/:id/delete/*")(writableUsersOnly { repository => params("id").toIntOpt.map { issueId => val branchName = multiParams("splat").head val userName = context.loginAccount.get.userName @@ -193,10 +150,79 @@ } createComment(repository.owner, repository.name, userName, issueId, branchName, "delete_branch") redirect(s"/${repository.owner}/${repository.name}/pull/${issueId}") - } getOrElse NotFound + } getOrElse NotFound() }) - post("/:owner/:repository/pull/:id/merge", mergeForm)(collaboratorsOnly { (form, repository) => + post("/:owner/:repository/pull/:id/update_branch")(writableUsersOnly { baseRepository => + (for { + issueId <- params("id").toIntOpt + loginAccount <- context.loginAccount + (issue, pullreq) <- getPullRequest(baseRepository.owner, baseRepository.name, issueId) + owner = pullreq.requestUserName + name = pullreq.requestRepositoryName + if hasDeveloperRole(owner, name, context.loginAccount) + } yield { + val repository = getRepository(owner, name).get + val branchProtection = getProtectedBranchInfo(owner, name, pullreq.requestBranch) + if(branchProtection.needStatusCheck(loginAccount.userName)){ + flash += "error" -> s"branch ${pullreq.requestBranch} is protected need status check." + } else { + LockUtil.lock(s"${owner}/${name}"){ + val alias = if(pullreq.repositoryName == pullreq.requestRepositoryName && pullreq.userName == pullreq.requestUserName){ + pullreq.branch + } else { + s"${pullreq.userName}:${pullreq.branch}" + } + val existIds = using(Git.open(Directory.getRepositoryDir(owner, name))) { git => JGitUtil.getAllCommitIds(git) }.toSet + pullRemote(owner, name, pullreq.requestBranch, pullreq.userName, pullreq.repositoryName, pullreq.branch, loginAccount, + s"Merge branch '${alias}' into ${pullreq.requestBranch}") match { + case None => // conflict + flash += "error" -> s"Can't automatic merging branch '${alias}' into ${pullreq.requestBranch}." + case Some(oldId) => + // update pull request + updatePullRequests(owner, name, pullreq.requestBranch) + + using(Git.open(Directory.getRepositoryDir(owner, name))) { git => + // after update branch + val newCommitId = git.getRepository.resolve(s"refs/heads/${pullreq.requestBranch}") + val commits = git.log.addRange(oldId, newCommitId).call.iterator.asScala.map(c => new JGitUtil.CommitInfo(c)).toList + + commits.foreach { commit => + if(!existIds.contains(commit.id)){ + createIssueComment(owner, name, commit) + } + } + + // record activity + recordPushActivity(owner, name, loginAccount.userName, pullreq.branch, commits) + + // close issue by commit message + if(pullreq.requestBranch == repository.repository.defaultBranch){ + commits.map { commit => + closeIssuesFromMessage(commit.fullMessage, loginAccount.userName, owner, name) + } + } + + // call web hook + callPullRequestWebHookByRequestBranch("synchronize", repository, pullreq.requestBranch, baseUrl, loginAccount) + callWebHookOf(owner, name, WebHook.Push) { + for { + ownerAccount <- getAccountByUserName(owner) + } yield { + WebHookService.WebHookPushPayload(git, loginAccount, pullreq.requestBranch, repository, commits, ownerAccount, oldId = oldId, newId = newCommitId) + } + } + } + flash += "info" -> s"Merge branch '${alias}' into ${pullreq.requestBranch}" + } + } + } + redirect(s"/${repository.owner}/${repository.name}/pull/${issueId}") + + }) getOrElse NotFound() + }) + + post("/:owner/:repository/pull/:id/merge", mergeForm)(writableUsersOnly { (form, repository) => params("id").toIntOpt.flatMap { issueId => val owner = repository.owner val name = repository.name @@ -221,17 +247,17 @@ pullreq.requestUserName, pullreq.requestRepositoryName, pullreq.commitIdTo) // close issue by content of pull request - val defaultBranch = getRepository(owner, name, context.baseUrl).get.repository.defaultBranch + val defaultBranch = getRepository(owner, name).get.repository.defaultBranch if(pullreq.branch == defaultBranch){ commits.flatten.foreach { commit => closeIssuesFromMessage(commit.fullMessage, loginAccount.userName, owner, name) } - issue.content match { - case Some(content) => closeIssuesFromMessage(content, loginAccount.userName, owner, name) - case _ => - } + closeIssuesFromMessage(issue.title + " " + issue.content.getOrElse(""), loginAccount.userName, owner, name) closeIssuesFromMessage(form.message, loginAccount.userName, owner, name) } + + updatePullRequests(owner, name, pullreq.branch) + // call web hook callPullRequestWebHook("closed", repository, issueId, context.baseUrl, context.loginAccount.get) @@ -244,14 +270,14 @@ } } } - } getOrElse NotFound + } getOrElse NotFound() }) get("/:owner/:repository/compare")(referrersOnly { forkedRepository => val headBranch:Option[String] = params.get("head") (forkedRepository.repository.originUserName, forkedRepository.repository.originRepositoryName) match { case (Some(originUserName), Some(originRepositoryName)) => { - getRepository(originUserName, originRepositoryName, context.baseUrl).map { originRepository => + getRepository(originUserName, originRepositoryName).map { originRepository => using( Git.open(getRepositoryDir(originUserName, originRepositoryName)), Git.open(getRepositoryDir(forkedRepository.owner, forkedRepository.name)) @@ -261,7 +287,7 @@ redirect(s"/${forkedRepository.owner}/${forkedRepository.name}/compare/${originUserName}:${oldBranch}...${newBranch}") } - } getOrElse NotFound + } getOrElse NotFound() } case _ => { using(Git.open(getRepositoryDir(forkedRepository.owner, forkedRepository.name))){ git => @@ -284,17 +310,20 @@ originRepositoryName <- if(originOwner == forkedOwner) { // Self repository Some(forkedRepository.name) + } else if(forkedRepository.repository.originUserName.isEmpty){ + // when ForkedRepository is the original repository + getForkedRepositories(forkedRepository.owner, forkedRepository.name).find(_._1 == originOwner).map(_._2) } else if(Some(originOwner) == forkedRepository.repository.originUserName){ // Original repository forkedRepository.repository.originRepositoryName } else { // Sibling repository - getUserRepositories(originOwner, context.baseUrl).find { x => + getUserRepositories(originOwner).find { x => x.repository.originUserName == forkedRepository.repository.originUserName && x.repository.originRepositoryName == forkedRepository.repository.originRepositoryName }.map(_.repository.repositoryName) }; - originRepository <- getRepository(originOwner, originRepositoryName, context.baseUrl) + originRepository <- getRepository(originOwner, originRepositoryName) ) yield { using( Git.open(getRepositoryDir(originRepository.owner, originRepository.name)), @@ -307,37 +336,57 @@ originRepository.owner, originRepository.name, originId, forkedRepository.owner, forkedRepository.name, forkedId) - (oldGit.getRepository.resolve(rootId), newGit.getRepository.resolve(forkedId)) + (Option(oldGit.getRepository.resolve(rootId)), Option(newGit.getRepository.resolve(forkedId))) } else { // Commit id - (oldGit.getRepository.resolve(originId), newGit.getRepository.resolve(forkedId)) + (Option(oldGit.getRepository.resolve(originId)), Option(newGit.getRepository.resolve(forkedId))) } - val (commits, diffs) = getRequestCompareInfo( - originRepository.owner, originRepository.name, oldId.getName, - forkedRepository.owner, forkedRepository.name, newId.getName) + (oldId, newId) match { + case (Some(oldId), Some(newId)) => { + val (commits, diffs) = getRequestCompareInfo( + originRepository.owner, originRepository.name, oldId.getName, + forkedRepository.owner, forkedRepository.name, newId.getName) - html.compare( - commits, - diffs, - (forkedRepository.repository.originUserName, forkedRepository.repository.originRepositoryName) match { - case (Some(userName), Some(repositoryName)) => (userName, repositoryName) :: getForkedRepositories(userName, repositoryName) - case _ => (forkedRepository.owner, forkedRepository.name) :: getForkedRepositories(forkedRepository.owner, forkedRepository.name) - }, - commits.flatten.map(commit => getCommitComments(forkedRepository.owner, forkedRepository.name, commit.id, false)).flatten.toList, - originId, - forkedId, - oldId.getName, - newId.getName, - forkedRepository, - originRepository, - forkedRepository, - hasWritePermission(forkedRepository.owner, forkedRepository.name, context.loginAccount)) + val title = if(commits.flatten.length == 1){ + commits.flatten.head.shortMessage + } else { + val text = forkedId.replaceAll("[\\-_]", " ") + text.substring(0, 1).toUpperCase + text.substring(1) + } + + html.compare( + title, + commits, + diffs, + (forkedRepository.repository.originUserName, forkedRepository.repository.originRepositoryName) match { + case (Some(userName), Some(repositoryName)) => (userName, repositoryName) :: getForkedRepositories(userName, repositoryName) + case _ => (forkedRepository.owner, forkedRepository.name) :: getForkedRepositories(forkedRepository.owner, forkedRepository.name) + }, + commits.flatten.map(commit => getCommitComments(forkedRepository.owner, forkedRepository.name, commit.id, false)).flatten.toList, + originId, + forkedId, + oldId.getName, + newId.getName, + forkedRepository, + originRepository, + forkedRepository, + hasDeveloperRole(originRepository.owner, originRepository.name, context.loginAccount), + getAssignableUserNames(originRepository.owner, originRepository.name), + getMilestones(originRepository.owner, originRepository.name), + getLabels(originRepository.owner, originRepository.name) + ) + } + case (oldId, newId) => + redirect(s"/${forkedRepository.owner}/${forkedRepository.name}/compare/" + + s"${originOwner}:${oldId.map(_ => originId).getOrElse(originRepository.repository.defaultBranch)}..." + + s"${forkedOwner}:${newId.map(_ => forkedId).getOrElse(forkedRepository.repository.defaultBranch)}") + } } - }) getOrElse NotFound + }) getOrElse NotFound() }) - ajaxGet("/:owner/:repository/compare/*...*/mergecheck")(collaboratorsOnly { forkedRepository => + ajaxGet("/:owner/:repository/compare/*...*/mergecheck")(readableUsersOnly { forkedRepository => val Seq(origin, forked) = multiParams("splat") val (originOwner, tmpOriginBranch) = parseCompareIdentifie(origin, forkedRepository.owner) val (forkedOwner, tmpForkedBranch) = parseCompareIdentifie(forked, forkedRepository.owner) @@ -350,7 +399,7 @@ getForkedRepositories(forkedRepository.owner, forkedRepository.name).find(_._1 == originOwner).map(_._2) } }; - originRepository <- getRepository(originOwner, originRepositoryName, context.baseUrl) + originRepository <- getRepository(originOwner, originRepositoryName) ) yield { using( Git.open(getRepositoryDir(originRepository.owner, originRepository.name)), @@ -364,50 +413,72 @@ } html.mergecheck(conflict) } - }) getOrElse NotFound + }) getOrElse NotFound() }) - post("/:owner/:repository/pulls/new", pullRequestForm)(referrersOnly { (form, repository) => - val loginUserName = context.loginAccount.get.userName + post("/:owner/:repository/pulls/new", pullRequestForm)(readableUsersOnly { (form, repository) => + defining(repository.owner, repository.name){ case (owner, name) => + val manageable = isManageable(repository) + val editable = isEditable(repository) - val issueId = createIssue( - owner = repository.owner, - repository = repository.name, - loginUser = loginUserName, - title = form.title, - content = form.content, - assignedUserName = None, - milestoneId = None, - isPullRequest = true) + if(editable) { + val loginUserName = context.loginAccount.get.userName - createPullRequest( - originUserName = repository.owner, - originRepositoryName = repository.name, - issueId = issueId, - originBranch = form.targetBranch, - requestUserName = form.requestUserName, - requestRepositoryName = form.requestRepositoryName, - requestBranch = form.requestBranch, - commitIdFrom = form.commitIdFrom, - commitIdTo = form.commitIdTo) + val issueId = insertIssue( + owner = repository.owner, + repository = repository.name, + loginUser = loginUserName, + title = form.title, + content = form.content, + assignedUserName = if (manageable) form.assignedUserName else None, + milestoneId = if (manageable) form.milestoneId else None, + isPullRequest = true) - // fetch requested branch - fetchAsPullRequest(repository.owner, repository.name, form.requestUserName, form.requestRepositoryName, form.requestBranch, issueId) + createPullRequest( + originUserName = repository.owner, + originRepositoryName = repository.name, + issueId = issueId, + originBranch = form.targetBranch, + requestUserName = form.requestUserName, + requestRepositoryName = form.requestRepositoryName, + requestBranch = form.requestBranch, + commitIdFrom = form.commitIdFrom, + commitIdTo = form.commitIdTo) - // record activity - recordPullRequestActivity(repository.owner, repository.name, loginUserName, issueId, form.title) + // insert labels + if (manageable) { + form.labelNames.map { value => + val labels = getLabels(owner, name) + value.split(",").foreach { labelName => + labels.find(_.labelName == labelName).map { label => + registerIssueLabel(repository.owner, repository.name, issueId, label.labelId) + } + } + } + } - // call web hook - callPullRequestWebHook("opened", repository, issueId, context.baseUrl, context.loginAccount.get) + // fetch requested branch + fetchAsPullRequest(owner, name, form.requestUserName, form.requestRepositoryName, form.requestBranch, issueId) - // notifications - getIssue(repository.owner, repository.name, issueId.toString) foreach { issue => - Notifier().toNotify(repository, issue, form.content.getOrElse("")){ - Notifier.msgPullRequest(s"${context.baseUrl}/${repository.owner}/${repository.name}/pull/${issueId}") - } + // record activity + recordPullRequestActivity(owner, name, loginUserName, issueId, form.title) + + // call web hook + callPullRequestWebHook("opened", repository, issueId, context.baseUrl, context.loginAccount.get) + + getIssue(owner, name, issueId.toString) foreach { issue => + // extract references and create refer comment + createReferComment(owner, name, issue, form.title + " " + form.content.getOrElse(""), context.loginAccount.get) + + // notifications + Notifier().toNotify(repository, issue, form.content.getOrElse("")) { + Notifier.msgPullRequest(s"${context.baseUrl}/${owner}/${name}/pull/${issueId}") + } + } + + redirect(s"/${owner}/${name}/pull/${issueId}") + } else Unauthorized() } - - redirect(s"/${repository.owner}/${repository.name}/pull/${issueId}") }) /** @@ -424,48 +495,45 @@ (defaultOwner, value) } - private def getRequestCompareInfo(userName: String, repositoryName: String, branch: String, - requestUserName: String, requestRepositoryName: String, requestCommitId: String): (Seq[Seq[CommitInfo]], Seq[DiffInfo]) = - using( - Git.open(getRepositoryDir(userName, repositoryName)), - Git.open(getRepositoryDir(requestUserName, requestRepositoryName)) - ){ (oldGit, newGit) => - val oldId = oldGit.getRepository.resolve(branch) - val newId = newGit.getRepository.resolve(requestCommitId) - - val commits = newGit.log.addRange(oldId, newId).call.iterator.asScala.map { revCommit => - new CommitInfo(revCommit) - }.toList.splitWith { (commit1, commit2) => - helpers.date(commit1.commitTime) == view.helpers.date(commit2.commitTime) - } - - val diffs = JGitUtil.getDiffs(newGit, oldId.getName, newId.getName, true) - - (commits, diffs) - } - private def searchPullRequests(userName: Option[String], repository: RepositoryService.RepositoryInfo) = defining(repository.owner, repository.name){ case (owner, repoName) => - val page = IssueSearchCondition.page(request) - val sessionKey = Keys.Session.Pulls(owner, repoName) + val page = IssueSearchCondition.page(request) // retrieve search condition - val condition = session.putAndGet(sessionKey, - if(request.hasQueryString) IssueSearchCondition(request) - else session.getAs[IssueSearchCondition](sessionKey).getOrElse(IssueSearchCondition()) - ) + val condition = IssueSearchCondition(request) gitbucket.core.issues.html.list( "pulls", searchIssue(condition, true, (page - 1) * PullRequestLimit, PullRequestLimit, owner -> repoName), page, - (getCollaborators(owner, repoName) :+ owner).sorted, + getAssignableUserNames(owner, repoName), getMilestones(owner, repoName), getLabels(owner, repoName), countIssue(condition.copy(state = "open" ), true, owner -> repoName), countIssue(condition.copy(state = "closed"), true, owner -> repoName), condition, repository, - hasWritePermission(owner, repoName, context.loginAccount)) + isEditable(repository), + isManageable(repository)) } + + /** + * Tests whether an logged-in user can manage pull requests. + */ + private def isManageable(repository: RepositoryInfo)(implicit context: Context): Boolean = { + hasDeveloperRole(repository.owner, repository.name, context.loginAccount) + } + + /** + * Tests whether an logged-in user can post pull requests. + */ + private def isEditable(repository: RepositoryInfo)(implicit context: Context): Boolean = { + repository.repository.options.issuesOption match { + case "ALL" => !repository.repository.isPrivate && context.loginAccount.isDefined + case "PUBLIC" => hasGuestRole(repository.owner, repository.name, context.loginAccount) + case "PRIVATE" => hasDeveloperRole(repository.owner, repository.name, context.loginAccount) + case "DISABLE" => false + } + } + } diff --git a/src/main/scala/gitbucket/core/controller/RepositorySettingsController.scala b/src/main/scala/gitbucket/core/controller/RepositorySettingsController.scala index 5fd8ec0..b0b30f5 100644 --- a/src/main/scala/gitbucket/core/controller/RepositorySettingsController.scala +++ b/src/main/scala/gitbucket/core/controller/RepositorySettingsController.scala @@ -2,51 +2,78 @@ import gitbucket.core.settings.html import gitbucket.core.model.WebHook -import gitbucket.core.service.{RepositoryService, AccountService, WebHookService} +import gitbucket.core.service.{RepositoryService, AccountService, WebHookService, ProtectedBranchService, CommitStatusService} import gitbucket.core.service.WebHookService._ import gitbucket.core.util._ import gitbucket.core.util.JGitUtil._ import gitbucket.core.util.ControlUtil._ import gitbucket.core.util.Implicits._ import gitbucket.core.util.Directory._ -import jp.sf.amateras.scalatra.forms._ +import io.github.gitbucket.scalatra.forms._ import org.apache.commons.io.FileUtils import org.scalatra.i18n.Messages import org.eclipse.jgit.api.Git import org.eclipse.jgit.lib.Constants +import org.eclipse.jgit.lib.ObjectId +import gitbucket.core.model.WebHookContentType class RepositorySettingsController extends RepositorySettingsControllerBase - with RepositoryService with AccountService with WebHookService + with RepositoryService with AccountService with WebHookService with ProtectedBranchService with CommitStatusService with OwnerAuthenticator with UsersAuthenticator trait RepositorySettingsControllerBase extends ControllerBase { - self: RepositoryService with AccountService with WebHookService + self: RepositoryService with AccountService with WebHookService with ProtectedBranchService with CommitStatusService with OwnerAuthenticator with UsersAuthenticator => // for repository options - case class OptionsForm(repositoryName: String, description: Option[String], defaultBranch: String, isPrivate: Boolean) + case class OptionsForm( + repositoryName: String, + description: Option[String], + isPrivate: Boolean, + issuesOption: String, + externalIssuesUrl: Option[String], + wikiOption: String, + externalWikiUrl: Option[String], + allowFork: Boolean + ) val optionsForm = mapping( - "repositoryName" -> trim(label("Description" , text(required, maxlength(40), identifier, renameRepositoryName))), - "description" -> trim(label("Description" , optional(text()))), - "defaultBranch" -> trim(label("Default Branch" , text(required, maxlength(100)))), - "isPrivate" -> trim(label("Repository Type", boolean())) + "repositoryName" -> trim(label("Repository Name" , text(required, maxlength(100), identifier, renameRepositoryName))), + "description" -> trim(label("Description" , optional(text()))), + "isPrivate" -> trim(label("Repository Type" , boolean())), + "issuesOption" -> trim(label("Issues Option" , text(required, featureOption))), + "externalIssuesUrl" -> trim(label("External Issues URL", optional(text(maxlength(200))))), + "wikiOption" -> trim(label("Wiki Option" , text(required, featureOption))), + "externalWikiUrl" -> trim(label("External Wiki URL" , optional(text(maxlength(200))))), + "allowFork" -> trim(label("Allow Forking" , boolean())) )(OptionsForm.apply) - // for collaborator addition - case class CollaboratorForm(userName: String) + // for default branch + case class DefaultBranchForm(defaultBranch: String) - val collaboratorForm = mapping( - "userName" -> trim(label("Username", text(required, collaborator))) - )(CollaboratorForm.apply) + val defaultBranchForm = mapping( + "defaultBranch" -> trim(label("Default Branch" , text(required, maxlength(100)))) + )(DefaultBranchForm.apply) + +// // for collaborator addition +// case class CollaboratorForm(userName: String) +// +// val collaboratorForm = mapping( +// "userName" -> trim(label("Username", text(required, collaborator))) +// )(CollaboratorForm.apply) // for web hook url addition - case class WebHookForm(url: String) + case class WebHookForm(url: String, events: Set[WebHook.Event], ctype: WebHookContentType, token: Option[String]) - val webHookForm = mapping( - "url" -> trim(label("url", text(required, webHook))) - )(WebHookForm.apply) + def webHookForm(update:Boolean) = mapping( + "url" -> trim(label("url", text(required, webHook(update)))), + "events" -> webhookEvents, + "ctype" -> label("ctype", text()), + "token" -> optional(trim(label("token", text(maxlength(100))))) + )( + (url, events, ctype, token) => WebHookForm(url, events, WebHookContentType.valueOf(ctype), token) + ) // for transfer ownership case class TransferOwnerShipForm(newOwner: String) @@ -73,15 +100,18 @@ * Save the repository options. */ post("/:owner/:repository/settings/options", optionsForm)(ownerOnly { (form, repository) => - val defaultBranch = if(repository.branchList.isEmpty) "master" else form.defaultBranch saveRepositoryOptions( repository.owner, repository.name, form.description, - defaultBranch, repository.repository.parentUserName.map { _ => repository.repository.isPrivate - } getOrElse form.isPrivate + } getOrElse form.isPrivate, + form.issuesOption, + form.externalIssuesUrl, + form.wikiOption, + form.externalWikiUrl, + form.allowFork ) // Change repository name if(repository.name != form.repositoryName){ @@ -96,14 +126,45 @@ FileUtils.moveDirectory(dir, getWikiRepositoryDir(repository.owner, form.repositoryName)) } } - // Change repository HEAD - using(Git.open(getRepositoryDir(repository.owner, form.repositoryName))) { git => - git.getRepository.updateRef(Constants.HEAD, true).link(Constants.R_HEADS + defaultBranch) - } flash += "info" -> "Repository settings has been updated." redirect(s"/${repository.owner}/${form.repositoryName}/settings/options") }) - + + /** branch settings */ + get("/:owner/:repository/settings/branches")(ownerOnly { repository => + val protecteions = getProtectedBranchList(repository.owner, repository.name) + html.branches(repository, protecteions, flash.get("info")) + }); + + /** Update default branch */ + post("/:owner/:repository/settings/update_default_branch", defaultBranchForm)(ownerOnly { (form, repository) => + if(repository.branchList.find(_ == form.defaultBranch).isEmpty){ + redirect(s"/${repository.owner}/${repository.name}/settings/options") + } else { + saveRepositoryDefaultBranch(repository.owner, repository.name, form.defaultBranch) + // Change repository HEAD + using(Git.open(getRepositoryDir(repository.owner, repository.name))) { git => + git.getRepository.updateRef(Constants.HEAD, true).link(Constants.R_HEADS + form.defaultBranch) + } + flash += "info" -> "Repository default branch has been updated." + redirect(s"/${repository.owner}/${repository.name}/settings/branches") + } + }) + + /** Branch protection for branch */ + get("/:owner/:repository/settings/branches/:branch")(ownerOnly { repository => + import gitbucket.core.api._ + val branch = params("branch") + if(repository.branchList.find(_ == branch).isEmpty){ + redirect(s"/${repository.owner}/${repository.name}/settings/branches") + } else { + val protection = ApiBranchProtection(getProtectedBranchInfo(repository.owner, repository.name, branch)) + val lastWeeks = getRecentStatuesContexts(repository.owner, repository.name, org.joda.time.LocalDateTime.now.minusWeeks(1).toDate).toSet + val knownContexts = (lastWeeks ++ protection.status.contexts).toSeq.sortBy(identity) + html.branchprotection(repository, branch, protection, knownContexts, flash.get("info")) + } + }) + /** * Display the Collaborators page. */ @@ -114,22 +175,12 @@ repository) }) - /** - * Add the collaborator. - */ - post("/:owner/:repository/settings/collaborators/add", collaboratorForm)(ownerOnly { (form, repository) => - if(!getAccountByUserName(repository.owner).get.isGroupAccount){ - addCollaborator(repository.owner, repository.name, form.userName) - } - redirect(s"/${repository.owner}/${repository.name}/settings/collaborators") - }) - - /** - * Add the collaborator. - */ - get("/:owner/:repository/settings/collaborators/remove")(ownerOnly { repository => - if(!getAccountByUserName(repository.owner).get.isGroupAccount){ - removeCollaborator(repository.owner, repository.name, params("name")) + post("/:owner/:repository/settings/collaborators")(ownerOnly { repository => + val collaborators = params("collaborators") + removeCollaborators(repository.owner, repository.name) + collaborators.split(",").withFilter(_.nonEmpty).map { collaborator => + val userName :: role :: Nil = collaborator.split(":").toList + addCollaborator(repository.owner, repository.name, userName, role) } redirect(s"/${repository.owner}/${repository.name}/settings/collaborators") }) @@ -138,14 +189,23 @@ * Display the web hook page. */ get("/:owner/:repository/settings/hooks")(ownerOnly { repository => - html.hooks(getWebHookURLs(repository.owner, repository.name), flash.get("url"), repository, flash.get("info")) + html.hooks(getWebHooks(repository.owner, repository.name), repository, flash.get("info")) + }) + + /** + * Display the web hook edit page. + */ + get("/:owner/:repository/settings/hooks/new")(ownerOnly { repository => + val webhook = WebHook(repository.owner, repository.name, "", WebHookContentType.FORM, None) + html.edithooks(webhook, Set(WebHook.Push), repository, flash.get("info"), true) }) /** * Add the web hook URL. */ - post("/:owner/:repository/settings/hooks/add", webHookForm)(ownerOnly { (form, repository) => - addWebHookURL(repository.owner, repository.name, form.url) + post("/:owner/:repository/settings/hooks/new", webHookForm(false))(ownerOnly { (form, repository) => + addWebHook(repository.owner, repository.name, form.url, form.events, form.ctype, form.token) + flash += "info" -> s"Webhook ${form.url} created" redirect(s"/${repository.owner}/${repository.name}/settings/hooks") }) @@ -153,30 +213,89 @@ * Delete the web hook URL. */ get("/:owner/:repository/settings/hooks/delete")(ownerOnly { repository => - deleteWebHookURL(repository.owner, repository.name, params("url")) + deleteWebHook(repository.owner, repository.name, params("url")) + flash += "info" -> s"Webhook ${params("url")} deleted" redirect(s"/${repository.owner}/${repository.name}/settings/hooks") }) /** * Send the test request to registered web hook URLs. */ - post("/:owner/:repository/settings/hooks/test", webHookForm)(ownerOnly { (form, repository) => + ajaxPost("/:owner/:repository/settings/hooks/test")(ownerOnly { repository => + def _headers(h: Array[org.apache.http.Header]): Array[Array[String]] = h.map { h => Array(h.getName, h.getValue) } + using(Git.open(getRepositoryDir(repository.owner, repository.name))){ git => import scala.collection.JavaConverters._ - val commits = if(repository.commitCount == 0) List.empty else git.log - .add(git.getRepository.resolve(repository.repository.defaultBranch)) - .setMaxCount(3) - .call.iterator.asScala.map(new CommitInfo(_)) + import scala.concurrent.duration._ + import scala.concurrent._ + import scala.util.control.NonFatal + import org.apache.http.util.EntityUtils + import scala.concurrent.ExecutionContext.Implicits.global - getAccountByUserName(repository.owner).foreach { ownerAccount => - callWebHook("push", - List(WebHook(repository.owner, repository.name, form.url)), - WebHookPushPayload(git, ownerAccount, "refs/heads/" + repository.repository.defaultBranch, repository, commits.toList, ownerAccount) + val url = params("url") + val token = Some(params("token")) + val ctype = WebHookContentType.valueOf(params("ctype")) + val dummyWebHookInfo = WebHook(repository.owner, repository.name, url, ctype, token) + val dummyPayload = { + val ownerAccount = getAccountByUserName(repository.owner).get + val commits = if(JGitUtil.isEmpty(git)) List.empty else git.log + .add(git.getRepository.resolve(repository.repository.defaultBranch)) + .setMaxCount(4) + .call.iterator.asScala.map(new CommitInfo(_)).toList + val pushedCommit = commits.drop(1) + + WebHookPushPayload( + git = git, + sender = ownerAccount, + refName = "refs/heads/" + repository.repository.defaultBranch, + repositoryInfo = repository, + commits = pushedCommit, + repositoryOwner = ownerAccount, + oldId = commits.lastOption.map(_.id).map(ObjectId.fromString).getOrElse(ObjectId.zeroId()), + newId = commits.headOption.map(_.id).map(ObjectId.fromString).getOrElse(ObjectId.zeroId()) ) } - flash += "url" -> form.url - flash += "info" -> "Test payload deployed!" + + val (webHook, json, reqFuture, resFuture) = callWebHook(WebHook.Push, List(dummyWebHookInfo), dummyPayload).head + + val toErrorMap: PartialFunction[Throwable, Map[String,String]] = { + case e: java.net.UnknownHostException => Map("error"-> ("Unknown host " + e.getMessage)) + case e: java.lang.IllegalArgumentException => Map("error"-> ("invalid url")) + case e: org.apache.http.client.ClientProtocolException => Map("error"-> ("invalid url")) + case NonFatal(e) => Map("error"-> (e.getClass + " "+ e.getMessage)) + } + + contentType = formats("json") + org.json4s.jackson.Serialization.write(Map( + "url" -> url, + "request" -> Await.result(reqFuture.map(req => Map( + "headers" -> _headers(req.getAllHeaders), + "payload" -> json + )).recover(toErrorMap), 20 seconds), + "responce" -> Await.result(resFuture.map(res => Map( + "status" -> res.getStatusLine(), + "body" -> EntityUtils.toString(res.getEntity()), + "headers" -> _headers(res.getAllHeaders()) + )).recover(toErrorMap), 20 seconds) + )) } + }) + + /** + * Display the web hook edit page. + */ + get("/:owner/:repository/settings/hooks/edit")(ownerOnly { repository => + getWebHook(repository.owner, repository.name, params("url")).map{ case (webhook, events) => + html.edithooks(webhook, events, repository, flash.get("info"), false) + } getOrElse NotFound() + }) + + /** + * Update web hook settings. + */ + post("/:owner/:repository/settings/hooks/edit", webHookForm(true))(ownerOnly { (form, repository) => + updateWebHook(repository.owner, repository.name, form.url, form.events, form.ctype, form.token) + flash += "info" -> s"webhook ${form.url} updated" redirect(s"/${repository.owner}/${repository.name}/settings/hooks") }) @@ -184,7 +303,7 @@ * Display the danger zone. */ get("/:owner/:repository/settings/danger")(ownerOnly { - html.danger(_) + html.danger(_, flash.get("info")) }) /** @@ -224,28 +343,62 @@ }) /** - * Provides duplication check for web hook url. + * Run GC */ - private def webHook: Constraint = new Constraint(){ - override def validate(name: String, value: String, messages: Messages): Option[String] = - getWebHookURLs(params("owner"), params("repository")).map(_.url).find(_ == value).map(_ => "URL had been registered already.") - } + post("/:owner/:repository/settings/gc")(ownerOnly { repository => + LockUtil.lock(s"${repository.owner}/${repository.name}") { + using(Git.open(getRepositoryDir(repository.owner, repository.name))) { git => + git.gc(); + } + } + flash += "info" -> "Garbage collection has been executed." + redirect(s"/${repository.owner}/${repository.name}/settings/danger") + }) /** - * Provides Constraint to validate the collaborator name. + * Provides duplication check for web hook url. */ - private def collaborator: Constraint = new Constraint(){ + private def webHook(needExists: Boolean): Constraint = new Constraint(){ override def validate(name: String, value: String, messages: Messages): Option[String] = - getAccountByUserName(value) match { - case None => Some("User does not exist.") - case Some(x) if(x.isGroupAccount) - => Some("User does not exist.") - case Some(x) if(x.userName == params("owner") || getCollaborators(params("owner"), params("repository")).contains(x.userName)) - => Some("User can access this repository already.") - case _ => None + if(getWebHook(params("owner"), params("repository"), value).isDefined != needExists){ + Some(if(needExists){ + "URL had not been registered yet." + } else { + "URL had been registered already." + }) + } else { + None } } + private def webhookEvents = new ValueType[Set[WebHook.Event]]{ + def convert(name: String, params: Map[String, String], messages: Messages): Set[WebHook.Event] = { + WebHook.Event.values.flatMap { t => + params.get(name + "." + t.name).map(_ => t) + }.toSet + } + def validate(name: String, params: Map[String, String], messages: Messages): Seq[(String, String)] = if(convert(name,params,messages).isEmpty){ + Seq(name -> messages("error.required").format(name)) + } else { + Nil + } + } + +// /** +// * Provides Constraint to validate the collaborator name. +// */ +// private def collaborator: Constraint = new Constraint(){ +// override def validate(name: String, value: String, messages: Messages): Option[String] = +// getAccountByUserName(value) match { +// case None => Some("User does not exist.") +//// case Some(x) if(x.isGroupAccount) +//// => Some("User does not exist.") +// case Some(x) if(x.userName == params("owner") || getCollaborators(params("owner"), params("repository")).contains(x.userName)) +// => Some(value + " is repository owner.") // TODO also group members? +// case _ => None +// } +// } + /** * Duplicate check for the rename repository name. */ @@ -259,6 +412,15 @@ } /** + * + */ + private def featureOption: Constraint = new Constraint(){ + override def validate(name: String, value: String, params: Map[String, String], messages: Messages): Option[String] = + if(Seq("DISABLE", "PRIVATE", "PUBLIC", "ALL").contains(value)) None else Some("Option is invalid.") + } + + + /** * Provides Constraint to validate the repository transfer user. */ private def transferUser: Constraint = new Constraint(){ diff --git a/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala b/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala index 75e1dee..b21b03f 100644 --- a/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala +++ b/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala @@ -1,6 +1,8 @@ package gitbucket.core.controller -import gitbucket.core.api._ +import java.io.FileInputStream +import javax.servlet.http.{HttpServletRequest, HttpServletResponse} + import gitbucket.core.plugin.PluginRegistry import gitbucket.core.repo.html import gitbucket.core.helper @@ -11,17 +13,16 @@ import gitbucket.core.util.ControlUtil._ import gitbucket.core.util.Implicits._ import gitbucket.core.util.Directory._ -import gitbucket.core.model.{Account, CommitState} -import gitbucket.core.service.CommitStatusService +import gitbucket.core.model.{Account, WebHook} import gitbucket.core.service.WebHookService._ import gitbucket.core.view import gitbucket.core.view.helpers - -import jp.sf.amateras.scalatra.forms._ -import org.apache.commons.io.FileUtils +import io.github.gitbucket.scalatra.forms._ +import org.apache.commons.io.IOUtils import org.eclipse.jgit.api.{ArchiveCommand, Git} import org.eclipse.jgit.archive.{TgzFormat, ZipFormat} import org.eclipse.jgit.dircache.DirCache +import org.eclipse.jgit.errors.MissingObjectException import org.eclipse.jgit.lib._ import org.eclipse.jgit.revwalk.RevCommit import org.eclipse.jgit.treewalk._ @@ -30,16 +31,16 @@ class RepositoryViewerController extends RepositoryViewerControllerBase with RepositoryService with AccountService with ActivityService with IssuesService with WebHookService with CommitsService - with ReadableUsersAuthenticator with ReferrerAuthenticator with CollaboratorsAuthenticator with PullRequestService with CommitStatusService - with WebHookPullRequestService + with ReadableUsersAuthenticator with ReferrerAuthenticator with WritableUsersAuthenticator with PullRequestService with CommitStatusService + with WebHookPullRequestService with WebHookPullRequestReviewCommentService with ProtectedBranchService /** * The repository viewer. */ trait RepositoryViewerControllerBase extends ControllerBase { self: RepositoryService with AccountService with ActivityService with IssuesService with WebHookService with CommitsService - with ReadableUsersAuthenticator with ReferrerAuthenticator with CollaboratorsAuthenticator with PullRequestService with CommitStatusService - with WebHookPullRequestService => + with ReadableUsersAuthenticator with ReferrerAuthenticator with WritableUsersAuthenticator with PullRequestService with CommitStatusService + with WebHookPullRequestService with WebHookPullRequestReviewCommentService with ProtectedBranchService => ArchiveCommand.registerFormat("zip", new ZipFormat) ArchiveCommand.registerFormat("tar.gz", new TgzFormat) @@ -101,32 +102,47 @@ */ post("/:owner/:repository/_preview")(referrersOnly { repository => contentType = "text/html" - helpers.markdown(params("content"), repository, - params("enableWikiLink").toBoolean, - params("enableRefsLink").toBoolean, - params("enableTaskList").toBoolean, - hasWritePermission(repository.owner, repository.name, context.loginAccount)) + val filename = params.get("filename") + filename match { + case Some(f) => helpers.renderMarkup( + filePath = List(f), + fileContent = params("content"), + branch = "master", + repository = repository, + enableWikiLink = params("enableWikiLink").toBoolean, + enableRefsLink = params("enableRefsLink").toBoolean, + enableAnchor = false + ) + case None => helpers.markdown( + markdown = params("content"), + repository = repository, + enableWikiLink = params("enableWikiLink").toBoolean, + enableRefsLink = params("enableRefsLink").toBoolean, + enableLineBreaks = params("enableLineBreaks").toBoolean, + enableTaskList = params("enableTaskList").toBoolean, + enableAnchor = false, + hasWritePermission = hasDeveloperRole(repository.owner, repository.name, context.loginAccount) + ) + } }) /** * Displays the file list of the repository root and the default branch. */ - get("/:owner/:repository")(referrersOnly { - fileList(_) - }) - - /** - * https://developer.github.com/v3/repos/#get - */ - get("/api/v3/repos/:owner/:repository")(referrersOnly { repository => - JsonFormat(ApiRepository(repository, ApiUser(getAccountByUserName(repository.owner).get))) - }) + get("/:owner/:repository") { + params.get("go-get") match { + case Some("1") => defining(request.paths){ paths => + getRepository(paths(0), paths(1)).map(gitbucket.core.html.goget(_))getOrElse NotFound() + } + case _ => referrersOnly(fileList(_)) + } + } /** * Displays the file list of the specified path and branch. */ get("/:owner/:repository/tree/*")(referrersOnly { repository => - val (id, path) = splitPath(repository, multiParams("splat").head) + val (id, path) = repository.splitPath(multiParams("splat").head) if(path.isEmpty){ fileList(repository, id) } else { @@ -138,7 +154,7 @@ * Displays the commit list of the specified resource. */ get("/:owner/:repository/commits/*")(referrersOnly { repository => - val (branchName, path) = splitPath(repository, multiParams("splat").head) + val (branchName, path) = repository.splitPath(multiParams("splat").head) val page = params.get("page").flatMap(_.toIntOpt).getOrElse(1) using(Git.open(getRepositoryDir(repository.owner, repository.name))){ git => @@ -147,70 +163,23 @@ html.commits(if(path.isEmpty) Nil else path.split("/").toList, branchName, repository, logs.splitWith{ (commit1, commit2) => view.helpers.date(commit1.commitTime) == view.helpers.date(commit2.commitTime) - }, page, hasNext, hasWritePermission(repository.owner, repository.name, context.loginAccount)) - case Left(_) => NotFound + }, page, hasNext, hasDeveloperRole(repository.owner, repository.name, context.loginAccount)) + case Left(_) => NotFound() } } }) - /** - * https://developer.github.com/v3/repos/statuses/#create-a-status - */ - post("/api/v3/repos/:owner/:repo/statuses/:sha")(collaboratorsOnly { repository => - (for{ - ref <- params.get("sha") - sha <- JGitUtil.getShaByRef(repository.owner, repository.name, ref) - data <- extractFromJsonBody[CreateAStatus] if data.isValid - creator <- context.loginAccount - state <- CommitState.valueOf(data.state) - statusId = createCommitStatus(repository.owner, repository.name, sha, data.context.getOrElse("default"), - state, data.target_url, data.description, new java.util.Date(), creator) - status <- getCommitStatus(repository.owner, repository.name, statusId) - } yield { - JsonFormat(ApiCommitStatus(status, ApiUser(creator))) - }) getOrElse NotFound - }) - - /** - * https://developer.github.com/v3/repos/statuses/#list-statuses-for-a-specific-ref - * - * ref is Ref to list the statuses from. It can be a SHA, a branch name, or a tag name. - */ - get("/api/v3/repos/:owner/:repo/commits/:ref/statuses")(referrersOnly { repository => - (for{ - ref <- params.get("ref") - sha <- JGitUtil.getShaByRef(repository.owner, repository.name, ref) - } yield { - JsonFormat(getCommitStatuesWithCreator(repository.owner, repository.name, sha).map{ case(status, creator) => - ApiCommitStatus(status, ApiUser(creator)) - }) - }) getOrElse NotFound - }) - - /** - * https://developer.github.com/v3/repos/statuses/#get-the-combined-status-for-a-specific-ref - * - * ref is Ref to list the statuses from. It can be a SHA, a branch name, or a tag name. - */ - get("/api/v3/repos/:owner/:repo/commits/:ref/status")(referrersOnly { repository => - (for{ - ref <- params.get("ref") - owner <- getAccountByUserName(repository.owner) - sha <- JGitUtil.getShaByRef(repository.owner, repository.name, ref) - } yield { - val statuses = getCommitStatuesWithCreator(repository.owner, repository.name, sha) - JsonFormat(ApiCombinedCommitStatus(sha, statuses, ApiRepository(repository, owner))) - }) getOrElse NotFound - }) - - get("/:owner/:repository/new/*")(collaboratorsOnly { repository => - val (branch, path) = splitPath(repository, multiParams("splat").head) + get("/:owner/:repository/new/*")(writableUsersOnly { repository => + val (branch, path) = repository.splitPath(multiParams("splat").head) + val protectedBranch = getProtectedBranchInfo(repository.owner, repository.name, branch).needStatusCheck(context.loginAccount.get.userName) html.editor(branch, repository, if(path.length == 0) Nil else path.split("/").toList, - None, JGitUtil.ContentInfo("text", None, Some("UTF-8"))) + None, JGitUtil.ContentInfo("text", None, Some("UTF-8")), + protectedBranch) }) - get("/:owner/:repository/edit/*")(collaboratorsOnly { repository => - val (branch, path) = splitPath(repository, multiParams("splat").head) + get("/:owner/:repository/edit/*")(writableUsersOnly { repository => + val (branch, path) = repository.splitPath(multiParams("splat").head) + val protectedBranch = getProtectedBranchInfo(repository.owner, repository.name, branch).needStatusCheck(context.loginAccount.get.userName) using(Git.open(getRepositoryDir(repository.owner, repository.name))){ git => val revCommit = JGitUtil.getRevCommitFromId(git, git.getRepository.resolve(branch)) @@ -218,13 +187,14 @@ getPathObjectId(git, path, revCommit).map { objectId => val paths = path.split("/") html.editor(branch, repository, paths.take(paths.size - 1).toList, Some(paths.last), - JGitUtil.getContentInfo(git, path, objectId)) - } getOrElse NotFound + JGitUtil.getContentInfo(git, path, objectId), + protectedBranch) + } getOrElse NotFound() } }) - get("/:owner/:repository/remove/*")(collaboratorsOnly { repository => - val (branch, path) = splitPath(repository, multiParams("splat").head) + get("/:owner/:repository/remove/*")(writableUsersOnly { repository => + val (branch, path) = repository.splitPath(multiParams("splat").head) using(Git.open(getRepositoryDir(repository.owner, repository.name))){ git => val revCommit = JGitUtil.getRevCommitFromId(git, git.getRepository.resolve(branch)) @@ -232,11 +202,11 @@ val paths = path.split("/") html.delete(branch, repository, paths.take(paths.size - 1).toList, paths.last, JGitUtil.getContentInfo(git, path, objectId)) - } getOrElse NotFound + } getOrElse NotFound() } }) - post("/:owner/:repository/create", editorForm)(collaboratorsOnly { (form, repository) => + post("/:owner/:repository/create", editorForm)(writableUsersOnly { (form, repository) => commitFile( repository = repository, branch = form.branch, @@ -249,11 +219,11 @@ ) redirect(s"/${repository.owner}/${repository.name}/blob/${form.branch}/${ - if(form.path.length == 0) form.newFileName else s"${form.path}/${form.newFileName}" + if(form.path.length == 0) urlEncode(form.newFileName) else s"${form.path}/${urlEncode(form.newFileName)}" }") }) - post("/:owner/:repository/update", editorForm)(collaboratorsOnly { (form, repository) => + post("/:owner/:repository/update", editorForm)(writableUsersOnly { (form, repository) => commitFile( repository = repository, branch = form.branch, @@ -270,42 +240,95 @@ ) redirect(s"/${repository.owner}/${repository.name}/blob/${form.branch}/${ - if(form.path.length == 0) form.newFileName else s"${form.path}/${form.newFileName}" + if(form.path.length == 0) urlEncode(form.newFileName) else s"${form.path}/${urlEncode(form.newFileName)}" }") }) - post("/:owner/:repository/remove", deleteForm)(collaboratorsOnly { (form, repository) => + post("/:owner/:repository/remove", deleteForm)(writableUsersOnly { (form, repository) => commitFile(repository, form.branch, form.path, None, Some(form.fileName), "", "", form.message.getOrElse(s"Delete ${form.fileName}")) redirect(s"/${repository.owner}/${repository.name}/tree/${form.branch}${if(form.path.length == 0) "" else form.path}") }) + get("/:owner/:repository/raw/*")(referrersOnly { repository => + val (id, path) = repository.splitPath(multiParams("splat").head) + using(Git.open(getRepositoryDir(repository.owner, repository.name))){ git => + val revCommit = JGitUtil.getRevCommitFromId(git, git.getRepository.resolve(id)) + + getPathObjectId(git, path, revCommit).map { objectId => + responseRawFile(git, objectId, path, repository) + } getOrElse NotFound() + } + }) + /** * Displays the file content of the specified branch or commit. */ val blobRoute = get("/:owner/:repository/blob/*")(referrersOnly { repository => - val (id, path) = splitPath(repository, multiParams("splat").head) + val (id, path) = repository.splitPath(multiParams("splat").head) val raw = params.get("raw").getOrElse("false").toBoolean using(Git.open(getRepositoryDir(repository.owner, repository.name))){ git => val revCommit = JGitUtil.getRevCommitFromId(git, git.getRepository.resolve(id)) getPathObjectId(git, path, revCommit).map { objectId => if(raw){ - // Download - JGitUtil.getContentFromId(git, objectId, true).map { bytes => - RawData("application/octet-stream", bytes) - } getOrElse NotFound + // Download (This route is left for backword compatibility) + responseRawFile(git, objectId, path, repository) } else { html.blob(id, repository, path.split("/").toList, JGitUtil.getContentInfo(git, path, objectId), new JGitUtil.CommitInfo(JGitUtil.getLastModifiedCommit(git, revCommit, path)), - hasWritePermission(repository.owner, repository.name, context.loginAccount), - request.paths(2) == "blame") + hasDeveloperRole(repository.owner, repository.name, context.loginAccount), + request.paths(2) == "blame", + isLfsFile(git, objectId)) } - } getOrElse NotFound + } getOrElse NotFound() } }) + private def isLfsFile(git: Git, objectId: ObjectId): Boolean = { + JGitUtil.getObjectLoaderFromId(git, objectId){ loader => + if(loader.isLarge){ + false + } else { + new String(loader.getCachedBytes, "UTF-8").startsWith("version https://git-lfs.github.com/spec/v1") + } + }.getOrElse(false) + } + + private def responseRawFile(git: Git, objectId: ObjectId, path: String, + repository: RepositoryService.RepositoryInfo): Unit = { + JGitUtil.getObjectLoaderFromId(git, objectId){ loader => + contentType = FileUtil.getMimeType(path) + + if(loader.isLarge){ + response.setContentLength(loader.getSize.toInt) + loader.copyTo(response.outputStream) + } else { + val bytes = loader.getCachedBytes + val text = new String(bytes, "UTF-8") + + if(text.startsWith("version https://git-lfs.github.com/spec/v1")){ + // LFS objects + val attrs = text.split("\n").map { line => + val dim = line.split(" ") + dim(0) -> dim(1) + }.toMap + + response.setContentLength(attrs("size").toInt) + val oid = attrs("oid").split(":")(1) + + using(new FileInputStream(FileUtil.getLfsFilePath(repository.owner, repository.name, oid))){ in => + IOUtils.copy(in, response.getOutputStream) + } + } else { + response.setContentLength(loader.getSize.toInt) + response.getOutputStream.write(bytes) + } + } + } + } + get("/:owner/:repository/blame/*"){ blobRoute.action() } @@ -314,7 +337,7 @@ * Blame data. */ ajaxGet("/:owner/:repository/get-blame/*")(referrersOnly { repository => - val (id, path) = splitPath(repository, multiParams("splat").head) + val (id, path) = repository.splitPath(multiParams("splat").head) contentType = formats("json") using(Git.open(getRepositoryDir(repository.owner, repository.name))){ git => val last = git.log.add(git.getRepository.resolve(id)).addPath(path).setMaxCount(1).call.iterator.next.name @@ -344,23 +367,28 @@ get("/:owner/:repository/commit/:id")(referrersOnly { repository => val id = params("id") - using(Git.open(getRepositoryDir(repository.owner, repository.name))){ git => - defining(JGitUtil.getRevCommitFromId(git, git.getRepository.resolve(id))){ revCommit => - JGitUtil.getDiffs(git, id) match { case (diffs, oldCommitId) => - html.commit(id, new JGitUtil.CommitInfo(revCommit), - JGitUtil.getBranchesOfCommit(git, revCommit.getName), - JGitUtil.getTagsOfCommit(git, revCommit.getName), - getCommitComments(repository.owner, repository.name, id, false), - repository, diffs, oldCommitId, hasWritePermission(repository.owner, repository.name, context.loginAccount)) + try { + using(Git.open(getRepositoryDir(repository.owner, repository.name))) { git => + defining(JGitUtil.getRevCommitFromId(git, git.getRepository.resolve(id))) { revCommit => + JGitUtil.getDiffs(git, id) match { + case (diffs, oldCommitId) => + html.commit(id, new JGitUtil.CommitInfo(revCommit), + JGitUtil.getBranchesOfCommit(git, revCommit.getName), + JGitUtil.getTagsOfCommit(git, revCommit.getName), + getCommitComments(repository.owner, repository.name, id, true), + repository, diffs, oldCommitId, hasDeveloperRole(repository.owner, repository.name, context.loginAccount)) + } } } + } catch { + case e:MissingObjectException => NotFound() } }) post("/:owner/:repository/commit/:id/comment/new", commentForm)(readableUsersOnly { (form, repository) => val id = params("id") createCommitComment(repository.owner, repository.name, id, context.loginAccount.get.userName, form.content, - form.fileName, form.oldLineNumber, form.newLineNumber, form.issueId.isDefined) + form.fileName, form.oldLineNumber, form.newLineNumber, form.issueId) form.issueId match { case Some(issueId) => recordCommentPullRequestActivity(repository.owner, repository.name, context.loginAccount.get.userName, issueId, form.content) case None => recordCommentCommitActivity(repository.owner, repository.name, context.loginAccount.get.userName, id, form.content) @@ -377,7 +405,7 @@ html.commentform( commitId = id, fileName, oldLineNumber, newLineNumber, issueId, - hasWritePermission = hasWritePermission(repository.owner, repository.name, context.loginAccount), + hasWritePermission = hasDeveloperRole(repository.owner, repository.name, context.loginAccount), repository = repository ) }) @@ -385,30 +413,39 @@ ajaxPost("/:owner/:repository/commit/:id/comment/_data/new", commentForm)(readableUsersOnly { (form, repository) => val id = params("id") val commentId = createCommitComment(repository.owner, repository.name, id, context.loginAccount.get.userName, - form.content, form.fileName, form.oldLineNumber, form.newLineNumber, form.issueId.isDefined) + form.content, form.fileName, form.oldLineNumber, form.newLineNumber, form.issueId) + val comment = getCommitComment(repository.owner, repository.name, commentId.toString).get form.issueId match { - case Some(issueId) => recordCommentPullRequestActivity(repository.owner, repository.name, context.loginAccount.get.userName, issueId, form.content) + case Some(issueId) => + recordCommentPullRequestActivity(repository.owner, repository.name, context.loginAccount.get.userName, issueId, form.content) + callPullRequestReviewCommentWebHook("create", comment, repository, issueId, context.baseUrl, context.loginAccount.get) case None => recordCommentCommitActivity(repository.owner, repository.name, context.loginAccount.get.userName, id, form.content) } - helper.html.commitcomment(getCommitComment(repository.owner, repository.name, commentId.toString).get, - hasWritePermission(repository.owner, repository.name, context.loginAccount), repository) + helper.html.commitcomment(comment, hasDeveloperRole(repository.owner, repository.name, context.loginAccount), repository) }) ajaxGet("/:owner/:repository/commit_comments/_data/:id")(readableUsersOnly { repository => getCommitComment(repository.owner, repository.name, params("id")) map { x => if(isEditable(x.userName, x.repositoryName, x.commentedUserName)){ params.get("dataType") collect { - case t if t == "html" => html.editcomment( - x.content, x.commentId, x.userName, x.repositoryName) + case t if t == "html" => html.editcomment(x.content, x.commentId, repository) } getOrElse { contentType = formats("json") org.json4s.jackson.Serialization.write( - Map("content" -> view.Markdown.toHtml(x.content, - repository, false, true, true, isEditable(x.userName, x.repositoryName, x.commentedUserName)) + Map( + "content" -> view.Markdown.toHtml( + markdown = x.content, + repository = repository, + enableWikiLink = false, + enableRefsLink = true, + enableAnchor = true, + enableLineBreaks = true, + hasWritePermission = true + ) )) } - } else Unauthorized - } getOrElse NotFound + } else Unauthorized() + } getOrElse NotFound() }) ajaxPost("/:owner/:repository/commit_comments/edit/:id", commentForm)(readableUsersOnly { (form, repository) => @@ -417,8 +454,8 @@ if(isEditable(owner, name, comment.commentedUserName)){ updateCommitComment(comment.commentId, form.content) redirect(s"/${owner}/${name}/commit_comments/_data/${comment.commentId}") - } else Unauthorized - } getOrElse NotFound + } else Unauthorized() + } getOrElse NotFound() } }) @@ -427,8 +464,8 @@ getCommitComment(owner, name, params("id")).map { comment => if(isEditable(owner, name, comment.commentedUserName)){ Ok(deleteCommitComment(comment.commentId)) - } else Unauthorized - } getOrElse NotFound + } else Unauthorized() + } getOrElse NotFound() } }) @@ -436,17 +473,24 @@ * Displays branches. */ get("/:owner/:repository/branches")(referrersOnly { repository => - val branches = JGitUtil.getBranches(repository.owner, repository.name, repository.repository.defaultBranch) - .sortBy(br => (br.mergeInfo.isEmpty, br.commitTime)) - .map(br => br -> getPullRequestByRequestCommit(repository.owner, repository.name, repository.repository.defaultBranch, br.name, br.commitId)) - .reverse - html.branches(branches, hasWritePermission(repository.owner, repository.name, context.loginAccount), repository) + val protectedBranches = getProtectedBranchList(repository.owner, repository.name).toSet + val branches = JGitUtil.getBranches( + owner = repository.owner, + name = repository.name, + defaultBranch = repository.repository.defaultBranch, + origin = repository.repository.originUserName.isEmpty + ) + .sortBy(br => (br.mergeInfo.isEmpty, br.commitTime)) + .map(br => (br, getPullRequestByRequestCommit(repository.owner, repository.name, repository.repository.defaultBranch, br.name, br.commitId), protectedBranches.contains(br.name))) + .reverse + + html.branches(branches, hasDeveloperRole(repository.owner, repository.name, context.loginAccount), repository) }) /** * Creates a branch. */ - post("/:owner/:repository/branches")(collaboratorsOnly { repository => + post("/:owner/:repository/branches")(writableUsersOnly { repository => val newBranchName = params.getOrElse("new", halt(400)) val fromBranchName = params.getOrElse("from", halt(400)) using(Git.open(getRepositoryDir(repository.owner, repository.name))){ git => @@ -464,7 +508,7 @@ /** * Deletes branch. */ - get("/:owner/:repository/delete/*")(collaboratorsOnly { repository => + get("/:owner/:repository/delete/*")(writableUsersOnly { repository => val branchName = multiParams("splat").head val userName = context.loginAccount.get.userName if(repository.repository.defaultBranch != branchName){ @@ -492,36 +536,36 @@ archiveRepository(name, ".zip", repository) case name if name.endsWith(".tar.gz") => archiveRepository(name, ".tar.gz", repository) - case _ => BadRequest + case _ => BadRequest() } }) get("/:owner/:repository/network/members")(referrersOnly { repository => - html.forked( - getRepository( - repository.repository.originUserName.getOrElse(repository.owner), - repository.repository.originRepositoryName.getOrElse(repository.name), - context.baseUrl), - getForkedRepositories( - repository.repository.originUserName.getOrElse(repository.owner), - repository.repository.originRepositoryName.getOrElse(repository.name)), - repository) + if(repository.repository.options.allowFork) { + html.forked( + getRepository( + repository.repository.originUserName.getOrElse(repository.owner), + repository.repository.originRepositoryName.getOrElse(repository.name)), + getForkedRepositories( + repository.repository.originUserName.getOrElse(repository.owner), + repository.repository.originRepositoryName.getOrElse(repository.name)), + context.loginAccount match { + case None => List() + case account: Option[Account] => getGroupsByUserName(account.get.userName) + }, // groups of current user + repository) + } else BadRequest() }) /** * Displays the file find of branch. */ - get("/:owner/:repository/find/:ref")(referrersOnly { repository => + get("/:owner/:repository/find/*")(referrersOnly { repository => using(Git.open(getRepositoryDir(repository.owner, repository.name))){ git => - JGitUtil.getTreeId(git, params("ref")).map{ treeId => - html.find(params("ref"), - treeId, - repository, - context.loginAccount match { - case None => List() - case account: Option[Account] => getGroupsByUserName(account.get.userName) - }) - } getOrElse NotFound + val ref = multiParams("splat").head + JGitUtil.getTreeId(git, ref).map{ treeId => + html.find(ref, treeId, repository) + } getOrElse NotFound() } }) @@ -536,17 +580,6 @@ } }) - private def splitPath(repository: RepositoryService.RepositoryInfo, path: String): (String, String) = { - val id = repository.branchList.collectFirst { - case branch if(path == branch || path.startsWith(branch + "/")) => branch - } orElse repository.tags.collectFirst { - case tag if(path == tag.name || path.startsWith(tag.name + "/")) => tag.name - } getOrElse path.split("/")(0) - - (id, path.substring(id.length).stripPrefix("/")) - } - - private val readmeFiles = PluginRegistry().renderableExtensions.map { extension => s"readme.${extension}" } ++ Seq("readme.txt", "readme") @@ -560,10 +593,10 @@ * @return HTML of the file list */ private def fileList(repository: RepositoryService.RepositoryInfo, revstr: String = "", path: String = ".") = { - if(repository.commitCount == 0){ - html.guide(repository, hasWritePermission(repository.owner, repository.name, context.loginAccount)) - } else { - using(Git.open(getRepositoryDir(repository.owner, repository.name))){ git => + using(Git.open(getRepositoryDir(repository.owner, repository.name))){ git => + if(JGitUtil.isEmpty(git)){ + html.guide(repository, hasDeveloperRole(repository.owner, repository.name, context.loginAccount)) + } else { // get specified commit JGitUtil.getDefaultBranch(git, repository, revstr).map { case (objectId, revision) => defining(JGitUtil.getRevCommitFromId(git, objectId)) { revCommit => @@ -573,7 +606,7 @@ val parentPath = if (path == ".") Nil else path.split("/").toList // process README.md or README.markdown val readme = files.find { file => - readmeFiles.contains(file.name.toLowerCase) + !file.isDirectory && readmeFiles.contains(file.name.toLowerCase) }.map { file => val path = (file.name :: parentPath.reverse).reverse path -> StringUtil.convertFromByteArray(JGitUtil.getContentFromId( @@ -582,16 +615,17 @@ html.files(revision, repository, if(path == ".") Nil else path.split("/").toList, // current path - context.loginAccount match { - case None => List() - case account: Option[Account] => getGroupsByUserName(account.get.userName) - }, // groups of current user new JGitUtil.CommitInfo(lastModifiedCommit), // last modified commit - files, readme, hasWritePermission(repository.owner, repository.name, context.loginAccount), + JGitUtil.getCommitCount(repository.owner, repository.name, revision), + files, + readme, + hasDeveloperRole(repository.owner, repository.name, context.loginAccount), getPullRequestFromBranch(repository.owner, repository.name, revstr, repository.repository.defaultBranch), - flash.get("info"), flash.get("error")) + flash.get("info"), + flash.get("error") + ) } - } getOrElse NotFound + } getOrElse NotFound() } } } @@ -611,14 +645,18 @@ val headName = s"refs/heads/${branch}" val headTip = git.getRepository.resolve(headName) - JGitUtil.processTree(git, headTip){ (path, tree) => + val permission = JGitUtil.processTree(git, headTip){ (path, tree) => + // Add all entries except the editing file if(!newPath.exists(_ == path) && !oldPath.exists(_ == path)){ builder.add(JGitUtil.createDirCacheEntry(path, tree.getEntryFileMode, tree.getEntryObjectId)) } - } + // Retrieve permission if file exists to keep it + oldPath.collect { case x if x == path => tree.getEntryFileMode.getBits } + }.flatten.headOption newPath.foreach { newPath => - builder.add(JGitUtil.createDirCacheEntry(newPath, FileMode.REGULAR_FILE, + builder.add(JGitUtil.createDirCacheEntry(newPath, + permission.map { bits => FileMode.fromBits(bits) } getOrElse FileMode.REGULAR_FILE, inserter.insert(Constants.OBJ_BLOB, content.getBytes(charset)))) } builder.finish() @@ -627,7 +665,7 @@ headName, loginAccount.fullName, loginAccount.mailAddress, message) inserter.flush() - inserter.release() + inserter.close() // update refs val refUpdate = git.getRepository.updateRef(headName) @@ -641,8 +679,11 @@ updatePullRequests(repository.owner, repository.name, branch) // record activity - recordPushActivity(repository.owner, repository.name, loginAccount.userName, branch, - List(new CommitInfo(JGitUtil.getRevCommitFromId(git, commitId)))) + val commitInfo = new CommitInfo(JGitUtil.getRevCommitFromId(git, commitId)) + recordPushActivity(repository.owner, repository.name, loginAccount.userName, branch, List(commitInfo)) + + // create issue comment by commit message + createIssueComment(repository.owner, repository.name, commitInfo) // close issue by commit message closeIssuesFromMessage(message, loginAccount.userName, repository.owner, repository.name) @@ -650,9 +691,10 @@ // call web hook callPullRequestWebHookByRequestBranch("synchronize", repository, branch, context.baseUrl, loginAccount) val commit = new JGitUtil.CommitInfo(JGitUtil.getRevCommitFromId(git, commitId)) - callWebHookOf(repository.owner, repository.name, "push") { + callWebHookOf(repository.owner, repository.name, WebHook.Push) { getAccountByUserName(repository.owner).map{ ownerAccount => - WebHookPushPayload(git, loginAccount, headName, repository, List(commit), ownerAccount) + WebHookPushPayload(git, loginAccount, headName, repository, List(commit), ownerAccount, + oldId = headTip, newId = commitId) } } } @@ -676,11 +718,6 @@ private def archiveRepository(name: String, suffix: String, repository: RepositoryService.RepositoryInfo): Unit = { val revision = name.stripSuffix(suffix) - val workDir = getDownloadWorkDir(repository.owner, repository.name, session.getId) - if(workDir.exists) { - FileUtils.deleteDirectory(workDir) - } - workDir.mkdirs val filename = repository.name + "-" + (if(revision.length == 40) revision.substring(0, 10) else revision).replace('/', '_') + suffix @@ -694,14 +731,17 @@ git.archive .setFormat(suffix.tail) - .setTree(revCommit.getTree) + .setTree(revCommit) .setOutputStream(response.getOutputStream) .call() - - Unit } } private def isEditable(owner: String, repository: String, author: String)(implicit context: Context): Boolean = - hasWritePermission(owner, repository, context.loginAccount) || author == context.loginAccount.get.userName + hasDeveloperRole(owner, repository, context.loginAccount) || author == context.loginAccount.get.userName + + override protected def renderUncaughtException(e: Throwable)(implicit request: HttpServletRequest, response: HttpServletResponse): Unit = { + e.printStackTrace() + } + } diff --git a/src/main/scala/gitbucket/core/controller/SearchController.scala b/src/main/scala/gitbucket/core/controller/SearchController.scala deleted file mode 100644 index 85f3907..0000000 --- a/src/main/scala/gitbucket/core/controller/SearchController.scala +++ /dev/null @@ -1,51 +0,0 @@ -package gitbucket.core.controller - -import gitbucket.core.search.html -import gitbucket.core.service._ -import gitbucket.core.util.{StringUtil, ControlUtil, ReferrerAuthenticator, Implicits} -import ControlUtil._ -import Implicits._ -import jp.sf.amateras.scalatra.forms._ - -class SearchController extends SearchControllerBase - with RepositoryService with AccountService with ActivityService with RepositorySearchService with IssuesService with ReferrerAuthenticator - -trait SearchControllerBase extends ControllerBase { self: RepositoryService - with ActivityService with RepositorySearchService with ReferrerAuthenticator => - - val searchForm = mapping( - "query" -> trim(text(required)), - "owner" -> trim(text(required)), - "repository" -> trim(text(required)) - )(SearchForm.apply) - - case class SearchForm(query: String, owner: String, repository: String) - - post("/search", searchForm){ form => - redirect(s"/${form.owner}/${form.repository}/search?q=${StringUtil.urlEncode(form.query)}") - } - - get("/:owner/:repository/search")(referrersOnly { repository => - defining(params("q").trim, params.getOrElse("type", "code")){ case (query, target) => - val page = try { - val i = params.getOrElse("page", "1").toInt - if(i <= 0) 1 else i - } catch { - case e: NumberFormatException => 1 - } - - target.toLowerCase match { - case "issue" => html.issues( - searchIssues(repository.owner, repository.name, query), - countFiles(repository.owner, repository.name, query), - query, page, repository) - - case _ => html.code( - searchFiles(repository.owner, repository.name, query), - countIssues(repository.owner, repository.name, query), - query, page, repository) - } - } - }) - -} diff --git a/src/main/scala/gitbucket/core/controller/SystemSettingsController.scala b/src/main/scala/gitbucket/core/controller/SystemSettingsController.scala index 1c1271d..e656aee 100644 --- a/src/main/scala/gitbucket/core/controller/SystemSettingsController.scala +++ b/src/main/scala/gitbucket/core/controller/SystemSettingsController.scala @@ -1,17 +1,26 @@ package gitbucket.core.controller +import java.io.FileInputStream + import gitbucket.core.admin.html -import gitbucket.core.service.{AccountService, SystemSettingsService} -import gitbucket.core.util.AdminAuthenticator +import gitbucket.core.service.{AccountService, RepositoryService, SystemSettingsService} +import gitbucket.core.util.{AdminAuthenticator, Mailer} import gitbucket.core.ssh.SshServer +import gitbucket.core.plugin.PluginRegistry import SystemSettingsService._ -import jp.sf.amateras.scalatra.forms._ +import gitbucket.core.util.Implicits._ +import gitbucket.core.util.ControlUtil._ +import gitbucket.core.util.Directory._ +import gitbucket.core.util.StringUtil._ +import io.github.gitbucket.scalatra.forms._ +import org.apache.commons.io.{FileUtils, IOUtils} +import org.scalatra.i18n.Messages class SystemSettingsController extends SystemSettingsControllerBase - with AccountService with AdminAuthenticator + with AccountService with RepositoryService with AdminAuthenticator -trait SystemSettingsControllerBase extends ControllerBase { - self: AccountService with AdminAuthenticator => +trait SystemSettingsControllerBase extends AccountManagementControllerBase { + self: AccountService with RepositoryService with AdminAuthenticator => private val form = mapping( "baseUrl" -> trim(label("Base URL", optional(text()))), @@ -23,13 +32,16 @@ "notification" -> trim(label("Notification", boolean())), "activityLogLimit" -> trim(label("Limit of activity logs", optional(number()))), "ssh" -> trim(label("SSH access", boolean())), + "sshHost" -> trim(label("SSH host", optional(text()))), "sshPort" -> trim(label("SSH port", optional(number()))), - "smtp" -> optionalIfNotChecked("notification", mapping( + "useSMTP" -> trim(label("SMTP", boolean())), + "smtp" -> optionalIfNotChecked("useSMTP", mapping( "host" -> trim(label("SMTP Host", text(required))), "port" -> trim(label("SMTP Port", optional(number()))), "user" -> trim(label("SMTP User", optional(text()))), "password" -> trim(label("SMTP Password", optional(text()))), "ssl" -> trim(label("Enable SSL", optional(boolean()))), + "starttls" -> trim(label("Enable STARTTLS", optional(boolean()))), "fromAddress" -> trim(label("FROM Address", optional(text()))), "fromName" -> trim(label("FROM Name", optional(text()))) )(Smtp.apply)), @@ -49,16 +61,89 @@ "keystore" -> trim(label("Keystore", optional(text()))) )(Ldap.apply)) )(SystemSettings.apply).verifying { settings => - if(settings.ssh && settings.baseUrl.isEmpty){ - Seq("baseUrl" -> "Base URL is required if SSH access is enabled.") - } else Nil + Vector( + if(settings.ssh && settings.baseUrl.isEmpty){ + Some("baseUrl" -> "Base URL is required if SSH access is enabled.") + } else None, + if(settings.ssh && settings.sshHost.isEmpty){ + Some("sshHost" -> "SSH host is required if SSH access is enabled.") + } else None + ).flatten } - private val pluginForm = mapping( - "pluginId" -> list(trim(label("", text()))) - )(PluginForm.apply) + private val sendMailForm = mapping( + "smtp" -> mapping( + "host" -> trim(label("SMTP Host", text(required))), + "port" -> trim(label("SMTP Port", optional(number()))), + "user" -> trim(label("SMTP User", optional(text()))), + "password" -> trim(label("SMTP Password", optional(text()))), + "ssl" -> trim(label("Enable SSL", optional(boolean()))), + "starttls" -> trim(label("Enable STARTTLS", optional(boolean()))), + "fromAddress" -> trim(label("FROM Address", optional(text()))), + "fromName" -> trim(label("FROM Name", optional(text()))) + )(Smtp.apply), + "testAddress" -> trim(label("", text(required))) + )(SendMailForm.apply) - case class PluginForm(pluginIds: List[String]) + case class SendMailForm(smtp: Smtp, testAddress: String) + + case class DataExportForm(tableNames: List[String]) + + case class NewUserForm(userName: String, password: String, fullName: String, + mailAddress: String, isAdmin: Boolean, + url: Option[String], fileId: Option[String]) + + case class EditUserForm(userName: String, password: Option[String], fullName: String, + mailAddress: String, isAdmin: Boolean, url: Option[String], + fileId: Option[String], clearImage: Boolean, isRemoved: Boolean) + + case class NewGroupForm(groupName: String, groupDescription: Option[String], url: Option[String], fileId: Option[String], + members: String) + + case class EditGroupForm(groupName: String, groupDescription: Option[String], url: Option[String], fileId: Option[String], + members: String, clearImage: Boolean, isRemoved: Boolean) + + + val newUserForm = mapping( + "userName" -> trim(label("Username" ,text(required, maxlength(100), identifier, uniqueUserName, reservedNames))), + "password" -> trim(label("Password" ,text(required, maxlength(20)))), + "fullName" -> trim(label("Full Name" ,text(required, maxlength(100)))), + "mailAddress" -> trim(label("Mail Address" ,text(required, maxlength(100), uniqueMailAddress()))), + "isAdmin" -> trim(label("User Type" ,boolean())), + "url" -> trim(label("URL" ,optional(text(maxlength(200))))), + "fileId" -> trim(label("File ID" ,optional(text()))) + )(NewUserForm.apply) + + val editUserForm = mapping( + "userName" -> trim(label("Username" ,text(required, maxlength(100), identifier))), + "password" -> trim(label("Password" ,optional(text(maxlength(20))))), + "fullName" -> trim(label("Full Name" ,text(required, maxlength(100)))), + "mailAddress" -> trim(label("Mail Address" ,text(required, maxlength(100), uniqueMailAddress("userName")))), + "isAdmin" -> trim(label("User Type" ,boolean())), + "url" -> trim(label("URL" ,optional(text(maxlength(200))))), + "fileId" -> trim(label("File ID" ,optional(text()))), + "clearImage" -> trim(label("Clear image" ,boolean())), + "removed" -> trim(label("Disable" ,boolean(disableByNotYourself("userName")))) + )(EditUserForm.apply) + + val newGroupForm = mapping( + "groupName" -> trim(label("Group name" ,text(required, maxlength(100), identifier, uniqueUserName, reservedNames))), + "groupDescription" -> trim(label("Group description", optional(text()))), + "url" -> trim(label("URL" ,optional(text(maxlength(200))))), + "fileId" -> trim(label("File ID" ,optional(text()))), + "members" -> trim(label("Members" ,text(required, members))) + )(NewGroupForm.apply) + + val editGroupForm = mapping( + "groupName" -> trim(label("Group name" ,text(required, maxlength(100), identifier))), + "groupDescription" -> trim(label("Group description", optional(text()))), + "url" -> trim(label("URL" ,optional(text(maxlength(200))))), + "fileId" -> trim(label("File ID" ,optional(text()))), + "members" -> trim(label("Members" ,text(required, members))), + "clearImage" -> trim(label("Clear image" ,boolean())), + "removed" -> trim(label("Disable" ,boolean())) + )(EditGroupForm.apply) + get("/admin/system")(adminOnly { html.system(flash.get("info")) @@ -67,20 +152,192 @@ post("/admin/system", form)(adminOnly { form => saveSystemSettings(form) - if(form.ssh && SshServer.isActive && context.settings.sshPort != form.sshPort){ + if (form.sshAddress != context.settings.sshAddress) { SshServer.stop() - } - - if(form.ssh && !SshServer.isActive && form.baseUrl.isDefined){ - SshServer.start( - form.sshPort.getOrElse(SystemSettingsService.DefaultSshPort), - form.baseUrl.get) - } else if(!form.ssh && SshServer.isActive){ - SshServer.stop() + for { + sshAddress <- form.sshAddress + baseUrl <- form.baseUrl + } + SshServer.start(sshAddress, baseUrl) } flash += "info" -> "System settings has been updated." redirect("/admin/system") }) + post("/admin/system/sendmail", sendMailForm)(adminOnly { form => + try { + new Mailer(form.smtp).send(form.testAddress, + "Test message from GitBucket", "This is a test message from GitBucket.", + context.loginAccount.get) + + "Test mail has been sent to: " + form.testAddress + + } catch { + case e: Exception => "[Error] " + e.toString + } + }) + + get("/admin/plugins")(adminOnly { + html.plugins(PluginRegistry().getPlugins()) + }) + + + get("/admin/users")(adminOnly { + val includeRemoved = params.get("includeRemoved").map(_.toBoolean).getOrElse(false) + val users = getAllUsers(includeRemoved) + val members = users.collect { case account if(account.isGroupAccount) => + account.userName -> getGroupMembers(account.userName).map(_.userName) + }.toMap + + html.userlist(users, members, includeRemoved) + }) + + get("/admin/users/_newuser")(adminOnly { + html.user(None) + }) + + post("/admin/users/_newuser", newUserForm)(adminOnly { form => + createAccount(form.userName, sha1(form.password), form.fullName, form.mailAddress, form.isAdmin, form.url) + updateImage(form.userName, form.fileId, false) + redirect("/admin/users") + }) + + get("/admin/users/:userName/_edituser")(adminOnly { + val userName = params("userName") + html.user(getAccountByUserName(userName, true), flash.get("error")) + }) + + post("/admin/users/:name/_edituser", editUserForm)(adminOnly { form => + val userName = params("userName") + getAccountByUserName(userName, true).map { account => + if(account.isAdmin && (form.isRemoved || !form.isAdmin) && isLastAdministrator(account)){ + flash += "error" -> "Account can't be turned off because this is last one administrator." + redirect(s"/admin/users/${userName}/_edituser") + } else { + if(form.isRemoved){ + // Remove repositories + // getRepositoryNamesOfUser(userName).foreach { repositoryName => + // deleteRepository(userName, repositoryName) + // FileUtils.deleteDirectory(getRepositoryDir(userName, repositoryName)) + // FileUtils.deleteDirectory(getWikiRepositoryDir(userName, repositoryName)) + // FileUtils.deleteDirectory(getTemporaryDir(userName, repositoryName)) + // } + // Remove from GROUP_MEMBER, COLLABORATOR and REPOSITORY + removeUserRelatedData(userName) + } + + updateAccount(account.copy( + password = form.password.map(sha1).getOrElse(account.password), + fullName = form.fullName, + mailAddress = form.mailAddress, + isAdmin = form.isAdmin, + url = form.url, + isRemoved = form.isRemoved)) + + updateImage(userName, form.fileId, form.clearImage) + redirect("/admin/users") + } + } getOrElse NotFound() + }) + + get("/admin/users/_newgroup")(adminOnly { + html.usergroup(None, Nil) + }) + + post("/admin/users/_newgroup", newGroupForm)(adminOnly { form => + createGroup(form.groupName, form.url, form.groupDescription) + updateGroupMembers(form.groupName, form.members.split(",").map { + _.split(":") match { + case Array(userName, isManager) => (userName, isManager.toBoolean) + } + }.toList) + updateImage(form.groupName, form.fileId, false) + redirect("/admin/users") + }) + + get("/admin/users/:groupName/_editgroup")(adminOnly { + defining(params("groupName")){ groupName => + html.usergroup(getAccountByUserName(groupName, true), getGroupMembers(groupName)) + } + }) + + post("/admin/users/:groupName/_editgroup", editGroupForm)(adminOnly { form => + defining(params("groupName"), form.members.split(",").map { + _.split(":") match { + case Array(userName, isManager) => (userName, isManager.toBoolean) + } + }.toList){ case (groupName, members) => + getAccountByUserName(groupName, true).map { account => + updateGroup(groupName, form.url, form.groupDescription, form.isRemoved) + + if(form.isRemoved){ + // Remove from GROUP_MEMBER + updateGroupMembers(form.groupName, Nil) + // Remove repositories + getRepositoryNamesOfUser(form.groupName).foreach { repositoryName => + deleteRepository(groupName, repositoryName) + FileUtils.deleteDirectory(getRepositoryDir(groupName, repositoryName)) + FileUtils.deleteDirectory(getWikiRepositoryDir(groupName, repositoryName)) + FileUtils.deleteDirectory(getTemporaryDir(groupName, repositoryName)) + } + } else { + // Update GROUP_MEMBER + updateGroupMembers(form.groupName, members) +// // Update COLLABORATOR for group repositories +// getRepositoryNamesOfUser(form.groupName).foreach { repositoryName => +// removeCollaborators(form.groupName, repositoryName) +// members.foreach { case (userName, isManager) => +// addCollaborator(form.groupName, repositoryName, userName) +// } +// } + } + + updateImage(form.groupName, form.fileId, form.clearImage) + redirect("/admin/users") + + } getOrElse NotFound() + } + }) + + get("/admin/data")(adminOnly { + import gitbucket.core.util.JDBCUtil._ + val session = request2Session(request) + html.data(session.conn.allTableNames()) + }) + + post("/admin/export")(adminOnly { + import gitbucket.core.util.JDBCUtil._ + val file = request2Session(request).conn.exportAsSQL(request.getParameterValues("tableNames").toSeq) + + contentType = "application/octet-stream" + response.setHeader("Content-Disposition", "attachment; filename=" + file.getName) + response.setContentLength(file.length.toInt) + + using(new FileInputStream(file)){ in => + IOUtils.copy(in, response.outputStream) + } + + () + }) + + private def members: Constraint = new Constraint(){ + override def validate(name: String, value: String, messages: Messages): Option[String] = { + if(value.split(",").exists { + _.split(":") match { case Array(userName, isManager) => isManager.toBoolean } + }) None else Some("Must select one manager at least.") + } + } + + protected def disableByNotYourself(paramName: String): Constraint = new Constraint() { + override def validate(name: String, value: String, messages: Messages): Option[String] = { + params.get(paramName).flatMap { userName => + if(userName == context.loginAccount.get.userName && params.get("removed") == Some("true")) + Some("You can't disable your account yourself") + else + None + } + } + } + } diff --git a/src/main/scala/gitbucket/core/controller/UserManagementController.scala b/src/main/scala/gitbucket/core/controller/UserManagementController.scala deleted file mode 100644 index a3b07fb..0000000 --- a/src/main/scala/gitbucket/core/controller/UserManagementController.scala +++ /dev/null @@ -1,208 +0,0 @@ -package gitbucket.core.controller - -import gitbucket.core.service.{RepositoryService, AccountService} -import gitbucket.core.admin.users.html -import gitbucket.core.util._ -import gitbucket.core.util.ControlUtil._ -import gitbucket.core.util.StringUtil._ -import gitbucket.core.util.Implicits._ -import gitbucket.core.util.Directory._ -import jp.sf.amateras.scalatra.forms._ -import org.scalatra.i18n.Messages -import org.apache.commons.io.FileUtils - -class UserManagementController extends UserManagementControllerBase - with AccountService with RepositoryService with AdminAuthenticator - -trait UserManagementControllerBase extends AccountManagementControllerBase { - self: AccountService with RepositoryService with AdminAuthenticator => - - case class NewUserForm(userName: String, password: String, fullName: String, - mailAddress: String, isAdmin: Boolean, - url: Option[String], fileId: Option[String]) - - case class EditUserForm(userName: String, password: Option[String], fullName: String, - mailAddress: String, isAdmin: Boolean, url: Option[String], - fileId: Option[String], clearImage: Boolean, isRemoved: Boolean) - - case class NewGroupForm(groupName: String, groupDescription: Option[String], - url: Option[String], fileId: Option[String], - members: String) - - case class EditGroupForm(groupName: String, groupDescription: Option[String], - url: Option[String], fileId: Option[String], - members: String, clearImage: Boolean, isRemoved: Boolean) - - val newUserForm = mapping( - "userName" -> trim(label("Username" ,text(required, maxlength(100), identifier, uniqueUserName))), - "password" -> trim(label("Password" ,text(required, maxlength(20)))), - "fullName" -> trim(label("Full Name" ,text(required, maxlength(100)))), - "mailAddress" -> trim(label("Mail Address" ,text(required, maxlength(100), uniqueMailAddress()))), - "isAdmin" -> trim(label("User Type" ,boolean())), - "url" -> trim(label("URL" ,optional(text(maxlength(200))))), - "fileId" -> trim(label("File ID" ,optional(text()))) - )(NewUserForm.apply) - - val editUserForm = mapping( - "userName" -> trim(label("Username" ,text(required, maxlength(100), identifier))), - "password" -> trim(label("Password" ,optional(text(maxlength(20))))), - "fullName" -> trim(label("Full Name" ,text(required, maxlength(100)))), - "mailAddress" -> trim(label("Mail Address" ,text(required, maxlength(100), uniqueMailAddress("userName")))), - "isAdmin" -> trim(label("User Type" ,boolean())), - "url" -> trim(label("URL" ,optional(text(maxlength(200))))), - "fileId" -> trim(label("File ID" ,optional(text()))), - "clearImage" -> trim(label("Clear image" ,boolean())), - "removed" -> trim(label("Disable" ,boolean(disableByNotYourself("userName")))) - )(EditUserForm.apply) - - val newGroupForm = mapping( - "groupName" -> trim(label("Group name" ,text(required, maxlength(100), identifier, uniqueUserName))), - "groupDescription" -> trim(label("Group description", optional(text()))), - "url" -> trim(label("URL" ,optional(text(maxlength(200))))), - "fileId" -> trim(label("File ID" ,optional(text()))), - "members" -> trim(label("Members" ,text(required, members))) - )(NewGroupForm.apply) - - val editGroupForm = mapping( - "groupName" -> trim(label("Group name" ,text(required, maxlength(100), identifier))), - "groupDescription" -> trim(label("Group description", optional(text()))), - "url" -> trim(label("URL" ,optional(text(maxlength(200))))), - "fileId" -> trim(label("File ID" ,optional(text()))), - "members" -> trim(label("Members" ,text(required, members))), - "clearImage" -> trim(label("Clear image" ,boolean())), - "removed" -> trim(label("Disable" ,boolean())) - )(EditGroupForm.apply) - - get("/admin/users")(adminOnly { - val includeRemoved = params.get("includeRemoved").map(_.toBoolean).getOrElse(false) - val users = getAllUsers(includeRemoved) - val members = users.collect { case account if(account.isGroupAccount) => - account.userName -> getGroupMembers(account.userName).map(_.userName) - }.toMap - - html.list(users, members, includeRemoved) - }) - - get("/admin/users/_newuser")(adminOnly { - html.user(None) - }) - - post("/admin/users/_newuser", newUserForm)(adminOnly { form => - createAccount(form.userName, sha1(form.password), form.fullName, form.mailAddress, form.isAdmin, form.url) - updateImage(form.userName, form.fileId, false) - redirect("/admin/users") - }) - - get("/admin/users/:userName/_edituser")(adminOnly { - val userName = params("userName") - html.user(getAccountByUserName(userName, true)) - }) - - post("/admin/users/:name/_edituser", editUserForm)(adminOnly { form => - val userName = params("userName") - getAccountByUserName(userName, true).map { account => - - if(form.isRemoved){ - // Remove repositories - getRepositoryNamesOfUser(userName).foreach { repositoryName => - deleteRepository(userName, repositoryName) - FileUtils.deleteDirectory(getRepositoryDir(userName, repositoryName)) - FileUtils.deleteDirectory(getWikiRepositoryDir(userName, repositoryName)) - FileUtils.deleteDirectory(getTemporaryDir(userName, repositoryName)) - } - // Remove from GROUP_MEMBER, COLLABORATOR and REPOSITORY - removeUserRelatedData(userName) - } - - updateAccount(account.copy( - password = form.password.map(sha1).getOrElse(account.password), - fullName = form.fullName, - mailAddress = form.mailAddress, - isAdmin = form.isAdmin, - url = form.url, - isRemoved = form.isRemoved)) - - updateImage(userName, form.fileId, form.clearImage) - redirect("/admin/users") - - } getOrElse NotFound - }) - - get("/admin/users/_newgroup")(adminOnly { - html.group(None, Nil) - }) - - post("/admin/users/_newgroup", newGroupForm)(adminOnly { form => - createGroup(form.groupName, form.url, form.groupDescription) - updateGroupMembers(form.groupName, form.members.split(",").map { - _.split(":") match { - case Array(userName, isManager) => (userName, isManager.toBoolean) - } - }.toList) - updateImage(form.groupName, form.fileId, false) - redirect("/admin/users") - }) - - get("/admin/users/:groupName/_editgroup")(adminOnly { - defining(params("groupName")){ groupName => - html.group(getAccountByUserName(groupName, true), getGroupMembers(groupName)) - } - }) - - post("/admin/users/:groupName/_editgroup", editGroupForm)(adminOnly { form => - defining(params("groupName"), form.members.split(",").map { - _.split(":") match { - case Array(userName, isManager) => (userName, isManager.toBoolean) - } - }.toList){ case (groupName, members) => - getAccountByUserName(groupName, true).map { account => - updateGroup(groupName, form.url, form.groupDescription, form.isRemoved) - - if(form.isRemoved){ - // Remove from GROUP_MEMBER - updateGroupMembers(form.groupName, Nil) - // Remove repositories - getRepositoryNamesOfUser(form.groupName).foreach { repositoryName => - deleteRepository(groupName, repositoryName) - FileUtils.deleteDirectory(getRepositoryDir(groupName, repositoryName)) - FileUtils.deleteDirectory(getWikiRepositoryDir(groupName, repositoryName)) - FileUtils.deleteDirectory(getTemporaryDir(groupName, repositoryName)) - } - } else { - // Update GROUP_MEMBER - updateGroupMembers(form.groupName, members) - // Update COLLABORATOR for group repositories - getRepositoryNamesOfUser(form.groupName).foreach { repositoryName => - removeCollaborators(form.groupName, repositoryName) - members.foreach { case (userName, isManager) => - addCollaborator(form.groupName, repositoryName, userName) - } - } - } - - updateImage(form.groupName, form.fileId, form.clearImage) - redirect("/admin/users") - - } getOrElse NotFound - } - }) - - private def members: Constraint = new Constraint(){ - override def validate(name: String, value: String, messages: Messages): Option[String] = { - if(value.split(",").exists { - _.split(":") match { case Array(userName, isManager) => isManager.toBoolean } - }) None else Some("Must select one manager at least.") - } - } - - protected def disableByNotYourself(paramName: String): Constraint = new Constraint() { - override def validate(name: String, value: String, messages: Messages): Option[String] = { - params.get(paramName).flatMap { userName => - if(userName == context.loginAccount.get.userName && params.get("removed") == Some("true")) - Some("You can't disable your account yourself") - else - None - } - } - } -} diff --git a/src/main/scala/gitbucket/core/controller/WikiController.scala b/src/main/scala/gitbucket/core/controller/WikiController.scala index 7f876d0..f46b4be 100644 --- a/src/main/scala/gitbucket/core/controller/WikiController.scala +++ b/src/main/scala/gitbucket/core/controller/WikiController.scala @@ -1,21 +1,23 @@ package gitbucket.core.controller +import gitbucket.core.service.RepositoryService.RepositoryInfo import gitbucket.core.wiki.html -import gitbucket.core.service.{RepositoryService, WikiService, ActivityService, AccountService} +import gitbucket.core.service.{AccountService, ActivityService, RepositoryService, WikiService} import gitbucket.core.util._ import gitbucket.core.util.StringUtil._ import gitbucket.core.util.ControlUtil._ import gitbucket.core.util.Implicits._ import gitbucket.core.util.Directory._ -import jp.sf.amateras.scalatra.forms._ +import io.github.gitbucket.scalatra.forms._ import org.eclipse.jgit.api.Git import org.scalatra.i18n.Messages class WikiController extends WikiControllerBase - with WikiService with RepositoryService with AccountService with ActivityService with CollaboratorsAuthenticator with ReferrerAuthenticator + with WikiService with RepositoryService with AccountService with ActivityService + with ReadableUsersAuthenticator with ReferrerAuthenticator trait WikiControllerBase extends ControllerBase { - self: WikiService with RepositoryService with ActivityService with CollaboratorsAuthenticator with ReferrerAuthenticator => + self: WikiService with RepositoryService with ActivityService with ReadableUsersAuthenticator with ReferrerAuthenticator => case class WikiPageEditForm(pageName: String, content: String, message: Option[String], currentPageName: String, id: String) @@ -38,7 +40,9 @@ get("/:owner/:repository/wiki")(referrersOnly { repository => getWikiPage(repository.owner, repository.name, "Home").map { page => html.page("Home", page, getWikiPageList(repository.owner, repository.name), - repository, hasWritePermission(repository.owner, repository.name, context.loginAccount)) + repository, isEditable(repository), + getWikiPage(repository.owner, repository.name, "_Sidebar"), + getWikiPage(repository.owner, repository.name, "_Footer")) } getOrElse redirect(s"/${repository.owner}/${repository.name}/wiki/Home/_edit") }) @@ -47,7 +51,9 @@ getWikiPage(repository.owner, repository.name, pageName).map { page => html.page(pageName, page, getWikiPageList(repository.owner, repository.name), - repository, hasWritePermission(repository.owner, repository.name, context.loginAccount)) + repository, isEditable(repository), + getWikiPage(repository.owner, repository.name, "_Sidebar"), + getWikiPage(repository.owner, repository.name, "_Footer")) } getOrElse redirect(s"/${repository.owner}/${repository.name}/wiki/${StringUtil.urlEncode(pageName)}/_edit") }) @@ -56,8 +62,8 @@ using(Git.open(getWikiRepositoryDir(repository.owner, repository.name))){ git => JGitUtil.getCommitLog(git, "master", path = pageName + ".md") match { - case Right((logs, hasNext)) => html.history(Some(pageName), logs, repository) - case Left(_) => NotFound + case Right((logs, hasNext)) => html.history(Some(pageName), logs, repository, isEditable(repository)) + case Left(_) => NotFound() } } }) @@ -68,7 +74,7 @@ using(Git.open(getWikiRepositoryDir(repository.owner, repository.name))){ git => html.compare(Some(pageName), from, to, JGitUtil.getDiffs(git, from, to, true).filter(_.newPath == pageName + ".md"), repository, - hasWritePermission(repository.owner, repository.name, context.loginAccount), flash.get("info")) + isEditable(repository), flash.get("info")) } }) @@ -77,94 +83,115 @@ using(Git.open(getWikiRepositoryDir(repository.owner, repository.name))){ git => html.compare(None, from, to, JGitUtil.getDiffs(git, from, to, true), repository, - hasWritePermission(repository.owner, repository.name, context.loginAccount), flash.get("info")) + isEditable(repository), flash.get("info")) } }) - get("/:owner/:repository/wiki/:page/_revert/:commitId")(collaboratorsOnly { repository => - val pageName = StringUtil.urlDecode(params("page")) - val Array(from, to) = params("commitId").split("\\.\\.\\.") + get("/:owner/:repository/wiki/:page/_revert/:commitId")(readableUsersOnly { repository => + if(isEditable(repository)){ + val pageName = StringUtil.urlDecode(params("page")) + val Array(from, to) = params("commitId").split("\\.\\.\\.") - if(revertWikiPage(repository.owner, repository.name, from, to, context.loginAccount.get, Some(pageName))){ - redirect(s"/${repository.owner}/${repository.name}/wiki/${StringUtil.urlEncode(pageName)}") - } else { - flash += "info" -> "This patch was not able to be reversed." - redirect(s"/${repository.owner}/${repository.name}/wiki/${StringUtil.urlEncode(pageName)}/_compare/${from}...${to}") - } - }) - - get("/:owner/:repository/wiki/_revert/:commitId")(collaboratorsOnly { repository => - val Array(from, to) = params("commitId").split("\\.\\.\\.") - - if(revertWikiPage(repository.owner, repository.name, from, to, context.loginAccount.get, None)){ - redirect(s"/${repository.owner}/${repository.name}/wiki/") - } else { - flash += "info" -> "This patch was not able to be reversed." - redirect(s"/${repository.owner}/${repository.name}/wiki/_compare/${from}...${to}") - } - }) - - get("/:owner/:repository/wiki/:page/_edit")(collaboratorsOnly { repository => - val pageName = StringUtil.urlDecode(params("page")) - html.edit(pageName, getWikiPage(repository.owner, repository.name, pageName), repository) - }) - - post("/:owner/:repository/wiki/_edit", editForm)(collaboratorsOnly { (form, repository) => - defining(context.loginAccount.get){ loginAccount => - saveWikiPage( - repository.owner, - repository.name, - form.currentPageName, - form.pageName, - appendNewLine(convertLineSeparator(form.content, "LF"), "LF"), - loginAccount, - form.message.getOrElse(""), - Some(form.id) - ).map { commitId => - updateLastActivityDate(repository.owner, repository.name) - recordEditWikiPageActivity(repository.owner, repository.name, loginAccount.userName, form.pageName, commitId) + if(revertWikiPage(repository.owner, repository.name, from, to, context.loginAccount.get, Some(pageName))){ + redirect(s"/${repository.owner}/${repository.name}/wiki/${StringUtil.urlEncode(pageName)}") + } else { + flash += "info" -> "This patch was not able to be reversed." + redirect(s"/${repository.owner}/${repository.name}/wiki/${StringUtil.urlEncode(pageName)}/_compare/${from}...${to}") } - redirect(s"/${repository.owner}/${repository.name}/wiki/${StringUtil.urlEncode(form.pageName)}") - } + } else Unauthorized() + }) + + get("/:owner/:repository/wiki/_revert/:commitId")(readableUsersOnly { repository => + if(isEditable(repository)){ + val Array(from, to) = params("commitId").split("\\.\\.\\.") + + if(revertWikiPage(repository.owner, repository.name, from, to, context.loginAccount.get, None)){ + redirect(s"/${repository.owner}/${repository.name}/wiki/") + } else { + flash += "info" -> "This patch was not able to be reversed." + redirect(s"/${repository.owner}/${repository.name}/wiki/_compare/${from}...${to}") + } + } else Unauthorized() + }) + + get("/:owner/:repository/wiki/:page/_edit")(readableUsersOnly { repository => + if(isEditable(repository)){ + val pageName = StringUtil.urlDecode(params("page")) + html.edit(pageName, getWikiPage(repository.owner, repository.name, pageName), repository) + } else Unauthorized() }) - get("/:owner/:repository/wiki/_new")(collaboratorsOnly { - html.edit("", None, _) + post("/:owner/:repository/wiki/_edit", editForm)(readableUsersOnly { (form, repository) => + if(isEditable(repository)){ + defining(context.loginAccount.get){ loginAccount => + saveWikiPage( + repository.owner, + repository.name, + form.currentPageName, + form.pageName, + appendNewLine(convertLineSeparator(form.content, "LF"), "LF"), + loginAccount, + form.message.getOrElse(""), + Some(form.id) + ).map { commitId => + updateLastActivityDate(repository.owner, repository.name) + recordEditWikiPageActivity(repository.owner, repository.name, loginAccount.userName, form.pageName, commitId) + } + if(notReservedPageName(form.pageName)) { + redirect(s"/${repository.owner}/${repository.name}/wiki/${StringUtil.urlEncode(form.pageName)}") + } else { + redirect(s"/${repository.owner}/${repository.name}/wiki") + } + } + } else Unauthorized() }) - post("/:owner/:repository/wiki/_new", newForm)(collaboratorsOnly { (form, repository) => - defining(context.loginAccount.get){ loginAccount => - saveWikiPage(repository.owner, repository.name, form.currentPageName, form.pageName, + get("/:owner/:repository/wiki/_new")(readableUsersOnly { repository => + if(isEditable(repository)){ + html.edit("", None, repository) + } else Unauthorized() + }) + + post("/:owner/:repository/wiki/_new", newForm)(readableUsersOnly { (form, repository) => + if(isEditable(repository)){ + defining(context.loginAccount.get){ loginAccount => + saveWikiPage(repository.owner, repository.name, form.currentPageName, form.pageName, form.content, loginAccount, form.message.getOrElse(""), None) - updateLastActivityDate(repository.owner, repository.name) - recordCreateWikiPageActivity(repository.owner, repository.name, loginAccount.userName, form.pageName) + updateLastActivityDate(repository.owner, repository.name) + recordCreateWikiPageActivity(repository.owner, repository.name, loginAccount.userName, form.pageName) - redirect(s"/${repository.owner}/${repository.name}/wiki/${StringUtil.urlEncode(form.pageName)}") - } + if(notReservedPageName(form.pageName)) { + redirect(s"/${repository.owner}/${repository.name}/wiki/${StringUtil.urlEncode(form.pageName)}") + } else { + redirect(s"/${repository.owner}/${repository.name}/wiki") + } + } + } else Unauthorized() }) - get("/:owner/:repository/wiki/:page/_delete")(collaboratorsOnly { repository => - val pageName = StringUtil.urlDecode(params("page")) + get("/:owner/:repository/wiki/:page/_delete")(readableUsersOnly { repository => + if(isEditable(repository)){ + val pageName = StringUtil.urlDecode(params("page")) - defining(context.loginAccount.get){ loginAccount => - deleteWikiPage(repository.owner, repository.name, pageName, loginAccount.fullName, loginAccount.mailAddress, s"Destroyed ${pageName}") - updateLastActivityDate(repository.owner, repository.name) + defining(context.loginAccount.get){ loginAccount => + deleteWikiPage(repository.owner, repository.name, pageName, loginAccount.fullName, loginAccount.mailAddress, s"Destroyed ${pageName}") + updateLastActivityDate(repository.owner, repository.name) - redirect(s"/${repository.owner}/${repository.name}/wiki") - } + redirect(s"/${repository.owner}/${repository.name}/wiki") + } + } else Unauthorized() }) - + get("/:owner/:repository/wiki/_pages")(referrersOnly { repository => - html.pages(getWikiPageList(repository.owner, repository.name), repository, - hasWritePermission(repository.owner, repository.name, context.loginAccount)) + html.pages(getWikiPageList(repository.owner, repository.name), repository, isEditable(repository)) }) get("/:owner/:repository/wiki/_history")(referrersOnly { repository => using(Git.open(getWikiRepositoryDir(repository.owner, repository.name))){ git => JGitUtil.getCommitLog(git, "master") match { - case Right((logs, hasNext)) => html.history(None, logs, repository) - case Left(_) => NotFound + case Right((logs, hasNext)) => html.history(None, logs, repository, isEditable(repository)) + case Left(_) => NotFound() } } }) @@ -174,7 +201,7 @@ getFileContent(repository.owner, repository.name, path).map { bytes => RawData(FileUtil.getContentType(path, bytes), bytes) - } getOrElse NotFound + } getOrElse NotFound() }) private def unique: Constraint = new Constraint(){ @@ -186,13 +213,15 @@ override def validate(name: String, value: String, messages: Messages): Option[String] = if(value.exists("\\/:*?\"<>|".contains(_))){ Some(s"${name} contains invalid character.") - } else if(value.startsWith("_") || value.startsWith("-")){ + } else if(notReservedPageName(value) && (value.startsWith("_") || value.startsWith("-"))){ Some(s"${name} starts with invalid character.") } else { None } } + private def notReservedPageName(value: String) = ! (Array[String]("_Sidebar","_Footer") contains value) + private def conflictForNew: Constraint = new Constraint(){ override def validate(name: String, value: String, messages: Messages): Option[String] = { targetWikiPage.map { _ => @@ -211,4 +240,13 @@ private def targetWikiPage = getWikiPage(params("owner"), params("repository"), params("pageName")) + private def isEditable(repository: RepositoryInfo)(implicit context: Context): Boolean = { + repository.repository.options.wikiOption match { + case "ALL" => !repository.repository.isPrivate && context.loginAccount.isDefined + case "PUBLIC" => hasGuestRole(repository.owner, repository.name, context.loginAccount) + case "PRIVATE" => hasDeveloperRole(repository.owner, repository.name, context.loginAccount) + case "DISABLE" => false + } + } + } diff --git a/src/main/scala/gitbucket/core/model/BasicTemplate.scala b/src/main/scala/gitbucket/core/model/BasicTemplate.scala index db3e1b4..53388fc 100644 --- a/src/main/scala/gitbucket/core/model/BasicTemplate.scala +++ b/src/main/scala/gitbucket/core/model/BasicTemplate.scala @@ -26,12 +26,16 @@ trait LabelTemplate extends BasicTemplate { self: Table[_] => val labelId = column[Int]("LABEL_ID") + val labelName = column[String]("LABEL_NAME") def byLabel(owner: String, repository: String, labelId: Int) = byRepository(owner, repository) && (this.labelId === labelId.bind) def byLabel(userName: Column[String], repositoryName: Column[String], labelId: Column[Int]) = byRepository(userName, repositoryName) && (this.labelId === labelId) + + def byLabel(owner: String, repository: String, labelName: String) = + byRepository(owner, repository) && (this.labelName === labelName.bind) } trait MilestoneTemplate extends BasicTemplate { self: Table[_] => @@ -54,4 +58,9 @@ byRepository(userName, repositoryName) && (this.commitId === commitId) } + trait BranchTemplate extends BasicTemplate{ self: Table[_] => + val branch = column[String]("BRANCH") + def byBranch(owner: String, repository: String, branchName: String) = byRepository(owner, repository) && (branch === branchName.bind) + def byBranch(owner: Column[String], repository: Column[String], branchName: Column[String]) = byRepository(owner, repository) && (this.branch === branchName) + } } diff --git a/src/main/scala/gitbucket/core/model/Collaborator.scala b/src/main/scala/gitbucket/core/model/Collaborator.scala index 55ae80f..c810613 100644 --- a/src/main/scala/gitbucket/core/model/Collaborator.scala +++ b/src/main/scala/gitbucket/core/model/Collaborator.scala @@ -7,7 +7,8 @@ class Collaborators(tag: Tag) extends Table[Collaborator](tag, "COLLABORATOR") with BasicTemplate { val collaboratorName = column[String]("COLLABORATOR_NAME") - def * = (userName, repositoryName, collaboratorName) <> (Collaborator.tupled, Collaborator.unapply) + val role = column[String]("ROLE") + def * = (userName, repositoryName, collaboratorName, role) <> (Collaborator.tupled, Collaborator.unapply) def byPrimaryKey(owner: String, repository: String, collaborator: String) = byRepository(owner, repository) && (collaboratorName === collaborator.bind) @@ -17,5 +18,23 @@ case class Collaborator( userName: String, repositoryName: String, - collaboratorName: String + collaboratorName: String, + role: String ) + +sealed abstract class Role(val name: String) + +object Role { + object ADMIN extends Role("ADMIN") + object DEVELOPER extends Role("DEVELOPER") + object GUEST extends Role("GUEST") + +// val values: Vector[Permission] = Vector(ADMIN, WRITE, READ) +// +// private val map: Map[String, Permission] = values.map(enum => enum.name -> enum).toMap +// +// def apply(name: String): Permission = map(name) +// +// def valueOf(name: String): Option[Permission] = map.get(name) + +} \ No newline at end of file diff --git a/src/main/scala/gitbucket/core/model/Comment.scala b/src/main/scala/gitbucket/core/model/Comment.scala index 5a11440..cab001c 100644 --- a/src/main/scala/gitbucket/core/model/Comment.scala +++ b/src/main/scala/gitbucket/core/model/Comment.scala @@ -55,8 +55,8 @@ val newLine = column[Option[Int]]("NEW_LINE_NUMBER") val registeredDate = column[java.util.Date]("REGISTERED_DATE") val updatedDate = column[java.util.Date]("UPDATED_DATE") - val pullRequest = column[Boolean]("PULL_REQUEST") - def * = (userName, repositoryName, commitId, commentId, commentedUserName, content, fileName, oldLine, newLine, registeredDate, updatedDate, pullRequest) <> (CommitComment.tupled, CommitComment.unapply) + val issueId = column[Option[Int]]("ISSUE_ID") + def * = (userName, repositoryName, commitId, commentId, commentedUserName, content, fileName, oldLine, newLine, registeredDate, updatedDate, issueId) <> (CommitComment.tupled, CommitComment.unapply) def byPrimaryKey(commentId: Int) = this.commentId === commentId.bind } @@ -74,5 +74,5 @@ newLine: Option[Int], registeredDate: java.util.Date, updatedDate: java.util.Date, - pullRequest: Boolean + issueId: Option[Int] ) extends Comment diff --git a/src/main/scala/gitbucket/core/model/CommitStatus.scala b/src/main/scala/gitbucket/core/model/CommitStatus.scala index 87b74f1..f108403 100644 --- a/src/main/scala/gitbucket/core/model/CommitStatus.scala +++ b/src/main/scala/gitbucket/core/model/CommitStatus.scala @@ -1,6 +1,5 @@ package gitbucket.core.model -import scala.slick.lifted.MappedTo import scala.slick.jdbc._ trait CommitStatusComponent extends TemplateComponent { self: Profile => @@ -19,7 +18,7 @@ val creator = column[String]("CREATOR") val registeredDate = column[java.util.Date]("REGISTERED_DATE") val updatedDate = column[java.util.Date]("UPDATED_DATE") - def * = (commitStatusId, userName, repositoryName, commitId, context, state, targetUrl, description, creator, registeredDate, updatedDate) <> (CommitStatus.tupled, CommitStatus.unapply) + def * = (commitStatusId, userName, repositoryName, commitId, context, state, targetUrl, description, creator, registeredDate, updatedDate) <> ((CommitStatus.apply _).tupled, CommitStatus.unapply) def byPrimaryKey(id: Int) = commitStatusId === id.bind } } @@ -38,7 +37,20 @@ registeredDate: java.util.Date, updatedDate: java.util.Date ) - +object CommitStatus { + def pending(owner: String, repository: String, context: String) = CommitStatus( + commitStatusId = 0, + userName = owner, + repositoryName = repository, + commitId = "", + context = context, + state = CommitState.PENDING, + targetUrl = None, + description = Some("Waiting for status to be reported"), + creator = "", + registeredDate = new java.util.Date(), + updatedDate = new java.util.Date()) +} sealed abstract class CommitState(val name: String) diff --git a/src/main/scala/gitbucket/core/model/Labels.scala b/src/main/scala/gitbucket/core/model/Labels.scala index 0143c9e..84a4e6d 100644 --- a/src/main/scala/gitbucket/core/model/Labels.scala +++ b/src/main/scala/gitbucket/core/model/Labels.scala @@ -7,7 +7,7 @@ class Labels(tag: Tag) extends Table[Label](tag, "LABEL") with LabelTemplate { override val labelId = column[Int]("LABEL_ID", O AutoInc) - val labelName = column[String]("LABEL_NAME") + override val labelName = column[String]("LABEL_NAME") val color = column[String]("COLOR") def * = (userName, repositoryName, labelId, labelName, color) <> (Label.tupled, Label.unapply) diff --git a/src/main/scala/gitbucket/core/model/Plugin.scala b/src/main/scala/gitbucket/core/model/Plugin.scala deleted file mode 100644 index 1e8aac5..0000000 --- a/src/main/scala/gitbucket/core/model/Plugin.scala +++ /dev/null @@ -1,19 +0,0 @@ -package gitbucket.core.model - -trait PluginComponent extends TemplateComponent { self: Profile => - import profile.simple._ - import self._ - - lazy val Plugins = TableQuery[Plugins] - - class Plugins(tag: Tag) extends Table[Plugin](tag, "PLUGIN"){ - val pluginId = column[String]("PLUGIN_ID", O PrimaryKey) - val version = column[String]("VERSION") - def * = (pluginId, version) <> (Plugin.tupled, Plugin.unapply) - } -} - -case class Plugin( - pluginId: String, - version: String -) diff --git a/src/main/scala/gitbucket/core/model/Profile.scala b/src/main/scala/gitbucket/core/model/Profile.scala index 7ba5584..26bb225 100644 --- a/src/main/scala/gitbucket/core/model/Profile.scala +++ b/src/main/scala/gitbucket/core/model/Profile.scala @@ -1,5 +1,6 @@ package gitbucket.core.model +import gitbucket.core.util.DatabaseConfig trait Profile { val profile: slick.driver.JdbcProfile @@ -28,7 +29,9 @@ } trait ProfileProvider { self: Profile => - val profile = slick.driver.H2Driver + + lazy val profile = DatabaseConfig.slickDriver + } trait CoreProfile extends ProfileProvider with Profile @@ -48,6 +51,7 @@ with RepositoryComponent with SshKeyComponent with WebHookComponent - with PluginComponent + with WebHookEventComponent + with ProtectedBranchComponent object Profile extends CoreProfile diff --git a/src/main/scala/gitbucket/core/model/ProtectedBranch.scala b/src/main/scala/gitbucket/core/model/ProtectedBranch.scala new file mode 100644 index 0000000..9679faf --- /dev/null +++ b/src/main/scala/gitbucket/core/model/ProtectedBranch.scala @@ -0,0 +1,34 @@ +package gitbucket.core.model + +trait ProtectedBranchComponent extends TemplateComponent { self: Profile => + import profile.simple._ + import self._ + + lazy val ProtectedBranches = TableQuery[ProtectedBranches] + class ProtectedBranches(tag: Tag) extends Table[ProtectedBranch](tag, "PROTECTED_BRANCH") with BranchTemplate { + val statusCheckAdmin = column[Boolean]("STATUS_CHECK_ADMIN") + def * = (userName, repositoryName, branch, statusCheckAdmin) <> (ProtectedBranch.tupled, ProtectedBranch.unapply) + def byPrimaryKey(userName: String, repositoryName: String, branch: String) = byBranch(userName, repositoryName, branch) + def byPrimaryKey(userName: Column[String], repositoryName: Column[String], branch: Column[String]) = byBranch(userName, repositoryName, branch) + } + + lazy val ProtectedBranchContexts = TableQuery[ProtectedBranchContexts] + class ProtectedBranchContexts(tag: Tag) extends Table[ProtectedBranchContext](tag, "PROTECTED_BRANCH_REQUIRE_CONTEXT") with BranchTemplate { + val context = column[String]("CONTEXT") + def * = (userName, repositoryName, branch, context) <> (ProtectedBranchContext.tupled, ProtectedBranchContext.unapply) + } +} + + +case class ProtectedBranch( + userName: String, + repositoryName: String, + branch: String, + statusCheckAdmin: Boolean) + + +case class ProtectedBranchContext( + userName: String, + repositoryName: String, + branch: String, + context: String) diff --git a/src/main/scala/gitbucket/core/model/Repository.scala b/src/main/scala/gitbucket/core/model/Repository.scala index 789f957..387b8ff 100644 --- a/src/main/scala/gitbucket/core/model/Repository.scala +++ b/src/main/scala/gitbucket/core/model/Repository.scala @@ -7,17 +7,61 @@ lazy val Repositories = TableQuery[Repositories] class Repositories(tag: Tag) extends Table[Repository](tag, "REPOSITORY") with BasicTemplate { - val isPrivate = column[Boolean]("PRIVATE") - val description = column[String]("DESCRIPTION") - val defaultBranch = column[String]("DEFAULT_BRANCH") - val registeredDate = column[java.util.Date]("REGISTERED_DATE") - val updatedDate = column[java.util.Date]("UPDATED_DATE") - val lastActivityDate = column[java.util.Date]("LAST_ACTIVITY_DATE") - val originUserName = column[String]("ORIGIN_USER_NAME") + val isPrivate = column[Boolean]("PRIVATE") + val description = column[String]("DESCRIPTION") + val defaultBranch = column[String]("DEFAULT_BRANCH") + val registeredDate = column[java.util.Date]("REGISTERED_DATE") + val updatedDate = column[java.util.Date]("UPDATED_DATE") + val lastActivityDate = column[java.util.Date]("LAST_ACTIVITY_DATE") + val originUserName = column[String]("ORIGIN_USER_NAME") val originRepositoryName = column[String]("ORIGIN_REPOSITORY_NAME") - val parentUserName = column[String]("PARENT_USER_NAME") + val parentUserName = column[String]("PARENT_USER_NAME") val parentRepositoryName = column[String]("PARENT_REPOSITORY_NAME") - def * = (userName, repositoryName, isPrivate, description.?, defaultBranch, registeredDate, updatedDate, lastActivityDate, originUserName.?, originRepositoryName.?, parentUserName.?, parentRepositoryName.?) <> (Repository.tupled, Repository.unapply) + val issuesOption = column[String]("ISSUES_OPTION") + val externalIssuesUrl = column[String]("EXTERNAL_ISSUES_URL") + val wikiOption = column[String]("WIKI_OPTION") + val externalWikiUrl = column[String]("EXTERNAL_WIKI_URL") + val allowFork = column[Boolean]("ALLOW_FORK") + + def * = ( + (userName, repositoryName, isPrivate, description.?, defaultBranch, + registeredDate, updatedDate, lastActivityDate, originUserName.?, originRepositoryName.?, parentUserName.?, parentRepositoryName.?), + (issuesOption, externalIssuesUrl.?, wikiOption, externalWikiUrl.?, allowFork) + ).shaped <> ( + { case (repository, options) => + Repository( + repository._1, + repository._2, + repository._3, + repository._4, + repository._5, + repository._6, + repository._7, + repository._8, + repository._9, + repository._10, + repository._11, + repository._12, + RepositoryOptions.tupled.apply(options) + ) + }, { (r: Repository) => + Some((( + r.userName, + r.repositoryName, + r.isPrivate, + r.description, + r.defaultBranch, + r.registeredDate, + r.updatedDate, + r.lastActivityDate, + r.originUserName, + r.originRepositoryName, + r.parentUserName, + r.parentRepositoryName + ),( + RepositoryOptions.unapply(r.options).get + ))) + }) def byPrimaryKey(owner: String, repository: String) = byRepository(owner, repository) } @@ -35,5 +79,14 @@ originUserName: Option[String], originRepositoryName: Option[String], parentUserName: Option[String], - parentRepositoryName: Option[String] + parentRepositoryName: Option[String], + options: RepositoryOptions +) + +case class RepositoryOptions( + issuesOption: String, + externalIssuesUrl: Option[String], + wikiOption: String, + externalWikiUrl: Option[String], + allowFork: Boolean ) diff --git a/src/main/scala/gitbucket/core/model/WebHook.scala b/src/main/scala/gitbucket/core/model/WebHook.scala index b6897da..d87f9cb 100644 --- a/src/main/scala/gitbucket/core/model/WebHook.scala +++ b/src/main/scala/gitbucket/core/model/WebHook.scala @@ -3,18 +3,70 @@ trait WebHookComponent extends TemplateComponent { self: Profile => import profile.simple._ + implicit val whContentTypeColumnType = MappedColumnType.base[WebHookContentType, String](whct => whct.code , code => WebHookContentType.valueOf(code)) + lazy val WebHooks = TableQuery[WebHooks] class WebHooks(tag: Tag) extends Table[WebHook](tag, "WEB_HOOK") with BasicTemplate { val url = column[String]("URL") - def * = (userName, repositoryName, url) <> (WebHook.tupled, WebHook.unapply) + val token = column[Option[String]]("TOKEN", O.Nullable) + val ctype = column[WebHookContentType]("CTYPE", O.NotNull) + def * = (userName, repositoryName, url, ctype, token) <> ((WebHook.apply _).tupled, WebHook.unapply) def byPrimaryKey(owner: String, repository: String, url: String) = byRepository(owner, repository) && (this.url === url.bind) } } +case class WebHookContentType(val code: String, val ctype: String) + +object WebHookContentType { + object JSON extends WebHookContentType("json", "application/json") + + object FORM extends WebHookContentType("form", "application/x-www-form-urlencoded") + + val values: Vector[WebHookContentType] = Vector(JSON, FORM) + + private val map: Map[String, WebHookContentType] = values.map(enum => enum.code -> enum).toMap + + def apply(code: String): WebHookContentType = map(code) + + def valueOf(code: String): WebHookContentType = map(code) + def valueOpt(code: String): Option[WebHookContentType] = map.get(code) +} + case class WebHook( userName: String, repositoryName: String, - url: String + url: String, + ctype: WebHookContentType, + token: Option[String] ) + +object WebHook { + sealed class Event(var name: String) + case object CommitComment extends Event("commit_comment") + case object Create extends Event("create") + case object Delete extends Event("delete") + case object Deployment extends Event("deployment") + case object DeploymentStatus extends Event("deployment_status") + case object Fork extends Event("fork") + case object Gollum extends Event("gollum") + case object IssueComment extends Event("issue_comment") + case object Issues extends Event("issues") + case object Member extends Event("member") + case object PageBuild extends Event("page_build") + case object Public extends Event("public") + case object PullRequest extends Event("pull_request") + case object PullRequestReviewComment extends Event("pull_request_review_comment") + case object Push extends Event("push") + case object Release extends Event("release") + case object Status extends Event("status") + case object TeamAdd extends Event("team_add") + case object Watch extends Event("watch") + object Event{ + val values = List(CommitComment,Create,Delete,Deployment,DeploymentStatus,Fork,Gollum,IssueComment,Issues,Member,PageBuild,Public,PullRequest,PullRequestReviewComment,Push,Release,Status,TeamAdd,Watch) + private val map:Map[String,Event] = values.map(e => e.name -> e).toMap + def valueOf(name: String): Event = map(name) + def valueOpt(name: String): Option[Event] = map.get(name) + } +} diff --git a/src/main/scala/gitbucket/core/model/WebHookEvent.scala b/src/main/scala/gitbucket/core/model/WebHookEvent.scala new file mode 100644 index 0000000..cc960e7 --- /dev/null +++ b/src/main/scala/gitbucket/core/model/WebHookEvent.scala @@ -0,0 +1,30 @@ +package gitbucket.core.model + +trait WebHookEventComponent extends TemplateComponent { self: Profile => + import profile.simple._ + import gitbucket.core.model.Profile.WebHooks + + lazy val WebHookEvents = TableQuery[WebHookEvents] + + implicit val typedType = MappedColumnType.base[WebHook.Event, String](_.name, WebHook.Event.valueOf(_)) + + class WebHookEvents(tag: Tag) extends Table[WebHookEvent](tag, "WEB_HOOK_EVENT") with BasicTemplate { + val url = column[String]("URL") + val event = column[WebHook.Event]("EVENT") + def * = (userName, repositoryName, url, event) <> ((WebHookEvent.apply _).tupled, WebHookEvent.unapply) + + def byWebHook(owner: String, repository: String, url: String) = byRepository(owner, repository) && (this.url === url.bind) + def byWebHook(owner: Column[String], repository: Column[String], url: Column[String]) = + byRepository(userName, repositoryName) && (this.url === url) + def byWebHook(webhook: WebHooks) = + byRepository(webhook.userName, webhook.repositoryName) && (this.url === webhook.url) + def byPrimaryKey(owner: String, repository: String, url: String, event: WebHook.Event) = byWebHook(owner, repository, url) && (this.event === event.bind) + } +} + +case class WebHookEvent( + userName: String, + repositoryName: String, + url: String, + event: WebHook.Event +) diff --git a/src/main/scala/gitbucket/core/plugin/GitRepositoryRouting.scala b/src/main/scala/gitbucket/core/plugin/GitRepositoryRouting.scala new file mode 100644 index 0000000..61089f0 --- /dev/null +++ b/src/main/scala/gitbucket/core/plugin/GitRepositoryRouting.scala @@ -0,0 +1,42 @@ +package gitbucket.core.plugin + +import gitbucket.core.model.Session +import gitbucket.core.service.SystemSettingsService.SystemSettings + +/** + * Define the Git repository routing. + * + * @param urlPattern the regular expression which matches the repository path (e.g. "gist/(.+?)/(.+?)\\.git") + * @param localPath the string to assemble local file path of repository (e.g. "gist/$1/$2") + * @param filter the filter for request to the Git repository which is defined by this routing + */ +case class GitRepositoryRouting(urlPattern: String, localPath: String, filter: GitRepositoryFilter){ + + def this(urlPattern: String, localPath: String) = { + this(urlPattern, localPath, new GitRepositoryFilter(){ + def filter(repositoryName: String, userName: Option[String], settings: SystemSettings, isUpdating: Boolean) + (implicit session: Session): Boolean = true + }) + } + +} + +/** + * Filters request to plug-in served repository. This is used to provide authentication mainly. + */ +trait GitRepositoryFilter { + + /** + * Filters request to Git repository. If this method returns true then request is accepted. + * + * @param path the repository path which starts with '/' + * @param userName the authenticated user name or None + * @param settings the system settings + * @param isUpdating true if update request, otherwise false + * @param session the database session + * @return true if allow accessing to repository, otherwise false. + */ + def filter(path: String, userName: Option[String], settings: SystemSettings, isUpdating: Boolean) + (implicit session: Session): Boolean + +} \ No newline at end of file diff --git a/src/main/scala/gitbucket/core/plugin/Plugin.scala b/src/main/scala/gitbucket/core/plugin/Plugin.scala index 3624d4e..dd3fcb0 100644 --- a/src/main/scala/gitbucket/core/plugin/Plugin.scala +++ b/src/main/scala/gitbucket/core/plugin/Plugin.scala @@ -1,16 +1,18 @@ package gitbucket.core.plugin import javax.servlet.ServletContext -import gitbucket.core.controller.ControllerBase +import gitbucket.core.controller.{Context, ControllerBase} +import gitbucket.core.model.Account +import gitbucket.core.service.RepositoryService.RepositoryInfo import gitbucket.core.service.SystemSettingsService.SystemSettings import gitbucket.core.util.ControlUtil._ -import gitbucket.core.util.Version +import io.github.gitbucket.solidbase.model.Version /** * Trait for define plugin interface. - * To provide plugin, put Plugin class which mixed in this trait into the package root. + * To provide a plugin, put a Plugin class which extends this class into the package root. */ -trait Plugin { +abstract class Plugin { val pluginId: String val pluginName: String @@ -58,6 +60,126 @@ def renderers(registry: PluginRegistry, context: ServletContext, settings: SystemSettings): Seq[(String, Renderer)] = Nil /** + * Override to add git repository routings. + */ + val repositoryRoutings: Seq[GitRepositoryRouting] = Nil + + /** + * Override to add git repository routings. + */ + def repositoryRoutings(registry: PluginRegistry, context: ServletContext, settings: SystemSettings): Seq[GitRepositoryRouting] = Nil + + /** + * Override to add receive hooks. + */ + val receiveHooks: Seq[ReceiveHook] = Nil + + /** + * Override to add receive hooks. + */ + def receiveHooks(registry: PluginRegistry, context: ServletContext, settings: SystemSettings): Seq[ReceiveHook] = Nil + + /** + * Override to add global menus. + */ + val globalMenus: Seq[(Context) => Option[Link]] = Nil + + /** + * Override to add global menus. + */ + def globalMenus(registry: PluginRegistry, context: ServletContext, settings: SystemSettings): Seq[(Context) => Option[Link]] = Nil + + /** + * Override to add repository menus. + */ + val repositoryMenus: Seq[(RepositoryInfo, Context) => Option[Link]] = Nil + + /** + * Override to add repository menus. + */ + def repositoryMenus(registry: PluginRegistry, context: ServletContext, settings: SystemSettings): Seq[(RepositoryInfo, Context) => Option[Link]] = Nil + + /** + * Override to add repository setting tabs. + */ + val repositorySettingTabs: Seq[(RepositoryInfo, Context) => Option[Link]] = Nil + + /** + * Override to add repository setting tabs. + */ + def repositorySettingTabs(registry: PluginRegistry, context: ServletContext, settings: SystemSettings): Seq[(RepositoryInfo, Context) => Option[Link]] = Nil + + /** + * Override to add profile tabs. + */ + val profileTabs: Seq[(Account, Context) => Option[Link]] = Nil + + /** + * Override to add profile tabs. + */ + def profileTabs(registry: PluginRegistry, context: ServletContext, settings: SystemSettings): Seq[(Account, Context) => Option[Link]] = Nil + + /** + * Override to add system setting menus. + */ + val systemSettingMenus: Seq[(Context) => Option[Link]] = Nil + + /** + * Override to add system setting menus. + */ + def systemSettingMenus(registry: PluginRegistry, context: ServletContext, settings: SystemSettings): Seq[(Context) => Option[Link]] = Nil + + /** + * Override to add account setting menus. + */ + val accountSettingMenus: Seq[(Context) => Option[Link]] = Nil + + /** + * Override to add account setting menus. + */ + def accountSettingMenus(registry: PluginRegistry, context: ServletContext, settings: SystemSettings): Seq[(Context) => Option[Link]] = Nil + + /** + * Override to add dashboard tabs. + */ + val dashboardTabs: Seq[(Context) => Option[Link]] = Nil + + /** + * Override to add dashboard tabs. + */ + def dashboardTabs(registry: PluginRegistry, context: ServletContext, settings: SystemSettings): Seq[(Context) => Option[Link]] = Nil + + /** + * Override to add assets mappings. + */ + val assetsMappings: Seq[(String, String)] = Nil + + /** + * Override to add assets mappings. + */ + def assetsMappings(registry: PluginRegistry, context: ServletContext, settings: SystemSettings): Seq[(String, String)] = Nil + + /** + * Override to add text decorators. + */ + val textDecorators: Seq[TextDecorator] = Nil + + /** + * Override to add text decorators. + */ + def textDecorators(registry: PluginRegistry, context: ServletContext, settings: SystemSettings): Seq[TextDecorator] = Nil + + /** + * Override to add suggestion provider. + */ + val suggestionProviders: Seq[SuggestionProvider] = Nil + + /** + * Override to add suggestion provider. + */ + def suggestionProviders(registry: PluginRegistry, context: ServletContext, settings: SystemSettings): Seq[SuggestionProvider] = Nil + + /** * This method is invoked in initialization of plugin system. * Register plugin functionality to PluginRegistry. */ @@ -74,6 +196,42 @@ (renderers ++ renderers(registry, context, settings)).foreach { case (extension, renderer) => registry.addRenderer(extension, renderer) } + (repositoryRoutings ++ repositoryRoutings(registry, context, settings)).foreach { routing => + registry.addRepositoryRouting(routing) + } + (receiveHooks ++ receiveHooks(registry, context, settings)).foreach { receiveHook => + registry.addReceiveHook(receiveHook) + } + (globalMenus ++ globalMenus(registry, context, settings)).foreach { globalMenu => + registry.addGlobalMenu(globalMenu) + } + (repositoryMenus ++ repositoryMenus(registry, context, settings)).foreach { repositoryMenu => + registry.addRepositoryMenu(repositoryMenu) + } + (repositorySettingTabs ++ repositorySettingTabs(registry, context, settings)).foreach { repositorySettingTab => + registry.addRepositorySettingTab(repositorySettingTab) + } + (profileTabs ++ profileTabs(registry, context, settings)).foreach { profileTab => + registry.addProfileTab(profileTab) + } + (systemSettingMenus ++ systemSettingMenus(registry, context, settings)).foreach { systemSettingMenu => + registry.addSystemSettingMenu(systemSettingMenu) + } + (accountSettingMenus ++ accountSettingMenus(registry, context, settings)).foreach { accountSettingMenu => + registry.addAccountSettingMenu(accountSettingMenu) + } + (dashboardTabs ++ dashboardTabs(registry, context, settings)).foreach { dashboardTab => + registry.addDashboardTab(dashboardTab) + } + (assetsMappings ++ assetsMappings(registry, context, settings)).foreach { assetMapping => + registry.addAssetsMapping((assetMapping._1, assetMapping._2, getClass.getClassLoader)) + } + (textDecorators ++ textDecorators(registry, context, settings)).foreach { textDecorator => + registry.addTextDecorator(textDecorator) + } + (suggestionProviders ++ suggestionProviders(registry, context, settings)).foreach { suggestionProvider => + registry.addSuggestionProvider(suggestionProvider) + } } /** diff --git a/src/main/scala/gitbucket/core/plugin/PluginRegistory.scala b/src/main/scala/gitbucket/core/plugin/PluginRegistory.scala index e5aac84..63203ca 100644 --- a/src/main/scala/gitbucket/core/plugin/PluginRegistory.scala +++ b/src/main/scala/gitbucket/core/plugin/PluginRegistory.scala @@ -3,15 +3,18 @@ import java.io.{File, FilenameFilter, InputStream} import java.net.URLClassLoader import javax.servlet.ServletContext -import javax.servlet.http.{HttpServletRequest, HttpServletResponse} import gitbucket.core.controller.{Context, ControllerBase} +import gitbucket.core.model.Account +import gitbucket.core.service.ProtectedBranchService.ProtectedBranchReceiveHook import gitbucket.core.service.RepositoryService.RepositoryInfo import gitbucket.core.service.SystemSettingsService.SystemSettings import gitbucket.core.util.ControlUtil._ +import gitbucket.core.util.DatabaseConfig import gitbucket.core.util.Directory._ -import gitbucket.core.util.JDBCUtil._ -import gitbucket.core.util.{Version, Versions} +import io.github.gitbucket.solidbase.Solidbase +import io.github.gitbucket.solidbase.manager.JDBCVersionManager +import io.github.gitbucket.solidbase.model.Module import org.apache.commons.codec.binary.{Base64, StringUtils} import org.slf4j.LoggerFactory @@ -28,10 +31,24 @@ renderers ++= Seq( "md" -> MarkdownRenderer, "markdown" -> MarkdownRenderer ) + private val repositoryRoutings = new ListBuffer[GitRepositoryRouting] + private val receiveHooks = new ListBuffer[ReceiveHook] + receiveHooks += new ProtectedBranchReceiveHook() - def addPlugin(pluginInfo: PluginInfo): Unit = { - plugins += pluginInfo - } + private val globalMenus = new ListBuffer[(Context) => Option[Link]] + private val repositoryMenus = new ListBuffer[(RepositoryInfo, Context) => Option[Link]] + private val repositorySettingTabs = new ListBuffer[(RepositoryInfo, Context) => Option[Link]] + private val profileTabs = new ListBuffer[(Account, Context) => Option[Link]] + private val systemSettingMenus = new ListBuffer[(Context) => Option[Link]] + private val accountSettingMenus = new ListBuffer[(Context) => Option[Link]] + private val dashboardTabs = new ListBuffer[(Context) => Option[Link]] + private val assetsMappings = new ListBuffer[(String, String, ClassLoader)] + private val textDecorators = new ListBuffer[TextDecorator] + + private val suggestionProviders = new ListBuffer[SuggestionProvider] + suggestionProviders += new UserNameSuggestionProvider() + + def addPlugin(pluginInfo: PluginInfo): Unit = plugins += pluginInfo def getPlugins(): List[PluginInfo] = plugins.toList @@ -52,47 +69,78 @@ def getImage(id: String): String = images(id) - def addController(path: String, controller: ControllerBase): Unit = { - controllers += ((controller, path)) - } + def addController(path: String, controller: ControllerBase): Unit = controllers += ((controller, path)) @deprecated("Use addController(path: String, controller: ControllerBase) instead", "3.4.0") - def addController(controller: ControllerBase, path: String): Unit = { - addController(path, controller) - } + def addController(controller: ControllerBase, path: String): Unit = addController(path, controller) - def getControllers(): List[(ControllerBase, String)] = controllers.toList + def getControllers(): Seq[(ControllerBase, String)] = controllers.toSeq - def addJavaScript(path: String, script: String): Unit = { - javaScripts += ((path, script)) - } + def addJavaScript(path: String, script: String): Unit = javaScripts += ((path, script)) - def getJavaScript(currentPath: String): List[String] = { - javaScripts.filter(x => currentPath.matches(x._1)).toList.map(_._2) - } + def getJavaScript(currentPath: String): List[String] = javaScripts.filter(x => currentPath.matches(x._1)).toList.map(_._2) - def addRenderer(extension: String, renderer: Renderer): Unit = { - renderers += ((extension, renderer)) - } + def addRenderer(extension: String, renderer: Renderer): Unit = renderers += ((extension, renderer)) - def getRenderer(extension: String): Renderer = { - renderers.get(extension).getOrElse(DefaultRenderer) - } + def getRenderer(extension: String): Renderer = renderers.get(extension).getOrElse(DefaultRenderer) def renderableExtensions: Seq[String] = renderers.keys.toSeq - private case class GlobalAction( - method: String, - path: String, - function: (HttpServletRequest, HttpServletResponse, Context) => Any - ) + def addRepositoryRouting(routing: GitRepositoryRouting): Unit = repositoryRoutings += routing - private case class RepositoryAction( - method: String, - path: String, - function: (HttpServletRequest, HttpServletResponse, Context, RepositoryInfo) => Any - ) + def getRepositoryRoutings(): Seq[GitRepositoryRouting] = repositoryRoutings.toSeq + def getRepositoryRouting(repositoryPath: String): Option[GitRepositoryRouting] = { + PluginRegistry().getRepositoryRoutings().find { + case GitRepositoryRouting(urlPath, _, _) => { + repositoryPath.matches("/" + urlPath + "(/.*)?") + } + } + } + + def addReceiveHook(commitHook: ReceiveHook): Unit = receiveHooks += commitHook + + def getReceiveHooks: Seq[ReceiveHook] = receiveHooks.toSeq + + def addGlobalMenu(globalMenu: (Context) => Option[Link]): Unit = globalMenus += globalMenu + + def getGlobalMenus: Seq[(Context) => Option[Link]] = globalMenus.toSeq + + def addRepositoryMenu(repositoryMenu: (RepositoryInfo, Context) => Option[Link]): Unit = repositoryMenus += repositoryMenu + + def getRepositoryMenus: Seq[(RepositoryInfo, Context) => Option[Link]] = repositoryMenus.toSeq + + def addRepositorySettingTab(repositorySettingTab: (RepositoryInfo, Context) => Option[Link]): Unit = repositorySettingTabs += repositorySettingTab + + def getRepositorySettingTabs: Seq[(RepositoryInfo, Context) => Option[Link]] = repositorySettingTabs.toSeq + + def addProfileTab(profileTab: (Account, Context) => Option[Link]): Unit = profileTabs += profileTab + + def getProfileTabs: Seq[(Account, Context) => Option[Link]] = profileTabs.toSeq + + def addSystemSettingMenu(systemSettingMenu: (Context) => Option[Link]): Unit = systemSettingMenus += systemSettingMenu + + def getSystemSettingMenus: Seq[(Context) => Option[Link]] = systemSettingMenus.toSeq + + def addAccountSettingMenu(accountSettingMenu: (Context) => Option[Link]): Unit = accountSettingMenus += accountSettingMenu + + def getAccountSettingMenus: Seq[(Context) => Option[Link]] = accountSettingMenus.toSeq + + def addDashboardTab(dashboardTab: (Context) => Option[Link]): Unit = dashboardTabs += dashboardTab + + def getDashboardTabs: Seq[(Context) => Option[Link]] = dashboardTabs.toSeq + + def addAssetsMapping(assetsMapping: (String, String, ClassLoader)): Unit = assetsMappings += assetsMapping + + def getAssetsMappings: Seq[(String, String, ClassLoader)] = assetsMappings.toSeq + + def addTextDecorator(textDecorator: TextDecorator): Unit = textDecorators += textDecorator + + def getTextDecorators: Seq[TextDecorator] = textDecorators.toSeq + + def addSuggestionProvider(suggestionProvider: SuggestionProvider): Unit = suggestionProviders += suggestionProvider + + def getSuggestionProviders: Seq[SuggestionProvider] = suggestionProviders.toSeq } /** @@ -114,6 +162,8 @@ */ def initialize(context: ServletContext, settings: SystemSettings, conn: java.sql.Connection): Unit = { val pluginDir = new File(PluginHome) + val manager = new JDBCVersionManager(conn) + if(pluginDir.exists && pluginDir.isDirectory){ pluginDir.listFiles(new FilenameFilter { override def accept(dir: File, name: String): Boolean = name.endsWith(".jar") @@ -123,37 +173,29 @@ val plugin = classLoader.loadClass("Plugin").newInstance().asInstanceOf[Plugin] // Migration - val headVersion = plugin.versions.head - val currentVersion = conn.find("SELECT * FROM PLUGIN WHERE PLUGIN_ID = ?", plugin.pluginId)(_.getString("VERSION")) match { - case Some(x) => { - val dim = x.split("\\.") - Version(dim(0).toInt, dim(1).toInt) - } - case None => Version(0, 0) - } + val solidbase = new Solidbase() + solidbase.migrate(conn, classLoader, DatabaseConfig.liquiDriver, new Module(plugin.pluginId, plugin.versions: _*)) - Versions.update(conn, headVersion, currentVersion, plugin.versions, new URLClassLoader(Array(pluginJar.toURI.toURL))){ conn => - currentVersion.versionString match { - case "0.0" => - conn.update("INSERT INTO PLUGIN (PLUGIN_ID, VERSION) VALUES (?, ?)", plugin.pluginId, headVersion.versionString) - case _ => - conn.update("UPDATE PLUGIN SET VERSION = ? WHERE PLUGIN_ID = ?", headVersion.versionString, plugin.pluginId) - } + // Check version + val databaseVersion = manager.getCurrentVersion(plugin.pluginId) + val pluginVersion = plugin.versions.last.getVersion + if(databaseVersion != pluginVersion){ + throw new IllegalStateException(s"Plugin version is ${pluginVersion}, but database version is ${databaseVersion}") } // Initialize plugin.initialize(instance, context, settings) instance.addPlugin(PluginInfo( - pluginId = plugin.pluginId, - pluginName = plugin.pluginName, - version = plugin.versions.head.versionString, - description = plugin.description, - pluginClass = plugin + pluginId = plugin.pluginId, + pluginName = plugin.pluginName, + pluginVersion = plugin.versions.last.getVersion, + description = plugin.description, + pluginClass = plugin )) } catch { - case e: Exception => { - logger.error(s"Error during plugin initialization", e) + case e: Throwable => { + logger.error(s"Error during plugin initialization: ${pluginJar.getAbsolutePath}", e) } } } @@ -175,10 +217,12 @@ } +case class Link(id: String, label: String, path: String, icon: Option[String] = None) + case class PluginInfo( pluginId: String, pluginName: String, - version: String, + pluginVersion: String, description: String, pluginClass: Plugin ) diff --git a/src/main/scala/gitbucket/core/plugin/ReceiveHook.scala b/src/main/scala/gitbucket/core/plugin/ReceiveHook.scala new file mode 100644 index 0000000..125f6a2 --- /dev/null +++ b/src/main/scala/gitbucket/core/plugin/ReceiveHook.scala @@ -0,0 +1,15 @@ +package gitbucket.core.plugin + +import gitbucket.core.model.Profile._ +import org.eclipse.jgit.transport.{ReceivePack, ReceiveCommand} +import profile.simple._ + +trait ReceiveHook { + + def preReceive(owner: String, repository: String, receivePack: ReceivePack, command: ReceiveCommand, pusher: String) + (implicit session: Session): Option[String] = None + + def postReceive(owner: String, repository: String, receivePack: ReceivePack, command: ReceiveCommand, pusher: String) + (implicit session: Session): Unit = () + +} diff --git a/src/main/scala/gitbucket/core/plugin/Renderer.scala b/src/main/scala/gitbucket/core/plugin/Renderer.scala index ed8a42b..16fd533 100644 --- a/src/main/scala/gitbucket/core/plugin/Renderer.scala +++ b/src/main/scala/gitbucket/core/plugin/Renderer.scala @@ -20,7 +20,14 @@ object MarkdownRenderer extends Renderer { override def render(request: RenderRequest): Html = { import request._ - Html(Markdown.toHtml(fileContent, repository, enableWikiLink, enableRefsLink, enableAnchor)(context)) + Html(Markdown.toHtml( + markdown = fileContent, + repository = repository, + enableWikiLink = enableWikiLink, + enableRefsLink = enableRefsLink, + enableAnchor = enableAnchor, + enableLineBreaks = false + )(context)) } } @@ -35,11 +42,13 @@ } } -case class RenderRequest(filePath: List[String], - fileContent: String, - branch: String, - repository: RepositoryService.RepositoryInfo, - enableWikiLink: Boolean, - enableRefsLink: Boolean, - enableAnchor: Boolean, - context: Context) \ No newline at end of file +case class RenderRequest( + filePath: List[String], + fileContent: String, + branch: String, + repository: RepositoryService.RepositoryInfo, + enableWikiLink: Boolean, + enableRefsLink: Boolean, + enableAnchor: Boolean, + context: Context +) \ No newline at end of file diff --git a/src/main/scala/gitbucket/core/plugin/SuggestionProvider.scala b/src/main/scala/gitbucket/core/plugin/SuggestionProvider.scala new file mode 100644 index 0000000..3aafa11 --- /dev/null +++ b/src/main/scala/gitbucket/core/plugin/SuggestionProvider.scala @@ -0,0 +1,27 @@ +package gitbucket.core.plugin + +import gitbucket.core.controller.Context +import gitbucket.core.service.RepositoryService.RepositoryInfo + +trait SuggestionProvider { + + val id: String + val prefix: String + val suffix: String = " " + val context: Seq[String] + + def values(repository: RepositoryInfo): Seq[String] + def template(implicit context: Context): String = "value" + def additionalScript(implicit context: Context): String = "" + +} + +class UserNameSuggestionProvider extends SuggestionProvider { + override val id: String = "user" + override val prefix: String = "@" + override val context: Seq[String] = Seq("issues") + override def values(repository: RepositoryInfo): Seq[String] = Nil + override def template(implicit context: Context): String = "'@' + value" + override def additionalScript(implicit context: Context): String = + s"""$$.get('${context.path}/_user/proposals', { query: '', user: true, group: false }, function (data) { user = data.options; });""" +} \ No newline at end of file diff --git a/src/main/scala/gitbucket/core/plugin/TextDecorator.scala b/src/main/scala/gitbucket/core/plugin/TextDecorator.scala new file mode 100644 index 0000000..4aeef54 --- /dev/null +++ b/src/main/scala/gitbucket/core/plugin/TextDecorator.scala @@ -0,0 +1,10 @@ +package gitbucket.core.plugin + +import gitbucket.core.controller.Context +import gitbucket.core.service.RepositoryService.RepositoryInfo + +trait TextDecorator { + + def decorate(text: String, repository: RepositoryInfo)(implicit context: Context): String + +} diff --git a/src/main/scala/gitbucket/core/service/AccesTokenService.scala b/src/main/scala/gitbucket/core/service/AccesTokenService.scala deleted file mode 100644 index 0a8109d..0000000 --- a/src/main/scala/gitbucket/core/service/AccesTokenService.scala +++ /dev/null @@ -1,55 +0,0 @@ -package gitbucket.core.service - -import gitbucket.core.model.Profile._ -import profile.simple._ - -import gitbucket.core.model.{Account, AccessToken} -import gitbucket.core.util.StringUtil - -import scala.util.Random - - -trait AccessTokenService { - - def makeAccessTokenString: String = { - val bytes = new Array[Byte](20) - Random.nextBytes(bytes) - bytes.map("%02x".format(_)).mkString - } - - def tokenToHash(token: String): String = StringUtil.sha1(token) - - /** - * @retuen (TokenId, Token) - */ - def generateAccessToken(userName: String, note: String)(implicit s: Session): (Int, String) = { - var token: String = null - var hash: String = null - do{ - token = makeAccessTokenString - hash = tokenToHash(token) - }while(AccessTokens.filter(_.tokenHash === hash.bind).exists.run) - val newToken = AccessToken( - userName = userName, - note = note, - tokenHash = hash) - val tokenId = (AccessTokens returning AccessTokens.map(_.accessTokenId)) += newToken - (tokenId, token) - } - - def getAccountByAccessToken(token: String)(implicit s: Session): Option[Account] = - Accounts - .innerJoin(AccessTokens) - .filter{ case (ac, t) => (ac.userName === t.userName) && (t.tokenHash === tokenToHash(token).bind) && (ac.removed === false.bind) } - .map{ case (ac, t) => ac } - .firstOption - - def getAccessTokens(userName: String)(implicit s: Session): List[AccessToken] = - AccessTokens.filter(_.userName === userName.bind).sortBy(_.accessTokenId.desc).list - - def deleteAccessToken(userName: String, accessTokenId: Int)(implicit s: Session): Unit = - AccessTokens filter (t => t.userName === userName.bind && t.accessTokenId === accessTokenId) delete - -} - -object AccessTokenService extends AccessTokenService diff --git a/src/main/scala/gitbucket/core/service/AccessTokenService.scala b/src/main/scala/gitbucket/core/service/AccessTokenService.scala new file mode 100644 index 0000000..a2345be --- /dev/null +++ b/src/main/scala/gitbucket/core/service/AccessTokenService.scala @@ -0,0 +1,55 @@ +package gitbucket.core.service + +import gitbucket.core.model.Profile._ +import profile.simple._ + +import gitbucket.core.model.{Account, AccessToken} +import gitbucket.core.util.StringUtil + +import scala.util.Random + + +trait AccessTokenService { + + def makeAccessTokenString: String = { + val bytes = new Array[Byte](20) + Random.nextBytes(bytes) + bytes.map("%02x".format(_)).mkString + } + + def tokenToHash(token: String): String = StringUtil.sha1(token) + + /** + * @return (TokenId, Token) + */ + def generateAccessToken(userName: String, note: String)(implicit s: Session): (Int, String) = { + var token: String = null + var hash: String = null + do{ + token = makeAccessTokenString + hash = tokenToHash(token) + }while(AccessTokens.filter(_.tokenHash === hash.bind).exists.run) + val newToken = AccessToken( + userName = userName, + note = note, + tokenHash = hash) + val tokenId = (AccessTokens returning AccessTokens.map(_.accessTokenId)) += newToken + (tokenId, token) + } + + def getAccountByAccessToken(token: String)(implicit s: Session): Option[Account] = + Accounts + .innerJoin(AccessTokens) + .filter{ case (ac, t) => (ac.userName === t.userName) && (t.tokenHash === tokenToHash(token).bind) && (ac.removed === false.bind) } + .map{ case (ac, t) => ac } + .firstOption + + def getAccessTokens(userName: String)(implicit s: Session): List[AccessToken] = + AccessTokens.filter(_.userName === userName.bind).sortBy(_.accessTokenId.desc).list + + def deleteAccessToken(userName: String, accessTokenId: Int)(implicit s: Session): Unit = + AccessTokens filter (t => t.userName === userName.bind && t.accessTokenId === accessTokenId) delete + +} + +object AccessTokenService extends AccessTokenService diff --git a/src/main/scala/gitbucket/core/service/AccountService.scala b/src/main/scala/gitbucket/core/service/AccountService.scala index 5b5eb02..2357d57 100644 --- a/src/main/scala/gitbucket/core/service/AccountService.scala +++ b/src/main/scala/gitbucket/core/service/AccountService.scala @@ -97,6 +97,12 @@ Accounts filter (_.removed === false.bind) sortBy(_.userName) list } + def isLastAdministrator(account: Account)(implicit s: Session): Boolean = { + if(account.isAdmin){ + (Accounts filter (_.removed === false.bind) filter (_.isAdmin === true.bind) map (_.userName.length)).first == 1 + } else false + } + def createAccount(userName: String, password: String, fullName: String, mailAddress: String, isAdmin: Boolean, url: Option[String]) (implicit s: Session): Unit = Accounts insert Account( @@ -151,9 +157,7 @@ isRemoved = false, groupDescription = description) - def updateGroup(groupName: String, url: Option[String], - groupDescription: Option[String], removed: Boolean) - (implicit s: Session): Unit = + def updateGroup(groupName: String, url: Option[String], groupDescription: Option[String], removed: Boolean)(implicit s: Session): Unit = Accounts.filter(_.userName === groupName.bind) .map(t => (t.url.?, t.groupDescription.?, t.removed)) .update(url, groupDescription, removed) @@ -181,12 +185,11 @@ def removeUserRelatedData(userName: String)(implicit s: Session): Unit = { GroupMembers.filter(_.userName === userName.bind).delete Collaborators.filter(_.collaboratorName === userName.bind).delete - Repositories.filter(_.userName === userName.bind).delete } def getGroupNames(userName: String)(implicit s: Session): List[String] = { List(userName) ++ - Collaborators.filter(_.collaboratorName === userName.bind).sortBy(_.userName).map(_.userName).list + Collaborators.filter(_.collaboratorName === userName.bind).sortBy(_.userName).map(_.userName).list.distinct } } diff --git a/src/main/scala/gitbucket/core/service/CommitStatusService.scala b/src/main/scala/gitbucket/core/service/CommitStatusService.scala index 2ebea2b..1c491c9 100644 --- a/src/main/scala/gitbucket/core/service/CommitStatusService.scala +++ b/src/main/scala/gitbucket/core/service/CommitStatusService.scala @@ -5,48 +5,50 @@ import gitbucket.core.model.{CommitState, CommitStatus, Account} import gitbucket.core.util.Implicits._ -import gitbucket.core.util.StringUtil._ -import gitbucket.core.service.RepositoryService.RepositoryInfo - +import gitbucket.core.model.Profile.dateColumnType trait CommitStatusService { /** insert or update */ - def createCommitStatus(userName: String, repositoryName: String, sha:String, context:String, state:CommitState, targetUrl:Option[String], description:Option[String], now:java.util.Date, creator:Account)(implicit s: Session): Int = - CommitStatuses.filter(t => t.byCommit(userName, repositoryName, sha) && t.context===context.bind ) + def createCommitStatus(userName: String, repositoryName: String, sha: String, context: String, state: CommitState, + targetUrl: Option[String], description: Option[String], now: java.util.Date, creator: Account)(implicit s: Session): Int = + CommitStatuses + .filter(t => t.byCommit(userName, repositoryName, sha) && t.context === context.bind ) .map(_.commitStatusId).firstOption match { - case Some(id:Int) => { - CommitStatuses.filter(_.byPrimaryKey(id)).map{ - t => (t.state , t.targetUrl , t.updatedDate , t.creator, t.description) - }.update( (state, targetUrl, now, creator.userName, description) ) - id - } - case None => (CommitStatuses returning CommitStatuses.map(_.commitStatusId)) += CommitStatus( - userName = userName, - repositoryName = repositoryName, - commitId = sha, - context = context, - state = state, - targetUrl = targetUrl, - description = description, - creator = creator.userName, - registeredDate = now, - updatedDate = now) + case Some(id: Int) => { + CommitStatuses.filter(_.byPrimaryKey(id)).map { t => + (t.state , t.targetUrl , t.updatedDate , t.creator, t.description) + }.update((state, targetUrl, now, creator.userName, description)) + id + } + case None => (CommitStatuses returning CommitStatuses.map(_.commitStatusId)) += CommitStatus( + userName = userName, + repositoryName = repositoryName, + commitId = sha, + context = context, + state = state, + targetUrl = targetUrl, + description = description, + creator = creator.userName, + registeredDate = now, + updatedDate = now) } def getCommitStatus(userName: String, repositoryName: String, id: Int)(implicit s: Session) :Option[CommitStatus] = CommitStatuses.filter(t => t.byPrimaryKey(id) && t.byRepository(userName, repositoryName)).firstOption def getCommitStatus(userName: String, repositoryName: String, sha: String, context: String)(implicit s: Session) :Option[CommitStatus] = - CommitStatuses.filter(t => t.byCommit(userName, repositoryName, sha) && t.context===context.bind ).firstOption + CommitStatuses.filter(t => t.byCommit(userName, repositoryName, sha) && t.context === context.bind).firstOption def getCommitStatues(userName: String, repositoryName: String, sha: String)(implicit s: Session) :List[CommitStatus] = byCommitStatues(userName, repositoryName, sha).list + def getRecentStatuesContexts(userName: String, repositoryName: String, time: java.util.Date)(implicit s: Session) :List[String] = + CommitStatuses.filter(t => t.byRepository(userName, repositoryName)).filter(t => t.updatedDate > time.bind).groupBy(_.context).map(_._1).list + def getCommitStatuesWithCreator(userName: String, repositoryName: String, sha: String)(implicit s: Session) :List[(CommitStatus, Account)] = - byCommitStatues(userName, repositoryName, sha).innerJoin(Accounts) - .filter{ case (t,a) => t.creator === a.userName }.list + byCommitStatues(userName, repositoryName, sha).innerJoin(Accounts).filter { case (t, a) => t.creator === a.userName }.list protected def byCommitStatues(userName: String, repositoryName: String, sha: String)(implicit s: Session) = - CommitStatuses.filter(t => t.byCommit(userName, repositoryName, sha) ).sortBy(_.updatedDate desc) + CommitStatuses.filter(t => t.byCommit(userName, repositoryName, sha)).sortBy(_.updatedDate desc) -} \ No newline at end of file +} diff --git a/src/main/scala/gitbucket/core/service/CommitsService.scala b/src/main/scala/gitbucket/core/service/CommitsService.scala index fbef7cd..36b30cd 100644 --- a/src/main/scala/gitbucket/core/service/CommitsService.scala +++ b/src/main/scala/gitbucket/core/service/CommitsService.scala @@ -1,21 +1,18 @@ package gitbucket.core.service import gitbucket.core.model.CommitComment -import gitbucket.core.util.{StringUtil, Implicits} +import gitbucket.core.util.Implicits -import scala.slick.jdbc.{StaticQuery => Q} -import Q.interpolation import gitbucket.core.model.Profile._ import profile.simple._ import Implicits._ -import StringUtil._ trait CommitsService { - def getCommitComments(owner: String, repository: String, commitId: String, pullRequest: Boolean)(implicit s: Session) = + def getCommitComments(owner: String, repository: String, commitId: String, includePullRequest: Boolean)(implicit s: Session) = CommitComments filter { - t => t.byCommit(owner, repository, commitId) && (t.pullRequest === pullRequest || pullRequest) + t => t.byCommit(owner, repository, commitId) && (t.issueId.isEmpty || includePullRequest) } list def getCommitComment(owner: String, repository: String, commentId: String)(implicit s: Session) = @@ -27,7 +24,8 @@ None def createCommitComment(owner: String, repository: String, commitId: String, loginUser: String, - content: String, fileName: Option[String], oldLine: Option[Int], newLine: Option[Int], pullRequest: Boolean)(implicit s: Session): Int = + content: String, fileName: Option[String], oldLine: Option[Int], newLine: Option[Int], + issueId: Option[Int])(implicit s: Session): Int = CommitComments.autoInc insert CommitComment( userName = owner, repositoryName = repository, @@ -39,14 +37,20 @@ newLine = newLine, registeredDate = currentDate, updatedDate = currentDate, - pullRequest = pullRequest) + issueId = issueId) + + def updateCommitCommentPosition(commentId: Int, commitId: String, oldLine: Option[Int], newLine: Option[Int])(implicit s: Session): Unit = + CommitComments.filter(_.byPrimaryKey(commentId)) + .map { t => + (t.commitId, t.oldLine, t.newLine) + }.update(commitId, oldLine, newLine) def updateCommitComment(commentId: Int, content: String)(implicit s: Session) = CommitComments .filter (_.byPrimaryKey(commentId)) .map { t => - t.content -> t.updatedDate - }.update (content, currentDate) + t.content -> t.updatedDate + }.update (content, currentDate) def deleteCommitComment(commentId: Int)(implicit s: Session) = CommitComments filter (_.byPrimaryKey(commentId)) delete diff --git a/src/main/scala/gitbucket/core/service/HandleCommentService.scala b/src/main/scala/gitbucket/core/service/HandleCommentService.scala new file mode 100644 index 0000000..cc5e219 --- /dev/null +++ b/src/main/scala/gitbucket/core/service/HandleCommentService.scala @@ -0,0 +1,94 @@ +package gitbucket.core.service + +import gitbucket.core.controller.Context +import gitbucket.core.model.Issue +import gitbucket.core.model.Profile._ +import gitbucket.core.util.ControlUtil._ +import gitbucket.core.util.Implicits._ +import gitbucket.core.util.Notifier +import profile.simple._ + +trait HandleCommentService { + self: RepositoryService with IssuesService with ActivityService + with WebHookService with WebHookIssueCommentService with WebHookPullRequestService => + + /** + * @see [[https://github.com/gitbucket/gitbucket/wiki/CommentAction]] + */ + def handleComment(issue: Issue, content: Option[String], repository: RepositoryService.RepositoryInfo, actionOpt: Option[String]) + (implicit context: Context, s: Session) = { + context.loginAccount.flatMap { loginAccount => + defining(repository.owner, repository.name){ case (owner, name) => + val userName = loginAccount.userName + + val (action, recordActivity) = actionOpt + .collect { + case "close" if(!issue.closed) => true -> + (Some("close") -> Some(if(issue.isPullRequest) recordClosePullRequestActivity _ else recordCloseIssueActivity _)) + case "reopen" if(issue.closed) => false -> + (Some("reopen") -> Some(recordReopenIssueActivity _)) + } + .map { case (closed, t) => + updateClosed(owner, name, issue.issueId, closed) + t + } + .getOrElse(None -> None) + + val commentId = (content, action) match { + case (None, None) => None + case (None, Some(action)) => Some(createComment(owner, name, userName, issue.issueId, action.capitalize, action)) + case (Some(content), _) => Some(createComment(owner, name, userName, issue.issueId, content, action.map(_+ "_comment").getOrElse("comment"))) + } + + // record comment activity if comment is entered + content foreach { + (if(issue.isPullRequest) recordCommentPullRequestActivity _ else recordCommentIssueActivity _) + (owner, name, userName, issue.issueId, _) + } + recordActivity foreach ( _ (owner, name, userName, issue.issueId, issue.title) ) + + // extract references and create refer comment + content.map { content => + createReferComment(owner, name, issue, content, loginAccount) + } + + // call web hooks + action match { + case None => commentId.map { commentIdSome => callIssueCommentWebHook(repository, issue, commentIdSome, loginAccount) } + case Some(act) => { + val webHookAction = act match { + case "open" => "opened" + case "reopen" => "reopened" + case "close" => "closed" + case _ => act + } + if (issue.isPullRequest) { + callPullRequestWebHook(webHookAction, repository, issue.issueId, context.baseUrl, loginAccount) + } else { + callIssuesWebHook(webHookAction, repository, issue, context.baseUrl, loginAccount) + } + } + } + + // notifications + Notifier() match { + case f => + content foreach { + f.toNotify(repository, issue, _){ + Notifier.msgComment(s"${context.baseUrl}/${owner}/${name}/${ + if(issue.isPullRequest) "pull" else "issues"}/${issue.issueId}#comment-${commentId.get}") + } + } + action foreach { + f.toNotify(repository, issue, _){ + Notifier.msgStatus(s"${context.baseUrl}/${owner}/${name}/issues/${issue.issueId}") + } + } + } + + commentId.map( issue -> _ ) + } + } + } + +} diff --git a/src/main/scala/gitbucket/core/service/IssueCreationService.scala b/src/main/scala/gitbucket/core/service/IssueCreationService.scala new file mode 100644 index 0000000..de2459f --- /dev/null +++ b/src/main/scala/gitbucket/core/service/IssueCreationService.scala @@ -0,0 +1,74 @@ +package gitbucket.core.service + +import gitbucket.core.controller.Context +import gitbucket.core.model.{Account, Issue} +import gitbucket.core.model.Profile.profile.simple.Session +import gitbucket.core.service.RepositoryService.RepositoryInfo +import gitbucket.core.util.Notifier +import gitbucket.core.util.Implicits._ + +// TODO: Merged with IssuesService? +trait IssueCreationService { + + self: RepositoryService with WebHookIssueCommentService with LabelsService with IssuesService with ActivityService => + + def createIssue(repository: RepositoryInfo, title:String, body:Option[String], + assignee: Option[String], milestoneId: Option[Int], labelNames: Seq[String], + loginAccount: Account)(implicit context: Context, s: Session) : Issue = { + + val owner = repository.owner + val name = repository.name + val userName = loginAccount.userName + val manageable = isIssueManageable(repository) + + // insert issue + val issueId = insertIssue(owner, name, userName, title, body, + if (manageable) assignee else None, + if (manageable) milestoneId else None) + val issue: Issue = getIssue(owner, name, issueId.toString).get + + // insert labels + if (manageable) { + val labels = getLabels(owner, name) + labelNames.map { labelName => + labels.find(_.labelName == labelName).map { label => + registerIssueLabel(owner, name, issueId, label.labelId) + } + } + } + + // record activity + recordCreateIssueActivity(owner, name, userName, issueId, title) + + // extract references and create refer comment + createReferComment(owner, name, issue, title + " " + body.getOrElse(""), loginAccount) + + // call web hooks + callIssuesWebHook("opened", repository, issue, context.baseUrl, loginAccount) + + // notifications + Notifier().toNotify(repository, issue, body.getOrElse("")) { + Notifier.msgIssue(s"${context.baseUrl}/${owner}/${name}/issues/${issueId}") + } + issue + } + + /** + * Tests whether an logged-in user can manage issues. + */ + protected def isIssueManageable(repository: RepositoryInfo)(implicit context: Context, s: Session): Boolean = { + hasDeveloperRole(repository.owner, repository.name, context.loginAccount) + } + + /** + * Tests whether an logged-in user can post issues. + */ + protected def isIssueEditable(repository: RepositoryInfo)(implicit context: Context, s: Session): Boolean = { + repository.repository.options.issuesOption match { + case "ALL" => !repository.repository.isPrivate && context.loginAccount.isDefined + case "PUBLIC" => hasGuestRole(repository.owner, repository.name, context.loginAccount) + case "PRIVATE" => hasDeveloperRole(repository.owner, repository.name, context.loginAccount) + case "DISABLE" => false + } + } +} diff --git a/src/main/scala/gitbucket/core/service/IssuesService.scala b/src/main/scala/gitbucket/core/service/IssuesService.scala index 251abbd..c75a120 100644 --- a/src/main/scala/gitbucket/core/service/IssuesService.scala +++ b/src/main/scala/gitbucket/core/service/IssuesService.scala @@ -1,6 +1,8 @@ package gitbucket.core.service import gitbucket.core.model.Profile._ +import gitbucket.core.util.JGitUtil.CommitInfo +import gitbucket.core.util.StringUtil import profile.simple._ import gitbucket.core.util.StringUtil._ @@ -11,24 +13,32 @@ import Q.interpolation + trait IssuesService { + self: AccountService with RepositoryService => import IssuesService._ def getIssue(owner: String, repository: String, issueId: String)(implicit s: Session) = - if (issueId forall (_.isDigit)) + if (isInteger(issueId)) Issues filter (_.byPrimaryKey(owner, repository, issueId.toInt)) firstOption else None def getComments(owner: String, repository: String, issueId: Int)(implicit s: Session) = IssueComments filter (_.byIssue(owner, repository, issueId)) list - /** @return IssueComment and commentedUser */ - def getCommentsForApi(owner: String, repository: String, issueId: Int)(implicit s: Session): List[(IssueComment, Account)] = + /** @return IssueComment and commentedUser and Issue */ + def getCommentsForApi(owner: String, repository: String, issueId: Int)(implicit s: Session): List[(IssueComment, Account, Issue)] = IssueComments.filter(_.byIssue(owner, repository, issueId)) .filter(_.action inSetBind Set("comment" , "close_comment", "reopen_comment")) .innerJoin(Accounts).on( (t1, t2) => t1.commentedUserName === t2.userName ) + .innerJoin(Issues).on{ case ((t1, t2), t3) => t3.byIssue(t1.userName, t1.repositoryName, t1.issueId) } + .map{ case ((t1, t2), t3) => (t1, t2, t3) } .list + def getMergedComment(owner: String, repository: String, issueId: Int)(implicit s: Session): Option[(IssueComment, Account)] = { + getCommentsForApi(owner, repository, issueId).collectFirst { case (comment, account, _) if comment.action == "merged" => (comment, account) } + } + def getComment(owner: String, repository: String, commentId: String)(implicit s: Session) = if (commentId forall (_.isDigit)) IssueComments filter { t => @@ -90,7 +100,7 @@ def getCommitStatues(issueList:Seq[(String, String, Int)])(implicit s: Session) :Map[(String, String, Int), CommitStatusInfo] ={ if(issueList.isEmpty){ Map.empty - }else{ + } else { import scala.slick.jdbc._ val issueIdQuery = issueList.map(i => "(PR.USER_NAME=? AND PR.REPOSITORY_NAME=? AND PR.ISSUE_ID=?)").mkString(" OR ") implicit val qset = SetParameter[Seq[(String, String, Int)]] { @@ -101,27 +111,42 @@ pp.setInt(a._3) } } - import gitbucket.core.model.Profile.commitStateColumnType val query = Q.query[Seq[(String, String, Int)], (String, String, Int, Int, Int, Option[String], Option[CommitState], Option[String], Option[String])](s""" - SELECT SUMM.USER_NAME, SUMM.REPOSITORY_NAME, SUMM.ISSUE_ID, CS_ALL, CS_SUCCESS - , CSD.CONTEXT, CSD.STATE, CSD.TARGET_URL, CSD.DESCRIPTION - FROM (SELECT - PR.USER_NAME - , PR.REPOSITORY_NAME - , PR.ISSUE_ID - , COUNT(CS.STATE) AS CS_ALL - , SUM(CS.STATE='success') AS CS_SUCCESS - , PR.COMMIT_ID_TO AS COMMIT_ID + SELECT + SUMM.USER_NAME, + SUMM.REPOSITORY_NAME, + SUMM.ISSUE_ID, + CS_ALL, + CS_SUCCESS, + CSD.CONTEXT, + CSD.STATE, + CSD.TARGET_URL, + CSD.DESCRIPTION + FROM ( + SELECT + PR.USER_NAME, + PR.REPOSITORY_NAME, + PR.ISSUE_ID, + COUNT(CS.STATE) AS CS_ALL, + CSS.CS_SUCCESS AS CS_SUCCESS, + PR.COMMIT_ID_TO AS COMMIT_ID FROM PULL_REQUEST PR JOIN COMMIT_STATUS CS - ON PR.USER_NAME=CS.USER_NAME - AND PR.REPOSITORY_NAME=CS.REPOSITORY_NAME - AND PR.COMMIT_ID_TO=CS.COMMIT_ID - WHERE $issueIdQuery - GROUP BY PR.USER_NAME, PR.REPOSITORY_NAME, PR.ISSUE_ID) as SUMM + ON PR.USER_NAME = CS.USER_NAME AND PR.REPOSITORY_NAME = CS.REPOSITORY_NAME AND PR.COMMIT_ID_TO = CS.COMMIT_ID + JOIN ( + SELECT + COUNT(*) AS CS_SUCCESS, + USER_NAME, + REPOSITORY_NAME, + COMMIT_ID + FROM COMMIT_STATUS WHERE STATE = 'success' GROUP BY USER_NAME, REPOSITORY_NAME, COMMIT_ID + ) CSS ON PR.USER_NAME = CSS.USER_NAME AND PR.REPOSITORY_NAME = CSS.REPOSITORY_NAME AND PR.COMMIT_ID_TO = CSS.COMMIT_ID + WHERE $issueIdQuery + GROUP BY PR.USER_NAME, PR.REPOSITORY_NAME, PR.ISSUE_ID, CSS.CS_SUCCESS + ) as SUMM LEFT OUTER JOIN COMMIT_STATUS CSD ON SUMM.CS_ALL = 1 AND SUMM.COMMIT_ID = CSD.COMMIT_ID"""); - query(issueList).list.map{ + query(issueList).list.map { case(userName, repositoryName, issueId, count, successCount, context, state, targetUrl, description) => (userName, repositoryName, issueId) -> CommitStatusInfo(count, successCount, context, state, targetUrl, description) }.toMap @@ -142,66 +167,75 @@ (implicit s: Session): List[IssueInfo] = { // get issues and comment count and labels val result = searchIssueQueryBase(condition, pullRequest, offset, limit, repos) - .leftJoin (IssueLabels) .on { case ((t1, t2), t3) => t1.byIssue(t3.userName, t3.repositoryName, t3.issueId) } - .leftJoin (Labels) .on { case (((t1, t2), t3), t4) => t3.byLabel(t4.userName, t4.repositoryName, t4.labelId) } - .leftJoin (Milestones) .on { case ((((t1, t2), t3), t4), t5) => t1.byMilestone(t5.userName, t5.repositoryName, t5.milestoneId) } - .map { case ((((t1, t2), t3), t4), t5) => - (t1, t2.commentCount, t4.labelId.?, t4.labelName.?, t4.color.?, t5.title.?) - } - .list - .splitWith { (c1, c2) => - c1._1.userName == c2._1.userName && - c1._1.repositoryName == c2._1.repositoryName && - c1._1.issueId == c2._1.issueId - } + .leftJoin (IssueLabels) .on { case (((t1, t2), i), t3) => t1.byIssue(t3.userName, t3.repositoryName, t3.issueId) } + .leftJoin (Labels) .on { case ((((t1, t2), i), t3), t4) => t3.byLabel(t4.userName, t4.repositoryName, t4.labelId) } + .leftJoin (Milestones) .on { case (((((t1, t2), i), t3), t4), t5) => t1.byMilestone(t5.userName, t5.repositoryName, t5.milestoneId) } + .sortBy { case (((((t1, t2), i), t3), t4), t5) => i asc } + .map { case (((((t1, t2), i), t3), t4), t5) => (t1, t2.commentCount, t4.labelId.?, t4.labelName.?, t4.color.?, t5.title.?) } + .list + .splitWith { (c1, c2) => c1._1.userName == c2._1.userName && c1._1.repositoryName == c2._1.repositoryName && c1._1.issueId == c2._1.issueId } + val status = getCommitStatues(result.map(_.head._1).map(is => (is.userName, is.repositoryName, is.issueId))) result.map { issues => issues.head match { - case (issue, commentCount, _, _, _, milestone) => - IssueInfo(issue, - issues.flatMap { t => t._3.map ( - Label(issue.userName, issue.repositoryName, _, t._4.get, t._5.get) - )} toList, - milestone, - commentCount, - status.get(issue.userName, issue.repositoryName, issue.issueId)) - }} toList + case (issue, commentCount, _, _, _, milestone) => + IssueInfo(issue, + issues.flatMap { t => t._3.map ( + Label(issue.userName, issue.repositoryName, _, t._4.get, t._5.get) + )} toList, + milestone, + commentCount, + status.get(issue.userName, issue.repositoryName, issue.issueId)) + }} toList + } + + /** for api + * @return (issue, issueUser, commentCount) + */ + def searchIssueByApi(condition: IssueSearchCondition, offset: Int, limit: Int, repos: (String, String)*) + (implicit s: Session): List[(Issue, Account)] = { + // get issues and comment count and labels + searchIssueQueryBase(condition, false, offset, limit, repos) + .innerJoin(Accounts).on { case (((t1, t2), i), t3) => t3.userName === t1.openedUserName } + .sortBy { case (((t1, t2), i), t3) => i asc } + .map { case (((t1, t2), i), t3) => (t1, t3) } + .list } /** for api * @return (issue, issueUser, commentCount, pullRequest, headRepo, headOwner) */ def searchPullRequestByApi(condition: IssueSearchCondition, offset: Int, limit: Int, repos: (String, String)*) - (implicit s: Session): List[(Issue, Account, Int, PullRequest, Repository, Account)] = { + (implicit s: Session): List[(Issue, Account, Int, PullRequest, Repository, Account)] = { // get issues and comment count and labels searchIssueQueryBase(condition, true, offset, limit, repos) - .innerJoin(PullRequests).on { case ((t1, t2), t3) => t3.byPrimaryKey(t1.userName, t1.repositoryName, t1.issueId) } - .innerJoin(Repositories).on { case (((t1, t2), t3), t4) => t4.byRepository(t1.userName, t1.repositoryName) } - .innerJoin(Accounts).on { case ((((t1, t2), t3), t4), t5) => t5.userName === t1.openedUserName } - .innerJoin(Accounts).on { case (((((t1, t2), t3), t4), t5), t6) => t6.userName === t4.userName } - .map { case (((((t1, t2), t3), t4), t5), t6) => - (t1, t5, t2.commentCount, t3, t4, t6) - } + .innerJoin(PullRequests).on { case (((t1, t2), i), t3) => t3.byPrimaryKey(t1.userName, t1.repositoryName, t1.issueId) } + .innerJoin(Repositories).on { case ((((t1, t2), i), t3), t4) => t4.byRepository(t1.userName, t1.repositoryName) } + .innerJoin(Accounts).on { case (((((t1, t2), i), t3), t4), t5) => t5.userName === t1.openedUserName } + .innerJoin(Accounts).on { case ((((((t1, t2), i), t3), t4), t5), t6) => t6.userName === t4.userName } + .sortBy { case ((((((t1, t2), i), t3), t4), t5), t6) => i asc } + .map { case ((((((t1, t2), i), t3), t4), t5), t6) => (t1, t5, t2.commentCount, t3, t4, t6) } .list } private def searchIssueQueryBase(condition: IssueSearchCondition, pullRequest: Boolean, offset: Int, limit: Int, repos: Seq[(String, String)]) - (implicit s: Session) = + (implicit s: Session) = searchIssueQuery(repos, condition, pullRequest) - .innerJoin(IssueOutline).on { (t1, t2) => t1.byIssue(t2.userName, t2.repositoryName, t2.issueId) } - .sortBy { case (t1, t2) => - (condition.sort match { - case "created" => t1.registeredDate - case "comments" => t2.commentCount - case "updated" => t1.updatedDate - }) match { - case sort => condition.direction match { - case "asc" => sort asc - case "desc" => sort desc - } + .innerJoin(IssueOutline).on { (t1, t2) => t1.byIssue(t2.userName, t2.repositoryName, t2.issueId) } + .sortBy { case (t1, t2) => t1.issueId desc } + .sortBy { case (t1, t2) => + (condition.sort match { + case "created" => t1.registeredDate + case "comments" => t2.commentCount + case "updated" => t1.updatedDate + }) match { + case sort => condition.direction match { + case "asc" => sort asc + case "desc" => sort desc } } - .drop(offset).take(limit) + } + .drop(offset).take(limit).zipWithIndex /** @@ -213,9 +247,8 @@ .map { case (owner, repository) => t1.byRepository(owner, repository) } .foldLeft[Column[Boolean]](false) ( _ || _ ) && (t1.closed === (condition.state == "closed").bind) && - //(t1.milestoneId === condition.milestoneId.get.get.bind, condition.milestoneId.flatten.isDefined) && - (t1.milestoneId.? isEmpty, condition.milestone == Some(None)) && - (t1.assignedUserName === condition.assigned.get.bind, condition.assigned.isDefined) && + (t1.milestoneId.? isEmpty, condition.milestone == Some(None)) && + (t1.assignedUserName.? isEmpty, condition.assigned == Some(None)) && (t1.openedUserName === condition.author.get.bind, condition.author.isDefined) && (t1.pullRequest === pullRequest.bind) && // Milestone filter @@ -223,6 +256,8 @@ (t2.byPrimaryKey(t1.userName, t1.repositoryName, t1.milestoneId)) && (t2.title === condition.milestone.get.get.bind) } exists, condition.milestone.flatten.isDefined) && + // Assignee filter + (t1.assignedUserName === condition.assigned.get.get.bind, condition.assigned.flatten.isDefined) && // Label filter (IssueLabels filter { t2 => (t2.byIssue(t1.userName, t1.repositoryName, t1.issueId)) && @@ -246,7 +281,7 @@ } exists), condition.mentioned.isDefined) } - def createIssue(owner: String, repository: String, loginUser: String, title: String, content: Option[String], + def insertIssue(owner: String, repository: String, loginUser: String, title: String, content: Option[String], assignedUserName: Option[String], milestoneId: Option[Int], isPullRequest: Boolean = false)(implicit s: Session) = // next id number @@ -392,18 +427,46 @@ } } } + + def createReferComment(owner: String, repository: String, fromIssue: Issue, message: String, loginAccount: Account)(implicit s: Session) = { + StringUtil.extractIssueId(message).foreach { issueId => + val content = fromIssue.issueId + ":" + fromIssue.title + if(getIssue(owner, repository, issueId).isDefined){ + // Not add if refer comment already exist. + if(!getComments(owner, repository, issueId.toInt).exists { x => x.action == "refer" && x.content == content }) { + createComment(owner, repository, loginAccount.userName, issueId.toInt, content, "refer") + } + } + } + } + + def createIssueComment(owner: String, repository: String, commit: CommitInfo)(implicit s: Session) = { + StringUtil.extractIssueId(commit.fullMessage).foreach { issueId => + if(getIssue(owner, repository, issueId).isDefined){ + getAccountByMailAddress(commit.committerEmailAddress).foreach { account => + createComment(owner, repository, account.userName, issueId.toInt, commit.fullMessage + " " + commit.id, "commit") + } + } + } + } + + def getAssignableUserNames(owner: String, repository: String)(implicit s: Session): List[String] = { + (getCollaboratorUserNames(owner, repository, Seq(Role.ADMIN, Role.DEVELOPER)) ::: + (if (getAccountByUserName(owner).get.isGroupAccount) getGroupMembers(owner).map(_.userName) else List(owner))).distinct.sorted + } + } object IssuesService { import javax.servlet.http.HttpServletRequest - val IssueLimit = 30 + val IssueLimit = 25 case class IssueSearchCondition( labels: Set[String] = Set.empty, milestone: Option[Option[String]] = None, author: Option[String] = None, - assigned: Option[String] = None, + assigned: Option[Option[String]] = None, mentioned: Option[String] = None, state: String = "open", sort: String = "created", @@ -447,12 +510,15 @@ def toURL: String = "?" + List( if(labels.isEmpty) None else Some("labels=" + urlEncode(labels.mkString(","))), - milestone.map { _ match { + milestone.map { case Some(x) => "milestone=" + urlEncode(x) case None => "milestone=none" - }}, + }, author .map(x => "author=" + urlEncode(x)), - assigned .map(x => "assigned=" + urlEncode(x)), + assigned.map { + case Some(x) => "assigned=" + urlEncode(x) + case None => "assigned=none" + }, mentioned.map(x => "mentioned=" + urlEncode(x)), Some("state=" + urlEncode(state)), Some("sort=" + urlEncode(sort)), @@ -471,44 +537,6 @@ } /** - * Restores IssueSearchCondition instance from filter query. - */ - def apply(filter: String, milestones: Map[String, Int]): IssueSearchCondition = { - val conditions = filter.split("[  \t]+").map { x => - val dim = x.split(":") - dim(0) -> dim(1) - }.groupBy(_._1).map { case (key, values) => - key -> values.map(_._2).toSeq - } - - val (sort, direction) = conditions.get("sort").flatMap(_.headOption).getOrElse("created-desc") match { - case "created-asc" => ("created" , "asc" ) - case "comments-desc" => ("comments", "desc") - case "comments-asc" => ("comments", "asc" ) - case "updated-desc" => ("comments", "desc") - case "updated-asc" => ("comments", "asc" ) - case _ => ("created" , "desc") - } - - IssueSearchCondition( - conditions.get("label").map(_.toSet).getOrElse(Set.empty), - conditions.get("milestone").flatMap(_.headOption) match { - case None => None - case Some("none") => Some(None) - case Some(x) => Some(Some(x)) //milestones.get(x).map(x => Some(x)) - }, - conditions.get("author").flatMap(_.headOption), - conditions.get("assignee").flatMap(_.headOption), - conditions.get("mentions").flatMap(_.headOption), - conditions.get("is").getOrElse(Seq.empty).find(x => x == "open" || x == "closed").getOrElse("open"), - sort, - direction, - conditions.get("visibility").flatMap(_.headOption), - conditions.get("group").map(_.toSet).getOrElse(Set.empty) - ) - } - - /** * Restores IssueSearchCondition instance from request parameters. */ def apply(request: HttpServletRequest): IssueSearchCondition = @@ -519,7 +547,10 @@ case x => Some(x) }, param(request, "author"), - param(request, "assigned"), + param(request, "assigned").map { + case "none" => None + case x => Some(x) + }, param(request, "mentioned"), param(request, "state", Seq("open", "closed")).getOrElse("open"), param(request, "sort", Seq("created", "comments", "updated")).getOrElse("created"), diff --git a/src/main/scala/gitbucket/core/service/LabelsService.scala b/src/main/scala/gitbucket/core/service/LabelsService.scala index 35b5d2d..f8026e0 100644 --- a/src/main/scala/gitbucket/core/service/LabelsService.scala +++ b/src/main/scala/gitbucket/core/service/LabelsService.scala @@ -12,6 +12,9 @@ def getLabel(owner: String, repository: String, labelId: Int)(implicit s: Session): Option[Label] = Labels.filter(_.byPrimaryKey(owner, repository, labelId)).firstOption + def getLabel(owner: String, repository: String, labelName: String)(implicit s: Session): Option[Label] = + Labels.filter(_.byLabel(owner, repository, labelName)).firstOption + def createLabel(owner: String, repository: String, labelName: String, color: String)(implicit s: Session): Int = Labels returning Labels.map(_.labelId) += Label( userName = owner, diff --git a/src/main/scala/gitbucket/core/service/MergeService.scala b/src/main/scala/gitbucket/core/service/MergeService.scala index 4b6cc3a..d58a4ec 100644 --- a/src/main/scala/gitbucket/core/service/MergeService.scala +++ b/src/main/scala/gitbucket/core/service/MergeService.scala @@ -1,19 +1,16 @@ package gitbucket.core.service import gitbucket.core.model.Account -import gitbucket.core.util.LockUtil import gitbucket.core.util.Directory._ -import gitbucket.core.util.Implicits._ import gitbucket.core.util.ControlUtil._ import org.eclipse.jgit.merge.MergeStrategy import org.eclipse.jgit.api.Git import org.eclipse.jgit.transport.RefSpec import org.eclipse.jgit.errors.NoMergeBaseException -import org.eclipse.jgit.lib.{ObjectId, CommitBuilder, PersonIdent} +import org.eclipse.jgit.lib.{ObjectId, CommitBuilder, PersonIdent, Repository} import org.eclipse.jgit.revwalk.RevWalk - trait MergeService { import MergeService._ /** @@ -52,26 +49,30 @@ /** * Checks whether conflict will be caused in merging. Returns true if conflict will be caused. */ - def checkConflict(userName: String, repositoryName: String, branch: String, - requestUserName: String, requestRepositoryName: String, requestBranch: String): Boolean = { - using(Git.open(getRepositoryDir(requestUserName, requestRepositoryName))) { git => - val remoteRefName = s"refs/heads/${branch}" - val tmpRefName = s"refs/merge-check/${userName}/${branch}" + def tryMergeRemote(localUserName: String, localRepositoryName: String, localBranch: String, + remoteUserName: String, remoteRepositoryName: String, remoteBranch: String): Option[(ObjectId, ObjectId, ObjectId)] = { + using(Git.open(getRepositoryDir(localUserName, localRepositoryName))) { git => + val remoteRefName = s"refs/heads/${remoteBranch}" + val tmpRefName = s"refs/remote-temp/${remoteUserName}/${remoteRepositoryName}/${remoteBranch}" val refSpec = new RefSpec(s"${remoteRefName}:${tmpRefName}").setForceUpdate(true) try { // fetch objects from origin repository branch git.fetch - .setRemote(getRepositoryDir(userName, repositoryName).toURI.toString) + .setRemote(getRepositoryDir(remoteUserName, remoteRepositoryName).toURI.toString) .setRefSpecs(refSpec) .call // merge conflict check val merger = MergeStrategy.RECURSIVE.newMerger(git.getRepository, true) - val mergeBaseTip = git.getRepository.resolve(s"refs/heads/${requestBranch}") + val mergeBaseTip = git.getRepository.resolve(s"refs/heads/${localBranch}") val mergeTip = git.getRepository.resolve(tmpRefName) try { - !merger.merge(mergeBaseTip, mergeTip) + if(merger.merge(mergeBaseTip, mergeTip)){ + Some((merger.getResultTreeId, mergeBaseTip, mergeTip)) + } else { + None + } } catch { - case e: NoMergeBaseException => true + case e: NoMergeBaseException => None } } finally { val refUpdate = git.getRepository.updateRef(refSpec.getDestination) @@ -80,8 +81,54 @@ } } } + /** + * Checks whether conflict will be caused in merging. Returns true if conflict will be caused. + */ + def checkConflict(userName: String, repositoryName: String, branch: String, + requestUserName: String, requestRepositoryName: String, requestBranch: String): Boolean = + tryMergeRemote(userName, repositoryName, branch, requestUserName, requestRepositoryName, requestBranch).isEmpty + + def pullRemote(localUserName: String, localRepositoryName: String, localBranch: String, + remoteUserName: String, remoteRepositoryName: String, remoteBranch: String, + loginAccount: Account, message: String): Option[ObjectId] = { + tryMergeRemote(localUserName, localRepositoryName, localBranch, remoteUserName, remoteRepositoryName, remoteBranch).map{ case (newTreeId, oldBaseId, oldHeadId) => + using(Git.open(getRepositoryDir(localUserName, localRepositoryName))) { git => + val committer = new PersonIdent(loginAccount.fullName, loginAccount.mailAddress) + val newCommit = Util.createMergeCommit(git.getRepository, newTreeId, committer, message, Seq(oldBaseId, oldHeadId)) + Util.updateRefs(git.getRepository, s"refs/heads/${localBranch}", newCommit, false, committer, Some("merge")) + } + oldBaseId + } + } + } object MergeService{ + object Util{ + // return treeId + def createMergeCommit(repository: Repository, treeId: ObjectId, committer: PersonIdent, message: String, parents: Seq[ObjectId]): ObjectId = { + val mergeCommit = new CommitBuilder() + mergeCommit.setTreeId(treeId) + mergeCommit.setParentIds(parents:_*) + mergeCommit.setAuthor(committer) + mergeCommit.setCommitter(committer) + mergeCommit.setMessage(message) + // insertObject and got mergeCommit Object Id + val inserter = repository.newObjectInserter + val mergeCommitId = inserter.insert(mergeCommit) + inserter.flush() + inserter.close() + mergeCommitId + } + def updateRefs(repository: Repository, ref: String, newObjectId: ObjectId, force: Boolean, committer: PersonIdent, refLogMessage: Option[String] = None):Unit = { + // update refs + val refUpdate = repository.updateRef(ref) + refUpdate.setNewObjectId(newObjectId) + refUpdate.setForceUpdate(force) + refUpdate.setRefLogIdent(committer) + refLogMessage.map(refUpdate.setRefLogMessage(_, true)) + refUpdate.update() + } + } case class MergeCacheInfo(git:Git, branch:String, issueId:Int){ val repository = git.getRepository val mergedBranchName = s"refs/pull/${issueId}/merge" @@ -90,17 +137,17 @@ lazy val mergeTip = repository.resolve(s"refs/pull/${issueId}/head") def checkConflictCache(): Option[Boolean] = { Option(repository.resolve(mergedBranchName)).flatMap{ merged => - if(parseCommit( merged ).getParents().toSet == Set( mergeBaseTip, mergeTip )){ + if(parseCommit(merged).getParents().toSet == Set( mergeBaseTip, mergeTip )){ // merged branch exists Some(false) - }else{ + } else { None } }.orElse(Option(repository.resolve(conflictedBranchName)).flatMap{ conflicted => - if(parseCommit( conflicted ).getParents().toSet == Set( mergeBaseTip, mergeTip )){ + if(parseCommit(conflicted).getParents().toSet == Set( mergeBaseTip, mergeTip )){ // conflict branch exists Some(true) - }else{ + } else { None } }) @@ -120,17 +167,12 @@ def updateBranch(treeId:ObjectId, message:String, branchName:String){ // creates merge commit val mergeCommitId = createMergeCommit(treeId, committer, message) - // update refs - val refUpdate = repository.updateRef(branchName) - refUpdate.setNewObjectId(mergeCommitId) - refUpdate.setForceUpdate(true) - refUpdate.setRefLogIdent(committer) - refUpdate.update() + Util.updateRefs(repository, branchName, mergeCommitId, true, committer) } if(!conflicted){ updateBranch(merger.getResultTreeId, s"Merge ${mergeTip.name} into ${mergeBaseTip.name}", mergedBranchName) git.branchDelete().setForce(true).setBranchNames(conflictedBranchName).call() - }else{ + } else { updateBranch(mergeTipCommit.getTree().getId(), s"can't merge ${mergeTip.name} into ${mergeBaseTip.name}", conflictedBranchName) git.branchDelete().setForce(true).setBranchNames(mergedBranchName).call() } @@ -145,28 +187,12 @@ // creates merge commit val mergeCommitId = createMergeCommit(mergeResultCommit.getTree().getId(), committer, message) // update refs - val refUpdate = repository.updateRef(s"refs/heads/${branch}") - refUpdate.setNewObjectId(mergeCommitId) - refUpdate.setForceUpdate(false) - refUpdate.setRefLogIdent(committer) - refUpdate.setRefLogMessage("merged", true) - refUpdate.update() + Util.updateRefs(repository, s"refs/heads/${branch}", mergeCommitId, false, committer, Some("merged")) } // return treeId - private def createMergeCommit(treeId:ObjectId, committer:PersonIdent, message:String) = { - val mergeCommit = new CommitBuilder() - mergeCommit.setTreeId(treeId) - mergeCommit.setParentIds(Array[ObjectId](mergeBaseTip, mergeTip): _*) - mergeCommit.setAuthor(committer) - mergeCommit.setCommitter(committer) - mergeCommit.setMessage(message) - // insertObject and got mergeCommit Object Id - val inserter = repository.newObjectInserter - val mergeCommitId = inserter.insert(mergeCommit) - inserter.flush() - inserter.release() - mergeCommitId - } + private def createMergeCommit(treeId: ObjectId, committer: PersonIdent, message: String) = + Util.createMergeCommit(repository, treeId, committer, message, Seq[ObjectId](mergeBaseTip, mergeTip)) + private def parseCommit(id:ObjectId) = using(new RevWalk( repository ))(_.parseCommit(id)) } -} \ No newline at end of file +} diff --git a/src/main/scala/gitbucket/core/service/MilestonesService.scala b/src/main/scala/gitbucket/core/service/MilestonesService.scala index 691ca2e..598c3ce 100644 --- a/src/main/scala/gitbucket/core/service/MilestonesService.scala +++ b/src/main/scala/gitbucket/core/service/MilestonesService.scala @@ -41,7 +41,7 @@ def getMilestonesWithIssueCount(owner: String, repository: String)(implicit s: Session): List[(Milestone, Int, Int)] = { val counts = Issues - .filter { t => (t.byRepository(owner, repository)) && (t.milestoneId.? isDefined) } + .filter { t => t.byRepository(owner, repository) && (t.milestoneId.? isDefined) } .groupBy { t => t.milestoneId -> t.closed } .map { case (t1, t2) => t1._1 -> t1._2 -> t2.length } .toMap @@ -52,6 +52,6 @@ } def getMilestones(owner: String, repository: String)(implicit s: Session): List[Milestone] = - Milestones.filter(_.byRepository(owner, repository)).sortBy(_.milestoneId asc).list + Milestones.filter(_.byRepository(owner, repository)).sortBy(t => (t.dueDate.asc, t.closedDate.desc, t.milestoneId.desc)).list } diff --git a/src/main/scala/gitbucket/core/service/PluginService.scala b/src/main/scala/gitbucket/core/service/PluginService.scala deleted file mode 100644 index 99a20d8..0000000 --- a/src/main/scala/gitbucket/core/service/PluginService.scala +++ /dev/null @@ -1,24 +0,0 @@ -package gitbucket.core.service - -import gitbucket.core.model.Plugin -import gitbucket.core.model.Profile._ -import profile.simple._ - -trait PluginService { - - def getPlugins()(implicit s: Session): List[Plugin] = - Plugins.sortBy(_.pluginId).list - - def registerPlugin(plugin: Plugin)(implicit s: Session): Unit = - Plugins.insert(plugin) - - def updatePlugin(plugin: Plugin)(implicit s: Session): Unit = - Plugins.filter(_.pluginId === plugin.pluginId.bind).map(_.version).update(plugin.version) - - def deletePlugin(pluginId: String)(implicit s: Session): Unit = - Plugins.filter(_.pluginId === pluginId.bind).delete - - def getPlugin(pluginId: String)(implicit s: Session): Option[Plugin] = - Plugins.filter(_.pluginId === pluginId.bind).firstOption - -} diff --git a/src/main/scala/gitbucket/core/service/ProtectedBranchService.scala b/src/main/scala/gitbucket/core/service/ProtectedBranchService.scala new file mode 100644 index 0000000..07cbb2a --- /dev/null +++ b/src/main/scala/gitbucket/core/service/ProtectedBranchService.scala @@ -0,0 +1,121 @@ +package gitbucket.core.service + +import gitbucket.core.model._ +import gitbucket.core.model.Profile._ +import gitbucket.core.plugin.ReceiveHook +import profile.simple._ + +import org.eclipse.jgit.transport.{ReceivePack, ReceiveCommand} + + +trait ProtectedBranchService { + import ProtectedBranchService._ + private def getProtectedBranchInfoOpt(owner: String, repository: String, branch: String)(implicit session: Session): Option[ProtectedBranchInfo] = + ProtectedBranches + .leftJoin(ProtectedBranchContexts) + .on{ case (pb, c) => pb.byBranch(c.userName, c.repositoryName, c.branch) } + .map{ case (pb, c) => pb -> c.context.? } + .filter(_._1.byPrimaryKey(owner, repository, branch)) + .list + .groupBy(_._1) + .map(p => p._1 -> p._2.flatMap(_._2)) + .map{ case (t1, contexts) => + new ProtectedBranchInfo(t1.userName, t1.repositoryName, true, contexts, t1.statusCheckAdmin) + }.headOption + + def getProtectedBranchInfo(owner: String, repository: String, branch: String)(implicit session: Session): ProtectedBranchInfo = + getProtectedBranchInfoOpt(owner, repository, branch).getOrElse(ProtectedBranchInfo.disabled(owner, repository)) + + def getProtectedBranchList(owner: String, repository: String)(implicit session: Session): List[String] = + ProtectedBranches.filter(_.byRepository(owner, repository)).map(_.branch).list + + def enableBranchProtection(owner: String, repository: String, branch:String, includeAdministrators: Boolean, contexts: Seq[String]) + (implicit session: Session): Unit = { + disableBranchProtection(owner, repository, branch) + ProtectedBranches.insert(new ProtectedBranch(owner, repository, branch, includeAdministrators && contexts.nonEmpty)) + contexts.map{ context => + ProtectedBranchContexts.insert(new ProtectedBranchContext(owner, repository, branch, context)) + } + } + + def disableBranchProtection(owner: String, repository: String, branch:String)(implicit session: Session): Unit = + ProtectedBranches.filter(_.byPrimaryKey(owner, repository, branch)).delete + +} + +object ProtectedBranchService { + + class ProtectedBranchReceiveHook extends ReceiveHook with ProtectedBranchService { + override def preReceive(owner: String, repository: String, receivePack: ReceivePack, command: ReceiveCommand, pusher: String) + (implicit session: Session): Option[String] = { + val branch = command.getRefName.stripPrefix("refs/heads/") + if(branch != command.getRefName){ + getProtectedBranchInfo(owner, repository, branch).getStopReason(receivePack.isAllowNonFastForwards, command, pusher) + } else { + None + } + } + } + + + case class ProtectedBranchInfo( + owner: String, + repository: String, + enabled: Boolean, + /** + * Require status checks to pass before merging + * Choose which status checks must pass before branches can be merged into test. + * When enabled, commits must first be pushed to another branch, + * then merged or pushed directly to test after status checks have passed. + */ + contexts: Seq[String], + /** + * Include administrators + * Enforce required status checks for repository administrators. + */ + includeAdministrators: Boolean) extends AccountService with CommitStatusService { + + def isAdministrator(pusher: String)(implicit session: Session): Boolean = + pusher == owner || getGroupMembers(owner).filter(gm => gm.userName == pusher && gm.isManager).nonEmpty + + /** + * Can't be force pushed + * Can't be deleted + * Can't have changes merged into them until required status checks pass + */ + def getStopReason(isAllowNonFastForwards: Boolean, command: ReceiveCommand, pusher: String)(implicit session: Session): Option[String] = { + if(enabled){ + command.getType() match { + case ReceiveCommand.Type.UPDATE_NONFASTFORWARD if isAllowNonFastForwards => + Some("Cannot force-push to a protected branch") + case ReceiveCommand.Type.UPDATE|ReceiveCommand.Type.UPDATE_NONFASTFORWARD if needStatusCheck(pusher) => + unSuccessedContexts(command.getNewId.name) match { + case s if s.size == 1 => Some(s"""Required status check "${s.toSeq(0)}" is expected""") + case s if s.size >= 1 => Some(s"${s.size} of ${contexts.size} required status checks are expected") + case _ => None + } + case ReceiveCommand.Type.DELETE => + Some("Cannot delete a protected branch") + case _ => None + } + } else { + None + } + } + def unSuccessedContexts(sha1: String)(implicit session: Session): Set[String] = if(contexts.isEmpty){ + Set.empty + } else { + contexts.toSet -- getCommitStatues(owner, repository, sha1).filter(_.state == CommitState.SUCCESS).map(_.context).toSet + } + def needStatusCheck(pusher: String)(implicit session: Session): Boolean = pusher match { + case _ if !enabled => false + case _ if contexts.isEmpty => false + case _ if includeAdministrators => true + case p if isAdministrator(p) => false + case _ => true + } + } + object ProtectedBranchInfo{ + def disabled(owner: String, repository: String): ProtectedBranchInfo = ProtectedBranchInfo(owner, repository, false, Nil, false) + } +} diff --git a/src/main/scala/gitbucket/core/service/PullRequestService.scala b/src/main/scala/gitbucket/core/service/PullRequestService.scala index 67db3c8..cbd3df0 100644 --- a/src/main/scala/gitbucket/core/service/PullRequestService.scala +++ b/src/main/scala/gitbucket/core/service/PullRequestService.scala @@ -1,12 +1,22 @@ package gitbucket.core.service -import gitbucket.core.model.{Account, Issue, PullRequest, WebHook} +import difflib.{Delta, DiffUtils} +import gitbucket.core.model.{Session => _, _} import gitbucket.core.model.Profile._ +import gitbucket.core.util.ControlUtil._ +import gitbucket.core.util.Directory._ +import gitbucket.core.util.Implicits._ import gitbucket.core.util.JGitUtil +import gitbucket.core.util.JGitUtil.{CommitInfo, DiffInfo} +import gitbucket.core.view +import gitbucket.core.view.helpers +import org.eclipse.jgit.api.Git import profile.simple._ +import scala.collection.JavaConverters._ -trait PullRequestService { self: IssuesService => + +trait PullRequestService { self: IssuesService with CommitsService => import PullRequestService._ def getPullRequest(owner: String, repository: String, issueId: Int) @@ -111,9 +121,26 @@ def updatePullRequests(owner: String, repository: String, branch: String)(implicit s: Session): Unit = getPullRequestsByRequest(owner, repository, branch, false).foreach { pullreq => if(Repositories.filter(_.byRepository(pullreq.userName, pullreq.repositoryName)).exists.run){ + // Update the git repository val (commitIdTo, commitIdFrom) = JGitUtil.updatePullRequest( pullreq.userName, pullreq.repositoryName, pullreq.branch, pullreq.issueId, pullreq.requestUserName, pullreq.requestRepositoryName, pullreq.requestBranch) + + // Collect comment positions + val positions = getCommitComments(pullreq.userName, pullreq.repositoryName, pullreq.commitIdTo, true) + .collect { + case CommitComment(_, _, _, commentId, _, _, Some(file), None, Some(newLine), _, _, _) => (file, commentId, Right(newLine)) + case CommitComment(_, _, _, commentId, _, _, Some(file), Some(oldLine), None, _, _, _) => (file, commentId, Left(oldLine)) + } + .groupBy { case (file, _, _) => file } + .map { case (file, comments) => file -> + comments.map { case (_, commentId, lineNumber) => (commentId, lineNumber) } + } + + // Update comments position + updatePullRequestCommentPositions(positions, pullreq.requestUserName, pullreq.requestRepositoryName, pullreq.commitIdTo, commitIdTo) + + // Update commit id in the PULL_REQUEST table updateCommitId(pullreq.userName, pullreq.repositoryName, pullreq.issueId, commitIdTo, commitIdFrom) } } @@ -137,6 +164,78 @@ .firstOption } } + + private def updatePullRequestCommentPositions(positions: Map[String, Seq[(Int, Either[Int, Int])]], userName: String, repositoryName: String, + oldCommitId: String, newCommitId: String)(implicit s: Session): Unit = { + + val (_, diffs) = getRequestCompareInfo(userName, repositoryName, oldCommitId, userName, repositoryName, newCommitId) + + val patchs = positions.map { case (file, _) => + diffs.find(x => x.oldPath == file).map { diff => + (diff.oldContent, diff.newContent) match { + case (Some(oldContent), Some(newContent)) => { + val oldLines = oldContent.replace("\r\n", "\n").split("\n") + val newLines = newContent.replace("\r\n", "\n").split("\n") + file -> Option(DiffUtils.diff(oldLines.toList.asJava, newLines.toList.asJava)) + } + case _ => + file -> None + } + }.getOrElse { + file -> None + } + } + + positions.foreach { case (file, comments) => + patchs(file) match { + case Some(patch) => file -> comments.foreach { case (commentId, lineNumber) => lineNumber match { + case Left(oldLine) => updateCommitCommentPosition(commentId, newCommitId, Some(oldLine), None) + case Right(newLine) => + var counter = newLine + patch.getDeltas.asScala.filter(_.getOriginal.getPosition < newLine).foreach { delta => + delta.getType match { + case Delta.TYPE.CHANGE => + if(delta.getOriginal.getPosition <= newLine - 1 && newLine <= delta.getOriginal.getPosition + delta.getRevised.getLines.size){ + counter = -1 + } else { + counter = counter + (delta.getRevised.getLines.size - delta.getOriginal.getLines.size) + } + case Delta.TYPE.INSERT => counter = counter + delta.getRevised.getLines.size + case Delta.TYPE.DELETE => counter = counter - delta.getOriginal.getLines.size + } + } + if(counter >= 0){ + updateCommitCommentPosition(commentId, newCommitId, None, Some(counter)) + } + }} + case _ => comments.foreach { case (commentId, lineNumber) => lineNumber match { + case Right(oldLine) => updateCommitCommentPosition(commentId, newCommitId, Some(oldLine), None) + case Left(newLine) => updateCommitCommentPosition(commentId, newCommitId, None, Some(newLine)) + }} + } + } + } + + def getRequestCompareInfo(userName: String, repositoryName: String, branch: String, + requestUserName: String, requestRepositoryName: String, requestCommitId: String): (Seq[Seq[CommitInfo]], Seq[DiffInfo]) = + using( + Git.open(getRepositoryDir(userName, repositoryName)), + Git.open(getRepositoryDir(requestUserName, requestRepositoryName)) + ){ (oldGit, newGit) => + val oldId = oldGit.getRepository.resolve(branch) + val newId = newGit.getRepository.resolve(requestCommitId) + + val commits = newGit.log.addRange(oldId, newId).call.iterator.asScala.map { revCommit => + new CommitInfo(revCommit) + }.toList.splitWith { (commit1, commit2) => + helpers.date(commit1.commitTime) == view.helpers.date(commit2.commitTime) + } + + val diffs = JGitUtil.getDiffs(newGit, oldId.getName, newId.getName, true) + + (commits, diffs) + } + } object PullRequestService { @@ -145,4 +244,29 @@ case class PullRequestCount(userName: String, count: Int) + case class MergeStatus( + hasConflict: Boolean, + commitStatues:List[CommitStatus], + branchProtection: ProtectedBranchService.ProtectedBranchInfo, + branchIsOutOfDate: Boolean, + hasUpdatePermission: Boolean, + needStatusCheck: Boolean, + hasMergePermission: Boolean, + commitIdTo: String){ + + val statuses: List[CommitStatus] = + commitStatues ++ (branchProtection.contexts.toSet -- commitStatues.map(_.context).toSet).map(CommitStatus.pending(branchProtection.owner, branchProtection.repository, _)) + val hasRequiredStatusProblem = needStatusCheck && branchProtection.contexts.exists(context => statuses.find(_.context == context).map(_.state) != Some(CommitState.SUCCESS)) + val hasProblem = hasRequiredStatusProblem || hasConflict || (!statuses.isEmpty && CommitState.combine(statuses.map(_.state).toSet) != CommitState.SUCCESS) + val canUpdate = branchIsOutOfDate && !hasConflict + val canMerge = hasMergePermission && !hasConflict && !hasRequiredStatusProblem + lazy val commitStateSummary:(CommitState, String) = { + val stateMap = statuses.groupBy(_.state) + val state = CommitState.combine(stateMap.keySet) + val summary = stateMap.map{ case (keyState, states) => states.size+" "+keyState.name }.mkString(", ") + state -> summary + } + lazy val statusesAndRequired:List[(CommitStatus, Boolean)] = statuses.map{ s => s -> branchProtection.contexts.exists(_==s.context) } + lazy val isAllSuccess = commitStateSummary._1==CommitState.SUCCESS + } } diff --git a/src/main/scala/gitbucket/core/service/RepositoryCreationService.scala b/src/main/scala/gitbucket/core/service/RepositoryCreationService.scala new file mode 100644 index 0000000..04aef50 --- /dev/null +++ b/src/main/scala/gitbucket/core/service/RepositoryCreationService.scala @@ -0,0 +1,79 @@ +package gitbucket.core.service + +import gitbucket.core.model.Profile._ +import gitbucket.core.util.ControlUtil._ +import gitbucket.core.util.Directory._ +import gitbucket.core.util.JGitUtil +import gitbucket.core.model.Account +import org.eclipse.jgit.api.Git +import org.eclipse.jgit.dircache.DirCache +import org.eclipse.jgit.lib.{FileMode, Constants} +import profile.simple._ + +trait RepositoryCreationService { + self: AccountService with RepositoryService with LabelsService with WikiService with ActivityService => + + def createRepository(loginAccount: Account, owner: String, name: String, description: Option[String], isPrivate: Boolean, createReadme: Boolean) + (implicit s: Session) { + val ownerAccount = getAccountByUserName(owner).get + val loginUserName = loginAccount.userName + + // Insert to the database at first + insertRepository(name, owner, description, isPrivate) + +// // Add collaborators for group repository +// if(ownerAccount.isGroupAccount){ +// getGroupMembers(owner).foreach { member => +// addCollaborator(owner, name, member.userName) +// } +// } + + // Insert default labels + insertDefaultLabels(owner, name) + + // Create the actual repository + val gitdir = getRepositoryDir(owner, name) + JGitUtil.initRepository(gitdir) + + if(createReadme){ + using(Git.open(gitdir)){ git => + val builder = DirCache.newInCore.builder() + val inserter = git.getRepository.newObjectInserter() + val headId = git.getRepository.resolve(Constants.HEAD + "^{commit}") + val content = if(description.nonEmpty){ + name + "\n" + + "===============\n" + + "\n" + + description.get + } else { + name + "\n" + + "===============\n" + } + + builder.add(JGitUtil.createDirCacheEntry("README.md", FileMode.REGULAR_FILE, + inserter.insert(Constants.OBJ_BLOB, content.getBytes("UTF-8")))) + builder.finish() + + JGitUtil.createNewCommit(git, inserter, headId, builder.getDirCache.writeTree(inserter), + Constants.HEAD, loginAccount.fullName, loginAccount.mailAddress, "Initial commit") + } + } + + // Create Wiki repository + createWikiRepository(loginAccount, owner, name) + + // Record activity + recordCreateRepositoryActivity(owner, name, loginUserName) + } + + def insertDefaultLabels(userName: String, repositoryName: String)(implicit s: Session): Unit = { + createLabel(userName, repositoryName, "bug", "fc2929") + createLabel(userName, repositoryName, "duplicate", "cccccc") + createLabel(userName, repositoryName, "enhancement", "84b6eb") + createLabel(userName, repositoryName, "invalid", "e6e6e6") + createLabel(userName, repositoryName, "question", "cc317c") + createLabel(userName, repositoryName, "wontfix", "ffffff") + } + + +} diff --git a/src/main/scala/gitbucket/core/service/RepositorySearchService.scala b/src/main/scala/gitbucket/core/service/RepositorySearchService.scala index 84e94e4..bba7172 100644 --- a/src/main/scala/gitbucket/core/service/RepositorySearchService.scala +++ b/src/main/scala/gitbucket/core/service/RepositorySearchService.scala @@ -53,7 +53,30 @@ } } - private def searchRepositoryFiles(git: Git, query: String): List[(String, String)] = { + def countWikiPages(owner: String, repository: String, query: String): Int = + using(Git.open(Directory.getWikiRepositoryDir(owner, repository))){ git => + if(JGitUtil.isEmpty(git)) 0 else searchRepositoryFiles(git, query).length + } + + def searchWikiPages(owner: String, repository: String, query: String): List[FileSearchResult] = + using(Git.open(Directory.getWikiRepositoryDir(owner, repository))){ git => + if(JGitUtil.isEmpty(git)){ + Nil + } else { + val files = searchRepositoryFiles(git, query) + val commits = JGitUtil.getLatestCommitFromPaths(git, files.map(_._1), "HEAD") + files.map { case (path, text) => + val (highlightText, lineNumber) = getHighlightText(text, query) + FileSearchResult( + path.replaceFirst("\\.md$", ""), + commits(path).getCommitterIdent.getWhen, + highlightText, + lineNumber) + } + } + } + + def searchRepositoryFiles(git: Git, query: String): List[(String, String)] = { val revWalk = new RevWalk(git.getRepository) val objectId = git.getRepository.resolve("HEAD") val revCommit = revWalk.parseCommit(objectId) @@ -79,8 +102,8 @@ } } } - treeWalk.release - revWalk.release + treeWalk.close() + revWalk.close() list.toList } diff --git a/src/main/scala/gitbucket/core/service/RepositoryService.scala b/src/main/scala/gitbucket/core/service/RepositoryService.scala index 9473722..30b4d5c 100644 --- a/src/main/scala/gitbucket/core/service/RepositoryService.scala +++ b/src/main/scala/gitbucket/core/service/RepositoryService.scala @@ -1,6 +1,7 @@ package gitbucket.core.service -import gitbucket.core.model.{Collaborator, Repository, Account} +import gitbucket.core.controller.Context +import gitbucket.core.model.{Collaborator, Repository, RepositoryOptions, Account, Role} import gitbucket.core.model.Profile._ import gitbucket.core.util.JGitUtil import profile.simple._ @@ -18,7 +19,7 @@ * @param originRepositoryName specify for the forked repository. (default is None) * @param originUserName specify for the forked repository. (default is None) */ - def createRepository(repositoryName: String, userName: String, description: Option[String], isPrivate: Boolean, + def insertRepository(repositoryName: String, userName: String, description: Option[String], isPrivate: Boolean, originRepositoryName: Option[String] = None, originUserName: Option[String] = None, parentRepositoryName: Option[String] = None, parentUserName: Option[String] = None) (implicit s: Session): Unit = { @@ -35,7 +36,15 @@ originUserName = originUserName, originRepositoryName = originRepositoryName, parentUserName = parentUserName, - parentRepositoryName = parentRepositoryName) + parentRepositoryName = parentRepositoryName, + options = RepositoryOptions( + issuesOption = "PUBLIC", // TODO DISABLE for the forked repository? + externalIssuesUrl = None, + wikiOption = "PUBLIC", // TODO DISABLE for the forked repository? + externalWikiUrl = None, + allowFork = true + ) + ) IssueId insert (userName, repositoryName, 0) } @@ -46,17 +55,20 @@ (Repositories filter { t => t.byRepository(oldUserName, oldRepositoryName) } firstOption).map { repository => Repositories insert repository.copy(userName = newUserName, repositoryName = newRepositoryName) - val webHooks = WebHooks .filter(_.byRepository(oldUserName, oldRepositoryName)).list - val milestones = Milestones .filter(_.byRepository(oldUserName, oldRepositoryName)).list - val issueId = IssueId .filter(_.byRepository(oldUserName, oldRepositoryName)).list - val issues = Issues .filter(_.byRepository(oldUserName, oldRepositoryName)).list - val pullRequests = PullRequests .filter(_.byRepository(oldUserName, oldRepositoryName)).list - val labels = Labels .filter(_.byRepository(oldUserName, oldRepositoryName)).list - val issueComments = IssueComments .filter(_.byRepository(oldUserName, oldRepositoryName)).list - val issueLabels = IssueLabels .filter(_.byRepository(oldUserName, oldRepositoryName)).list - val commitComments = CommitComments.filter(_.byRepository(oldUserName, oldRepositoryName)).list - val commitStatuses = CommitStatuses.filter(_.byRepository(oldUserName, oldRepositoryName)).list - val collaborators = Collaborators .filter(_.byRepository(oldUserName, oldRepositoryName)).list + val webHooks = WebHooks .filter(_.byRepository(oldUserName, oldRepositoryName)).list + val webHookEvents = WebHookEvents .filter(_.byRepository(oldUserName, oldRepositoryName)).list + val milestones = Milestones .filter(_.byRepository(oldUserName, oldRepositoryName)).list + val issueId = IssueId .filter(_.byRepository(oldUserName, oldRepositoryName)).list + val issues = Issues .filter(_.byRepository(oldUserName, oldRepositoryName)).list + val pullRequests = PullRequests .filter(_.byRepository(oldUserName, oldRepositoryName)).list + val labels = Labels .filter(_.byRepository(oldUserName, oldRepositoryName)).list + val issueComments = IssueComments .filter(_.byRepository(oldUserName, oldRepositoryName)).list + val issueLabels = IssueLabels .filter(_.byRepository(oldUserName, oldRepositoryName)).list + val commitComments = CommitComments .filter(_.byRepository(oldUserName, oldRepositoryName)).list + val commitStatuses = CommitStatuses .filter(_.byRepository(oldUserName, oldRepositoryName)).list + val collaborators = Collaborators .filter(_.byRepository(oldUserName, oldRepositoryName)).list + val protectedBranches = ProtectedBranches .filter(_.byRepository(oldUserName, oldRepositoryName)).list + val protectedBranchContexts = ProtectedBranchContexts.filter(_.byRepository(oldUserName, oldRepositoryName)).list Repositories.filter { t => (t.originUserName === oldUserName.bind) && (t.originRepositoryName === oldRepositoryName.bind) @@ -66,10 +78,6 @@ (t.parentUserName === oldUserName.bind) && (t.parentRepositoryName === oldRepositoryName.bind) }.map { t => t.originUserName -> t.originRepositoryName }.update(newUserName, newRepositoryName) - PullRequests.filter { t => - t.requestRepositoryName === oldRepositoryName.bind - }.map { t => t.requestUserName -> t.requestRepositoryName }.update(newUserName, newRepositoryName) - // Updates activity fk before deleting repository because activity is sorted by activityId // and it can't be changed by deleting-and-inserting record. Activities.filter(_.byRepository(oldUserName, oldRepositoryName)).list.foreach { activity => @@ -79,9 +87,10 @@ deleteRepository(oldUserName, oldRepositoryName) - WebHooks .insertAll(webHooks .map(_.copy(userName = newUserName, repositoryName = newRepositoryName)) :_*) - Milestones.insertAll(milestones .map(_.copy(userName = newUserName, repositoryName = newRepositoryName)) :_*) - IssueId .insertAll(issueId .map(_.copy(_1 = newUserName, _2 = newRepositoryName)) :_*) + WebHooks .insertAll(webHooks .map(_.copy(userName = newUserName, repositoryName = newRepositoryName)) :_*) + WebHookEvents.insertAll(webHookEvents .map(_.copy(userName = newUserName, repositoryName = newRepositoryName)) :_*) + Milestones .insertAll(milestones .map(_.copy(userName = newUserName, repositoryName = newRepositoryName)) :_*) + IssueId .insertAll(issueId .map(_.copy(_1 = newUserName, _2 = newRepositoryName)) :_*) val newMilestones = Milestones.filter(_.byRepository(newUserName, newRepositoryName)).list Issues.insertAll(issues.map { x => x.copy( @@ -92,11 +101,18 @@ } )} :_*) - PullRequests .insertAll(pullRequests .map(_.copy(userName = newUserName, repositoryName = newRepositoryName)) :_*) - IssueComments .insertAll(issueComments .map(_.copy(userName = newUserName, repositoryName = newRepositoryName)) :_*) - Labels .insertAll(labels .map(_.copy(userName = newUserName, repositoryName = newRepositoryName)) :_*) - CommitComments.insertAll(commitComments.map(_.copy(userName = newUserName, repositoryName = newRepositoryName)) :_*) - CommitStatuses.insertAll(commitStatuses.map(_.copy(userName = newUserName, repositoryName = newRepositoryName)) :_*) + PullRequests .insertAll(pullRequests .map(_.copy(userName = newUserName, repositoryName = newRepositoryName)) :_*) + IssueComments .insertAll(issueComments .map(_.copy(userName = newUserName, repositoryName = newRepositoryName)) :_*) + Labels .insertAll(labels .map(_.copy(userName = newUserName, repositoryName = newRepositoryName)) :_*) + CommitComments .insertAll(commitComments.map(_.copy(userName = newUserName, repositoryName = newRepositoryName)) :_*) + CommitStatuses .insertAll(commitStatuses.map(_.copy(userName = newUserName, repositoryName = newRepositoryName)) :_*) + ProtectedBranches .insertAll(protectedBranches.map(_.copy(userName = newUserName, repositoryName = newRepositoryName)) :_*) + ProtectedBranchContexts.insertAll(protectedBranchContexts.map(_.copy(userName = newUserName, repositoryName = newRepositoryName)) :_*) + + // Update source repository of pull requests + PullRequests.filter { t => + (t.requestUserName === oldUserName.bind) && (t.requestRepositoryName === oldRepositoryName.bind) + }.map { t => t.requestUserName -> t.requestRepositoryName }.update(newUserName, newRepositoryName) // Convert labelId val oldLabelMap = labels.map(x => (x.labelId, x.labelName)).toMap @@ -107,11 +123,8 @@ repositoryName = newRepositoryName )) :_*) - if(account.isGroupAccount){ - Collaborators.insertAll(getGroupMembers(newUserName).map(m => Collaborator(newUserName, newRepositoryName, m.userName)) :_*) - } else { - Collaborators.insertAll(collaborators.map(_.copy(userName = newUserName, repositoryName = newRepositoryName)) :_*) - } + // TODO Drop transfered owner from collaborators? + Collaborators.insertAll(collaborators.map(_.copy(userName = newUserName, repositoryName = newRepositoryName)) :_*) // Update activity messages Activities.filter { t => @@ -145,6 +158,7 @@ IssueId .filter(_.byRepository(userName, repositoryName)).delete Milestones .filter(_.byRepository(userName, repositoryName)).delete WebHooks .filter(_.byRepository(userName, repositoryName)).delete + WebHookEvents .filter(_.byRepository(userName, repositoryName)).delete Repositories .filter(_.byRepository(userName, repositoryName)).delete // Update ORIGIN_USER_NAME and ORIGIN_REPOSITORY_NAME @@ -186,10 +200,9 @@ * * @param userName the user name of the repository owner * @param repositoryName the repository name - * @param baseUrl the base url of this application * @return the repository information */ - def getRepository(userName: String, repositoryName: String, baseUrl: String)(implicit s: Session): Option[RepositoryInfo] = { + def getRepository(userName: String, repositoryName: String)(implicit s: Session): Option[RepositoryInfo] = { (Repositories filter { t => t.byRepository(userName, repositoryName) } firstOption) map { repository => // for getting issue count and pull request count val issues = Issues.filter { t => @@ -197,7 +210,7 @@ }.map(_.pullRequest).list new RepositoryInfo( - JGitUtil.getRepositoryInfo(repository.userName, repository.repositoryName, baseUrl), + JGitUtil.getRepositoryInfo(repository.userName, repository.repositoryName), repository, issues.count(_ == false), issues.count(_ == true), @@ -210,33 +223,37 @@ } /** - * Returns the repositories without private repository that user does not have access right. + * Returns the repositories except private repository that user does not have access right. * Include public repository, private own repository and private but collaborator repository. * * @param userName the user name of collaborator - * @return the repository infomation list + * @return the repository information list */ def getAllRepositories(userName: String)(implicit s: Session): List[(String, String)] = { Repositories.filter { t1 => (t1.isPrivate === false.bind) || - (t1.userName === userName.bind) || - (Collaborators.filter { t2 => t2.byRepository(t1.userName, t1.repositoryName) && (t2.collaboratorName === userName.bind)} exists) + (t1.userName === userName.bind) || (t1.userName in (GroupMembers.filter(_.userName === userName.bind).map(_.groupName))) || + (Collaborators.filter { t2 => t2.byRepository(t1.userName, t1.repositoryName) && + ((t2.collaboratorName === userName.bind) || (t2.collaboratorName in GroupMembers.filter(_.userName === userName.bind).map(_.groupName))) + } exists) }.sortBy(_.lastActivityDate desc).map{ t => (t.userName, t.repositoryName) }.list } - def getUserRepositories(userName: String, baseUrl: String, withoutPhysicalInfo: Boolean = false) + def getUserRepositories(userName: String, withoutPhysicalInfo: Boolean = false) (implicit s: Session): List[RepositoryInfo] = { Repositories.filter { t1 => - (t1.userName === userName.bind) || - (Collaborators.filter { t2 => t2.byRepository(t1.userName, t1.repositoryName) && (t2.collaboratorName === userName.bind)} exists) + (t1.userName === userName.bind) || (t1.userName in (GroupMembers.filter(_.userName === userName.bind).map(_.groupName))) || + (Collaborators.filter { t2 => t2.byRepository(t1.userName, t1.repositoryName) && + ((t2.collaboratorName === userName.bind) || (t2.collaboratorName in GroupMembers.filter(_.userName === userName.bind).map(_.groupName))) + } exists) }.sortBy(_.lastActivityDate desc).list.map{ repository => new RepositoryInfo( if(withoutPhysicalInfo){ - new JGitUtil.RepositoryInfo(repository.userName, repository.repositoryName, baseUrl) + new JGitUtil.RepositoryInfo(repository.userName, repository.repositoryName) } else { - JGitUtil.getRepositoryInfo(repository.userName, repository.repositoryName, baseUrl) + JGitUtil.getRepositoryInfo(repository.userName, repository.repositoryName) }, repository, getForkedCount( @@ -252,13 +269,12 @@ * If repositoryUserName is given then filters results by repository owner. * * @param loginAccount the logged in account - * @param baseUrl the base url of this application * @param repositoryUserName the repository owner (if None then returns all repositories which are visible for logged in user) * @param withoutPhysicalInfo if true then the result does not include physical repository information such as commit count, * branches and tags * @return the repository information which is sorted in descending order of lastActivityDate. */ - def getVisibleRepositories(loginAccount: Option[Account], baseUrl: String, repositoryUserName: Option[String] = None, + def getVisibleRepositories(loginAccount: Option[Account], repositoryUserName: Option[String] = None, withoutPhysicalInfo: Boolean = false) (implicit s: Session): List[RepositoryInfo] = { (loginAccount match { @@ -266,8 +282,13 @@ case Some(x) if(x.isAdmin) => Repositories // for Normal Users case Some(x) if(!x.isAdmin) => - Repositories filter { t => (t.isPrivate === false.bind) || (t.userName === x.userName) || - (Collaborators.filter { t2 => t2.byRepository(t.userName, t.repositoryName) && (t2.collaboratorName === x.userName.bind)} exists) + Repositories filter { t => + (t.isPrivate === false.bind) || (t.userName === x.userName) || + (t.userName in GroupMembers.filter(_.userName === x.userName.bind).map(_.groupName)) || + (Collaborators.filter { t2 => + t2.byRepository(t.userName, t.repositoryName) && + ((t2.collaboratorName === x.userName.bind) || (t2.collaboratorName in GroupMembers.filter(_.userName === x.userName.bind).map(_.groupName))) + } exists) } // for Guests case None => Repositories filter(_.isPrivate === false.bind) @@ -276,9 +297,9 @@ }.sortBy(_.lastActivityDate desc).list.map{ repository => new RepositoryInfo( if(withoutPhysicalInfo){ - new JGitUtil.RepositoryInfo(repository.userName, repository.repositoryName, baseUrl) + new JGitUtil.RepositoryInfo(repository.userName, repository.repositoryName) } else { - JGitUtil.getRepositoryInfo(repository.userName, repository.repositoryName, baseUrl) + JGitUtil.getRepositoryInfo(repository.userName, repository.repositoryName) }, repository, getForkedCount( @@ -305,56 +326,80 @@ /** * Save repository options. */ - def saveRepositoryOptions(userName: String, repositoryName: String, - description: Option[String], defaultBranch: String, isPrivate: Boolean)(implicit s: Session): Unit = + def saveRepositoryOptions(userName: String, repositoryName: String, + description: Option[String], isPrivate: Boolean, + issuesOption: String, externalIssuesUrl: Option[String], + wikiOption: String, externalWikiUrl: Option[String], + allowFork: Boolean)(implicit s: Session): Unit = Repositories.filter(_.byRepository(userName, repositoryName)) - .map { r => (r.description.?, r.defaultBranch, r.isPrivate, r.updatedDate) } - .update (description, defaultBranch, isPrivate, currentDate) + .map { r => (r.description.?, r.isPrivate, r.issuesOption, r.externalIssuesUrl.?, r.wikiOption, r.externalWikiUrl.?, r.allowFork, r.updatedDate) } + .update (description, isPrivate, issuesOption, externalIssuesUrl, wikiOption, externalWikiUrl, allowFork, currentDate) + + def saveRepositoryDefaultBranch(userName: String, repositoryName: String, + defaultBranch: String)(implicit s: Session): Unit = + Repositories.filter(_.byRepository(userName, repositoryName)) + .map { r => r.defaultBranch } + .update (defaultBranch) /** - * Add collaborator to the repository. - * - * @param userName the user name of the repository owner - * @param repositoryName the repository name - * @param collaboratorName the collaborator name + * Add collaborator (user or group) to the repository. */ - def addCollaborator(userName: String, repositoryName: String, collaboratorName: String)(implicit s: Session): Unit = - Collaborators insert Collaborator(userName, repositoryName, collaboratorName) - - /** - * Remove collaborator from the repository. - * - * @param userName the user name of the repository owner - * @param repositoryName the repository name - * @param collaboratorName the collaborator name - */ - def removeCollaborator(userName: String, repositoryName: String, collaboratorName: String)(implicit s: Session): Unit = - Collaborators.filter(_.byPrimaryKey(userName, repositoryName, collaboratorName)).delete + def addCollaborator(userName: String, repositoryName: String, collaboratorName: String, role: String)(implicit s: Session): Unit = + Collaborators insert Collaborator(userName, repositoryName, collaboratorName, role) /** * Remove all collaborators from the repository. - * - * @param userName the user name of the repository owner - * @param repositoryName the repository name */ def removeCollaborators(userName: String, repositoryName: String)(implicit s: Session): Unit = Collaborators.filter(_.byRepository(userName, repositoryName)).delete /** - * Returns the list of collaborators name which is sorted with ascending order. - * - * @param userName the user name of the repository owner - * @param repositoryName the repository name - * @return the list of collaborators name + * Returns the list of collaborators name (user name or group name) which is sorted with ascending order. */ - def getCollaborators(userName: String, repositoryName: String)(implicit s: Session): List[String] = - Collaborators.filter(_.byRepository(userName, repositoryName)).sortBy(_.collaboratorName).map(_.collaboratorName).list + def getCollaborators(userName: String, repositoryName: String)(implicit s: Session): List[(Collaborator, Boolean)] = + Collaborators + .innerJoin(Accounts).on(_.collaboratorName === _.userName) + .filter { case (t1, t2) => t1.byRepository(userName, repositoryName) } + .map { case (t1, t2) => (t1, t2.groupAccount) } + .sortBy { case (t1, t2) => t1.collaboratorName } + .list - def hasWritePermission(owner: String, repository: String, loginAccount: Option[Account])(implicit s: Session): Boolean = { + /** + * Returns the list of all collaborator name and permission which is sorted with ascending order. + * If a group is added as a collaborator, this method returns users who are belong to that group. + */ + def getCollaboratorUserNames(userName: String, repositoryName: String, filter: Seq[Role] = Nil)(implicit s: Session): List[String] = { + val q1 = Collaborators + .innerJoin(Accounts).on { case (t1, t2) => (t1.collaboratorName === t2.userName) && (t2.groupAccount === false.bind) } + .filter { case (t1, t2) => t1.byRepository(userName, repositoryName) } + .map { case (t1, t2) => (t1.collaboratorName, t1.role) } + + val q2 = Collaborators + .innerJoin(Accounts).on { case (t1, t2) => (t1.collaboratorName === t2.userName) && (t2.groupAccount === true.bind) } + .innerJoin(GroupMembers).on { case ((t1, t2), t3) => t2.userName === t3.groupName } + .filter { case ((t1, t2), t3) => t1.byRepository(userName, repositoryName) } + .map { case ((t1, t2), t3) => (t3.userName, t1.role) } + + q1.union(q2).list.filter { x => filter.isEmpty || filter.exists(_.name == x._2) }.map(_._1) + } + + + def hasDeveloperRole(owner: String, repository: String, loginAccount: Option[Account])(implicit s: Session): Boolean = { loginAccount match { case Some(a) if(a.isAdmin) => true case Some(a) if(a.userName == owner) => true - case Some(a) if(getCollaborators(owner, repository).contains(a.userName)) => true + case Some(a) if(getGroupMembers(owner).exists(_.userName == a.userName)) => true + case Some(a) if(getCollaboratorUserNames(owner, repository, Seq(Role.ADMIN, Role.DEVELOPER)).contains(a.userName)) => true + case _ => false + } + } + + def hasGuestRole(owner: String, repository: String, loginAccount: Option[Account])(implicit s: Session): Boolean = { + loginAccount match { + case Some(a) if(a.isAdmin) => true + case Some(a) if(a.userName == owner) => true + case Some(a) if(getGroupMembers(owner).exists(_.userName == a.userName)) => true + case Some(a) if(getCollaboratorUserNames(owner, repository, Seq(Role.ADMIN, Role.DEVELOPER, Role.GUEST)).contains(a.userName)) => true case _ => false } } @@ -375,26 +420,41 @@ object RepositoryService { - case class RepositoryInfo(owner: String, name: String, httpUrl: String, repository: Repository, - issueCount: Int, pullCount: Int, commitCount: Int, forkedCount: Int, - branchList: Seq[String], tags: Seq[JGitUtil.TagInfo], managers: Seq[String]){ - - lazy val host = """^https?://(.+?)(:\d+)?/""".r.findFirstMatchIn(httpUrl).get.group(1) - - def sshUrl(port: Int, userName: String) = s"ssh://${userName}@${host}:${port}/${owner}/${name}.git" + case class RepositoryInfo(owner: String, name: String, repository: Repository, + issueCount: Int, pullCount: Int, forkedCount: Int, + branchList: Seq[String], tags: Seq[JGitUtil.TagInfo], managers: Seq[String]) { /** * Creates instance with issue count and pull request count. */ def this(repo: JGitUtil.RepositoryInfo, model: Repository, issueCount: Int, pullCount: Int, forkedCount: Int, managers: Seq[String]) = - this(repo.owner, repo.name, repo.url, model, issueCount, pullCount, repo.commitCount, forkedCount, repo.branchList, repo.tags, managers) + this(repo.owner, repo.name, model, issueCount, pullCount, forkedCount, repo.branchList, repo.tags, managers) /** * Creates instance without issue count and pull request count. */ - def this(repo: JGitUtil.RepositoryInfo, model: Repository, forkedCount: Int, managers: Seq[String]) = - this(repo.owner, repo.name, repo.url, model, 0, 0, repo.commitCount, forkedCount, repo.branchList, repo.tags, managers) + def this(repo: JGitUtil.RepositoryInfo, model: Repository, forkedCount: Int, managers: Seq[String]) = + this(repo.owner, repo.name, model, 0, 0, forkedCount, repo.branchList, repo.tags, managers) + + def httpUrl(implicit context: Context): String = RepositoryService.httpUrl(owner, name) + def sshUrl(implicit context: Context): Option[String] = RepositoryService.sshUrl(owner, name) + + def splitPath(path: String): (String, String) = { + val id = branchList.collectFirst { + case branch if(path == branch || path.startsWith(branch + "/")) => branch + } orElse tags.collectFirst { + case tag if(path == tag.name || path.startsWith(tag.name + "/")) => tag.name + } getOrElse path.split("/")(0) + + (id, path.substring(id.length).stripPrefix("/")) + } } - case class RepositoryTreeNode(owner: String, name: String, children: List[RepositoryTreeNode]) + def httpUrl(owner: String, name: String)(implicit context: Context): String = s"${context.baseUrl}/git/${owner}/${name}.git" + def sshUrl(owner: String, name: String)(implicit context: Context): Option[String] = + if(context.settings.ssh){ + context.settings.sshAddress.map { x => s"ssh://${x.genericUser}@${x.host}:${x.port}/${owner}/${name}.git" } + } else None + def openRepoUrl(openUrl: String)(implicit context: Context): String = s"github-${context.platform}://openRepo/${openUrl}" + } diff --git a/src/main/scala/gitbucket/core/service/RequestCache.scala b/src/main/scala/gitbucket/core/service/RequestCache.scala index 768a3b4..bd03cd8 100644 --- a/src/main/scala/gitbucket/core/service/RequestCache.scala +++ b/src/main/scala/gitbucket/core/service/RequestCache.scala @@ -11,7 +11,7 @@ * It may be called many times in one request, so each method stores * its result into the cache which available during a request. */ -trait RequestCache extends SystemSettingsService with AccountService with IssuesService { +trait RequestCache extends SystemSettingsService with AccountService with IssuesService with RepositoryService { private implicit def context2Session(implicit context: Context): Session = request2Session(context.request) diff --git a/src/main/scala/gitbucket/core/service/SshKeyService.scala b/src/main/scala/gitbucket/core/service/SshKeyService.scala index 4113d2c..5feb119 100644 --- a/src/main/scala/gitbucket/core/service/SshKeyService.scala +++ b/src/main/scala/gitbucket/core/service/SshKeyService.scala @@ -12,6 +12,9 @@ def getPublicKeys(userName: String)(implicit s: Session): List[SshKey] = SshKeys.filter(_.userName === userName.bind).sortBy(_.sshKeyId).list + def getAllKeys()(implicit s: Session): List[SshKey] = + SshKeys.filter(_.publicKey.trim =!= "").list + def deletePublicKey(userName: String, sshKeyId: Int)(implicit s: Session): Unit = SshKeys filter (_.byPrimaryKey(userName, sshKeyId)) delete diff --git a/src/main/scala/gitbucket/core/service/SystemSettingsService.scala b/src/main/scala/gitbucket/core/service/SystemSettingsService.scala index af4a6ef..0e1b677 100644 --- a/src/main/scala/gitbucket/core/service/SystemSettingsService.scala +++ b/src/main/scala/gitbucket/core/service/SystemSettingsService.scala @@ -1,6 +1,7 @@ package gitbucket.core.service import gitbucket.core.util.{Directory, ControlUtil} +import gitbucket.core.util.Implicits._ import Directory._ import ControlUtil._ import SystemSettingsService._ @@ -21,14 +22,17 @@ props.setProperty(Notification, settings.notification.toString) settings.activityLogLimit.foreach(x => props.setProperty(ActivityLogLimit, x.toString)) props.setProperty(Ssh, settings.ssh.toString) + settings.sshHost.foreach(x => props.setProperty(SshHost, x.trim)) settings.sshPort.foreach(x => props.setProperty(SshPort, x.toString)) - if(settings.notification) { + props.setProperty(UseSMTP, settings.useSMTP.toString) + if(settings.useSMTP) { settings.smtp.foreach { smtp => props.setProperty(SmtpHost, smtp.host) smtp.port.foreach(x => props.setProperty(SmtpPort, x.toString)) smtp.user.foreach(props.setProperty(SmtpUser, _)) smtp.password.foreach(props.setProperty(SmtpPassword, _)) smtp.ssl.foreach(x => props.setProperty(SmtpSsl, x.toString)) + smtp.starttls.foreach(x => props.setProperty(SmtpStarttls, x.toString)) smtp.fromAddress.foreach(props.setProperty(SmtpFromAddress, _)) smtp.fromName.foreach(props.setProperty(SmtpFromName, _)) } @@ -70,18 +74,21 @@ getValue(props, AllowAccountRegistration, false), getValue(props, AllowAnonymousAccess, true), getValue(props, IsCreateRepoOptionPublic, true), - getValue(props, Gravatar, true), + getValue(props, Gravatar, false), getValue(props, Notification, false), getOptionValue[Int](props, ActivityLogLimit, None), getValue(props, Ssh, false), + getOptionValue[String](props, SshHost, None).map(_.trim), getOptionValue(props, SshPort, Some(DefaultSshPort)), - if(getValue(props, Notification, false)){ + getValue(props, UseSMTP, getValue(props, Notification, false)), // handle migration scenario from only notification to useSMTP + if(getValue(props, UseSMTP, getValue(props, Notification, false))){ Some(Smtp( getValue(props, SmtpHost, ""), getOptionValue(props, SmtpPort, Some(DefaultSmtpPort)), getOptionValue(props, SmtpUser, None), getOptionValue(props, SmtpPassword, None), getOptionValue[Boolean](props, SmtpSsl, None), + getOptionValue[Boolean](props, SmtpStarttls, None), getOptionValue(props, SmtpFromAddress, None), getOptionValue(props, SmtpFromName, None))) } else { @@ -124,15 +131,23 @@ notification: Boolean, activityLogLimit: Option[Int], ssh: Boolean, + sshHost: Option[String], sshPort: Option[Int], + useSMTP: Boolean, smtp: Option[Smtp], ldapAuthentication: Boolean, ldap: Option[Ldap]){ - def baseUrl(request: HttpServletRequest): String = baseUrl.getOrElse { - defining(request.getRequestURL.toString){ url => - url.substring(0, url.length - (request.getRequestURI.length - request.getContextPath.length)) + def baseUrl(request: HttpServletRequest): String = baseUrl.fold(request.baseUrl)(_.stripSuffix("/")) + + def sshAddress:Option[SshAddress] = + for { + host <- sshHost if ssh } - }.stripSuffix("/") + yield SshAddress( + host, + sshPort.getOrElse(DefaultSshPort), + "git" + ) } case class Ldap( @@ -155,9 +170,18 @@ user: Option[String], password: Option[String], ssl: Option[Boolean], + starttls: Option[Boolean], fromAddress: Option[String], fromName: Option[String]) + case class SshAddress( + host:String, + port:Int, + genericUser:String) + + case class Lfs( + serverUrl: Option[String]) + val DefaultSshPort = 29418 val DefaultSmtpPort = 25 val DefaultLdapPort = 389 @@ -171,12 +195,15 @@ private val Notification = "notification" private val ActivityLogLimit = "activity_log_limit" private val Ssh = "ssh" + private val SshHost = "ssh.host" private val SshPort = "ssh.port" + private val UseSMTP = "useSMTP" private val SmtpHost = "smtp.host" private val SmtpPort = "smtp.port" private val SmtpUser = "smtp.user" private val SmtpPassword = "smtp.password" private val SmtpSsl = "smtp.ssl" + private val SmtpStarttls = "smtp.starttls" private val SmtpFromAddress = "smtp.from_address" private val SmtpFromName = "smtp.from_name" private val LdapAuthentication = "ldap_authentication" @@ -212,7 +239,4 @@ else value } -// // TODO temporary flag -// val enablePluginSystem = Option(System.getProperty("enable.plugin")).getOrElse("false").toBoolean - } diff --git a/src/main/scala/gitbucket/core/service/WebHookService.scala b/src/main/scala/gitbucket/core/service/WebHookService.scala index 04d4d50..ad3a304 100644 --- a/src/main/scala/gitbucket/core/service/WebHookService.scala +++ b/src/main/scala/gitbucket/core/service/WebHookService.scala @@ -1,75 +1,156 @@ package gitbucket.core.service +import fr.brouillard.oss.security.xhub.XHub +import fr.brouillard.oss.security.xhub.XHub.{XHubConverter, XHubDigest} import gitbucket.core.api._ -import gitbucket.core.model.{WebHook, Account, Issue, PullRequest, IssueComment} +import gitbucket.core.model.{Account, CommitComment, Issue, IssueComment, PullRequest, WebHook, WebHookEvent} import gitbucket.core.model.Profile._ +import org.apache.http.client.utils.URLEncodedUtils import profile.simple._ import gitbucket.core.util.JGitUtil.CommitInfo import gitbucket.core.util.RepositoryName import gitbucket.core.service.RepositoryService.RepositoryInfo - import org.apache.http.NameValuePair import org.apache.http.client.entity.UrlEncodedFormEntity import org.apache.http.message.BasicNameValuePair import org.eclipse.jgit.api.Git +import org.eclipse.jgit.lib.ObjectId import org.slf4j.LoggerFactory +import scala.concurrent._ +import org.apache.http.HttpRequest +import org.apache.http.HttpResponse +import gitbucket.core.model.WebHookContentType +import org.apache.http.client.entity.EntityBuilder +import org.apache.http.entity.ContentType + trait WebHookService { import WebHookService._ private val logger = LoggerFactory.getLogger(classOf[WebHookService]) - def getWebHookURLs(owner: String, repository: String)(implicit s: Session): List[WebHook] = - WebHooks.filter(_.byRepository(owner, repository)).sortBy(_.url).list + /** get All WebHook informations of repository */ + def getWebHooks(owner: String, repository: String)(implicit s: Session): List[(WebHook, Set[WebHook.Event])] = + WebHooks.filter(_.byRepository(owner, repository)) + .innerJoin(WebHookEvents).on { (w, t) => t.byWebHook(w) } + .map { case (w,t) => w -> t.event } + .list.groupBy(_._1).mapValues(_.map(_._2).toSet).toList.sortBy(_._1.url) - def addWebHookURL(owner: String, repository: String, url :String)(implicit s: Session): Unit = - WebHooks insert WebHook(owner, repository, url) + /** get All WebHook informations of repository event */ + def getWebHooksByEvent(owner: String, repository: String, event: WebHook.Event)(implicit s: Session): List[WebHook] = + WebHooks.filter(_.byRepository(owner, repository)) + .innerJoin(WebHookEvents).on { (wh, whe) => whe.byWebHook(wh) } + .filter { case (wh, whe) => whe.event === event.bind} + .map { case (wh, whe) => wh } + .list.distinct - def deleteWebHookURL(owner: String, repository: String, url :String)(implicit s: Session): Unit = - WebHooks.filter(_.byPrimaryKey(owner, repository, url)).delete + /** get All WebHook information from repository to url */ + def getWebHook(owner: String, repository: String, url: String)(implicit s: Session): Option[(WebHook, Set[WebHook.Event])] = + WebHooks + .filter(_.byPrimaryKey(owner, repository, url)) + .innerJoin(WebHookEvents).on { (w, t) => t.byWebHook(w) } + .map { case (w,t) => w -> t.event } + .list.groupBy(_._1).mapValues(_.map(_._2).toSet).headOption - def callWebHookOf(owner: String, repository: String, eventName: String)(makePayload: => Option[WebHookPayload])(implicit s: Session, c: JsonFormat.Context): Unit = { - val webHookURLs = getWebHookURLs(owner, repository) - if(webHookURLs.nonEmpty){ - makePayload.map(callWebHook(eventName, webHookURLs, _)) + def addWebHook(owner: String, repository: String, url :String, events: Set[WebHook.Event], ctype: WebHookContentType, token: Option[String])(implicit s: Session): Unit = { + WebHooks insert WebHook(owner, repository, url, ctype, token) + events.map { event: WebHook.Event => + WebHookEvents insert WebHookEvent(owner, repository, url, event) } } - def callWebHook(eventName: String, webHookURLs: List[WebHook], payload: WebHookPayload)(implicit c: JsonFormat.Context): Unit = { - import org.apache.http.client.methods.HttpPost + def updateWebHook(owner: String, repository: String, url :String, events: Set[WebHook.Event], ctype: WebHookContentType, token: Option[String])(implicit s: Session): Unit = { + WebHooks.filter(_.byPrimaryKey(owner, repository, url)).map(w => (w.ctype, w.token)).update((ctype, token)) + WebHookEvents.filter(_.byWebHook(owner, repository, url)).delete + events.map { event: WebHook.Event => + WebHookEvents insert WebHookEvent(owner, repository, url, event) + } + } + + def deleteWebHook(owner: String, repository: String, url :String)(implicit s: Session): Unit = + WebHooks.filter(_.byPrimaryKey(owner, repository, url)).delete + + def callWebHookOf(owner: String, repository: String, event: WebHook.Event)(makePayload: => Option[WebHookPayload]) + (implicit s: Session, c: JsonFormat.Context): Unit = { + val webHooks = getWebHooksByEvent(owner, repository, event) + if(webHooks.nonEmpty){ + makePayload.map(callWebHook(event, webHooks, _)) + } + } + + def callWebHook(event: WebHook.Event, webHooks: List[WebHook], payload: WebHookPayload) + (implicit c: JsonFormat.Context): List[(WebHook, String, Future[HttpRequest], Future[HttpResponse])] = { import org.apache.http.impl.client.HttpClientBuilder - import scala.concurrent._ - import ExecutionContext.Implicits.global + import ExecutionContext.Implicits.global // TODO Shouldn't use the default execution context + import org.apache.http.protocol.HttpContext + import org.apache.http.client.methods.HttpPost - if(webHookURLs.nonEmpty){ + if(webHooks.nonEmpty){ val json = JsonFormat(payload) - val httpClient = HttpClientBuilder.create.build - webHookURLs.foreach { webHookUrl => + webHooks.map { webHook => + val reqPromise = Promise[HttpRequest] val f = Future { - logger.debug(s"start web hook invocation for ${webHookUrl}") - val httpPost = new HttpPost(webHookUrl.url) - httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded") - httpPost.addHeader("X-Github-Event", eventName) + val itcp = new org.apache.http.HttpRequestInterceptor { + def process(res: HttpRequest, ctx: HttpContext): Unit = { + reqPromise.success(res) + } + } + try{ + val httpClient = HttpClientBuilder.create.useSystemProperties.addInterceptorLast(itcp).build + logger.debug(s"start web hook invocation for ${webHook.url}") + val httpPost = new HttpPost(webHook.url) + logger.info(s"Content-Type: ${webHook.ctype.ctype}") + httpPost.addHeader("Content-Type", webHook.ctype.ctype) + httpPost.addHeader("X-Github-Event", event.name) + httpPost.addHeader("X-Github-Delivery", java.util.UUID.randomUUID().toString) - val params: java.util.List[NameValuePair] = new java.util.ArrayList() - params.add(new BasicNameValuePair("payload", json)) - httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")) + webHook.ctype match { + case WebHookContentType.FORM => { + val params: java.util.List[NameValuePair] = new java.util.ArrayList() + params.add(new BasicNameValuePair("payload", json)) + def postContent = new UrlEncodedFormEntity(params, "UTF-8") + httpPost.setEntity(postContent) + if (webHook.token.exists(_.trim.nonEmpty)) { + // TODO find a better way and see how to extract content from postContent + val contentAsBytes = URLEncodedUtils.format(params, "UTF-8").getBytes("UTF-8") + httpPost.addHeader("X-Hub-Signature", XHub.generateHeaderXHubToken(XHubConverter.HEXA_LOWERCASE, XHubDigest.SHA1, webHook.token.get, contentAsBytes)) + } + } + case WebHookContentType.JSON => { + httpPost.setEntity(EntityBuilder.create().setContentType(ContentType.APPLICATION_JSON).setText(json).build()) + if (webHook.token.exists(_.trim.nonEmpty)) { + httpPost.addHeader("X-Hub-Signature", XHub.generateHeaderXHubToken(XHubConverter.HEXA_LOWERCASE, XHubDigest.SHA1, webHook.token.orNull, json.getBytes("UTF-8"))) + } + } + } - httpClient.execute(httpPost) - httpPost.releaseConnection() - logger.debug(s"end web hook invocation for ${webHookUrl}") + val res = httpClient.execute(httpPost) + httpPost.releaseConnection() + logger.debug(s"end web hook invocation for ${webHook}") + res + } catch { + case e: Throwable => { + if(!reqPromise.isCompleted){ + reqPromise.failure(e) + } + throw e + } + } } f.onSuccess { - case s => logger.debug(s"Success: web hook request to ${webHookUrl.url}") + case s => logger.debug(s"Success: web hook request to ${webHook.url}") } f.onFailure { - case t => logger.error(s"Failed: web hook request to ${webHookUrl.url}", t) + case t => logger.error(s"Failed: web hook request to ${webHook.url}", t) } + (webHook, json, reqPromise.future, f) } + } else { + Nil } - logger.debug("end callWebHook") + // logger.debug("end callWebHook") } } @@ -79,33 +160,35 @@ import WebHookService._ // https://developer.github.com/v3/activity/events/types/#issuesevent - def callIssuesWebHook(action: String, repository: RepositoryService.RepositoryInfo, issue: Issue, baseUrl: String, sender: Account)(implicit s: Session, context:JsonFormat.Context): Unit = { - callWebHookOf(repository.owner, repository.name, "issues"){ + def callIssuesWebHook(action: String, repository: RepositoryService.RepositoryInfo, issue: Issue, baseUrl: String, sender: Account) + (implicit s: Session, context:JsonFormat.Context): Unit = { + callWebHookOf(repository.owner, repository.name, WebHook.Issues){ val users = getAccountsByUserNames(Set(repository.owner, issue.openedUserName), Set(sender)) for{ repoOwner <- users.get(repository.owner) issueUser <- users.get(issue.openedUserName) } yield { WebHookIssuesPayload( - action = action, - number = issue.issueId, - repository = ApiRepository(repository, ApiUser(repoOwner)), - issue = ApiIssue(issue, RepositoryName(repository), ApiUser(issueUser)), - sender = ApiUser(sender)) + action = action, + number = issue.issueId, + repository = ApiRepository(repository, ApiUser(repoOwner)), + issue = ApiIssue(issue, RepositoryName(repository), ApiUser(issueUser)), + sender = ApiUser(sender)) } } } - def callPullRequestWebHook(action: String, repository: RepositoryService.RepositoryInfo, issueId: Int, baseUrl: String, sender: Account)(implicit s: Session, context:JsonFormat.Context): Unit = { + def callPullRequestWebHook(action: String, repository: RepositoryService.RepositoryInfo, issueId: Int, baseUrl: String, sender: Account) + (implicit s: Session, context:JsonFormat.Context): Unit = { import WebHookService._ - callWebHookOf(repository.owner, repository.name, "pull_request"){ + callWebHookOf(repository.owner, repository.name, WebHook.PullRequest){ for{ (issue, pullRequest) <- getPullRequest(repository.owner, repository.name, issueId) users = getAccountsByUserNames(Set(repository.owner, pullRequest.requestUserName, issue.openedUserName), Set(sender)) baseOwner <- users.get(repository.owner) headOwner <- users.get(pullRequest.requestUserName) issueUser <- users.get(issue.openedUserName) - headRepo <- getRepository(pullRequest.requestUserName, pullRequest.requestRepositoryName, baseUrl) + headRepo <- getRepository(pullRequest.requestUserName, pullRequest.requestRepositoryName) } yield { WebHookPullRequestPayload( action = action, @@ -116,7 +199,9 @@ headOwner = headOwner, baseRepository = repository, baseOwner = baseOwner, - sender = sender) + sender = sender, + mergedComment = getMergedComment(repository.owner, repository.name, issueId) + ) } } } @@ -134,15 +219,17 @@ ru <- Accounts if ru.userName === pr.requestUserName iu <- Accounts if iu.userName === is.openedUserName wh <- WebHooks if wh.byRepository(is.userName , is.repositoryName) + wht <- WebHookEvents if wht.event === WebHook.PullRequest.asInstanceOf[WebHook.Event].bind && wht.byWebHook(wh) } yield { ((is, iu, pr, bu, ru), wh) }).list.groupBy(_._1).mapValues(_.map(_._2)) - def callPullRequestWebHookByRequestBranch(action: String, requestRepository: RepositoryService.RepositoryInfo, requestBranch: String, baseUrl: String, sender: Account)(implicit s: Session, context:JsonFormat.Context): Unit = { + def callPullRequestWebHookByRequestBranch(action: String, requestRepository: RepositoryService.RepositoryInfo, requestBranch: String, baseUrl: String, sender: Account) + (implicit s: Session, context:JsonFormat.Context): Unit = { import WebHookService._ for{ ((issue, issueUser, pullRequest, baseOwner, headOwner), webHooks) <- getPullRequestsByRequestForWebhook(requestRepository.owner, requestRepository.name, requestBranch) - baseRepo <- getRepository(pullRequest.userName, pullRequest.repositoryName, baseUrl) + baseRepo <- getRepository(pullRequest.userName, pullRequest.repositoryName) } yield { val payload = WebHookPullRequestPayload( action = action, @@ -153,8 +240,43 @@ headOwner = headOwner, baseRepository = baseRepo, baseOwner = baseOwner, - sender = sender) - callWebHook("pull_request", webHooks, payload) + sender = sender, + mergedComment = getMergedComment(baseRepo.owner, baseRepo.name, issue.issueId) + ) + + callWebHook(WebHook.PullRequest, webHooks, payload) + } + } +} + +trait WebHookPullRequestReviewCommentService extends WebHookService { + self: AccountService with RepositoryService with PullRequestService with IssuesService with CommitsService => + def callPullRequestReviewCommentWebHook(action: String, comment: CommitComment, repository: RepositoryService.RepositoryInfo, issueId: Int, baseUrl: String, sender: Account) + (implicit s: Session, context:JsonFormat.Context): Unit = { + import WebHookService._ + callWebHookOf(repository.owner, repository.name, WebHook.PullRequestReviewComment){ + for{ + (issue, pullRequest) <- getPullRequest(repository.owner, repository.name, issueId) + users = getAccountsByUserNames(Set(repository.owner, pullRequest.requestUserName, issue.openedUserName), Set(sender)) + baseOwner <- users.get(repository.owner) + headOwner <- users.get(pullRequest.requestUserName) + issueUser <- users.get(issue.openedUserName) + headRepo <- getRepository(pullRequest.requestUserName, pullRequest.requestRepositoryName) + } yield { + WebHookPullRequestReviewCommentPayload( + action = action, + comment = comment, + issue = issue, + issueUser = issueUser, + pullRequest = pullRequest, + headRepository = headRepo, + headOwner = headOwner, + baseRepository = repository, + baseOwner = baseOwner, + sender = sender, + mergedComment = getMergedComment(repository.owner, repository.name, issue.issueId) + ) + } } } } @@ -163,8 +285,9 @@ self: AccountService with RepositoryService with PullRequestService with IssuesService => import WebHookService._ - def callIssueCommentWebHook(repository: RepositoryService.RepositoryInfo, issue: Issue, issueCommentId: Int, sender: Account)(implicit s: Session, context:JsonFormat.Context): Unit = { - callWebHookOf(repository.owner, repository.name, "issue_comment"){ + def callIssueCommentWebHook(repository: RepositoryService.RepositoryInfo, issue: Issue, issueCommentId: Int, sender: Account) + (implicit s: Session, context:JsonFormat.Context): Unit = { + callWebHookOf(repository.owner, repository.name, WebHook.IssueComment){ for{ issueComment <- getComment(repository.owner, repository.name, issueCommentId.toString()) users = getAccountsByUserNames(Set(issue.openedUserName, repository.owner, issueComment.commentedUserName), Set(sender)) @@ -190,23 +313,37 @@ // https://developer.github.com/v3/activity/events/types/#pushevent case class WebHookPushPayload( - pusher: ApiUser, + pusher: ApiPusher, + sender: ApiUser, ref: String, + before: String, + after: String, commits: List[ApiCommit], repository: ApiRepository - ) extends WebHookPayload + ) extends FieldSerializable with WebHookPayload { + val compare = commits.size match { + case 0 => ApiPath(s"/${repository.full_name}") // maybe test hook on un-initalied repository + case 1 => ApiPath(s"/${repository.full_name}/commit/${after}") + case _ if before.filterNot(_=='0').isEmpty => ApiPath(s"/${repository.full_name}/compare/${commits.head.id}^...${after}") + case _ => ApiPath(s"/${repository.full_name}/compare/${before}...${after}") + } + val head_commit = commits.lastOption + } object WebHookPushPayload { - def apply(git: Git, pusher: Account, refName: String, repositoryInfo: RepositoryInfo, - commits: List[CommitInfo], repositoryOwner: Account): WebHookPushPayload = + def apply(git: Git, sender: Account, refName: String, repositoryInfo: RepositoryInfo, + commits: List[CommitInfo], repositoryOwner: Account, + newId: ObjectId, oldId: ObjectId): WebHookPushPayload = WebHookPushPayload( - ApiUser(pusher), - refName, - commits.map{ commit => ApiCommit(git, RepositoryName(repositoryInfo), commit) }, - ApiRepository( + pusher = ApiPusher(sender), + sender = ApiUser(sender), + ref = refName, + before = ObjectId.toString(oldId), + after = ObjectId.toString(newId), + commits = commits.map{ commit => ApiCommit.forPushPayload(git, RepositoryName(repositoryInfo), commit) }, + repository = ApiRepository.forPushPayload( repositoryInfo, - owner= ApiUser(repositoryOwner) - ) + owner= ApiUser(repositoryOwner)) ) } @@ -236,11 +373,21 @@ headOwner: Account, baseRepository: RepositoryInfo, baseOwner: Account, - sender: Account): WebHookPullRequestPayload = { + sender: Account, + mergedComment: Option[(IssueComment, Account)]): WebHookPullRequestPayload = { + val headRepoPayload = ApiRepository(headRepository, headOwner) val baseRepoPayload = ApiRepository(baseRepository, baseOwner) val senderPayload = ApiUser(sender) - val pr = ApiPullRequest(issue, pullRequest, headRepoPayload, baseRepoPayload, ApiUser(issueUser)) + val pr = ApiPullRequest( + issue = issue, + pullRequest = pullRequest, + headRepo = headRepoPayload, + baseRepo = baseRepoPayload, + user = ApiUser(issueUser), + mergedComment = mergedComment + ) + WebHookPullRequestPayload( action = action, number = issue.issueId, @@ -260,7 +407,7 @@ sender: ApiUser ) extends WebHookPayload - object WebHookIssueCommentPayload{ + object WebHookIssueCommentPayload { def apply( issue: Issue, issueUser: Account, @@ -273,7 +420,55 @@ action = "created", repository = ApiRepository(repository, repositoryUser), issue = ApiIssue(issue, RepositoryName(repository), ApiUser(issueUser)), - comment = ApiComment(comment, RepositoryName(repository), issue.issueId, ApiUser(commentUser)), + comment = ApiComment(comment, RepositoryName(repository), issue.issueId, ApiUser(commentUser), issue.isPullRequest), sender = ApiUser(sender)) } + + // https://developer.github.com/v3/activity/events/types/#pullrequestreviewcommentevent + case class WebHookPullRequestReviewCommentPayload( + action: String, + comment: ApiPullRequestReviewComment, + pull_request: ApiPullRequest, + repository: ApiRepository, + sender: ApiUser + ) extends WebHookPayload + + object WebHookPullRequestReviewCommentPayload { + def apply( + action: String, + comment: CommitComment, + issue: Issue, + issueUser: Account, + pullRequest: PullRequest, + headRepository: RepositoryInfo, + headOwner: Account, + baseRepository: RepositoryInfo, + baseOwner: Account, + sender: Account, + mergedComment: Option[(IssueComment, Account)] + ) : WebHookPullRequestReviewCommentPayload = { + val headRepoPayload = ApiRepository(headRepository, headOwner) + val baseRepoPayload = ApiRepository(baseRepository, baseOwner) + val senderPayload = ApiUser(sender) + + WebHookPullRequestReviewCommentPayload( + action = action, + comment = ApiPullRequestReviewComment( + comment = comment, + commentedUser = senderPayload, + repositoryName = RepositoryName(baseRepository), + issueId = issue.issueId + ), + pull_request = ApiPullRequest( + issue = issue, + pullRequest = pullRequest, + headRepo = headRepoPayload, + baseRepo = baseRepoPayload, + user = ApiUser(issueUser), + mergedComment = mergedComment + ), + repository = baseRepoPayload, + sender = senderPayload) + } + } } diff --git a/src/main/scala/gitbucket/core/service/WikiService.scala b/src/main/scala/gitbucket/core/service/WikiService.scala index 4a8d1eb..1bff5dc 100644 --- a/src/main/scala/gitbucket/core/service/WikiService.scala +++ b/src/main/scala/gitbucket/core/service/WikiService.scala @@ -1,7 +1,9 @@ package gitbucket.core.service import java.util.Date +import gitbucket.core.controller.Context import gitbucket.core.model.Account +import gitbucket.core.service.RepositoryService.RepositoryInfo import gitbucket.core.util._ import gitbucket.core.util.ControlUtil._ import org.eclipse.jgit.api.Git @@ -13,7 +15,6 @@ import org.eclipse.jgit.patch._ import org.eclipse.jgit.api.errors.PatchFormatException import scala.collection.JavaConverters._ -import RepositoryService.RepositoryInfo object WikiService { @@ -38,10 +39,13 @@ */ case class WikiPageHistoryInfo(name: String, committer: String, message: String, date: Date) - def httpUrl(repository: RepositoryInfo) = repository.httpUrl.replaceFirst("\\.git\\Z", ".wiki.git") - def sshUrl(repository: RepositoryInfo, settings: SystemSettingsService.SystemSettings, userName: String) = - repository.sshUrl(settings.sshPort.getOrElse(SystemSettingsService.DefaultSshPort), userName).replaceFirst("\\.git\\Z", ".wiki.git") + def wikiHttpUrl(repositoryInfo: RepositoryInfo)(implicit context: Context): String + = RepositoryService.httpUrl(repositoryInfo.owner, repositoryInfo.name + ".wiki") + + def wikiSshUrl(repositoryInfo: RepositoryInfo)(implicit context: Context): Option[String] + = RepositoryService.sshUrl(repositoryInfo.owner, repositoryInfo.name + ".wiki") + } trait WikiService { @@ -93,7 +97,7 @@ def getWikiPageList(owner: String, repository: String): List[String] = { using(Git.open(Directory.getWikiRepositoryDir(owner, repository))){ git => JGitUtil.getFileList(git, "master", ".") - .filter(_.name.endsWith(".md")) + .filter(_.name.endsWith(".md")).filterNot(_.name.startsWith("_")) .map(_.name.stripSuffix(".md")) .sortBy(x => x) } diff --git a/src/main/scala/gitbucket/core/servlet/AccessTokenAuthenticationFilter.scala b/src/main/scala/gitbucket/core/servlet/AccessTokenAuthenticationFilter.scala deleted file mode 100644 index 7cc3754..0000000 --- a/src/main/scala/gitbucket/core/servlet/AccessTokenAuthenticationFilter.scala +++ /dev/null @@ -1,43 +0,0 @@ -package gitbucket.core.servlet - -import javax.servlet._ -import javax.servlet.http.{HttpServletRequest, HttpServletResponse} - -import gitbucket.core.model.Account -import gitbucket.core.service.AccessTokenService -import gitbucket.core.util.Keys - -import org.scalatra.servlet.ServletApiImplicits._ -import org.scalatra._ - - -class AccessTokenAuthenticationFilter extends Filter with AccessTokenService { - private val tokenHeaderPrefix = "token " - - override def init(filterConfig: FilterConfig): Unit = {} - - override def destroy(): Unit = {} - - override def doFilter(req: ServletRequest, res: ServletResponse, chain: FilterChain): Unit = { - implicit val request = req.asInstanceOf[HttpServletRequest] - implicit val session = req.getAttribute(Keys.Request.DBSession).asInstanceOf[slick.jdbc.JdbcBackend#Session] - val response = res.asInstanceOf[HttpServletResponse] - Option(request.getHeader("Authorization")).map{ - case auth if auth.startsWith("token ") => AccessTokenService.getAccountByAccessToken(auth.substring(6).trim).toRight(Unit) - // TODO Basic Authentication Support - case _ => Left(Unit) - }.orElse{ - Option(request.getSession.getAttribute(Keys.Session.LoginAccount).asInstanceOf[Account]).map(Right(_)) - } match { - case Some(Right(account)) => request.setAttribute(Keys.Session.LoginAccount, account); chain.doFilter(req, res) - case None => chain.doFilter(req, res) - case Some(Left(_)) => { - response.setStatus(HttpServletResponse.SC_UNAUTHORIZED) - response.setContentType("Content-Type: application/json; charset=utf-8") - val w = response.getWriter() - w.print("""{ "message": "Bad credentials" }""") - w.close() - } - } - } -} diff --git a/src/main/scala/gitbucket/core/servlet/ApiAuthenticationFilter.scala b/src/main/scala/gitbucket/core/servlet/ApiAuthenticationFilter.scala new file mode 100644 index 0000000..c3bd85c --- /dev/null +++ b/src/main/scala/gitbucket/core/servlet/ApiAuthenticationFilter.scala @@ -0,0 +1,46 @@ +package gitbucket.core.servlet + +import javax.servlet._ +import javax.servlet.http.{HttpServletRequest, HttpServletResponse} + +import gitbucket.core.model.Account +import gitbucket.core.service.SystemSettingsService.SystemSettings +import gitbucket.core.service.{AccessTokenService, AccountService, SystemSettingsService} +import gitbucket.core.util.{AuthUtil, Keys} + + +class ApiAuthenticationFilter extends Filter with AccessTokenService with AccountService with SystemSettingsService { + + override def init(filterConfig: FilterConfig): Unit = {} + + override def destroy(): Unit = {} + + override def doFilter(req: ServletRequest, res: ServletResponse, chain: FilterChain): Unit = { + implicit val request = req.asInstanceOf[HttpServletRequest] + implicit val session = req.getAttribute(Keys.Request.DBSession).asInstanceOf[slick.jdbc.JdbcBackend#Session] + val response = res.asInstanceOf[HttpServletResponse] + Option(request.getHeader("Authorization")).map{ + case auth if auth.startsWith("token ") => AccessTokenService.getAccountByAccessToken(auth.substring(6).trim).toRight(()) + case auth if auth.startsWith("Basic ") => doBasicAuth(auth, loadSystemSettings(), request).toRight(()) + case _ => Left(()) + }.orElse{ + Option(request.getSession.getAttribute(Keys.Session.LoginAccount).asInstanceOf[Account]).map(Right(_)) + } match { + case Some(Right(account)) => request.setAttribute(Keys.Session.LoginAccount, account); chain.doFilter(req, res) + case None => chain.doFilter(req, res) + case Some(Left(_)) => { + response.setStatus(HttpServletResponse.SC_UNAUTHORIZED) + response.setContentType("application/json; charset=utf-8") + val w = response.getWriter() + w.print("""{ "message": "Bad credentials" }""") + w.close() + } + } + } + + def doBasicAuth(auth: String, settings: SystemSettings, request: HttpServletRequest): Option[Account] = { + implicit val session = request.getAttribute(Keys.Request.DBSession).asInstanceOf[slick.jdbc.JdbcBackend#Session] + val Array(username, password) = AuthUtil.decodeAuthHeader(auth).split(":", 2) + authenticate(settings, username, password) + } +} diff --git a/src/main/scala/gitbucket/core/servlet/AutoUpdate.scala b/src/main/scala/gitbucket/core/servlet/AutoUpdate.scala deleted file mode 100644 index 377b219..0000000 --- a/src/main/scala/gitbucket/core/servlet/AutoUpdate.scala +++ /dev/null @@ -1,170 +0,0 @@ -package gitbucket.core.servlet - -import java.io.File -import java.sql.{DriverManager, Connection} -import gitbucket.core.plugin.PluginRegistry -import gitbucket.core.service.SystemSettingsService -import gitbucket.core.util._ -import org.apache.commons.io.FileUtils -import javax.servlet.{ServletContextListener, ServletContextEvent} -import org.slf4j.LoggerFactory -import Directory._ -import ControlUtil._ -import JDBCUtil._ -import org.eclipse.jgit.api.Git -import gitbucket.core.util.Versions -import gitbucket.core.util.Directory - -object AutoUpdate { - - /** - * The history of versions. A head of this sequence is the current BitBucket version. - */ - val versions = Seq( - new Version(3, 5), - new Version(3, 4), - new Version(3, 3), - new Version(3, 2), - new Version(3, 1), - new Version(3, 0), - new Version(2, 8), - new Version(2, 7) { - override def update(conn: Connection, cl: ClassLoader): Unit = { - super.update(conn, cl) - conn.select("SELECT * FROM REPOSITORY"){ rs => - // Rename attached files directory from /issues to /comments - val userName = rs.getString("USER_NAME") - val repoName = rs.getString("REPOSITORY_NAME") - defining(Directory.getAttachedDir(userName, repoName)){ newDir => - val oldDir = new File(newDir.getParentFile, "issues") - if(oldDir.exists && oldDir.isDirectory){ - oldDir.renameTo(newDir) - } - } - // Update ORIGIN_USER_NAME and ORIGIN_REPOSITORY_NAME if it does not exist - val originalUserName = rs.getString("ORIGIN_USER_NAME") - val originalRepoName = rs.getString("ORIGIN_REPOSITORY_NAME") - if(originalUserName != null && originalRepoName != null){ - if(conn.selectInt("SELECT COUNT(*) FROM REPOSITORY WHERE USER_NAME = ? AND REPOSITORY_NAME = ?", - originalUserName, originalRepoName) == 0){ - conn.update("UPDATE REPOSITORY SET ORIGIN_USER_NAME = NULL, ORIGIN_REPOSITORY_NAME = NULL " + - "WHERE USER_NAME = ? AND REPOSITORY_NAME = ?", userName, repoName) - } - } - // Update PARENT_USER_NAME and PARENT_REPOSITORY_NAME if it does not exist - val parentUserName = rs.getString("PARENT_USER_NAME") - val parentRepoName = rs.getString("PARENT_REPOSITORY_NAME") - if(parentUserName != null && parentRepoName != null){ - if(conn.selectInt("SELECT COUNT(*) FROM REPOSITORY WHERE USER_NAME = ? AND REPOSITORY_NAME = ?", - parentUserName, parentRepoName) == 0){ - conn.update("UPDATE REPOSITORY SET PARENT_USER_NAME = NULL, PARENT_REPOSITORY_NAME = NULL " + - "WHERE USER_NAME = ? AND REPOSITORY_NAME = ?", userName, repoName) - } - } - } - } - }, - new Version(2, 6), - new Version(2, 5), - new Version(2, 4), - new Version(2, 3) { - override def update(conn: Connection, cl: ClassLoader): Unit = { - super.update(conn, cl) - conn.select("SELECT ACTIVITY_ID, ADDITIONAL_INFO FROM ACTIVITY WHERE ACTIVITY_TYPE='push'"){ rs => - val curInfo = rs.getString("ADDITIONAL_INFO") - val newInfo = curInfo.split("\n").filter(_ matches "^[0-9a-z]{40}:.*").mkString("\n") - if (curInfo != newInfo) { - conn.update("UPDATE ACTIVITY SET ADDITIONAL_INFO = ? WHERE ACTIVITY_ID = ?", newInfo, rs.getInt("ACTIVITY_ID")) - } - } - ignore { - FileUtils.deleteDirectory(Directory.getPluginCacheDir()) - //FileUtils.deleteDirectory(new File(Directory.PluginHome)) - } - } - }, - new Version(2, 2), - new Version(2, 1), - new Version(2, 0){ - override def update(conn: Connection, cl: ClassLoader): Unit = { - import eu.medsea.mimeutil.{MimeUtil2, MimeType} - - val mimeUtil = new MimeUtil2() - mimeUtil.registerMimeDetector("eu.medsea.mimeutil.detector.MagicMimeMimeDetector") - - super.update(conn, cl) - conn.select("SELECT USER_NAME, REPOSITORY_NAME FROM REPOSITORY"){ rs => - defining(Directory.getAttachedDir(rs.getString("USER_NAME"), rs.getString("REPOSITORY_NAME"))){ dir => - if(dir.exists && dir.isDirectory){ - dir.listFiles.foreach { file => - if(file.getName.indexOf('.') < 0){ - val mimeType = MimeUtil2.getMostSpecificMimeType(mimeUtil.getMimeTypes(file, new MimeType("application/octet-stream"))).toString - if(mimeType.startsWith("image/")){ - file.renameTo(new File(file.getParent, file.getName + "." + mimeType.split("/")(1))) - } - } - } - } - } - } - } - }, - Version(1, 13), - Version(1, 12), - Version(1, 11), - Version(1, 10), - Version(1, 9), - Version(1, 8), - Version(1, 7), - Version(1, 6), - Version(1, 5), - Version(1, 4), - new Version(1, 3){ - override def update(conn: Connection, cl: ClassLoader): Unit = { - super.update(conn, cl) - // Fix wiki repository configuration - conn.select("SELECT USER_NAME, REPOSITORY_NAME FROM REPOSITORY"){ rs => - using(Git.open(getWikiRepositoryDir(rs.getString("USER_NAME"), rs.getString("REPOSITORY_NAME")))){ git => - defining(git.getRepository.getConfig){ config => - if(!config.getBoolean("http", "receivepack", false)){ - config.setBoolean("http", null, "receivepack", true) - config.save - } - } - } - } - } - }, - Version(1, 2), - Version(1, 1), - Version(1, 0), - Version(0, 0) - ) - - /** - * The head version of BitBucket. - */ - val headVersion = versions.head - - /** - * The version file (GITBUCKET_HOME/version). - */ - lazy val versionFile = new File(GitBucketHome, "version") - - /** - * Returns the current version from the version file. - */ - def getCurrentVersion(): Version = { - if(versionFile.exists){ - FileUtils.readFileToString(versionFile, "UTF-8").trim.split("\\.") match { - case Array(majorVersion, minorVersion) => { - versions.find { v => - v.majorVersion == majorVersion.toInt && v.minorVersion == minorVersion.toInt - }.getOrElse(Version(0, 0)) - } - case _ => Version(0, 0) - } - } else Version(0, 0) - } - -} diff --git a/src/main/scala/gitbucket/core/servlet/BasicAuthenticationFilter.scala b/src/main/scala/gitbucket/core/servlet/BasicAuthenticationFilter.scala deleted file mode 100644 index 42acffd..0000000 --- a/src/main/scala/gitbucket/core/servlet/BasicAuthenticationFilter.scala +++ /dev/null @@ -1,96 +0,0 @@ -package gitbucket.core.servlet - -import javax.servlet._ -import javax.servlet.http._ -import gitbucket.core.service.{RepositoryService, AccountService, SystemSettingsService} -import gitbucket.core.util.{ControlUtil, Keys, Implicits} -import org.slf4j.LoggerFactory -import Implicits._ -import ControlUtil._ - -/** - * Provides BASIC Authentication for [[GitRepositoryServlet]]. - */ -class BasicAuthenticationFilter extends Filter with RepositoryService with AccountService with SystemSettingsService { - - private val logger = LoggerFactory.getLogger(classOf[BasicAuthenticationFilter]) - - def init(config: FilterConfig) = {} - - def destroy(): Unit = {} - - def doFilter(req: ServletRequest, res: ServletResponse, chain: FilterChain): Unit = { - implicit val request = req.asInstanceOf[HttpServletRequest] - val response = res.asInstanceOf[HttpServletResponse] - - val wrappedResponse = new HttpServletResponseWrapper(response){ - override def setCharacterEncoding(encoding: String) = {} - } - - val isUpdating = request.getRequestURI.endsWith("/git-receive-pack") || "service=git-receive-pack".equals(request.getQueryString) - val settings = loadSystemSettings() - - try { - defining(request.paths){ - case Array(_, repositoryOwner, repositoryName, _*) => - getRepository(repositoryOwner, repositoryName.replaceFirst("\\.wiki\\.git$|\\.git$", ""), "") match { - case Some(repository) => { - if(!isUpdating && !repository.repository.isPrivate && settings.allowAnonymousAccess){ - chain.doFilter(req, wrappedResponse) - } else { - request.getHeader("Authorization") match { - case null => requireAuth(response) - case auth => decodeAuthHeader(auth).split(":", 2) match { - case Array(username, password) => { - authenticate(settings, username, password) match { - case Some(account) => { - if (isUpdating || repository.repository.isPrivate) { - if(hasWritePermission(repository.owner, repository.name, Some(account))){ - request.setAttribute(Keys.Request.UserName, account.userName) - chain.doFilter(req, wrappedResponse) - } else { - requireAuth(response) - } - } else { - chain.doFilter(req, wrappedResponse) - } - } - case _ => requireAuth(response) - } - } - case _ => requireAuth(response) - } - } - } - } - case None => { - logger.debug(s"Repository ${repositoryOwner}/${repositoryName} is not found.") - response.sendError(HttpServletResponse.SC_NOT_FOUND) - } - } - case _ => { - logger.debug(s"Not enough path arguments: ${request.paths}") - response.sendError(HttpServletResponse.SC_NOT_FOUND) - } - } - } catch { - case ex: Exception => { - logger.error("error", ex) - requireAuth(response) - } - } - } - - private def requireAuth(response: HttpServletResponse): Unit = { - response.setHeader("WWW-Authenticate", "BASIC realm=\"GitBucket\"") - response.sendError(HttpServletResponse.SC_UNAUTHORIZED) - } - - private def decodeAuthHeader(header: String): String = { - try { - new String(new sun.misc.BASE64Decoder().decodeBuffer(header.substring(6))) - } catch { - case _: Throwable => "" - } - } -} \ No newline at end of file diff --git a/src/main/scala/gitbucket/core/servlet/GHCompatRepositoryAccessFilter.scala b/src/main/scala/gitbucket/core/servlet/GHCompatRepositoryAccessFilter.scala new file mode 100644 index 0000000..0f1322d --- /dev/null +++ b/src/main/scala/gitbucket/core/servlet/GHCompatRepositoryAccessFilter.scala @@ -0,0 +1,36 @@ +package gitbucket.core.servlet + +import javax.servlet._ +import javax.servlet.http.{HttpServletRequest, HttpServletResponse} + +import gitbucket.core.service.SystemSettingsService + +/** + * A controller to provide GitHub compatible URL for Git clients. + */ +class GHCompatRepositoryAccessFilter extends Filter with SystemSettingsService { + + /** + * Pattern of GitHub compatible repository URL. + * /:user/:repo.git/ + */ + private val githubRepositoryPattern = """^/[^/]+/[^/]+\.git/.*""".r + + override def init(filterConfig: FilterConfig) = {} + + override def doFilter(req: ServletRequest, res: ServletResponse, chain: FilterChain) = { + implicit val request = req.asInstanceOf[HttpServletRequest] + val response = res.asInstanceOf[HttpServletResponse] + val requestPath = request.getRequestURI.substring(request.getContextPath.length) + requestPath match { + case githubRepositoryPattern() => + response.sendRedirect(baseUrl + "/git" + requestPath) + + case _ => + chain.doFilter(req, res) + } + } + + override def destroy() = {} + +} diff --git a/src/main/scala/gitbucket/core/servlet/GitAuthenticationFilter.scala b/src/main/scala/gitbucket/core/servlet/GitAuthenticationFilter.scala new file mode 100644 index 0000000..59cd058 --- /dev/null +++ b/src/main/scala/gitbucket/core/servlet/GitAuthenticationFilter.scala @@ -0,0 +1,116 @@ +package gitbucket.core.servlet + +import javax.servlet._ +import javax.servlet.http._ +import gitbucket.core.plugin.{GitRepositoryFilter, GitRepositoryRouting, PluginRegistry} +import gitbucket.core.service.SystemSettingsService.SystemSettings +import gitbucket.core.service.{RepositoryService, AccountService, SystemSettingsService} +import gitbucket.core.util.{Keys, Implicits, AuthUtil} +import org.slf4j.LoggerFactory +import Implicits._ + +/** + * Provides BASIC Authentication for [[GitRepositoryServlet]]. + */ +class GitAuthenticationFilter extends Filter with RepositoryService with AccountService with SystemSettingsService { + + private val logger = LoggerFactory.getLogger(classOf[GitAuthenticationFilter]) + + def init(config: FilterConfig) = {} + + def destroy(): Unit = {} + + def doFilter(req: ServletRequest, res: ServletResponse, chain: FilterChain): Unit = { + val request = req.asInstanceOf[HttpServletRequest] + val response = res.asInstanceOf[HttpServletResponse] + + val wrappedResponse = new HttpServletResponseWrapper(response){ + override def setCharacterEncoding(encoding: String) = {} + } + + val isUpdating = request.getRequestURI.endsWith("/git-receive-pack") || "service=git-receive-pack".equals(request.getQueryString) + val settings = loadSystemSettings() + + try { + PluginRegistry().getRepositoryRouting(request.gitRepositoryPath).map { case GitRepositoryRouting(_, _, filter) => + // served by plug-ins + pluginRepository(request, wrappedResponse, chain, settings, isUpdating, filter) + + }.getOrElse { + // default repositories + defaultRepository(request, wrappedResponse, chain, settings, isUpdating) + } + } catch { + case ex: Exception => { + logger.error("error", ex) + AuthUtil.requireAuth(response) + } + } + } + + private def pluginRepository(request: HttpServletRequest, response: HttpServletResponse, chain: FilterChain, + settings: SystemSettings, isUpdating: Boolean, filter: GitRepositoryFilter): Unit = { + implicit val r = request + + val account = for { + auth <- Option(request.getHeader("Authorization")) + Array(username, password) = AuthUtil.decodeAuthHeader(auth).split(":", 2) + account <- authenticate(settings, username, password) + } yield { + request.setAttribute(Keys.Request.UserName, account.userName) + account + } + + if(filter.filter(request.gitRepositoryPath, account.map(_.userName), settings, isUpdating)){ + chain.doFilter(request, response) + } else { + AuthUtil.requireAuth(response) + } + } + + private def defaultRepository(request: HttpServletRequest, response: HttpServletResponse, chain: FilterChain, + settings: SystemSettings, isUpdating: Boolean): Unit = { + val action = request.paths match { + case Array(_, repositoryOwner, repositoryName, _*) => + Database() withSession { implicit session => + getRepository(repositoryOwner, repositoryName.replaceFirst("\\.wiki\\.git$|\\.git$", "")) match { + case Some(repository) => { + val execute = if (!isUpdating && !repository.repository.isPrivate && settings.allowAnonymousAccess) { + // Authentication is not required + true + } else { + // Authentication is required + val passed = for { + auth <- Option(request.getHeader("Authorization")) + Array(username, password) = AuthUtil.decodeAuthHeader(auth).split(":", 2) + account <- authenticate(settings, username, password) + } yield if (isUpdating || repository.repository.isPrivate) { + if (hasDeveloperRole(repository.owner, repository.name, Some(account))) { + request.setAttribute(Keys.Request.UserName, account.userName) + true + } else false + } else true + passed.getOrElse(false) + } + + if (execute) { + () => chain.doFilter(request, response) + } else { + () => AuthUtil.requireAuth(response) + } + } + case None => () => { + logger.debug(s"Repository ${repositoryOwner}/${repositoryName} is not found.") + response.sendError(HttpServletResponse.SC_NOT_FOUND) + } + } + } + case _ => () => { + logger.debug(s"Not enough path arguments: ${request.paths}") + response.sendError(HttpServletResponse.SC_NOT_FOUND) + } + } + + action() + } +} \ No newline at end of file diff --git a/src/main/scala/gitbucket/core/servlet/GitLfsTransferServlet.scala b/src/main/scala/gitbucket/core/servlet/GitLfsTransferServlet.scala new file mode 100644 index 0000000..4c31884 --- /dev/null +++ b/src/main/scala/gitbucket/core/servlet/GitLfsTransferServlet.scala @@ -0,0 +1,83 @@ +package gitbucket.core.servlet + +import java.io.{File, FileInputStream, FileOutputStream} +import java.text.MessageFormat +import javax.servlet.http.{HttpServlet, HttpServletRequest, HttpServletResponse} + +import gitbucket.core.util.{FileUtil, StringUtil} +import org.apache.commons.io.{FileUtils, IOUtils} +import org.json4s.jackson.Serialization._ +import org.apache.http.HttpStatus +import gitbucket.core.util.ControlUtil._ + +/** + * Provides GitLFS Transfer API + * https://github.com/git-lfs/git-lfs/blob/master/docs/api/basic-transfers.md + */ +class GitLfsTransferServlet extends HttpServlet { + + private implicit val jsonFormats = gitbucket.core.api.JsonFormat.jsonFormats + private val LongObjectIdLength = 32 + private val LongObjectIdStringLength = LongObjectIdLength * 2 + + override protected def doGet(req: HttpServletRequest, res: HttpServletResponse): Unit = { + for { + (owner, repository, oid) <- getPathInfo(req, res) if checkToken(req, oid) + } yield { + val file = new File(FileUtil.getLfsFilePath(owner, repository, oid)) + if(file.exists()){ + res.setStatus(HttpStatus.SC_OK) + res.setContentType("application/octet-stream") + res.setContentLength(file.length.toInt) + using(new FileInputStream(file), res.getOutputStream){ (in, out) => + IOUtils.copy(in, out) + out.flush() + } + } else { + sendError(res, HttpStatus.SC_NOT_FOUND, + MessageFormat.format("Object ''{0}'' not found", oid)) + } + } + } + + override protected def doPut(req: HttpServletRequest, res: HttpServletResponse): Unit = { + for { + (owner, repository, oid) <- getPathInfo(req, res) if checkToken(req, oid) + } yield { + val file = new File(FileUtil.getLfsFilePath(owner, repository, oid)) + FileUtils.forceMkdir(file.getParentFile) + using(req.getInputStream, new FileOutputStream(file)){ (in, out) => + IOUtils.copy(in, out) + } + res.setStatus(HttpStatus.SC_OK) + } + } + + private def checkToken(req: HttpServletRequest, oid: String): Boolean = { + val token = req.getHeader("Authorization") + if(token != null){ + val Array(expireAt, targetOid) = StringUtil.decodeBlowfish(token).split(" ") + oid == targetOid && expireAt.toLong > System.currentTimeMillis + } else { + false + } + } + + private def getPathInfo(req: HttpServletRequest, res: HttpServletResponse): Option[(String, String, String)] = { + req.getRequestURI.substring(1).split("/") match { + case Array(_, owner, repository, oid) => Some((owner, repository, oid)) + case _ => None + } + } + + private def sendError(res: HttpServletResponse, status: Int, message: String): Unit = { + res.setStatus(status) + using(res.getWriter()){ out => + out.write(write(GitLfs.Error(message))) + out.flush() + } + } + +} + + diff --git a/src/main/scala/gitbucket/core/servlet/GitRepositoryServlet.scala b/src/main/scala/gitbucket/core/servlet/GitRepositoryServlet.scala index 88b8124..5ca72be 100644 --- a/src/main/scala/gitbucket/core/servlet/GitRepositoryServlet.scala +++ b/src/main/scala/gitbucket/core/servlet/GitRepositoryServlet.scala @@ -1,54 +1,45 @@ package gitbucket.core.servlet +import java.io.File +import java.util.Date + import gitbucket.core.api -import gitbucket.core.model.Session +import gitbucket.core.model.WebHook +import gitbucket.core.plugin.{GitRepositoryRouting, PluginRegistry} import gitbucket.core.service.IssuesService.IssueSearchCondition import gitbucket.core.service.WebHookService._ import gitbucket.core.service._ import gitbucket.core.util.ControlUtil._ import gitbucket.core.util.Implicits._ -import gitbucket.core.util.JGitUtil.CommitInfo import gitbucket.core.util._ - import org.eclipse.jgit.api.Git import org.eclipse.jgit.http.server.GitServlet import org.eclipse.jgit.lib._ import org.eclipse.jgit.transport._ import org.eclipse.jgit.transport.resolver._ import org.slf4j.LoggerFactory - import javax.servlet.ServletConfig -import javax.servlet.ServletContext -import javax.servlet.http.{HttpServletResponse, HttpServletRequest} +import javax.servlet.http.{HttpServletRequest, HttpServletResponse} + +import org.json4s.jackson.Serialization._ /** * Provides Git repository via HTTP. * * This servlet provides only Git repository functionality. - * Authentication is provided by [[BasicAuthenticationFilter]]. + * Authentication is provided by [[GitAuthenticationFilter]]. */ class GitRepositoryServlet extends GitServlet with SystemSettingsService { private val logger = LoggerFactory.getLogger(classOf[GitRepositoryServlet]) - + private implicit val jsonFormats = gitbucket.core.api.JsonFormat.jsonFormats + override def init(config: ServletConfig): Unit = { setReceivePackFactory(new GitBucketReceivePackFactory()) - // TODO are there any other ways...? - super.init(new ServletConfig(){ - def getInitParameter(name: String): String = name match { - case "base-path" => Directory.RepositoryHome - case "export-all" => "true" - case name => config.getInitParameter(name) - } - def getInitParameterNames(): java.util.Enumeration[String] = { - config.getInitParameterNames - } - - def getServletContext(): ServletContext = config.getServletContext - def getServletName(): String = config.getServletName - }) + val root: File = new File(Directory.RepositoryHome) + setRepositoryResolver(new GitBucketRepositoryResolver(new FileResolver[HttpServletRequest](root, true))) super.init(config) } @@ -56,15 +47,89 @@ override def service(req: HttpServletRequest, res: HttpServletResponse): Unit = { val agent = req.getHeader("USER-AGENT") val index = req.getRequestURI.indexOf(".git") - if(index >= 0 && (agent == null || agent.toLowerCase.indexOf("git/") < 0)){ + if(index >= 0 && (agent == null || agent.toLowerCase.indexOf("git") < 0)){ // redirect for browsers val paths = req.getRequestURI.substring(0, index).split("/") res.sendRedirect(baseUrl(req) + "/" + paths.dropRight(1).last + "/" + paths.last) + + } else if(req.getMethod.toUpperCase == "POST" && req.getRequestURI.endsWith("/info/lfs/objects/batch")){ + serviceGitLfsBatchAPI(req, res) + } else { // response for git client super.service(req, res) } } + + /** + * Provides GitLFS Batch API + * https://github.com/git-lfs/git-lfs/blob/master/docs/api/batch.md + */ + protected def serviceGitLfsBatchAPI(req: HttpServletRequest, res: HttpServletResponse): Unit = { + val batchRequest = read[GitLfs.BatchRequest](req.getInputStream) + val settings = loadSystemSettings() + + settings.baseUrl match { + case None => { + throw new IllegalStateException("lfs.server_url is not configured.") + } + case Some(baseUrl) => { + req.getRequestURI.substring(1).replace(".git/", "/").split("/") match { + case Array(_, owner, repository, _*) => { + val timeout = System.currentTimeMillis + (60000 * 10) // 10 min. + val batchResponse = batchRequest.operation match { + case "upload" => + GitLfs.BatchUploadResponse("basic", batchRequest.objects.map { requestObject => + GitLfs.BatchResponseObject(requestObject.oid, requestObject.size, true, + GitLfs.Actions( + upload = Some(GitLfs.Action( + href = baseUrl + "/git-lfs/" + owner + "/" + repository + "/" + requestObject.oid, + header = Map("Authorization" -> StringUtil.encodeBlowfish(timeout + " " + requestObject.oid)), + expires_at = new Date(timeout) + )) + ) + ) + }) + case "download" => + GitLfs.BatchUploadResponse("basic", batchRequest.objects.map { requestObject => + GitLfs.BatchResponseObject(requestObject.oid, requestObject.size, true, + GitLfs.Actions( + download = Some(GitLfs.Action( + href = baseUrl + "/git-lfs/" + owner + "/" + repository + "/" + requestObject.oid, + header = Map("Authorization" -> StringUtil.encodeBlowfish(timeout + " " + requestObject.oid)), + expires_at = new Date(timeout) + )) + ) + ) + }) + } + + res.setContentType("application/vnd.git-lfs+json") + using(res.getWriter){ out => + out.print(write(batchResponse)) + out.flush() + } + } + } + } + } + } +} + +class GitBucketRepositoryResolver(parent: FileResolver[HttpServletRequest]) extends RepositoryResolver[HttpServletRequest] { + + private val resolver = new FileResolver[HttpServletRequest](new File(Directory.GitBucketHome), true) + + override def open(req: HttpServletRequest, name: String): Repository = { + // Rewrite repository path if routing is marched + PluginRegistry().getRepositoryRouting("/" + name).map { case GitRepositoryRouting(urlPattern, localPath, _) => + val path = urlPattern.r.replaceFirstIn(name, localPath) + resolver.open(req, path) + }.getOrElse { + parent.open(req, name) + } + } + } class GitBucketReceivePackFactory extends ReceivePackFactory[HttpServletRequest] with SystemSettingsService { @@ -73,148 +138,203 @@ override def create(request: HttpServletRequest, db: Repository): ReceivePack = { val receivePack = new ReceivePack(db) - val pusher = request.getAttribute(Keys.Request.UserName).asInstanceOf[String] - logger.debug("requestURI: " + request.getRequestURI) - logger.debug("pusher:" + pusher) + if(PluginRegistry().getRepositoryRouting(request.gitRepositoryPath).isEmpty){ + val pusher = request.getAttribute(Keys.Request.UserName).asInstanceOf[String] - defining(request.paths){ paths => - val owner = paths(1) - val repository = paths(2).stripSuffix(".git") + logger.debug("requestURI: " + request.getRequestURI) + logger.debug("pusher:" + pusher) - logger.debug("repository:" + owner + "/" + repository) + defining(request.paths){ paths => + val owner = paths(1) + val repository = paths(2).stripSuffix(".git") - if(!repository.endsWith(".wiki")){ - defining(request) { implicit r => - val hook = new CommitLogHook(owner, repository, pusher, baseUrl) - receivePack.setPreReceiveHook(hook) - receivePack.setPostReceiveHook(hook) + logger.debug("repository:" + owner + "/" + repository) + + if(!repository.endsWith(".wiki")){ + defining(request) { implicit r => + val hook = new CommitLogHook(owner, repository, pusher, baseUrl) + receivePack.setPreReceiveHook(hook) + receivePack.setPostReceiveHook(hook) + } } } - receivePack } + + receivePack } } import scala.collection.JavaConverters._ -class CommitLogHook(owner: String, repository: String, pusher: String, baseUrl: String)(implicit session: Session) +class CommitLogHook(owner: String, repository: String, pusher: String, baseUrl: String)/*(implicit session: Session)*/ extends PostReceiveHook with PreReceiveHook with RepositoryService with AccountService with IssuesService with ActivityService with PullRequestService with WebHookService - with WebHookPullRequestService { + with WebHookPullRequestService with CommitsService { private val logger = LoggerFactory.getLogger(classOf[CommitLogHook]) private var existIds: Seq[String] = Nil def onPreReceive(receivePack: ReceivePack, commands: java.util.Collection[ReceiveCommand]): Unit = { - try { - using(Git.open(Directory.getRepositoryDir(owner, repository))) { git => - existIds = JGitUtil.getAllCommitIds(git) - } - } catch { - case ex: Exception => { - logger.error(ex.toString, ex) - throw ex + Database() withTransaction { implicit session => + try { + commands.asScala.foreach { command => + // call pre-commit hook + PluginRegistry().getReceiveHooks + .flatMap(_.preReceive(owner, repository, receivePack, command, pusher)) + .headOption.foreach { error => + command.setResult(ReceiveCommand.Result.REJECTED_OTHER_REASON, error) + } + } + using(Git.open(Directory.getRepositoryDir(owner, repository))) { git => + existIds = JGitUtil.getAllCommitIds(git) + } + } catch { + case ex: Exception => { + logger.error(ex.toString, ex) + throw ex + } } } } def onPostReceive(receivePack: ReceivePack, commands: java.util.Collection[ReceiveCommand]): Unit = { - try { - using(Git.open(Directory.getRepositoryDir(owner, repository))) { git => - val pushedIds = scala.collection.mutable.Set[String]() - commands.asScala.foreach { command => - logger.debug(s"commandType: ${command.getType}, refName: ${command.getRefName}") - implicit val apiContext = api.JsonFormat.Context(baseUrl) - val refName = command.getRefName.split("/") - val branchName = refName.drop(2).mkString("/") - val commits = if (refName(1) == "tags") { - Nil - } else { - command.getType match { - case ReceiveCommand.Type.DELETE => Nil - case _ => JGitUtil.getCommitLog(git, command.getOldId.name, command.getNewId.name) - } - } + Database() withTransaction { implicit session => + try { + using(Git.open(Directory.getRepositoryDir(owner, repository))) { git => + JGitUtil.removeCache(git) - // Retrieve all issue count in the repository - val issueCount = - countIssue(IssueSearchCondition(state = "open"), false, owner -> repository) + - countIssue(IssueSearchCondition(state = "closed"), false, owner -> repository) - - val repositoryInfo = getRepository(owner, repository, baseUrl).get - - // Extract new commit and apply issue comment - val defaultBranch = repositoryInfo.repository.defaultBranch - val newCommits = commits.flatMap { commit => - if (!existIds.contains(commit.id) && !pushedIds.contains(commit.id)) { - if (issueCount > 0) { - pushedIds.add(commit.id) - createIssueComment(commit) - // close issues - if(refName(1) == "heads" && branchName == defaultBranch && command.getType == ReceiveCommand.Type.UPDATE){ - closeIssuesFromMessage(commit.fullMessage, pusher, owner, repository) - } + val pushedIds = scala.collection.mutable.Set[String]() + commands.asScala.foreach { command => + logger.debug(s"commandType: ${command.getType}, refName: ${command.getRefName}") + implicit val apiContext = api.JsonFormat.Context(baseUrl) + val refName = command.getRefName.split("/") + val branchName = refName.drop(2).mkString("/") + val commits = if (refName(1) == "tags") { + Nil + } else { + command.getType match { + case ReceiveCommand.Type.DELETE => Nil + case _ => JGitUtil.getCommitLog(git, command.getOldId.name, command.getNewId.name) } - Some(commit) - } else None - } - - // record activity - if(refName(1) == "heads"){ - command.getType match { - case ReceiveCommand.Type.CREATE => recordCreateBranchActivity(owner, repository, pusher, branchName) - case ReceiveCommand.Type.UPDATE => recordPushActivity(owner, repository, pusher, branchName, newCommits) - case ReceiveCommand.Type.DELETE => recordDeleteBranchActivity(owner, repository, pusher, branchName) - case _ => } - } else if(refName(1) == "tags"){ - command.getType match { - case ReceiveCommand.Type.CREATE => recordCreateTagActivity(owner, repository, pusher, branchName, newCommits) - case ReceiveCommand.Type.DELETE => recordDeleteTagActivity(owner, repository, pusher, branchName, newCommits) - case _ => - } - } - if(refName(1) == "heads"){ - command.getType match { - case ReceiveCommand.Type.CREATE | - ReceiveCommand.Type.UPDATE | - ReceiveCommand.Type.UPDATE_NONFASTFORWARD => - updatePullRequests(owner, repository, branchName) - getAccountByUserName(pusher).map{ pusherAccount => - callPullRequestWebHookByRequestBranch("synchronize", repositoryInfo, branchName, baseUrl, pusherAccount) + // Retrieve all issue count in the repository + val issueCount = + countIssue(IssueSearchCondition(state = "open"), false, owner -> repository) + + countIssue(IssueSearchCondition(state = "closed"), false, owner -> repository) + + val repositoryInfo = getRepository(owner, repository).get + + // Extract new commit and apply issue comment + val defaultBranch = repositoryInfo.repository.defaultBranch + val newCommits = commits.flatMap { commit => + if (!existIds.contains(commit.id) && !pushedIds.contains(commit.id)) { + if (issueCount > 0) { + pushedIds.add(commit.id) + createIssueComment(owner, repository, commit) + // close issues + if (refName(1) == "heads" && branchName == defaultBranch && command.getType == ReceiveCommand.Type.UPDATE) { + closeIssuesFromMessage(commit.fullMessage, pusher, owner, repository) + } } - case _ => + Some(commit) + } else None } - } - // call web hook - callWebHookOf(owner, repository, "push"){ - for(pusherAccount <- getAccountByUserName(pusher); - ownerAccount <- getAccountByUserName(owner)) yield { - WebHookPushPayload(git, pusherAccount, command.getRefName, repositoryInfo, newCommits, ownerAccount) + // record activity + if (refName(1) == "heads") { + command.getType match { + case ReceiveCommand.Type.CREATE => recordCreateBranchActivity(owner, repository, pusher, branchName) + case ReceiveCommand.Type.UPDATE => recordPushActivity(owner, repository, pusher, branchName, newCommits) + case ReceiveCommand.Type.DELETE => recordDeleteBranchActivity(owner, repository, pusher, branchName) + case _ => + } + } else if (refName(1) == "tags") { + command.getType match { + case ReceiveCommand.Type.CREATE => recordCreateTagActivity(owner, repository, pusher, branchName, newCommits) + case ReceiveCommand.Type.DELETE => recordDeleteTagActivity(owner, repository, pusher, branchName, newCommits) + case _ => + } } + + if (refName(1) == "heads") { + command.getType match { + case ReceiveCommand.Type.CREATE | + ReceiveCommand.Type.UPDATE | + ReceiveCommand.Type.UPDATE_NONFASTFORWARD => + updatePullRequests(owner, repository, branchName) + getAccountByUserName(pusher).map { pusherAccount => + callPullRequestWebHookByRequestBranch("synchronize", repositoryInfo, branchName, baseUrl, pusherAccount) + } + case _ => + } + } + + // call web hook + callWebHookOf(owner, repository, WebHook.Push) { + for (pusherAccount <- getAccountByUserName(pusher); + ownerAccount <- getAccountByUserName(owner)) yield { + WebHookPushPayload(git, pusherAccount, command.getRefName, repositoryInfo, newCommits, ownerAccount, + newId = command.getNewId(), oldId = command.getOldId()) + } + } + + // call post-commit hook + PluginRegistry().getReceiveHooks.foreach(_.postReceive(owner, repository, receivePack, command, pusher)) } } - } - // update repository last modified time. - updateLastActivityDate(owner, repository) - } catch { - case ex: Exception => { - logger.error(ex.toString, ex) - throw ex - } - } - } - - private def createIssueComment(commit: CommitInfo) = { - StringUtil.extractIssueId(commit.fullMessage).foreach { issueId => - if(getIssue(owner, repository, issueId).isDefined){ - getAccountByMailAddress(commit.committerEmailAddress).foreach { account => - createComment(owner, repository, account.userName, issueId.toInt, commit.fullMessage + " " + commit.id, "commit") + // update repository last modified time. + updateLastActivityDate(owner, repository) + } catch { + case ex: Exception => { + logger.error(ex.toString, ex) + throw ex } } } } + +} + +object GitLfs { + + case class BatchRequest( + operation: String, + transfers: Seq[String], + objects: Seq[BatchRequestObject] + ) + + case class BatchRequestObject( + oid: String, + size: Long + ) + + case class BatchUploadResponse( + transfer: String, + objects: Seq[BatchResponseObject] + ) + + case class BatchResponseObject( + oid: String, + size: Long, + authenticated: Boolean, + actions: Actions + ) + + case class Actions( + download: Option[Action] = None, + upload: Option[Action] = None + ) + + case class Action( + href: String, + header: Map[String, String] = Map.empty, + expires_at: Date + ) + + case class Error( + message: String + ) + } diff --git a/src/main/scala/gitbucket/core/servlet/InitializeListener.scala b/src/main/scala/gitbucket/core/servlet/InitializeListener.scala index a375b37..8d55a81 100644 --- a/src/main/scala/gitbucket/core/servlet/InitializeListener.scala +++ b/src/main/scala/gitbucket/core/servlet/InitializeListener.scala @@ -1,16 +1,23 @@ package gitbucket.core.servlet +import java.io.File + import akka.event.Logging import com.typesafe.config.ConfigFactory +import gitbucket.core.GitBucketCoreModule import gitbucket.core.plugin.PluginRegistry import gitbucket.core.service.{ActivityService, SystemSettingsService} -import org.apache.commons.io.FileUtils +import gitbucket.core.util.DatabaseConfig +import gitbucket.core.util.Directory._ +import gitbucket.core.util.JDBCUtil._ +import io.github.gitbucket.solidbase.Solidbase +import io.github.gitbucket.solidbase.manager.JDBCVersionManager import javax.servlet.{ServletContextListener, ServletContextEvent} +import org.apache.commons.io.FileUtils import org.slf4j.LoggerFactory -import gitbucket.core.util.Versions import akka.actor.{Actor, Props, ActorSystem} import com.typesafe.akka.extension.quartz.QuartzSchedulerExtension -import AutoUpdate._ +import scala.collection.JavaConverters._ /** * Initialize GitBucket system. @@ -29,15 +36,63 @@ Database() withTransaction { session => val conn = session.conn + val manager = new JDBCVersionManager(conn) - // Migration - logger.debug("Start schema update") - Versions.update(conn, headVersion, getCurrentVersion(), versions, Thread.currentThread.getContextClassLoader){ conn => - FileUtils.writeStringToFile(versionFile, headVersion.versionString, "UTF-8") + // Check version + val versionFile = new File(GitBucketHome, "version") + + if(versionFile.exists()){ + val version = FileUtils.readFileToString(versionFile, "UTF-8") + if(version == "3.14"){ + // Initialization for GitBucket 3.14 + logger.info("Migration to GitBucket 4.x start") + + // Backup current data + val dataMvFile = new File(GitBucketHome, "data.mv.db") + if(dataMvFile.exists) { + FileUtils.copyFile(dataMvFile, new File(GitBucketHome, "data.mv.db_3.14")) + } + val dataTraceFile = new File(GitBucketHome, "data.trace.db") + if(dataTraceFile.exists) { + FileUtils.copyFile(dataTraceFile, new File(GitBucketHome, "data.trace.db_3.14")) + } + + // Change form + manager.initialize() + manager.updateVersion(GitBucketCoreModule.getModuleId, "4.0.0") + conn.select("SELECT PLUGIN_ID, VERSION FROM PLUGIN"){ rs => + manager.updateVersion(rs.getString("PLUGIN_ID"), rs.getString("VERSION")) + } + conn.update("DROP TABLE PLUGIN") + versionFile.delete() + + logger.info("Migration to GitBucket 4.x completed") + + } else { + throw new Exception("GitBucket can't migrate from this version. Please update to 3.14 at first.") + } + } + + // Run normal migration + logger.info("Start schema update") + val solidbase = new Solidbase() + solidbase.migrate(conn, Thread.currentThread.getContextClassLoader, DatabaseConfig.liquiDriver, GitBucketCoreModule) + + // Rescue code for users who updated from 3.14 to 4.0.0 + // https://github.com/gitbucket/gitbucket/issues/1227 + val currentVersion = manager.getCurrentVersion(GitBucketCoreModule.getModuleId) + val databaseVersion = if(currentVersion == "4.0"){ + manager.updateVersion(GitBucketCoreModule.getModuleId, "4.0.0") + "4.0.0" + } else currentVersion + + val gitbucketVersion = GitBucketCoreModule.getVersions.asScala.last.getVersion + if(databaseVersion != gitbucketVersion){ + throw new IllegalStateException(s"Initialization failed. GitBucket version is ${gitbucketVersion}, but database version is ${databaseVersion}.") } // Load plugins - logger.debug("Initialize plugins") + logger.info("Initialize plugins") PluginRegistry.initialize(event.getServletContext, loadSystemSettings(), conn) } diff --git a/src/main/scala/gitbucket/core/servlet/PluginAssetsServlet.scala b/src/main/scala/gitbucket/core/servlet/PluginAssetsServlet.scala new file mode 100644 index 0000000..0d43af3 --- /dev/null +++ b/src/main/scala/gitbucket/core/servlet/PluginAssetsServlet.scala @@ -0,0 +1,39 @@ +package gitbucket.core.servlet + +import javax.servlet.http.{HttpServlet, HttpServletRequest, HttpServletResponse} + +import gitbucket.core.plugin.PluginRegistry +import gitbucket.core.util.FileUtil +import org.apache.commons.io.IOUtils + +/** + * Supply assets which are provided by plugins. + */ +class PluginAssetsServlet extends HttpServlet { + + override def doGet(req: HttpServletRequest, resp: HttpServletResponse): Unit = { + val assetsMappings = PluginRegistry().getAssetsMappings + val path = req.getRequestURI.substring(req.getContextPath.length) + + assetsMappings + .find { case (prefix, _, _) => path.startsWith("/plugin-assets" + prefix) } + .flatMap { case (prefix, resourcePath, classLoader) => + val resourceName = path.substring(("/plugin-assets" + prefix).length) + Option(classLoader.getResourceAsStream(resourcePath.replaceFirst("^/", "") + resourceName)) + } + .map { in => + try { + val bytes = IOUtils.toByteArray(in) + resp.setContentLength(bytes.length) + resp.setContentType(FileUtil.getContentType(path, bytes)) + resp.getOutputStream.write(bytes) + } finally { + in.close() + } + } + .getOrElse { + resp.setStatus(404) + } + } + +} diff --git a/src/main/scala/gitbucket/core/servlet/TransactionFilter.scala b/src/main/scala/gitbucket/core/servlet/TransactionFilter.scala index fc2e457..f4dc1b9 100644 --- a/src/main/scala/gitbucket/core/servlet/TransactionFilter.scala +++ b/src/main/scala/gitbucket/core/servlet/TransactionFilter.scala @@ -2,7 +2,7 @@ import javax.servlet._ import javax.servlet.http.HttpServletRequest -import com.mchange.v2.c3p0.ComboPooledDataSource +import com.zaxxer.hikari._ import gitbucket.core.util.DatabaseConfig import org.scalatra.ScalatraBase import org.slf4j.LoggerFactory @@ -21,8 +21,9 @@ def destroy(): Unit = {} def doFilter(req: ServletRequest, res: ServletResponse, chain: FilterChain): Unit = { - if(req.asInstanceOf[HttpServletRequest].getServletPath().startsWith("/assets/")){ - // assets don't need transaction + val servletPath = req.asInstanceOf[HttpServletRequest].getServletPath() + if(servletPath.startsWith("/assets/") || servletPath == "/git" || servletPath == "/git-lfs"){ + // assets and git-lfs don't need transaction chain.doFilter(req, res) } else { Database() withTransaction { session => @@ -46,14 +47,21 @@ private val logger = LoggerFactory.getLogger(Database.getClass) - private val dataSource: ComboPooledDataSource = { - val ds = new ComboPooledDataSource - ds.setDriverClass(DatabaseConfig.driver) - ds.setJdbcUrl(DatabaseConfig.url) - ds.setUser(DatabaseConfig.user) - ds.setPassword(DatabaseConfig.password) + private val dataSource: HikariDataSource = { + val config = new HikariConfig() + config.setDriverClassName(DatabaseConfig.jdbcDriver) + config.setJdbcUrl(DatabaseConfig.url) + config.setUsername(DatabaseConfig.user) + config.setPassword(DatabaseConfig.password) + config.setAutoCommit(false) + DatabaseConfig.connectionTimeout.foreach(config.setConnectionTimeout) + DatabaseConfig.idleTimeout.foreach(config.setIdleTimeout) + DatabaseConfig.maxLifetime.foreach(config.setMaxLifetime) + DatabaseConfig.minimumIdle.foreach(config.setMinimumIdle) + DatabaseConfig.maximumPoolSize.foreach(config.setMaximumPoolSize) + logger.debug("load database connection pool") - ds + new HikariDataSource(config) } private val db: SlickDatabase = { diff --git a/src/main/scala/gitbucket/core/ssh/GitCommand.scala b/src/main/scala/gitbucket/core/ssh/GitCommand.scala index c78ff82..d5d8135 100644 --- a/src/main/scala/gitbucket/core/ssh/GitCommand.scala +++ b/src/main/scala/gitbucket/core/ssh/GitCommand.scala @@ -1,55 +1,62 @@ package gitbucket.core.ssh import gitbucket.core.model.Session +import gitbucket.core.plugin.{GitRepositoryRouting, PluginRegistry} import gitbucket.core.service.{RepositoryService, AccountService, SystemSettingsService} import gitbucket.core.servlet.{Database, CommitLogHook} import gitbucket.core.util.{Directory, ControlUtil} -import org.apache.sshd.server.{CommandFactory, Environment, ExitCallback, Command} +import org.apache.sshd.server.{CommandFactory, Environment, ExitCallback, Command, SessionAware} +import org.apache.sshd.server.session.ServerSession import org.slf4j.LoggerFactory -import java.io.{InputStream, OutputStream} +import java.io.{File, InputStream, OutputStream} import ControlUtil._ import org.eclipse.jgit.api.Git import Directory._ import org.eclipse.jgit.transport.{ReceivePack, UploadPack} -import org.apache.sshd.server.command.UnknownCommand +import org.apache.sshd.server.scp.UnknownCommand import org.eclipse.jgit.errors.RepositoryNotFoundException object GitCommand { - val CommandRegex = """\Agit-(upload|receive)-pack '/([a-zA-Z0-9\-_.]+)/([a-zA-Z0-9\-_.]+).git'\Z""".r + val DefaultCommandRegex = """\Agit-(upload|receive)-pack '/([a-zA-Z0-9\-_.]+)/([a-zA-Z0-9\-_.]+).git'\Z""".r + val SimpleCommandRegex = """\Agit-(upload|receive)-pack '/(.+\.git)'\Z""".r } -abstract class GitCommand(val owner: String, val repoName: String) extends Command { - self: RepositoryService with AccountService => +abstract class GitCommand extends Command with SessionAware { private val logger = LoggerFactory.getLogger(classOf[GitCommand]) - protected var err: OutputStream = null - protected var in: InputStream = null - protected var out: OutputStream = null - protected var callback: ExitCallback = null + @volatile protected var err: OutputStream = null + @volatile protected var in: InputStream = null + @volatile protected var out: OutputStream = null + @volatile protected var callback: ExitCallback = null + @volatile private var authUser:Option[String] = None - protected def runTask(user: String)(implicit session: Session): Unit + protected def runTask(authUser: String): Unit - private def newTask(user: String): Runnable = new Runnable { + private def newTask(): Runnable = new Runnable { override def run(): Unit = { - Database() withSession { implicit session => - try { - runTask(user) - callback.onExit(0) - } catch { - case e: RepositoryNotFoundException => - logger.info(e.getMessage) - callback.onExit(1, "Repository Not Found") - case e: Throwable => - logger.error(e.getMessage, e) - callback.onExit(1) - } + authUser match { + case Some(authUser) => + try { + runTask(authUser) + callback.onExit(0) + } catch { + case e: RepositoryNotFoundException => + logger.info(e.getMessage) + callback.onExit(1, "Repository Not Found") + case e: Throwable => + logger.error(e.getMessage, e) + callback.onExit(1) + } + case None => + val message = "User not authenticated" + logger.error(message) + callback.onExit(1, message) } } } - override def start(env: Environment): Unit = { - val user = env.getEnv.get("USER") - val thread = new Thread(newTask(user)) + final override def start(env: Environment): Unit = { + val thread = new Thread(newTask()) thread.start() } @@ -71,63 +78,125 @@ this.in = in } + override def setSession(serverSession:ServerSession) { + this.authUser = PublicKeyAuthenticator.getUserName(serverSession) + } + +} + +abstract class DefaultGitCommand(val owner: String, val repoName: String) extends GitCommand { + self: RepositoryService with AccountService => + protected def isWritableUser(username: String, repositoryInfo: RepositoryService.RepositoryInfo) (implicit session: Session): Boolean = getAccountByUserName(username) match { - case Some(account) => hasWritePermission(repositoryInfo.owner, repositoryInfo.name, Some(account)) + case Some(account) => hasDeveloperRole(repositoryInfo.owner, repositoryInfo.name, Some(account)) case None => false } } -class GitUploadPack(owner: String, repoName: String, baseUrl: String) extends GitCommand(owner, repoName) + +class DefaultGitUploadPack(owner: String, repoName: String) extends DefaultGitCommand(owner, repoName) with RepositoryService with AccountService { - override protected def runTask(user: String)(implicit session: Session): Unit = { - getRepository(owner, repoName.replaceFirst("\\.wiki\\Z", ""), baseUrl).foreach { repositoryInfo => - if(!repositoryInfo.repository.isPrivate || isWritableUser(user, repositoryInfo)){ - using(Git.open(getRepositoryDir(owner, repoName))) { git => - val repository = git.getRepository - val upload = new UploadPack(repository) - upload.upload(in, out, err) - } + override protected def runTask(user: String): Unit = { + val execute = Database() withSession { implicit session => + getRepository(owner, repoName.replaceFirst("\\.wiki\\Z", "")).map { repositoryInfo => + !repositoryInfo.repository.isPrivate || isWritableUser(user, repositoryInfo) + }.getOrElse(false) + } + + if(execute){ + using(Git.open(getRepositoryDir(owner, repoName))) { git => + val repository = git.getRepository + val upload = new UploadPack(repository) + upload.upload(in, out, err) } } } - } -class GitReceivePack(owner: String, repoName: String, baseUrl: String) extends GitCommand(owner, repoName) - with SystemSettingsService with RepositoryService with AccountService { +class DefaultGitReceivePack(owner: String, repoName: String, baseUrl: String) extends DefaultGitCommand(owner, repoName) + with RepositoryService with AccountService { - override protected def runTask(user: String)(implicit session: Session): Unit = { - getRepository(owner, repoName.replaceFirst("\\.wiki\\Z", ""), baseUrl).foreach { repositoryInfo => - if(isWritableUser(user, repositoryInfo)){ - using(Git.open(getRepositoryDir(owner, repoName))) { git => - val repository = git.getRepository - val receive = new ReceivePack(repository) - if(!repoName.endsWith(".wiki")){ - val hook = new CommitLogHook(owner, repoName, user, baseUrl) - receive.setPreReceiveHook(hook) - receive.setPostReceiveHook(hook) - } - receive.receive(in, out, err) + override protected def runTask(user: String): Unit = { + val execute = Database() withSession { implicit session => + getRepository(owner, repoName.replaceFirst("\\.wiki\\Z", "")).map { repositoryInfo => + isWritableUser(user, repositoryInfo) + }.getOrElse(false) + } + + if(execute) { + using(Git.open(getRepositoryDir(owner, repoName))) { git => + val repository = git.getRepository + val receive = new ReceivePack(repository) + if (!repoName.endsWith(".wiki")) { + val hook = new CommitLogHook(owner, repoName, user, baseUrl) + receive.setPreReceiveHook(hook) + receive.setPostReceiveHook(hook) } + receive.receive(in, out, err) } } } - } +class PluginGitUploadPack(repoName: String, routing: GitRepositoryRouting) extends GitCommand + with SystemSettingsService { + + override protected def runTask(user: String): Unit = { + val execute = Database() withSession { implicit session => + routing.filter.filter("/" + repoName, Some(user), loadSystemSettings(), false) + } + + if(execute){ + val path = routing.urlPattern.r.replaceFirstIn(repoName, routing.localPath) + using(Git.open(new File(Directory.GitBucketHome, path))){ git => + val repository = git.getRepository + val upload = new UploadPack(repository) + upload.upload(in, out, err) + } + } + } +} + +class PluginGitReceivePack(repoName: String, routing: GitRepositoryRouting) extends GitCommand + with SystemSettingsService { + + override protected def runTask(user: String): Unit = { + val execute = Database() withSession { implicit session => + routing.filter.filter("/" + repoName, Some(user), loadSystemSettings(), true) + } + if(execute){ + val path = routing.urlPattern.r.replaceFirstIn(repoName, routing.localPath) + using(Git.open(new File(Directory.GitBucketHome, path))){ git => + val repository = git.getRepository + val receive = new ReceivePack(repository) + receive.receive(in, out, err) + } + } + } +} + + class GitCommandFactory(baseUrl: String) extends CommandFactory { private val logger = LoggerFactory.getLogger(classOf[GitCommandFactory]) override def createCommand(command: String): Command = { + import GitCommand._ logger.debug(s"command: $command") + command match { - case GitCommand.CommandRegex("upload", owner, repoName) => new GitUploadPack(owner, repoName, baseUrl) - case GitCommand.CommandRegex("receive", owner, repoName) => new GitReceivePack(owner, repoName, baseUrl) + case SimpleCommandRegex ("upload" , repoName) if(pluginRepository(repoName)) => new PluginGitUploadPack (repoName, routing(repoName)) + case SimpleCommandRegex ("receive", repoName) if(pluginRepository(repoName)) => new PluginGitReceivePack(repoName, routing(repoName)) + case DefaultCommandRegex("upload" , owner, repoName) => new DefaultGitUploadPack (owner, repoName) + case DefaultCommandRegex("receive", owner, repoName) => new DefaultGitReceivePack(owner, repoName, baseUrl) case _ => new UnknownCommand(command) } } + + private def pluginRepository(repoName: String): Boolean = PluginRegistry().getRepositoryRouting("/" + repoName).isDefined + private def routing(repoName: String): GitRepositoryRouting = PluginRegistry().getRepositoryRouting("/" + repoName).get + } diff --git a/src/main/scala/gitbucket/core/ssh/NoShell.scala b/src/main/scala/gitbucket/core/ssh/NoShell.scala index bd30ccf..065bee0 100644 --- a/src/main/scala/gitbucket/core/ssh/NoShell.scala +++ b/src/main/scala/gitbucket/core/ssh/NoShell.scala @@ -1,12 +1,12 @@ package gitbucket.core.ssh -import gitbucket.core.service.SystemSettingsService +import gitbucket.core.service.SystemSettingsService.SshAddress import org.apache.sshd.common.Factory import org.apache.sshd.server.{Environment, ExitCallback, Command} import java.io.{OutputStream, InputStream} import org.eclipse.jgit.lib.Constants -class NoShell extends Factory[Command] with SystemSettingsService { +class NoShell(sshAddress:SshAddress) extends Factory[Command] { override def create(): Command = new Command() { private var in: InputStream = null private var out: OutputStream = null @@ -14,8 +14,6 @@ private var callback: ExitCallback = null override def start(env: Environment): Unit = { - val user = env.getEnv.get("USER") - val port = loadSystemSettings().sshPort.getOrElse(SystemSettingsService.DefaultSshPort) val message = """ | Welcome to @@ -31,8 +29,8 @@ | | Please use: | - | git clone ssh://%s@GITBUCKET_HOST:%d/OWNER/REPOSITORY_NAME.git - """.stripMargin.format(user, port).replace("\n", "\r\n") + "\r\n" + | git clone ssh://%s@%s:%d/OWNER/REPOSITORY_NAME.git + """.stripMargin.format(sshAddress.genericUser, sshAddress.host, sshAddress.port).replace("\n", "\r\n") + "\r\n" err.write(Constants.encode(message)) err.flush() in.close() diff --git a/src/main/scala/gitbucket/core/ssh/PublicKeyAuthenticator.scala b/src/main/scala/gitbucket/core/ssh/PublicKeyAuthenticator.scala index daf5c30..cc7a905 100644 --- a/src/main/scala/gitbucket/core/ssh/PublicKeyAuthenticator.scala +++ b/src/main/scala/gitbucket/core/ssh/PublicKeyAuthenticator.scala @@ -1,22 +1,73 @@ package gitbucket.core.ssh -import gitbucket.core.service.SshKeyService -import gitbucket.core.servlet.Database -import org.apache.sshd.server.PublickeyAuthenticator -import org.apache.sshd.server.session.ServerSession import java.security.PublicKey -class PublicKeyAuthenticator extends PublickeyAuthenticator with SshKeyService { +import gitbucket.core.service.SshKeyService +import gitbucket.core.servlet.Database +import org.apache.sshd.server.auth.pubkey.PublickeyAuthenticator +import org.apache.sshd.server.session.ServerSession +import org.apache.sshd.common.AttributeStore +import org.slf4j.LoggerFactory - override def authenticate(username: String, key: PublicKey, session: ServerSession): Boolean = { - Database() withSession { implicit session => - getPublicKeys(username).exists { sshKey => - SshUtil.str2PublicKey(sshKey.publicKey) match { - case Some(publicKey) => key.equals(publicKey) - case _ => false - } - } +object PublicKeyAuthenticator { + // put in the ServerSession here to be read by GitCommand later + private val userNameSessionKey = new AttributeStore.AttributeKey[String] + + def putUserName(serverSession:ServerSession, userName:String):Unit = + serverSession.setAttribute(userNameSessionKey, userName) + + def getUserName(serverSession:ServerSession):Option[String] = + Option(serverSession.getAttribute(userNameSessionKey)) +} + +class PublicKeyAuthenticator(genericUser:String) extends PublickeyAuthenticator with SshKeyService { + private val logger = LoggerFactory.getLogger(classOf[PublicKeyAuthenticator]) + + override def authenticate(username: String, key: PublicKey, session: ServerSession): Boolean = + if (username == genericUser) authenticateGenericUser(username, key, session, genericUser) + else authenticateLoginUser(username, key, session) + + private def authenticateLoginUser(username: String, key: PublicKey, session: ServerSession): Boolean = { + val authenticated = + Database() + .withSession { implicit dbSession => getPublicKeys(username) } + .map(_.publicKey) + .flatMap(SshUtil.str2PublicKey) + .contains(key) + if (authenticated) { + logger.info(s"authentication as ssh user ${username} succeeded") + PublicKeyAuthenticator.putUserName(session, username) } + else { + logger.info(s"authentication as ssh user ${username} failed") + } + authenticated } + private def authenticateGenericUser(username: String, key: PublicKey, session: ServerSession, genericUser:String): Boolean = { + // find all users having the key we got from ssh + val possibleUserNames = + Database() + .withSession { implicit dbSession => getAllKeys() } + .filter { sshKey => + SshUtil.str2PublicKey(sshKey.publicKey).exists(_ == key) + } + .map(_.userName) + .distinct + // determine the user - if different accounts share the same key, tough luck + val uniqueUserName = + possibleUserNames match { + case List() => + logger.info(s"authentication as generic user ${genericUser} failed, public key not found") + None + case List(name) => + logger.info(s"authentication as generic user ${genericUser} succeeded, identified ${name}") + Some(name) + case _ => + logger.info(s"authentication as generic user ${genericUser} failed, public key is ambiguous") + None + } + uniqueUserName.foreach(PublicKeyAuthenticator.putUserName(session, _)) + uniqueUserName.isDefined + } } diff --git a/src/main/scala/gitbucket/core/ssh/SshServerListener.scala b/src/main/scala/gitbucket/core/ssh/SshServerListener.scala index 1288eb1..6d3c123 100644 --- a/src/main/scala/gitbucket/core/ssh/SshServerListener.scala +++ b/src/main/scala/gitbucket/core/ssh/SshServerListener.scala @@ -1,28 +1,34 @@ package gitbucket.core.ssh +import java.io.File +import java.util.concurrent.atomic.AtomicBoolean import javax.servlet.{ServletContextEvent, ServletContextListener} + import gitbucket.core.service.SystemSettingsService -import gitbucket.core.util.Directory +import gitbucket.core.service.SystemSettingsService.SshAddress +import gitbucket.core.util.{Directory} import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider import org.slf4j.LoggerFactory -import java.util.concurrent.atomic.AtomicBoolean object SshServer { private val logger = LoggerFactory.getLogger(SshServer.getClass) - private val server = org.apache.sshd.SshServer.setUpDefaultServer() + private val server = org.apache.sshd.server.SshServer.setUpDefaultServer() private val active = new AtomicBoolean(false) - private def configure(port: Int, baseUrl: String) = { - server.setPort(port) - server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(s"${Directory.GitBucketHome}/gitbucket.ser")) - server.setPublickeyAuthenticator(new PublicKeyAuthenticator) + private def configure(sshAddress: SshAddress, baseUrl: String) = { + server.setPort(sshAddress.port) + val provider = new SimpleGeneratorHostKeyProvider(new File(s"${Directory.GitBucketHome}/gitbucket.ser")) + provider.setAlgorithm("RSA") + provider.setOverwriteAllowed(false) + server.setKeyPairProvider(provider) + server.setPublickeyAuthenticator(new PublicKeyAuthenticator(sshAddress.genericUser)) server.setCommandFactory(new GitCommandFactory(baseUrl)) - server.setShellFactory(new NoShell) + server.setShellFactory(new NoShell(sshAddress)) } - def start(port: Int, baseUrl: String) = { + def start(sshAddress: SshAddress, baseUrl: String) = { if(active.compareAndSet(false, true)){ - configure(port, baseUrl) + configure(sshAddress, baseUrl) server.start() logger.info(s"Start SSH Server Listen on ${server.getPort}") } @@ -50,20 +56,18 @@ override def contextInitialized(sce: ServletContextEvent): Unit = { val settings = loadSystemSettings() - if(settings.ssh){ - settings.baseUrl match { - case None => - logger.error("Could not start SshServer because the baseUrl is not configured.") - case Some(baseUrl) => - SshServer.start(settings.sshPort.getOrElse(SystemSettingsService.DefaultSshPort), baseUrl) - } + if (settings.sshAddress.isDefined && settings.baseUrl.isEmpty) { + logger.error("Could not start SshServer because the baseUrl is not configured.") } + for { + sshAddress <- settings.sshAddress + baseUrl <- settings.baseUrl + } + SshServer.start(sshAddress, baseUrl) } override def contextDestroyed(sce: ServletContextEvent): Unit = { - if(loadSystemSettings().ssh){ - SshServer.stop() - } + SshServer.stop() } } diff --git a/src/main/scala/gitbucket/core/ssh/SshUtil.scala b/src/main/scala/gitbucket/core/ssh/SshUtil.scala index 512332c..42167ed 100644 --- a/src/main/scala/gitbucket/core/ssh/SshUtil.scala +++ b/src/main/scala/gitbucket/core/ssh/SshUtil.scala @@ -1,10 +1,13 @@ package gitbucket.core.ssh import java.security.PublicKey -import org.slf4j.LoggerFactory + import org.apache.commons.codec.binary.Base64 +import org.apache.sshd.common.config.keys.KeyUtils +import org.apache.sshd.common.util.buffer.ByteArrayBuffer import org.eclipse.jgit.lib.Constants -import org.apache.sshd.common.util.{KeyUtils, Buffer} +import org.slf4j.LoggerFactory + object SshUtil { @@ -20,7 +23,7 @@ try { val encodedKey = parts(1) val decode = Base64.decodeBase64(Constants.encodeASCII(encodedKey)) - Some(new Buffer(decode).getRawPublicKey) + Some(new ByteArrayBuffer(decode).getRawPublicKey) } catch { case e: Throwable => logger.debug(e.getMessage, e) @@ -28,9 +31,7 @@ } } - def fingerPrint(key: String): Option[String] = str2PublicKey(key) match { - case Some(publicKey) => Some(KeyUtils.getFingerPrint(publicKey)) - case None => None - } + def fingerPrint(key: String): Option[String] = + str2PublicKey(key) map KeyUtils.getFingerPrint } diff --git a/src/main/scala/gitbucket/core/util/AuthUtil.scala b/src/main/scala/gitbucket/core/util/AuthUtil.scala new file mode 100644 index 0000000..7a83c13 --- /dev/null +++ b/src/main/scala/gitbucket/core/util/AuthUtil.scala @@ -0,0 +1,21 @@ +package gitbucket.core.util + +import javax.servlet.http.HttpServletResponse + +/** + * Provides HTTP (Basic) Authentication related functions. + */ +object AuthUtil { + def requireAuth(response: HttpServletResponse): Unit = { + response.setHeader("WWW-Authenticate", "BASIC realm=\"GitBucket\"") + response.sendError(HttpServletResponse.SC_UNAUTHORIZED) + } + + def decodeAuthHeader(header: String): String = { + try { + new String(new sun.misc.BASE64Decoder().decodeBuffer(header.substring(6))) + } catch { + case _: Throwable => "" + } + } +} \ No newline at end of file diff --git a/src/main/scala/gitbucket/core/util/Authenticator.scala b/src/main/scala/gitbucket/core/util/Authenticator.scala index 49de665..bbcb339 100644 --- a/src/main/scala/gitbucket/core/util/Authenticator.scala +++ b/src/main/scala/gitbucket/core/util/Authenticator.scala @@ -1,7 +1,8 @@ package gitbucket.core.util import gitbucket.core.controller.ControllerBase -import gitbucket.core.service.{RepositoryService, AccountService} +import gitbucket.core.service.{AccountService, RepositoryService} +import gitbucket.core.model.Role import RepositoryService.RepositoryInfo import Implicits._ import ControlUtil._ @@ -36,13 +37,13 @@ private def authenticate(action: (RepositoryInfo) => Any) = { { defining(request.paths){ paths => - getRepository(paths(0), paths(1), baseUrl).map { repository => + getRepository(paths(0), paths(1)).map { repository => context.loginAccount match { case Some(x) if(x.isAdmin) => action(repository) case Some(x) if(repository.owner == x.userName) => action(repository) - case Some(x) if(getGroupMembers(repository.owner).exists { member => - member.userName == x.userName && member.isManager == true - }) => action(repository) + // TODO Repository management is allowed for only group managers? + case Some(x) if(getGroupMembers(repository.owner).exists { m => m.userName == x.userName && m.isManager == true }) => action(repository) + case Some(x) if(getCollaboratorUserNames(paths(0), paths(1), Seq(Role.ADMIN)).contains(x.userName)) => action(repository) case _ => Unauthorized() } } getOrElse NotFound() @@ -86,46 +87,24 @@ } /** - * Allows only collaborators and administrators. + * Allows only guests and signed in users who can access the repository. */ -trait CollaboratorsAuthenticator { self: ControllerBase with RepositoryService => - protected def collaboratorsOnly(action: (RepositoryInfo) => Any) = { authenticate(action) } - protected def collaboratorsOnly[T](action: (T, RepositoryInfo) => Any) = (form: T) => { authenticate(action(form, _)) } - - private def authenticate(action: (RepositoryInfo) => Any) = { - { - defining(request.paths){ paths => - getRepository(paths(0), paths(1), baseUrl).map { repository => - context.loginAccount match { - case Some(x) if(x.isAdmin) => action(repository) - case Some(x) if(paths(0) == x.userName) => action(repository) - case Some(x) if(getCollaborators(paths(0), paths(1)).contains(x.userName)) => action(repository) - case _ => Unauthorized() - } - } getOrElse NotFound() - } - } - } -} - -/** - * Allows only the repository owner (or manager for group repository) and administrators. - */ -trait ReferrerAuthenticator { self: ControllerBase with RepositoryService => +trait ReferrerAuthenticator { self: ControllerBase with RepositoryService with AccountService => protected def referrersOnly(action: (RepositoryInfo) => Any) = { authenticate(action) } protected def referrersOnly[T](action: (T, RepositoryInfo) => Any) = (form: T) => { authenticate(action(form, _)) } private def authenticate(action: (RepositoryInfo) => Any) = { { defining(request.paths){ paths => - getRepository(paths(0), paths(1), baseUrl).map { repository => + getRepository(paths(0), paths(1)).map { repository => if(!repository.repository.isPrivate){ action(repository) } else { context.loginAccount match { case Some(x) if(x.isAdmin) => action(repository) case Some(x) if(paths(0) == x.userName) => action(repository) - case Some(x) if(getCollaborators(paths(0), paths(1)).contains(x.userName)) => action(repository) + case Some(x) if(getGroupMembers(repository.owner).exists(_.userName == x.userName)) => action(repository) + case Some(x) if(getCollaboratorUserNames(paths(0), paths(1)).contains(x.userName)) => action(repository) case _ => Unauthorized() } } @@ -136,21 +115,46 @@ } /** - * Allows only signed in users which can access the repository. + * Allows only signed in users who have read permission for the repository. */ -trait ReadableUsersAuthenticator { self: ControllerBase with RepositoryService => +trait ReadableUsersAuthenticator { self: ControllerBase with RepositoryService with AccountService => protected def readableUsersOnly(action: (RepositoryInfo) => Any) = { authenticate(action) } protected def readableUsersOnly[T](action: (T, RepositoryInfo) => Any) = (form: T) => { authenticate(action(form, _)) } private def authenticate(action: (RepositoryInfo) => Any) = { { defining(request.paths){ paths => - getRepository(paths(0), paths(1), baseUrl).map { repository => + getRepository(paths(0), paths(1)).map { repository => context.loginAccount match { case Some(x) if(x.isAdmin) => action(repository) case Some(x) if(!repository.repository.isPrivate) => action(repository) case Some(x) if(paths(0) == x.userName) => action(repository) - case Some(x) if(getCollaborators(paths(0), paths(1)).contains(x.userName)) => action(repository) + case Some(x) if(getGroupMembers(repository.owner).exists(_.userName == x.userName)) => action(repository) + case Some(x) if(getCollaboratorUserNames(paths(0), paths(1)).contains(x.userName)) => action(repository) + case _ => Unauthorized() + } + } getOrElse NotFound() + } + } + } +} + +/** + * Allows only signed in users who have write permission for the repository. + */ +trait WritableUsersAuthenticator { self: ControllerBase with RepositoryService with AccountService => + protected def writableUsersOnly(action: (RepositoryInfo) => Any) = { authenticate(action) } + protected def writableUsersOnly[T](action: (T, RepositoryInfo) => Any) = (form: T) => { authenticate(action(form, _)) } + + private def authenticate(action: (RepositoryInfo) => Any) = { + { + defining(request.paths){ paths => + getRepository(paths(0), paths(1)).map { repository => + context.loginAccount match { + case Some(x) if(x.isAdmin) => action(repository) + case Some(x) if(paths(0) == x.userName) => action(repository) + case Some(x) if(getGroupMembers(repository.owner).exists(_.userName == x.userName)) => action(repository) + case Some(x) if(getCollaboratorUserNames(paths(0), paths(1), Seq(Role.ADMIN, Role.DEVELOPER)).contains(x.userName)) => action(repository) case _ => Unauthorized() } } getOrElse NotFound() diff --git a/src/main/scala/gitbucket/core/util/ControlUtil.scala b/src/main/scala/gitbucket/core/util/ControlUtil.scala index 268e692..74569ac 100644 --- a/src/main/scala/gitbucket/core/util/ControlUtil.scala +++ b/src/main/scala/gitbucket/core/util/ControlUtil.scala @@ -1,8 +1,6 @@ package gitbucket.core.util import org.eclipse.jgit.api.Git -import org.eclipse.jgit.revwalk.RevWalk -import org.eclipse.jgit.treewalk.TreeWalk import scala.util.control.Exception._ import scala.language.reflectiveCalls @@ -22,6 +20,20 @@ } } + def using[A <% { def close(): Unit }, B <% { def close(): Unit }, C](resource1: A, resource2: B)(f: (A, B) => C): C = + try f(resource1, resource2) finally { + if(resource1 != null){ + ignoring(classOf[Throwable]) { + resource1.close() + } + } + if(resource2 != null){ + ignoring(classOf[Throwable]) { + resource2.close() + } + } + } + def using[T](git: Git)(f: Git => T): T = try f(git) finally git.getRepository.close() @@ -31,12 +43,6 @@ git2.getRepository.close() } - def using[T](revWalk: RevWalk)(f: RevWalk => T): T = - try f(revWalk) finally revWalk.release() - - def using[T](treeWalk: TreeWalk)(f: TreeWalk => T): T = - try f(treeWalk) finally treeWalk.release() - def ignore[T](f: => Unit): Unit = try { f } catch { diff --git a/src/main/scala/gitbucket/core/util/DatabaseConfig.scala b/src/main/scala/gitbucket/core/util/DatabaseConfig.scala index ffe7cee..fb8d184 100644 --- a/src/main/scala/gitbucket/core/util/DatabaseConfig.scala +++ b/src/main/scala/gitbucket/core/util/DatabaseConfig.scala @@ -1,19 +1,97 @@ package gitbucket.core.util import com.typesafe.config.ConfigFactory -import Directory.DatabaseHome +import java.io.File +import Directory._ +import liquibase.database.AbstractJdbcDatabase +import liquibase.database.core.{PostgresDatabase, MySQLDatabase, H2Database} +import org.apache.commons.io.FileUtils object DatabaseConfig { - private val config = ConfigFactory.load("database") - private val dbUrl = config.getString("db.url") + private lazy val config = { + val file = new File(GitBucketHome, "database.conf") + if(!file.exists){ + FileUtils.write(file, + """db { + | url = "jdbc:h2:${DatabaseHome};MVCC=true" + | user = "sa" + | password = "sa" + |# connectionTimeout = 30000 + |# idleTimeout = 600000 + |# maxLifetime = 1800000 + |# minimumIdle = 10 + |# maximumPoolSize = 10 + |} + |""".stripMargin, "UTF-8") + } + ConfigFactory.parseFile(file) + } + + private lazy val dbUrl = config.getString("db.url") def url(directory: Option[String]): String = dbUrl.replace("${DatabaseHome}", directory.getOrElse(DatabaseHome)) - val url: String = url(None) - val user: String = config.getString("db.user") - val password: String = config.getString("db.password") - val driver: String = config.getString("db.driver") + lazy val url : String = url(None) + lazy val user : String = config.getString("db.user") + lazy val password : String = config.getString("db.password") + lazy val jdbcDriver : String = DatabaseType(url).jdbcDriver + lazy val slickDriver : slick.driver.JdbcProfile = DatabaseType(url).slickDriver + lazy val liquiDriver : AbstractJdbcDatabase = DatabaseType(url).liquiDriver + lazy val connectionTimeout : Option[Long] = getOptionValue("db.connectionTimeout", config.getLong) + lazy val idleTimeout : Option[Long] = getOptionValue("db.idleTimeout" , config.getLong) + lazy val maxLifetime : Option[Long] = getOptionValue("db.maxLifetime" , config.getLong) + lazy val minimumIdle : Option[Int] = getOptionValue("db.minimumIdle" , config.getInt) + lazy val maximumPoolSize : Option[Int] = getOptionValue("db.maximumPoolSize" , config.getInt) + private def getOptionValue[T](path: String, f: String => T): Option[T] = { + if(config.hasPath(path)) Some(f(path)) else None + } + +} + +sealed trait DatabaseType { + val jdbcDriver: String + val slickDriver: slick.driver.JdbcProfile + val liquiDriver: AbstractJdbcDatabase +} + +object DatabaseType { + + def apply(url: String): DatabaseType = { + if(url.startsWith("jdbc:h2:")){ + H2 + } else if(url.startsWith("jdbc:mysql:")){ + MySQL + } else if(url.startsWith("jdbc:postgresql:")){ + PostgreSQL + } else { + throw new IllegalArgumentException(s"${url} is not supported.") + } + } + + object H2 extends DatabaseType { + val jdbcDriver = "org.h2.Driver" + val slickDriver = slick.driver.H2Driver + val liquiDriver = new H2Database() + } + + object MySQL extends DatabaseType { + val jdbcDriver = "com.mysql.jdbc.Driver" + val slickDriver = slick.driver.MySQLDriver + val liquiDriver = new MySQLDatabase() + } + + object PostgreSQL extends DatabaseType { + val jdbcDriver = "org.postgresql.Driver2" + val slickDriver = new slick.driver.PostgresDriver { + override def quoteIdentifier(id: String): String = { + val s = new StringBuilder(id.length + 4) append '"' + for(c <- id) if(c == '"') s append "\"\"" else s append c.toLower + (s append '"').toString + } + } + val liquiDriver = new PostgresDatabase() + } } diff --git a/src/main/scala/gitbucket/core/util/Directory.scala b/src/main/scala/gitbucket/core/util/Directory.scala index de97bbb..e73bca8 100644 --- a/src/main/scala/gitbucket/core/util/Directory.scala +++ b/src/main/scala/gitbucket/core/util/Directory.scala @@ -1,11 +1,9 @@ package gitbucket.core.util import java.io.File -import ControlUtil._ -import org.apache.commons.io.FileUtils /** - * Provides directories used by GitBucket. + * Provides directory locations used by GitBucket. */ object Directory { @@ -51,6 +49,12 @@ new File(s"${RepositoryHome}/${owner}/${repository}/comments") /** + * Directory for files which are attached to issue. + */ + def getLfsDir(owner: String, repository: String): File = + new File(s"${RepositoryHome}/${owner}/${repository}/lfs") + + /** * Directory for uploaded files by the specified user. */ def getUserUploadDir(userName: String): File = new File(s"${GitBucketHome}/data/${userName}/files") @@ -73,12 +77,6 @@ def getPluginCacheDir(): File = new File(s"${TemporaryHome}/_plugins") /** - * Temporary directory which is used to create an archive to download repository contents. - */ - def getDownloadWorkDir(owner: String, repository: String, sessionId: String): File = - new File(getTemporaryDir(owner, repository), s"download/${sessionId}") - - /** * Substance directory of the wiki repository. */ def getWikiRepositoryDir(owner: String, repository: String): File = diff --git a/src/main/scala/gitbucket/core/util/FileUtil.scala b/src/main/scala/gitbucket/core/util/FileUtil.scala index d3428fb..4836c23 100644 --- a/src/main/scala/gitbucket/core/util/FileUtil.scala +++ b/src/main/scala/gitbucket/core/util/FileUtil.scala @@ -1,7 +1,7 @@ package gitbucket.core.util import org.apache.commons.io.FileUtils -import java.net.URLConnection +import org.apache.tika.Tika import java.io.File import ControlUtil._ import scala.util.Random @@ -9,8 +9,8 @@ object FileUtil { def getMimeType(name: String): String = - defining(URLConnection.getFileNameMap()){ fileNameMap => - fileNameMap.getContentTypeFor(name) match { + defining(new Tika()){ tika => + tika.detect(name) match { case null => "application/octet-stream" case mimeType => mimeType } @@ -28,6 +28,8 @@ def isImage(name: String): Boolean = getMimeType(name).startsWith("image/") + def isUploadableType(name: String): Boolean = mimeTypeWhiteList contains getMimeType(name) + def isLarge(size: Long): Boolean = (size > 1024 * 1000) def isText(content: Array[Byte]): Boolean = !content.contains(0) @@ -50,4 +52,18 @@ FileUtils.deleteDirectory(dir) } } + + val mimeTypeWhiteList: Array[String] = Array( + "application/pdf", + "application/vnd.openxmlformats-officedocument.presentationml.presentation", + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + "application/vnd.openxmlformats-officedocument.wordprocessingml.document", + "image/gif", + "image/jpeg", + "image/png", + "text/plain") + + def getLfsFilePath(owner: String, repository: String, oid: String): String = + Directory.getLfsDir(owner, repository) + "/" + oid + } diff --git a/src/main/scala/gitbucket/core/util/Implicits.scala b/src/main/scala/gitbucket/core/util/Implicits.scala index 13c316a..d93ebe2 100644 --- a/src/main/scala/gitbucket/core/util/Implicits.scala +++ b/src/main/scala/gitbucket/core/util/Implicits.scala @@ -4,6 +4,8 @@ import gitbucket.core.controller.Context import gitbucket.core.servlet.Database +import java.util.regex.Pattern.quote + import javax.servlet.http.{HttpSession, HttpServletRequest} import scala.util.matching.Regex @@ -65,6 +67,7 @@ def paths: Array[String] = (request.getRequestURI.substring(request.getContextPath.length + 1) match{ case path if path.startsWith("api/v3/repos/") => path.substring(13/* "/api/v3/repos".length */) + case path if path.startsWith("api/v3/orgs/") => path.substring(12/* "/api/v3/orgs".length */) case path => path }).split("/") @@ -72,15 +75,16 @@ def hasAttribute(name: String): Boolean = request.getAttribute(name) != null + def gitRepositoryPath: String = request.getRequestURI.replaceFirst("^" + quote(request.getContextPath) + "/git/", "/") + + def baseUrl:String = { + val url = request.getRequestURL.toString + val len = url.length - (request.getRequestURI.length - request.getContextPath.length) + url.substring(0, len).stripSuffix("/") + } } implicit class RichSession(session: HttpSession){ - - def putAndGet[T](key: String, value: T): T = { - session.setAttribute(key, value) - value - } - def getAndRemove[T](key: String): Option[T] = { val value = session.getAttribute(key).asInstanceOf[T] if(value == null){ diff --git a/src/main/scala/gitbucket/core/util/JDBCUtil.scala b/src/main/scala/gitbucket/core/util/JDBCUtil.scala index 41a2b1a..edc41ec 100644 --- a/src/main/scala/gitbucket/core/util/JDBCUtil.scala +++ b/src/main/scala/gitbucket/core/util/JDBCUtil.scala @@ -1,12 +1,18 @@ package gitbucket.core.util +import java.io._ import java.sql._ +import java.text.SimpleDateFormat import ControlUtil._ +import scala.annotation.tailrec import scala.collection.mutable.ListBuffer /** * Provides implicit class which extends java.sql.Connection. - * This is used in automatic migration in [[servlet.AutoUpdateListener]]. + * This is used in following points: + * + * - Automatic migration in [[gitbucket.core.servlet.InitializeListener]] + * - Data importing / exporting in [[gitbucket.core.controller.SystemSettingsController]] and [[gitbucket.core.controller.FileUploadController]] */ object JDBCUtil { @@ -58,6 +64,176 @@ } } + def importAsSQL(in: InputStream): Unit = { + conn.setAutoCommit(false) + try { + using(in){ in => + var out = new ByteArrayOutputStream() + + var length = 0 + val bytes = new scala.Array[Byte](1024 * 8) + var stringLiteral = false + + while({ length = in.read(bytes); length != -1 }){ + for(i <- 0 to length - 1){ + val c = bytes(i) + if(c == '\''){ + stringLiteral = !stringLiteral + } + if(c == ';' && !stringLiteral){ + val sql = new String(out.toByteArray, "UTF-8") + conn.update(sql.trim) + out = new ByteArrayOutputStream() + } else { + out.write(c) + } + } + } + + val remain = out.toByteArray + if(remain.length != 0){ + val sql = new String(remain, "UTF-8") + conn.update(sql.trim) + } + } + conn.commit() + + } catch { + case e: Exception => { + conn.rollback() + throw e + } + } + } + + def exportAsSQL(targetTables: Seq[String]): File = { + val dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss") + val file = File.createTempFile("gitbucket-export-", ".sql") + + using(new FileOutputStream(file)) { out => + val dbMeta = conn.getMetaData + val allTablesInDatabase = allTablesOrderByDependencies(dbMeta) + + allTablesInDatabase.reverse.foreach { tableName => + if (targetTables.contains(tableName)) { + out.write(s"DELETE FROM ${tableName};\n".getBytes("UTF-8")) + } + } + + allTablesInDatabase.foreach { tableName => + if (targetTables.contains(tableName)) { + val sb = new StringBuilder() + select(s"SELECT * FROM ${tableName}") { rs => + sb.append(s"INSERT INTO ${tableName} (") + + val rsMeta = rs.getMetaData + val columns = (1 to rsMeta.getColumnCount).map { i => + (rsMeta.getColumnName(i), rsMeta.getColumnType(i)) + } + sb.append(columns.map(_._1).mkString(", ")) + sb.append(") VALUES (") + + val values = columns.map { case (columnName, columnType) => + if(rs.getObject(columnName) == null){ + null + } else { + columnType match { + case Types.BOOLEAN | Types.BIT => rs.getBoolean(columnName) + case Types.VARCHAR | Types.CLOB | Types.CHAR | Types.LONGVARCHAR => rs.getString(columnName) + case Types.INTEGER => rs.getInt(columnName) + case Types.TIMESTAMP => rs.getTimestamp(columnName) + } + } + } + + val columnValues = values.map { value => + value match { + case x: String => "'" + x.replace("'", "''") + "'" + case x: Timestamp => "'" + dateFormat.format(x) + "'" + case null => "NULL" + case x => x + } + } + sb.append(columnValues.mkString(", ")) + sb.append(");\n") + } + + out.write(sb.toString.getBytes("UTF-8")) + } + } + } + + file + } + + def allTableNames(): Seq[String] = { + using(conn.getMetaData.getTables(null, null, "%", Seq("TABLE").toArray)) { rs => + val tableNames = new ListBuffer[String] + while (rs.next) { + val name = rs.getString("TABLE_NAME").toUpperCase + if (name != "VERSIONS" && name != "PLUGIN") { + tableNames += name + } + } + tableNames.toSeq + } + } + + private def childTables(meta: DatabaseMetaData, tableName: String): Seq[String] = { + val normalizedTableName = + if(meta.getDatabaseProductName == "PostgreSQL"){ + tableName.toLowerCase + } else { + tableName + } + + using(meta.getExportedKeys(null, null, normalizedTableName)) { rs => + val children = new ListBuffer[String] + while (rs.next) { + val childTableName = rs.getString("FKTABLE_NAME").toUpperCase + if(!children.contains(childTableName)){ + children += childTableName + children ++= childTables(meta, childTableName) + } + } + children.distinct.toSeq + } + } + + + private def allTablesOrderByDependencies(meta: DatabaseMetaData): Seq[String] = { + val tables = allTableNames.map { tableName => + val result = TableDependency(tableName, childTables(meta, tableName)) + result + } + + val edges = tables.flatMap { table => + table.children.map { child => (table.tableName, child) } + } + + tsort(edges).toSeq + } + + case class TableDependency(tableName: String, children: Seq[String]) + + + def tsort[A](edges: Traversable[(A, A)]): Iterable[A] = { + @tailrec + def tsort(toPreds: Map[A, Set[A]], done: Iterable[A]): Iterable[A] = { + val (noPreds, hasPreds) = toPreds.partition { _._2.isEmpty } + if (noPreds.isEmpty) { + if (hasPreds.isEmpty) done else sys.error(hasPreds.toString) + } else { + val found = noPreds.map { _._1 } + tsort(hasPreds.mapValues { _ -- found }, done ++ found) + } + } + + val toPred = edges.foldLeft(Map[A, Set[A]]()) { (acc, e) => + acc + (e._1 -> acc.getOrElse(e._1, Set())) + (e._2 -> (acc.getOrElse(e._2, Set()) + e._1)) + } + tsort(toPred, Seq()) + } } } diff --git a/src/main/scala/gitbucket/core/util/JGitUtil.scala b/src/main/scala/gitbucket/core/util/JGitUtil.scala index 01e52c9..786edca 100644 --- a/src/main/scala/gitbucket/core/util/JGitUtil.scala +++ b/src/main/scala/gitbucket/core/util/JGitUtil.scala @@ -5,6 +5,7 @@ import Directory._ import StringUtil._ import ControlUtil._ + import scala.annotation.tailrec import scala.collection.JavaConverters._ import org.eclipse.jgit.lib._ @@ -16,7 +17,11 @@ import org.eclipse.jgit.errors.{ConfigInvalidException, MissingObjectException} import org.eclipse.jgit.transport.RefSpec import java.util.Date -import org.eclipse.jgit.api.errors.{JGitInternalException, InvalidRefNameException, RefAlreadyExistsException, NoHeadException} +import java.util.concurrent.TimeUnit +import java.util.function.Consumer + +import org.cache2k.{Cache2kBuilder, CacheEntry} +import org.eclipse.jgit.api.errors.{InvalidRefNameException, JGitInternalException, NoHeadException, RefAlreadyExistsException} import org.eclipse.jgit.dircache.DirCacheEntry import org.slf4j.LoggerFactory @@ -32,15 +37,11 @@ * * @param owner the user name of the repository owner * @param name the repository name - * @param url the repository URL - * @param commitCount the commit count. If the repository has over 1000 commits then this property is 1001. * @param branchList the list of branch names * @param tags the list of tags */ - case class RepositoryInfo(owner: String, name: String, url: String, commitCount: Int, branchList: List[String], tags: List[TagInfo]){ - def this(owner: String, name: String, baseUrl: String) = { - this(owner, name, s"${baseUrl}/git/${owner}/${name}.git", 0, Nil, Nil) - } + case class RepositoryInfo(owner: String, name: String, branchList: List[String], tags: List[TagInfo]){ + def this(owner: String, name: String) = this(owner, name, Nil, Nil) } /** @@ -100,8 +101,20 @@ def isDifferentFromAuthor: Boolean = authorName != committerName || authorEmailAddress != committerEmailAddress } - case class DiffInfo(changeType: ChangeType, oldPath: String, newPath: String, oldContent: Option[String], newContent: Option[String], - oldIsImage: Boolean, newIsImage: Boolean, oldObjectId: Option[String], newObjectId: Option[String]) + case class DiffInfo( + changeType: ChangeType, + oldPath: String, + newPath: String, + oldContent: Option[String], + newContent: Option[String], + oldIsImage: Boolean, + newIsImage: Boolean, + oldObjectId: Option[String], + newObjectId: Option[String], + oldMode: String, + newMode: String, + tooLarge: Boolean + ) /** * The file content data for the file content view of the repository viewer. @@ -158,20 +171,54 @@ revWalk.dispose revCommit } - + + private val cache = new Cache2kBuilder[String, Int]() {} + .name("commit-count") + .expireAfterWrite(24, TimeUnit.HOURS) + .entryCapacity(10000) + .build() + + def removeCache(git: Git): Unit = { + val dir = git.getRepository.getDirectory + val keyPrefix = dir.getAbsolutePath + "@" + + cache.forEach(new Consumer[CacheEntry[String, Int]] { + override def accept(entry: CacheEntry[String, Int]): Unit = { + if(entry.getKey.startsWith(keyPrefix)){ + cache.remove(entry.getKey) + } + } + }) + } + + /** + * Returns the number of commits in the specified branch or commit. + * If the specified branch has over 10000 commits, this method returns 100001. + */ + def getCommitCount(owner: String, repository: String, branch: String): Int = { + val dir = getRepositoryDir(owner, repository) + val key = dir.getAbsolutePath + "@" + branch + val entry = cache.getEntry(key) + + if(entry == null) { + using(Git.open(dir)) { git => + val commitId = git.getRepository.resolve(branch) + val commitCount = git.log.add(commitId).call.iterator.asScala.take(10001).size + cache.put(key, commitCount) + commitCount + } + } else { + entry.getValue + } + } + /** * Returns the repository information. It contains branch names and tag names. */ - def getRepositoryInfo(owner: String, repository: String, baseUrl: String): RepositoryInfo = { + def getRepositoryInfo(owner: String, repository: String): RepositoryInfo = { using(Git.open(getRepositoryDir(owner, repository))){ git => try { - // get commit count - val commitCount = git.log.all.call.iterator.asScala.map(_ => 1).take(10001).sum - - RepositoryInfo( - owner, repository, s"${baseUrl}/git/${owner}/${repository}.git", - // commit count - commitCount, + RepositoryInfo(owner, repository, // branches git.branchList.call.asScala.map { ref => ref.getName.stripPrefix("refs/heads/") @@ -184,9 +231,7 @@ ) } catch { // not initialized - case e: NoHeadException => RepositoryInfo( - owner, repository, s"${baseUrl}/git/${owner}/${repository}.git", 0, Nil, Nil) - + case e: NoHeadException => RepositoryInfo(owner, repository, Nil, Nil) } } } @@ -201,8 +246,8 @@ */ def getFileList(git: Git, revision: String, path: String = "."): List[FileInfo] = { using(new RevWalk(git.getRepository)){ revWalk => - val objectId = git.getRepository.resolve(revision) - if(objectId==null) return Nil + val objectId = git.getRepository.resolve(revision) + if(objectId == null) return Nil val revCommit = revWalk.parseCommit(objectId) def useTreeWalk(rev:RevCommit)(f:TreeWalk => Any): Unit = if (path == ".") { @@ -244,14 +289,14 @@ revIterator:java.util.Iterator[RevCommit]): List[(ObjectId, FileMode, String, Option[String], RevCommit)] ={ if(restList.isEmpty){ result - }else if(!revIterator.hasNext){ // maybe, revCommit has only 1 log. other case, restList be empty - result ++ restList.map{ case (tuple, map) => tupleAdd(tuple, map.values.headOption.getOrElse(revCommit)) } - }else{ + } else if(!revIterator.hasNext){ // maybe, revCommit has only 1 log. other case, restList be empty + result ++ restList.map { case (tuple, map) => tupleAdd(tuple, map.values.headOption.getOrElse(revCommit)) } + } else { val newCommit = revIterator.next - val (thisTimeChecks,skips) = restList.partition{ case (tuple, parentsMap) => parentsMap.contains(newCommit) } + val (thisTimeChecks,skips) = restList.partition { case (tuple, parentsMap) => parentsMap.contains(newCommit) } if(thisTimeChecks.isEmpty){ findLastCommits(result, restList, revIterator) - }else{ + } else { var nextRest = skips var nextResult = result // Map[(name, oid), (tuple, parentsMap)] @@ -259,20 +304,20 @@ lazy val newParentsMap = newCommit.getParents.map(_ -> newCommit).toMap useTreeWalk(newCommit){ walk => while(walk.next){ - rest.remove(walk.getNameString -> walk.getObjectId(0)).map{ case (tuple, _) => + rest.remove(walk.getNameString -> walk.getObjectId(0)).map { case (tuple, _) => if(newParentsMap.isEmpty){ nextResult +:= tupleAdd(tuple, newCommit) - }else{ + } else { nextRest +:= tuple -> newParentsMap } } } } - rest.values.map{ case (tuple, parentsMap) => + rest.values.map { case (tuple, parentsMap) => val restParentsMap = parentsMap - newCommit if(restParentsMap.isEmpty){ nextResult +:= tupleAdd(tuple, parentsMap(newCommit)) - }else{ + } else { nextRest +:= tuple -> restParentsMap } } @@ -284,7 +329,7 @@ var fileList: List[(ObjectId, FileMode, String, Option[String])] = Nil useTreeWalk(revCommit){ treeWalk => while (treeWalk.next()) { - val linkUrl =if (treeWalk.getFileMode(0) == FileMode.GITLINK) { + val linkUrl = if (treeWalk.getFileMode(0) == FileMode.GITLINK) { getSubmodules(git, revCommit.getTree).find(_.path == treeWalk.getPathString).map(_.url) } else None fileList +:= (treeWalk.getObjectId(0), treeWalk.getFileMode(0), treeWalk.getNameString, linkUrl) @@ -334,7 +379,7 @@ def getTreeId(git: Git, revision: String): Option[String] = { using(new RevWalk(git.getRepository)){ revWalk => val objectId = git.getRepository.resolve(revision) - if(objectId==null) return None + if(objectId == null) return None val revCommit = revWalk.parseCommit(objectId) Some(revCommit.getTree.name) } @@ -346,7 +391,7 @@ def getAllFileListByTreeId(git: Git, treeId: String): List[String] = { using(new RevWalk(git.getRepository)){ revWalk => val objectId = git.getRepository.resolve(treeId+"^{tree}") - if(objectId==null) return Nil + if(objectId == null) return Nil using(new TreeWalk(git.getRepository)){ treeWalk => treeWalk.addTree(objectId) treeWalk.setRecursive(true) @@ -495,11 +540,35 @@ while(treeWalk.next){ val newIsImage = FileUtil.isImage(treeWalk.getPathString) buffer.append((if(!fetchContent){ - DiffInfo(ChangeType.ADD, null, treeWalk.getPathString, None, None, false, newIsImage, None, Option(treeWalk.getObjectId(0)).map(_.name)) + DiffInfo( + changeType = ChangeType.ADD, + oldPath = null, + newPath = treeWalk.getPathString, + oldContent = None, + newContent = None, + oldIsImage = false, + newIsImage = newIsImage, + oldObjectId = None, + newObjectId = Option(treeWalk.getObjectId(0)).map(_.name), + oldMode = treeWalk.getFileMode(0).toString, + newMode = treeWalk.getFileMode(0).toString, + tooLarge = false + ) } else { - DiffInfo(ChangeType.ADD, null, treeWalk.getPathString, None, - JGitUtil.getContentFromId(git, treeWalk.getObjectId(0), false).filter(FileUtil.isText).map(convertFromByteArray), - false, newIsImage, None, Option(treeWalk.getObjectId(0)).map(_.name)) + DiffInfo( + changeType = ChangeType.ADD, + oldPath = null, + newPath = treeWalk.getPathString, + oldContent = None, + newContent = JGitUtil.getContentFromId(git, treeWalk.getObjectId(0), false).filter(FileUtil.isText).map(convertFromByteArray), + oldIsImage = false, + newIsImage = newIsImage, + oldObjectId = None, + newObjectId = Option(treeWalk.getObjectId(0)).map(_.name), + oldMode = treeWalk.getFileMode(0).toString, + newMode = treeWalk.getFileMode(0).toString, + tooLarge = false + ) })) } (buffer.toList, None) @@ -518,16 +587,58 @@ import scala.collection.JavaConverters._ git.getRepository.getConfig.setString("diff", null, "renames", "copies") - git.diff.setNewTree(newTreeIter).setOldTree(oldTreeIter).call.asScala.map { diff => - val oldIsImage = FileUtil.isImage(diff.getOldPath) - val newIsImage = FileUtil.isImage(diff.getNewPath) - if(!fetchContent || oldIsImage || newIsImage){ - DiffInfo(diff.getChangeType, diff.getOldPath, diff.getNewPath, None, None, oldIsImage, newIsImage, Option(diff.getOldId).map(_.name), Option(diff.getNewId).map(_.name)) + + val diffs = git.diff.setNewTree(newTreeIter).setOldTree(oldTreeIter).call.asScala + diffs.map { diff => + if(diffs.size > 100){ + DiffInfo( + changeType = diff.getChangeType, + oldPath = diff.getOldPath, + newPath = diff.getNewPath, + oldContent = None, + newContent = None, + oldIsImage = false, + newIsImage = false, + oldObjectId = Option(diff.getOldId).map(_.name), + newObjectId = Option(diff.getNewId).map(_.name), + oldMode = diff.getOldMode.toString, + newMode = diff.getNewMode.toString, + tooLarge = true + ) } else { - DiffInfo(diff.getChangeType, diff.getOldPath, diff.getNewPath, - JGitUtil.getContentFromId(git, diff.getOldId.toObjectId, false).filter(FileUtil.isText).map(convertFromByteArray), - JGitUtil.getContentFromId(git, diff.getNewId.toObjectId, false).filter(FileUtil.isText).map(convertFromByteArray), - oldIsImage, newIsImage, Option(diff.getOldId).map(_.name), Option(diff.getNewId).map(_.name)) + val oldIsImage = FileUtil.isImage(diff.getOldPath) + val newIsImage = FileUtil.isImage(diff.getNewPath) + if(!fetchContent || oldIsImage || newIsImage){ + DiffInfo( + changeType = diff.getChangeType, + oldPath = diff.getOldPath, + newPath = diff.getNewPath, + oldContent = None, + newContent = None, + oldIsImage = oldIsImage, + newIsImage = newIsImage, + oldObjectId = Option(diff.getOldId).map(_.name), + newObjectId = Option(diff.getNewId).map(_.name), + oldMode = diff.getOldMode.toString, + newMode = diff.getNewMode.toString, + tooLarge = false + ) + } else { + DiffInfo( + changeType = diff.getChangeType, + oldPath = diff.getOldPath, + newPath = diff.getNewPath, + oldContent = JGitUtil.getContentFromId(git, diff.getOldId.toObjectId, false).filter(FileUtil.isText).map(convertFromByteArray), + newContent = JGitUtil.getContentFromId(git, diff.getNewId.toObjectId, false).filter(FileUtil.isText).map(convertFromByteArray), + oldIsImage = oldIsImage, + newIsImage = newIsImage, + oldObjectId = Option(diff.getOldId).map(_.name), + newObjectId = Option(diff.getNewId).map(_.name), + oldMode = diff.getOldMode.toString, + newMode = diff.getNewMode.toString, + tooLarge = false + ) + } } }.toList } @@ -563,7 +674,7 @@ def initRepository(dir: java.io.File): Unit = using(new RepositoryBuilder().setGitDir(dir).setBare.build){ repository => - repository.create + repository.create(true) setReceivePack(repository) } @@ -622,12 +733,14 @@ val newHeadId = inserter.insert(newCommit) inserter.flush() - inserter.release() + inserter.close() val refUpdate = git.getRepository.updateRef(ref) refUpdate.setNewObjectId(newHeadId) refUpdate.update() + removeCache(git) + newHeadId } @@ -713,7 +826,7 @@ def getContentFromId(git: Git, id: ObjectId, fetchLargeFile: Boolean): Option[Array[Byte]] = try { using(git.getRepository.getObjectDatabase){ db => val loader = db.open(id) - if(fetchLargeFile == false && FileUtil.isLarge(loader.getSize)){ + if(loader.isLarge || (fetchLargeFile == false && FileUtil.isLarge(loader.getSize))){ None } else { Some(loader.getBytes) @@ -724,6 +837,22 @@ } /** + * Get objectLoader of the given object id from the Git repository. + * + * @param git the Git object + * @param id the object id + * @param f the function process ObjectLoader + * @return None if object does not exist + */ + def getObjectLoaderFromId[A](git: Git, id: ObjectId)(f: ObjectLoader => A):Option[A] = try { + using(git.getRepository.getObjectDatabase){ db => + Some(f(db.open(id))) + } + } catch { + case e: MissingObjectException => None + } + + /** * Returns all commit id in the specified repository. */ def getAllCommitIds(git: Git): Seq[String] = if(isEmpty(git)) { @@ -737,14 +866,16 @@ existIds.toSeq } - def processTree(git: Git, id: ObjectId)(f: (String, CanonicalTreeParser) => Unit) = { + def processTree[T](git: Git, id: ObjectId)(f: (String, CanonicalTreeParser) => T): Seq[T] = { using(new RevWalk(git.getRepository)){ revWalk => using(new TreeWalk(git.getRepository)){ treeWalk => val index = treeWalk.addTree(revWalk.parseTree(id)) treeWalk.setRecursive(true) + val result = new collection.mutable.ListBuffer[T]() while(treeWalk.next){ - f(treeWalk.getPathString, treeWalk.getTree(index, classOf[CanonicalTreeParser])) + result += f(treeWalk.getPathString, treeWalk.getTree(index, classOf[CanonicalTreeParser])) } + result.toSeq } } } @@ -782,6 +913,7 @@ /** * Returns the last modified commit of specified path + * * @param git the Git object * @param startCommit the search base commit id * @param path the path of target file or directory @@ -791,7 +923,7 @@ return git.log.add(startCommit).addPath(path).setMaxCount(1).call.iterator.next } - def getBranches(owner: String, name: String, defaultBranch: String): Seq[BranchInfo] = { + def getBranches(owner: String, name: String, defaultBranch: String, origin: Boolean): Seq[BranchInfo] = { using(Git.open(getRepositoryDir(owner, name))){ git => val repo = git.getRepository val defaultObject = if (repo.getAllRefs.keySet().contains(defaultBranch)) { @@ -802,20 +934,16 @@ git.branchList.call.asScala.map { ref => val walk = new RevWalk(repo) - try{ - val defaultCommit = walk.parseCommit(defaultObject) - val branchName = ref.getName.stripPrefix("refs/heads/") - val branchCommit = if(branchName == defaultBranch){ - defaultCommit - }else{ - walk.parseCommit(ref.getObjectId) - } - val when = branchCommit.getCommitterIdent.getWhen - val committer = branchCommit.getCommitterIdent.getName + try { + val defaultCommit = walk.parseCommit(defaultObject) + val branchName = ref.getName.stripPrefix("refs/heads/") + val branchCommit = walk.parseCommit(ref.getObjectId) + val when = branchCommit.getCommitterIdent.getWhen + val committer = branchCommit.getCommitterIdent.getName val committerEmail = branchCommit.getCommitterIdent.getEmailAddress - val mergeInfo = if(branchName==defaultBranch){ + val mergeInfo = if(origin && branchName == defaultBranch){ None - }else{ + } else { walk.reset() walk.setRevFilter( RevFilter.MERGE_BASE ) walk.markStart(branchCommit) @@ -868,6 +996,7 @@ /** * Returns sha1 + * * @param owner repository owner * @param name repository name * @param revstr A git object references expression diff --git a/src/main/scala/gitbucket/core/util/Notifier.scala b/src/main/scala/gitbucket/core/util/Notifier.scala index 54b01e3..1897598 100644 --- a/src/main/scala/gitbucket/core/util/Notifier.scala +++ b/src/main/scala/gitbucket/core/util/Notifier.scala @@ -1,7 +1,7 @@ package gitbucket.core.util -import gitbucket.core.model.{Session, Issue} -import gitbucket.core.service.{RepositoryService, AccountService, IssuesService, SystemSettingsService} +import gitbucket.core.model.{Account, Issue, Session} +import gitbucket.core.service.{AccountService, IssuesService, RepositoryService, SystemSettingsService} import gitbucket.core.servlet.Database import gitbucket.core.view.Markdown @@ -9,35 +9,41 @@ import ExecutionContext.Implicits.global import org.apache.commons.mail.{DefaultAuthenticator, HtmlEmail} import org.slf4j.LoggerFactory - import gitbucket.core.controller.Context import SystemSettingsService.Smtp import ControlUtil.defining trait Notifier extends RepositoryService with AccountService with IssuesService { + def toNotify(r: RepositoryService.RepositoryInfo, issue: Issue, content: String) (msg: String => String)(implicit context: Context): Unit - protected def recipients(issue: Issue)(notify: String => Unit)(implicit session: Session, context: Context) = + protected def recipients(issue: Issue, loginAccount: Account)(notify: String => Unit)(implicit session: Session) = ( // individual repository's owner issue.userName :: + // group members of group repository + getGroupMembers(issue.userName).map(_.userName) ::: // collaborators - getCollaborators(issue.userName, issue.repositoryName) ::: + getCollaboratorUserNames(issue.userName, issue.repositoryName) ::: // participants issue.openedUserName :: getComments(issue.userName, issue.repositoryName, issue.issueId).map(_.commentedUserName) ) .distinct - .withFilter ( _ != context.loginAccount.get.userName ) // the operation in person is excluded - .foreach ( getAccountByUserName(_) filterNot (_.isGroupAccount) filterNot (LDAPUtil.isDummyMailAddress(_)) foreach (x => notify(x.mailAddress)) ) - + .withFilter ( _ != loginAccount.userName ) // the operation in person is excluded + .foreach ( + getAccountByUserName(_) + .filterNot (_.isGroupAccount) + .filterNot (LDAPUtil.isDummyMailAddress(_)) + .foreach (x => notify(x.mailAddress)) + ) } object Notifier { // TODO We want to be able to switch to mock. def apply(): Notifier = new SystemSettingsService {}.loadSystemSettings match { - case settings if settings.notification => new Mailer(settings.smtp.get) + case settings if (settings.notification && settings.useSMTP) => new Mailer(settings.smtp.get) case _ => new MockMailer } @@ -68,47 +74,67 @@ private val logger = LoggerFactory.getLogger(classOf[Mailer]) def toNotify(r: RepositoryService.RepositoryInfo, issue: Issue, content: String) - (msg: String => String)(implicit context: Context) = { - val database = Database() + (msg: String => String)(implicit context: Context): Unit = { + context.loginAccount.foreach { loginAccount => + val database = Database() - val f = Future { - database withSession { implicit session => - defining( - s"[${r.name}] ${issue.title} (#${issue.issueId})" -> - msg(Markdown.toHtml(content, r, false, true, false))) { case (subject, msg) => - recipients(issue) { to => - val email = new HtmlEmail - email.setHostName(smtp.host) - email.setSmtpPort(smtp.port.get) - smtp.user.foreach { user => - email.setAuthenticator(new DefaultAuthenticator(user, smtp.password.getOrElse(""))) - } - smtp.ssl.foreach { ssl => - email.setSSLOnConnect(ssl) - } - smtp.fromAddress - .map (_ -> smtp.fromName.orNull) - .orElse (Some("notifications@gitbucket.com" -> context.loginAccount.get.userName)) - .foreach { case (address, name) => - email.setFrom(address, name) - } - email.setCharset("UTF-8") - email.setSubject(subject) - email.setHtmlMsg(msg) - - email.addTo(to).send - } + val f = Future { + database withSession { implicit session => + defining( + s"[${r.name}] ${issue.title} (#${issue.issueId})" -> + msg(Markdown.toHtml( + markdown = content, + repository = r, + enableWikiLink = false, + enableRefsLink = true, + enableAnchor = false, + enableLineBreaks = false + )) + ) { case (subject, msg) => + recipients(issue, loginAccount) { to => send(to, subject, msg, loginAccount) } + } } + "Notifications Successful." } - "Notifications Successful." - } - f onSuccess { - case s => logger.debug(s) - } - f onFailure { - case t => logger.error("Notifications Failed.", t) + f onSuccess { + case s => logger.debug(s) + } + f onFailure { + case t => logger.error("Notifications Failed.", t) + } } } + + def send(to: String, subject: String, msg: String, loginAccount: Account): Unit = { + val email = new HtmlEmail + email.setHostName(smtp.host) + email.setSmtpPort(smtp.port.get) + smtp.user.foreach { user => + email.setAuthenticator(new DefaultAuthenticator(user, smtp.password.getOrElse(""))) + } + smtp.ssl.foreach { ssl => + email.setSSLOnConnect(ssl) + if(ssl == true) { + email.setSslSmtpPort(smtp.port.get.toString) + } + } + smtp.starttls.foreach { starttls => + email.setStartTLSEnabled(starttls) + email.setStartTLSRequired(starttls) + } + smtp.fromAddress + .map (_ -> smtp.fromName.getOrElse(loginAccount.userName)) + .orElse (Some("notifications@gitbucket.com" -> loginAccount.userName)) + .foreach { case (address, name) => + email.setFrom(address, name) + } + email.setCharset("UTF-8") + email.setSubject(subject) + email.setHtmlMsg(msg) + + email.addTo(to).send + } + } class MockMailer extends Notifier { def toNotify(r: RepositoryService.RepositoryInfo, issue: Issue, content: String) diff --git a/src/main/scala/gitbucket/core/util/RepoitoryName.scala b/src/main/scala/gitbucket/core/util/RepoitoryName.scala deleted file mode 100644 index 415b20b..0000000 --- a/src/main/scala/gitbucket/core/util/RepoitoryName.scala +++ /dev/null @@ -1,18 +0,0 @@ -package gitbucket.core.util - -case class RepositoryName(owner:String, name:String){ - val fullName = s"${owner}/${name}" -} - -object RepositoryName{ - def apply(fullName: String): RepositoryName = { - fullName.split("/").toList match { - case owner :: name :: Nil => RepositoryName(owner, name) - case _ => throw new IllegalArgumentException(s"${fullName} is not repositoryName (only 'owner/name')") - } - } - def apply(repository: gitbucket.core.model.Repository): RepositoryName = RepositoryName(repository.userName, repository.repositoryName) - def apply(repository: gitbucket.core.util.JGitUtil.RepositoryInfo): RepositoryName = RepositoryName(repository.owner, repository.name) - def apply(repository: gitbucket.core.service.RepositoryService.RepositoryInfo): RepositoryName = RepositoryName(repository.owner, repository.name) - def apply(repository: gitbucket.core.model.CommitStatus): RepositoryName = RepositoryName(repository.userName, repository.repositoryName) -} diff --git a/src/main/scala/gitbucket/core/util/RepositoryName.scala b/src/main/scala/gitbucket/core/util/RepositoryName.scala new file mode 100644 index 0000000..e7d293d --- /dev/null +++ b/src/main/scala/gitbucket/core/util/RepositoryName.scala @@ -0,0 +1,19 @@ +package gitbucket.core.util + +// TODO Move to gitbucket.core.api package? +case class RepositoryName(owner:String, name:String){ + val fullName = s"${owner}/${name}" +} + +object RepositoryName{ + def apply(fullName: String): RepositoryName = { + fullName.split("/").toList match { + case owner :: name :: Nil => RepositoryName(owner, name) + case _ => throw new IllegalArgumentException(s"${fullName} is not repositoryName (only 'owner/name')") + } + } + def apply(repository: gitbucket.core.model.Repository): RepositoryName = RepositoryName(repository.userName, repository.repositoryName) + def apply(repository: gitbucket.core.util.JGitUtil.RepositoryInfo): RepositoryName = RepositoryName(repository.owner, repository.name) + def apply(repository: gitbucket.core.service.RepositoryService.RepositoryInfo): RepositoryName = RepositoryName(repository.owner, repository.name) + def apply(repository: gitbucket.core.model.CommitStatus): RepositoryName = RepositoryName(repository.userName, repository.repositoryName) +} diff --git a/src/main/scala/gitbucket/core/util/StringUtil.scala b/src/main/scala/gitbucket/core/util/StringUtil.scala index 5633598..0db3efe 100644 --- a/src/main/scala/gitbucket/core/util/StringUtil.scala +++ b/src/main/scala/gitbucket/core/util/StringUtil.scala @@ -1,13 +1,23 @@ package gitbucket.core.util import java.net.{URLDecoder, URLEncoder} + import org.mozilla.universalchardet.UniversalDetector import ControlUtil._ import org.apache.commons.io.input.BOMInputStream import org.apache.commons.io.IOUtils +import org.apache.commons.codec.binary.Base64 + +import scala.util.control.Exception._ object StringUtil { + private lazy val BlowfishKey = { + // last 4 numbers in current timestamp + val time = System.currentTimeMillis.toString + time.substring(time.length - 4) + } + def sha1(value: String): String = defining(java.security.MessageDigest.getInstance("SHA-1")){ md => md.update(value.getBytes) @@ -20,12 +30,28 @@ md.digest.map(b => "%02x".format(b)).mkString } - def urlEncode(value: String): String = URLEncoder.encode(value, "UTF-8") + def encodeBlowfish(value: String): String = { + val spec = new javax.crypto.spec.SecretKeySpec(BlowfishKey.getBytes(), "Blowfish") + val cipher = javax.crypto.Cipher.getInstance("Blowfish") + cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, spec) + new String(Base64.encodeBase64(cipher.doFinal(value.getBytes("UTF-8"))), "UTF-8") + } + + def decodeBlowfish(value: String): String = { + val spec = new javax.crypto.spec.SecretKeySpec(BlowfishKey.getBytes(), "Blowfish") + val cipher = javax.crypto.Cipher.getInstance("Blowfish") + cipher.init(javax.crypto.Cipher.DECRYPT_MODE, spec) + new String(cipher.doFinal(Base64.decodeBase64(value)), "UTF-8") + } + + def urlEncode(value: String): String = URLEncoder.encode(value, "UTF-8").replace("+", "%20") def urlDecode(value: String): String = URLDecoder.decode(value, "UTF-8") def splitWords(value: String): Array[String] = value.split("[ \\t ]+") + def isInteger(value: String): Boolean = allCatch opt { value.toInt } map(_ => true) getOrElse(false) + def escapeHtml(value: String): String = value.replace("&", "&").replace("<", "<").replace(">", ">").replace("\"", """) @@ -83,8 +109,9 @@ *@param message the message which may contains issue id * @return the iterator of issue id */ - def extractIssueId(message: String): Iterator[String] = - "(^|\\W)#(\\d+)(\\W|$)".r.findAllIn(message).matchData.map(_.group(2)) + def extractIssueId(message: String): Seq[String] = + "(^|\\W)#(\\d+)(\\W|$)".r + .findAllIn(message).matchData.map(_.group(2)).toSeq.distinct /** * Extract close issue id like ```close #issueId ``` from the given message. @@ -92,7 +119,8 @@ * @param message the message which may contains close command * @return the iterator of issue id */ - def extractCloseId(message: String): Iterator[String] = - "(?i)(? - if(in != null){ - val sql = IOUtils.toString(in, "UTF-8") - using(conn.createStatement()){ stmt => - logger.debug(sqlPath + "=" + sql) - stmt.executeUpdate(sql) - } - } - } - } - - - /** - * MAJOR.MINOR - */ - val versionString = s"${majorVersion}.${minorVersion}" - -} - -object Versions { - - private val logger = LoggerFactory.getLogger(Versions.getClass) - - def update(conn: Connection, headVersion: Version, currentVersion: Version, versions: Seq[Version], cl: ClassLoader) - (save: Connection => Unit): Unit = { - logger.debug("Start schema update") - try { - if(currentVersion == headVersion){ - logger.debug("No update") - } else if(currentVersion.versionString != "0.0" && !versions.contains(currentVersion)){ - logger.warn(s"Skip migration because ${currentVersion.versionString} is illegal version.") - } else { - versions.takeWhile(_ != currentVersion).reverse.foreach(_.update(conn, cl)) - save(conn) - logger.debug(s"Updated from ${currentVersion.versionString} to ${headVersion.versionString}") - } - } catch { - case ex: Throwable => { - logger.error("Failed to schema update", ex) - ex.printStackTrace() - conn.rollback() - } - } - logger.debug("End schema update") - } - -} - diff --git a/src/main/scala/gitbucket/core/view/LinkConverter.scala b/src/main/scala/gitbucket/core/view/LinkConverter.scala index 59bebe6..00d4e88 100644 --- a/src/main/scala/gitbucket/core/view/LinkConverter.scala +++ b/src/main/scala/gitbucket/core/view/LinkConverter.scala @@ -7,31 +7,94 @@ trait LinkConverter { self: RequestCache => /** - * Converts issue id, username and commit id to link. + * Creates a link to the issue or the pull request from the issue id. */ - protected def convertRefsLinks(value: String, repository: RepositoryService.RepositoryInfo, - issueIdPrefix: String = "#", escapeHtml: Boolean = true)(implicit context: Context): String = { + protected def createIssueLink(repository: RepositoryService.RepositoryInfo, issueId: Int)(implicit context: Context): String = { + val userName = repository.repository.userName + val repositoryName = repository.repository.repositoryName + + getIssue(userName, repositoryName, issueId.toString) match { + case Some(issue) if (issue.isPullRequest) => + s"""Pull #${issueId}""" + case Some(_) => + s"""Issue #${issueId}""" + case None => + s"Unknown #${issueId}" + } + } + + + /** + * Converts issue id, username and commit id to link in the given text. + */ + protected def convertRefsLinks(text: String, repository: RepositoryService.RepositoryInfo, + issueIdPrefix: String = "#", escapeHtml: Boolean = true)(implicit context: Context): String = { // escape HTML tags - val escaped = if(escapeHtml) value.replace("&", "&").replace("<", "<").replace(">", ">").replace("\"", """) else value + val escaped = if(escapeHtml) text.replace("&", "&").replace("<", "<").replace(">", ">").replace("\"", """) else text escaped - // convert issue id to link - .replaceBy(("(?<=(^|\\W))" + issueIdPrefix + "([0-9]+)(?=(\\W|$))").r){ m => - getIssue(repository.owner, repository.name, m.group(2)) match { - case Some(issue) if(issue.isPullRequest) - => Some(s"""#${m.group(2)}""") - case Some(_) => Some(s"""#${m.group(2)}""") - case None => Some(s"""#${m.group(2)}""") + // convert username/project@SHA to link + .replaceBy("(?<=(^|\\W))([a-zA-Z0-9\\-_]+)/([a-zA-Z0-9\\-_\\.]+)@([a-f0-9]{40})(?=(\\W|$))".r){ m => + getAccountByUserName(m.group(2)).map { _ => + s"""${m.group(2)}/${m.group(3)}@${m.group(4).substring(0, 7)}""" } } + + // convert username/project#Num to link + .replaceBy( ("(?<=(^|\\W))([a-zA-Z0-9\\-_]+)/([a-zA-Z0-9\\-_\\.]+)" + issueIdPrefix + "([0-9]+)(?=(\\W|$))").r){ m => + getIssue(m.group(2), m.group(3), m.group(4)) match { + case Some(issue) if (issue.isPullRequest) => + Some(s"""${m.group(2)}/${m.group(3)}#${m.group(4)}""") + case Some(_) => + Some(s"""${m.group(2)}/${m.group(3)}#${m.group(4)}""") + case None => + Some(s"""${m.group(2)}/${m.group(3)}#${m.group(4)}""") + } + } + + // convert username@SHA to link + .replaceBy( ("(?<=(^|\\W))([a-zA-Z0-9\\-_]+)@([a-f0-9]{40})(?=(\\W|$))").r ) { m => + getAccountByUserName(m.group(2)).map { _ => + s"""${m.group(2)}@${m.group(3).substring(0, 7)}""" + } + } + + // convert username#Num to link + .replaceBy( ("(?<=(^|\\W))([a-zA-Z0-9\\-_]+)" + issueIdPrefix + "([0-9]+)(?=(\\W|$))").r ) { m => + getIssue(m.group(2), repository.name, m.group(3)) match { + case Some(issue) if(issue.isPullRequest) => + Some(s"""${m.group(2)}#${m.group(3)}""") + case Some(_) => + Some(s"""${m.group(2)}#${m.group(3)}""") + case None => + Some(s"""${m.group(2)}#${m.group(3)}""") + } + } + + // convert issue id to link + .replaceBy(("(?<=(^|\\W))(GH-|(? + val prefix = if(m.group(2) == "issue:") "#" else m.group(2) + getIssue(repository.owner, repository.name, m.group(3)) match { + case Some(issue) if(issue.isPullRequest) => + Some(s"""${prefix}${m.group(3)}""") + case Some(_) => + Some(s"""${prefix}${m.group(3)}""") + case None => + Some(s"""${m.group(2)}${m.group(3)}""") + } + } + // convert @username to link - .replaceBy("(?<=(^|\\W))@([a-zA-Z0-9\\-_]+)(?=(\\W|$))".r){ m => + .replaceBy("(?<=(^|\\W))@([a-zA-Z0-9\\-_\\.]+)(?=(\\W|$))".r){ m => getAccountByUserName(m.group(2)).map { _ => s"""@${m.group(2)}""" } } + // convert commit id to link - .replaceAll("(?<=(^|\\W))([a-f0-9]{40})(?=(\\W|$))", s"""$$2""") + .replaceBy("(?<=(^|[^\\w/@]))([a-f0-9]{40})(?=(\\W|$))".r){ m => + Some(s"""${m.group(2).substring(0, 7)}""") + } } } diff --git a/src/main/scala/gitbucket/core/view/Markdown.scala b/src/main/scala/gitbucket/core/view/Markdown.scala index 859d29c..46b39b2 100644 --- a/src/main/scala/gitbucket/core/view/Markdown.scala +++ b/src/main/scala/gitbucket/core/view/Markdown.scala @@ -1,18 +1,14 @@ package gitbucket.core.view import java.text.Normalizer -import java.util.Locale import java.util.regex.Pattern +import java.util.Locale import gitbucket.core.controller.Context -import gitbucket.core.service.{RepositoryService, RequestCache, WikiService} +import gitbucket.core.service.{RepositoryService, RequestCache} import gitbucket.core.util.StringUtil -import org.parboiled.common.StringUtils -import org.pegdown.LinkRenderer.Rendering -import org.pegdown._ -import org.pegdown.ast._ - -import scala.collection.JavaConverters._ +import io.github.gitbucket.markedj._ +import io.github.gitbucket.markedj.Utils._ object Markdown { @@ -23,8 +19,9 @@ * @param enableWikiLink if true then wiki style link is available in markdown * @param enableRefsLink if true then issue reference (e.g. #123) is rendered as link * @param enableAnchor if true then anchor for headline is generated + * @param enableLineBreaks if true then render line breaks as <br> * @param enableTaskList if true then task list syntax is available - * @param hasWritePermission + * @param hasWritePermission true if user has writable to ths given repository * @param pages the list of existing Wiki pages */ def toHtml(markdown: String, @@ -32,242 +29,164 @@ enableWikiLink: Boolean, enableRefsLink: Boolean, enableAnchor: Boolean, + enableLineBreaks: Boolean, enableTaskList: Boolean = false, hasWritePermission: Boolean = false, pages: List[String] = Nil)(implicit context: Context): String = { - // escape issue id - val s = if(enableRefsLink){ - markdown.replaceAll("(?<=(\\W|^))#(\\d+)(?=(\\W|$))", "issue:$2") - } else markdown - // escape task list - val source = if(enableTaskList){ - GitBucketHtmlSerializer.escapeTaskList(s) - } else s + val source = if(enableTaskList) escapeTaskList(markdown) else markdown - val rootNode = new PegDownProcessor( - Extensions.AUTOLINKS | Extensions.WIKILINKS | Extensions.FENCED_CODE_BLOCKS | Extensions.TABLES | Extensions.HARDWRAPS | Extensions.SUPPRESS_ALL_HTML - ).parseMarkdown(source.toCharArray) + val options = new Options() + options.setSanitize(true) + options.setBreaks(enableLineBreaks) - new GitBucketHtmlSerializer(markdown, repository, enableWikiLink, enableRefsLink, enableAnchor, enableTaskList, hasWritePermission, pages).toHtml(rootNode) + val renderer = new GitBucketMarkedRenderer(options, repository, + enableWikiLink, enableRefsLink, enableAnchor, enableTaskList, hasWritePermission, pages) + + //helpers.decorateHtml(Marked.marked(source, options, renderer), repository) + Marked.marked(source, options, renderer) } -} -class GitBucketLinkRender( - context: Context, - repository: RepositoryService.RepositoryInfo, - enableWikiLink: Boolean, - pages: List[String]) extends LinkRenderer with WikiService { + /** + * Extends markedj Renderer for GitBucket + */ + class GitBucketMarkedRenderer(options: Options, + repository: RepositoryService.RepositoryInfo, + enableWikiLink: Boolean, + enableRefsLink: Boolean, + enableAnchor: Boolean, + enableTaskList: Boolean, + hasWritePermission: Boolean, + pages: List[String]) + (implicit val context: Context) extends Renderer(options) with LinkConverter with RequestCache { - override def render(node: WikiLinkNode): Rendering = { - if(enableWikiLink){ - try { - val text = node.getText - val (label, page) = if(text.contains('|')){ - val i = text.indexOf('|') - (text.substring(0, i), text.substring(i + 1)) + override def heading(text: String, level: Int, raw: String): String = { + val id = generateAnchorName(text) + val out = new StringBuilder() + + out.append("") + out.append("") + out.append("") + } else { + out.append(">") + } + + out.append(text) + out.append("\n") + out.toString() + } + + override def code(code: String, lang: String, escaped: Boolean): String = { + "
" +
+        (if(escaped) code else escape(code, true)) + "
" + } + + override def list(body: String, ordered: Boolean): String = { + var listType: String = null + if (ordered) { + listType = "ol" + } else { + listType = "ul" + } + if(body.contains("""class="task-list-item-checkbox"""")){ + "<" + listType + " class=\"task-list\">\n" + body + "\n" + } else { + "<" + listType + ">\n" + body + "\n" + } + } + + override def listitem(text: String): String = { + if(text.contains("""class="task-list-item-checkbox" """)){ + "
  • " + text + "
  • \n" + } else { + "
  • " + text + "
  • \n" + } + } + + override def text(text: String): String = { + // convert commit id and username to link. + val t1 = if(enableRefsLink) convertRefsLinks(text, repository, "#", false) else text + // convert task list to checkbox. + val t2 = if(enableTaskList) convertCheckBox(t1, hasWritePermission) else t1 + // decorate by TextDecorator plugins + helpers.decorateHtml(t2, repository) + } + + override def link(href: String, title: String, text: String): String = { + super.link(fixUrl(href, false), title, text) + } + + override def image(href: String, title: String, text: String): String = { + super.image(fixUrl(href, true), title, text) + } + + override def nolink(text: String): String = { + if(enableWikiLink && text.startsWith("[[") && text.endsWith("]]")){ + val link = text.replaceAll("(^\\[\\[|\\]\\]$)", "") + + val (label, page) = if(link.contains('|')){ + val i = link.indexOf('|') + (link.substring(0, i), link.substring(i + 1)) } else { - (text, text) + (link, link) } val url = repository.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/wiki/" + StringUtil.urlEncode(page) - if(pages.contains(page)){ - new Rendering(url, label) + "" + escape(label) + "" } else { - new Rendering(url, label).withAttribute("class", "absent") + "" + escape(label) + "" } - } catch { - case e: java.io.UnsupportedEncodingException => throw new IllegalStateException - } - } else { - super.render(node) - } - } -} - -class GitBucketVerbatimSerializer extends VerbatimSerializer { - def serialize(node: VerbatimNode, printer: Printer): Unit = { - printer.println.print("") - var text: String = node.getText - while (text.charAt(0) == '\n') { - printer.print("
    ") - text = text.substring(1) - } - printer.printEncoded(text) - printer.print("") - } -} - -class GitBucketHtmlSerializer( - markdown: String, - repository: RepositoryService.RepositoryInfo, - enableWikiLink: Boolean, - enableRefsLink: Boolean, - enableTaskList: Boolean, - enableAnchor: Boolean, - hasWritePermission: Boolean, - pages: List[String] - )(implicit val context: Context) extends ToHtmlSerializer( - new GitBucketLinkRender(context, repository, enableWikiLink, pages), - Map[String, VerbatimSerializer](VerbatimSerializer.DEFAULT -> new GitBucketVerbatimSerializer).asJava - ) with LinkConverter with RequestCache { - - override protected def printImageTag(imageNode: SuperNode, url: String): Unit = { - printer.print("") - .print("\"").printEncoded(printChildrenToString(imageNode)).print("\"/") - } - - override protected def printLink(rendering: LinkRenderer.Rendering): Unit = { - printer.print('<').print('a') - printAttribute("href", fixUrl(rendering.href)) - for (attr <- rendering.attributes.asScala) { - printAttribute(attr.name, attr.value) - } - printer.print('>').print(rendering.text).print("") - } - - private def fixUrl(url: String, isImage: Boolean = false): String = { - if(url.startsWith("http://") || url.startsWith("https://") || url.startsWith("/")){ - url - } else if(url.startsWith("#")){ - ("#" + GitBucketHtmlSerializer.generateAnchorName(url.substring(1))) - } else if(!enableWikiLink){ - if(context.currentPath.contains("/blob/")){ - url + (if(isImage) "?raw=true" else "") - } else if(context.currentPath.contains("/tree/")){ - val paths = context.currentPath.split("/") - val branch = if(paths.length > 3) paths.drop(4).mkString("/") else repository.repository.defaultBranch - repository.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/blob/" + branch + "/" + url + (if(isImage) "?raw=true" else "") } else { - val paths = context.currentPath.split("/") - val branch = if(paths.length > 3) paths.last else repository.repository.defaultBranch - repository.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/blob/" + branch + "/" + url + (if(isImage) "?raw=true" else "") + escape(text) } - } else { - repository.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/wiki/_blob/" + url } - } - private def printAttribute(name: String, value: String): Unit = { - printer.print(' ').print(name).print('=').print('"').print(value).print('"') - } + private def fixUrl(url: String, isImage: Boolean = false): String = { + lazy val urlWithRawParam: String = url + (if(isImage && !url.endsWith("?raw=true")) "?raw=true" else "") - private def printHeaderTag(node: HeaderNode): Unit = { - val tag = s"h${node.getLevel}" - val headerTextString = printChildrenToString(node) - val anchorName = GitBucketHtmlSerializer.generateAnchorName(headerTextString) - printer.print(s"""<$tag class="markdown-head">""") - if(enableAnchor){ - printer.print(s"""""") - printer.print(s"""""") + if(url.startsWith("http://") || url.startsWith("https://") || url.startsWith("/")){ + url + } else if(url.startsWith("#")){ + ("#" + generateAnchorName(url.substring(1))) + } else if(!enableWikiLink){ + if(context.currentPath.contains("/blob/")){ + urlWithRawParam + } else if(context.currentPath.contains("/tree/")){ + val paths = context.currentPath.split("/") + val branch = if(paths.length > 3) paths.drop(4).mkString("/") else repository.repository.defaultBranch + repository.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/blob/" + branch + "/" + urlWithRawParam + } else { + val paths = context.currentPath.split("/") + val branch = if(paths.length > 3) paths.last else repository.repository.defaultBranch + repository.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/blob/" + branch + "/" + urlWithRawParam + } + } else { + repository.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/wiki/_blob/" + url + } } - visitChildren(node) - printer.print(s"") - } - override def visit(node: HeaderNode): Unit = { - printHeaderTag(node) - } - - override def visit(node: TextNode): Unit = { - // convert commit id and username to link. - val t = if(enableRefsLink) convertRefsLinks(node.getText, repository, "issue:") else node.getText - - // convert task list to checkbox. - val text = if(enableTaskList) GitBucketHtmlSerializer.convertCheckBox(t, hasWritePermission) else t - - if (abbreviations.isEmpty) { - printer.print(text) - } else { - printWithAbbreviations(text) - } - } - - override def visit(node: VerbatimNode) { - val printer = new Printer() - val serializer = verbatimSerializers.get(VerbatimSerializer.DEFAULT) - serializer.serialize(node, printer) - val html = printer.getString - - // convert commit id and username to link. - val t = if(enableRefsLink) convertRefsLinks(html, repository, "issue:", escapeHtml = false) else html - - this.printer.print(t) - } - - override def visit(node: BulletListNode): Unit = { - if (printChildrenToString(node).contains("""class="task-list-item-checkbox" """)) { - printer.println().print("""
      """).indent(+2) - visitChildren(node) - printer.indent(-2).println().print("
    ") - } else { - printIndentedTag(node, "ul") - } - } - - override def visit(node: ListItemNode): Unit = { - if (printChildrenToString(node).contains("""class="task-list-item-checkbox" """)) { - printer.println() - printer.print("""
  • """) - visitChildren(node) - printer.print("
  • ") - } else { - printer.println() - printTag(node, "li") - } - } - - override def visit(node: ExpLinkNode) { - printLink(linkRenderer.render(node, printLinkChildrenToString(node))) - } - - def printLinkChildrenToString(node: SuperNode) = { - val priorPrinter = printer - printer = new Printer() - visitLinkChildren(node) - val result = printer.getString() - printer = priorPrinter - result - } - - def visitLinkChildren(node: SuperNode) { - import scala.collection.JavaConversions._ - node.getChildren.foreach(child => child match { - case node: ExpImageNode => visitLinkChild(node) - case node: SuperNode => visitLinkChildren(node) - case _ => child.accept(this) - }) - } - - def visitLinkChild(node: ExpImageNode) { - printer.print("\"").printEncoded(printChildrenToString(node)).print("\"/") - } -} - -object GitBucketHtmlSerializer { - - private val Whitespace = "[\\s]".r - - def generateAnchorName(text: String): String = { - val noWhitespace = Whitespace.replaceAllIn(text, "-") - val normalized = Normalizer.normalize(noWhitespace, Normalizer.Form.NFD) - val noSpecialChars = StringUtil.urlEncode(normalized) - noSpecialChars.toLowerCase(Locale.ENGLISH) } def escapeTaskList(text: String): String = { Pattern.compile("""^( *)- \[([x| ])\] """, Pattern.MULTILINE).matcher(text).replaceAll("$1* task:$2: ") } + def generateAnchorName(text: String): String = { + val normalized = Normalizer.normalize(text.replaceAll("<.*>", "").replaceAll("[\\s]", "-"), Normalizer.Form.NFD) + val encoded = StringUtil.urlEncode(normalized) + encoded.toLowerCase(Locale.ENGLISH) + } + def convertCheckBox(text: String, hasWritePermission: Boolean): String = { val disabled = if (hasWritePermission) "" else "disabled" text.replaceAll("task:x:", """") - .replaceAll("task: :", """") + .replaceAll("task: :", """") } + } + diff --git a/src/main/scala/gitbucket/core/view/helpers.scala b/src/main/scala/gitbucket/core/view/helpers.scala index a0320ce..d29248a 100644 --- a/src/main/scala/gitbucket/core/view/helpers.scala +++ b/src/main/scala/gitbucket/core/view/helpers.scala @@ -5,11 +5,11 @@ import gitbucket.core.controller.Context import gitbucket.core.model.CommitState -import gitbucket.core.plugin.{RenderRequest, PluginRegistry, Renderer} +import gitbucket.core.plugin.{PluginRegistry, RenderRequest} +import gitbucket.core.service.RepositoryService.RepositoryInfo import gitbucket.core.service.{RepositoryService, RequestCache} import gitbucket.core.util.{FileUtil, JGitUtil, StringUtil} - -import play.twirl.api.Html +import play.twirl.api.{Html, HtmlFormat} /** * Provides helper methods for Twirl templates. @@ -84,15 +84,31 @@ /** * Converts Markdown of Wiki pages to HTML. */ - def markdown(value: String, + def markdown(markdown: String, repository: RepositoryService.RepositoryInfo, enableWikiLink: Boolean, enableRefsLink: Boolean, + enableLineBreaks: Boolean, + enableAnchor: Boolean = true, enableTaskList: Boolean = false, hasWritePermission: Boolean = false, pages: List[String] = Nil)(implicit context: Context): Html = - Html(Markdown.toHtml(value, repository, enableWikiLink, enableRefsLink, enableTaskList, true, hasWritePermission, pages)) + Html(Markdown.toHtml( + markdown = markdown, + repository = repository, + enableWikiLink = enableWikiLink, + enableRefsLink = enableRefsLink, + enableAnchor = enableAnchor, + enableLineBreaks = enableLineBreaks, + enableTaskList = enableTaskList, + hasWritePermission = hasWritePermission, + pages = pages + )) + /** + * Render the given source (only markdown is supported in default) as HTML. + * You can test if a file is renderable in this method by [[isRenderable()]]. + */ def renderMarkup(filePath: List[String], fileContent: String, branch: String, repository: RepositoryService.RepositoryInfo, enableWikiLink: Boolean, enableRefsLink: Boolean, enableAnchor: Boolean)(implicit context: Context): Html = { @@ -103,11 +119,21 @@ renderer.render(RenderRequest(filePath, fileContent, branch, repository, enableWikiLink, enableRefsLink, enableAnchor, context)) } + /** + * Tests whether the given file is renderable. It's tested by the file extension. + */ def isRenderable(fileName: String): Boolean = { PluginRegistry().renderableExtensions.exists(extension => fileName.toLowerCase.endsWith("." + extension)) } /** + * Creates a link to the issue or the pull request from the issue id. + */ + def issueLink(repository: RepositoryService.RepositoryInfo, issueId: Int)(implicit context: Context): Html = { + Html(createIssueLink(repository, issueId)) + } + + /** * Returns <img> which displays the avatar icon for the given user name. * This method looks up Gravatar if avatar icon has not been configured in user settings. */ @@ -125,7 +151,7 @@ * Converts commit id, issue id and username to the link. */ def link(value: String, repository: RepositoryService.RepositoryInfo)(implicit context: Context): Html = - Html(convertRefsLinks(value, repository)) + Html(decorateHtml(convertRefsLinks(value, repository), repository)) def cut(value: String, length: Int): String = if(value.length > length){ @@ -156,6 +182,11 @@ ) /** + * Remove html tags from the given Html instance. + */ + def removeHtml(html: Html): Html = Html(html.body.replaceAll("<.+?>", "")) + + /** * URL encode except '/'. */ def encodeRefName(value: String): String = StringUtil.urlEncode(value).replace("%2F", "/") @@ -191,10 +222,24 @@ * Generates the avatar link to the account page. * If user does not exist or disabled, this method returns avatar image without link. */ - def avatarLink(userName: String, size: Int, mailAddress: String = "", tooltip: Boolean = false)(implicit context: Context): Html = - userWithContent(userName, mailAddress)(avatar(userName, size, tooltip, mailAddress)) + def avatarLink(userName: String, size: Int, mailAddress: String = "", tooltip: Boolean = false, label: Boolean = false) + (implicit context: Context): Html = { - private def userWithContent(userName: String, mailAddress: String = "", styleClass: String = "")(content: Html)(implicit context: Context): Html = + val avatarHtml = avatar(userName, size, tooltip, mailAddress) + val contentHtml = if(label == true) Html(avatarHtml.body + " " + userName) else avatarHtml + + userWithContent(userName, mailAddress)(contentHtml) + } + + /** + * Generates the avatar link to the account page. + * If user does not exist or disabled, this method returns avatar image without link. + */ + def avatarLink(commit: JGitUtil.CommitInfo, size: Int)(implicit context: Context): Html = + userWithContent(commit.authorName, commit.authorEmailAddress)(avatar(commit, size)) + + private def userWithContent(userName: String, mailAddress: String = "", styleClass: String = "")(content: Html) + (implicit context: Context): Html = (if(mailAddress.isEmpty){ getAccountByUserName(userName) } else { @@ -258,10 +303,10 @@ } def commitStateIcon(state: CommitState) = Html(state match { - case CommitState.PENDING => "●" - case CommitState.SUCCESS => "✔" - case CommitState.ERROR => "×" - case CommitState.FAILURE => "×" + case CommitState.PENDING => """""" + case CommitState.SUCCESS => """""" + case CommitState.ERROR => """""" + case CommitState.FAILURE => """""" }) def commitStateText(state: CommitState, commitId:String) = state match { @@ -270,4 +315,69 @@ case CommitState.ERROR => "Failed" case CommitState.FAILURE => "Failed" } + + /** + * Render a given object as the JSON string. + */ + def json(obj: AnyRef): String = { + implicit val formats = org.json4s.DefaultFormats + org.json4s.jackson.Serialization.write(obj) + } + + // This pattern comes from: http://stackoverflow.com/a/4390768/1771641 (extract-url-from-string) + private[this] val detectAndRenderLinksRegex = """(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,13}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))""".r + + def detectAndRenderLinks(text: String, repository: RepositoryInfo)(implicit context: Context): String = { + val matches = detectAndRenderLinksRegex.findAllMatchIn(text).toSeq + + val (x, pos) = matches.foldLeft((collection.immutable.Seq.empty[Html], 0)){ case ((x, pos), m) => + val url = m.group(0) + val href = url.replace("\"", """) + (x ++ (Seq( + if(pos < m.start) Some(HtmlFormat.escape(text.substring(pos, m.start))) else None, + Some(Html(s"""${url}""")) + ).flatten), m.end) + } + // append rest fragment + val out = if (pos < text.length) x :+ HtmlFormat.escape(text.substring(pos)) else x + + decorateHtml(HtmlFormat.fill(out).toString, repository) + } + + def decorateHtml(html: String, repository: RepositoryInfo)(implicit context: Context): String = { + PluginRegistry().getTextDecorators.foldLeft(html){ case (html, decorator) => + val text = new StringBuilder() + val result = new StringBuilder() + var tag = false + + html.foreach { c => + c match { + case '<' if tag == false => { + tag = true + if(text.nonEmpty){ + result.append(decorator.decorate(text.toString, repository)) + text.setLength(0) + } + result.append(c) + } + case '>' if tag == true => { + tag = false + result.append(c) + } + case _ if tag == false => { + text.append(c) + } + case _ if tag == true => { + result.append(c) + } + } + } + if(text.nonEmpty){ + result.append(decorator.decorate(text.toString, repository)) + } + + result.toString + } + } + } diff --git a/src/main/twirl/gitbucket/core/account/activity.scala.html b/src/main/twirl/gitbucket/core/account/activity.scala.html index 24d2c28..ed986ef 100644 --- a/src/main/twirl/gitbucket/core/account/activity.scala.html +++ b/src/main/twirl/gitbucket/core/account/activity.scala.html @@ -1,11 +1,10 @@ @(account: gitbucket.core.model.Account, groupNames: List[String], activities: List[gitbucket.core.model.Activity])(implicit context: gitbucket.core.controller.Context) -@import context._ -@import gitbucket.core.view.helpers._ -@main(account, groupNames, "activity"){ +@import gitbucket.core.view.helpers +@gitbucket.core.account.html.main(account, groupNames, "activity"){
    - activities + activities
    - @helper.html.activities(activities) + @gitbucket.core.helper.html.activities(activities) } diff --git a/src/main/twirl/gitbucket/core/account/application.scala.html b/src/main/twirl/gitbucket/core/account/application.scala.html index ba8915e..a065e66 100644 --- a/src/main/twirl/gitbucket/core/account/application.scala.html +++ b/src/main/twirl/gitbucket/core/account/application.scala.html @@ -1,57 +1,53 @@ @(account: gitbucket.core.model.Account, personalTokens: List[gitbucket.core.model.AccessToken], gneratedToken: Option[(gitbucket.core.model.AccessToken, String)])(implicit context: gitbucket.core.controller.Context) -@import context._ -@import gitbucket.core.view.helpers._ -@html.main("Applications"){ -
    -
    -
    - @menu("application", settings.ssh) -
    -
    -
    -
    Personal access tokens
    -
    - @if(personalTokens.isEmpty && gneratedToken.isEmpty){ - No tokens. - }else{ - Tokens you have generated that can be used to access the GitBucket API.
    - } - @gneratedToken.map{ case (token, tokenString) => -
    - Make sure to copy your new personal access token now. You won't be able to see it again! -
    - @helper.html.copy("generated-token-copy", tokenString){ - +@gitbucket.core.html.main("Applications"){ +
    + @gitbucket.core.account.html.menu("application", context.settings.ssh){ +
    +
    Personal access tokens
    +
    + @if(personalTokens.isEmpty && gneratedToken.isEmpty){ + No tokens. + } else { + Tokens you have generated which can be used to access the GitBucket API. +
    + } + @gneratedToken.map { case (token, tokenString) => +
    + Make sure to copy your new personal access token now. You won't be able to see it again! +
    + Delete +
    + @gitbucket.core.helper.html.copy("generated-token", "generated-token-copy", tokenString){ + } - Delete +
    +
    + } + @personalTokens.zipWithIndex.map { case (token, i) => + @if(i != 0){
    } - @personalTokens.zipWithIndex.map { case (token, i) => - @if(i != 0){ -
    - } - @token.note - Delete - } + @token.note + Delete + } +
    +
    +
    +
    +
    Generate new token
    +
    +
    + +
    + +

    What is this token for?

    +
    +
    - -
    -
    Generate new token
    -
    -
    - -
    - -

    What's this token for?

    -
    - -
    -
    -
    -
    -
    + + }
    } diff --git a/src/main/twirl/gitbucket/core/account/edit.scala.html b/src/main/twirl/gitbucket/core/account/edit.scala.html index c49d482..21377e5 100644 --- a/src/main/twirl/gitbucket/core/account/edit.scala.html +++ b/src/main/twirl/gitbucket/core/account/edit.scala.html @@ -1,65 +1,61 @@ -@(account: gitbucket.core.model.Account, info: Option[Any])(implicit context: gitbucket.core.controller.Context) +@(account: gitbucket.core.model.Account, info: Option[Any], error: Option[Any])(implicit context: gitbucket.core.controller.Context) @import gitbucket.core.util.LDAPUtil -@import context._ -@import gitbucket.core.view.helpers._ -@html.main("Edit your profile"){ -
    -
    -
    - @menu("profile", settings.ssh) -
    -
    - @helper.html.information(info) - @if(LDAPUtil.isDummyMailAddress(account)){
    Please register your mail address.
    } -
    -
    -
    Profile
    -
    -
    -
    +@import gitbucket.core.view.helpers +@gitbucket.core.html.main("Edit your profile"){ +
    + @gitbucket.core.account.html.menu("profile", context.settings.ssh){ + @gitbucket.core.helper.html.information(info) + @gitbucket.core.helper.html.error(error) + @if(LDAPUtil.isDummyMailAddress(account)){
    Please register your mail address.
    } + +
    +
    Profile
    +
    +
    +
    @if(account.password.nonEmpty){ -
    +
    - +
    } -
    +
    - +
    -
    +
    - +
    -
    +
    - +
    -
    -
    +
    +
    - @helper.html.uploadavatar(Some(account)) + @gitbucket.core.helper.html.uploadavatar(Some(account))
    -
    - - - @if(!LDAPUtil.isDummyMailAddress(account)){Cancel} -
    +
    + + + @if(!LDAPUtil.isDummyMailAddress(account)){Cancel} +
    -
    + }
    } \ No newline at end of file diff --git a/src/main/twirl/gitbucket/core/admin/menu.scala.html b/src/main/twirl/gitbucket/core/admin/menu.scala.html index e5f6f3d..c7e3ab0 100644 --- a/src/main/twirl/gitbucket/core/admin/menu.scala.html +++ b/src/main/twirl/gitbucket/core/admin/menu.scala.html @@ -1,24 +1,34 @@ @(active: String)(body: Html)(implicit context: gitbucket.core.controller.Context) -@import context._ -
    -
    -
    -
    -
    -
    \ No newline at end of file +
    +
    +
    + @body +
    +
    diff --git a/src/main/twirl/gitbucket/core/admin/plugins.scala.html b/src/main/twirl/gitbucket/core/admin/plugins.scala.html new file mode 100644 index 0000000..505c1bc --- /dev/null +++ b/src/main/twirl/gitbucket/core/admin/plugins.scala.html @@ -0,0 +1,40 @@ +@(plugins: List[gitbucket.core.plugin.PluginInfo])(implicit context: gitbucket.core.controller.Context) +@gitbucket.core.html.main("Plugins"){ + @gitbucket.core.admin.html.menu("plugins") { +

    Installed plugins

    + + @if(plugins.size > 0) { + + + @plugins.map { plugin => +
    +
    @plugin.pluginName
    +
    +
    + + @plugin.pluginId +
    +
    + + @plugin.pluginVersion +
    +
    + + @plugin.pluginName +
    +
    + + @plugin.description +
    +
    +
    + } + } else { +

    No plugin detected on your gitbucket installation.

    + } + } +} diff --git a/src/main/twirl/gitbucket/core/admin/system.scala.html b/src/main/twirl/gitbucket/core/admin/system.scala.html index 6bb5caa..c78ad6d 100644 --- a/src/main/twirl/gitbucket/core/admin/system.scala.html +++ b/src/main/twirl/gitbucket/core/admin/system.scala.html @@ -1,19 +1,28 @@ @(info: Option[Any])(implicit context: gitbucket.core.controller.Context) -@import context._ -@import gitbucket.core.view.helpers._ -@import gitbucket.core.util.Directory._ -@html.main("System Settings"){ - @menu("system"){ - @helper.html.information(info) -
    -
    -
    System Settings
    -
    - - - - - @GitBucketHome +@gitbucket.core.html.main("System settings"){ + @gitbucket.core.admin.html.menu("system"){ + @gitbucket.core.helper.html.information(info) + +
    +
    System settings
    +
    + + + + + + + + + + + + + + + + +
    PropertyValue
    GITBUCKET_HOME@gitbucket.core.util.Directory.GitBucketHome
    DATABASE_URL@gitbucket.core.util.DatabaseConfig.url
    @@ -21,14 +30,14 @@
    - +

    The base URL is used for redirect, notification email, git repository URL box and more. - If the base URL is empty, GitBucket generates URL from request information. - You can use this property to adjust URL difference between the reverse proxy and GitBucket. + If the base URL is empty, GitBucket generates URL from the request information. + You can use this property to adjust to URL differences between the reverse proxy and GitBucket.

    @@ -36,7 +45,7 @@
    - +
    @@ -45,24 +54,24 @@

    - +
    @@ -72,23 +81,28 @@

    - -
    - - -
    + +
    +
    + +
    + + +
    +
    +
    @@ -96,8 +110,8 @@
    @@ -107,22 +121,27 @@
    -
    -
    - -
    - +
    +
    + +
    + + +
    +
    +
    + +
    +
    -

    - Base URL is required if SSH access is enabled. -

    @@ -130,92 +149,90 @@
    -
    -
    - -
    - +
    +
    + +
    +
    -
    - -
    - +
    + +
    +
    -
    - -
    - +
    + +
    +
    -
    - -
    - +
    + +
    +
    -
    - -
    - +
    + +
    +
    -
    - -
    - +
    + +
    +
    -
    - -
    - +
    + +
    +
    -
    - -
    - +
    + +
    +
    -
    - -
    - +
    + +
    +
    -
    -
    - +
    + +
    +
    -
    -
    - +
    + +
    +
    -
    - -
    - +
    + +
    +
    @@ -224,80 +241,162 @@
    - +
    -
    -
    - -
    - + + + +
    + +
    + +
    +
    +
    + +
    +
    -
    - -
    - +
    + +
    +
    -
    - -
    - +
    + +
    +
    -
    - -
    - +
    + +
    +
    -
    -
    - +
    + +
    +
    -
    - -
    - +
    + +
    +
    -
    - -
    - +
    + +
    +
    +
    + +
    + +
    +
    +
    + Send test mail to: + + +
    + + + + @* +
    + +
    + +
    + + +
    +
    + *@
    -
    +
    -
    +
    } } \ No newline at end of file + diff --git a/src/main/twirl/gitbucket/core/admin/user.scala.html b/src/main/twirl/gitbucket/core/admin/user.scala.html new file mode 100644 index 0000000..843354f --- /dev/null +++ b/src/main/twirl/gitbucket/core/admin/user.scala.html @@ -0,0 +1,83 @@ +@(account: Option[gitbucket.core.model.Account], error: Option[Any] = None)(implicit context: gitbucket.core.controller.Context) +@gitbucket.core.html.main(if(account.isEmpty) "New user" else "Update user"){ + @gitbucket.core.admin.html.menu("users"){ + @gitbucket.core.helper.html.error(error) +
    +
    +
    +
    + +
    + +
    + + @if(account.isDefined){ + +
    + +
    + } +
    + @if(account.map(_.password.nonEmpty).getOrElse(true)){ +
    + +
    + +
    + +
    + } +
    + +
    + +
    + +
    +
    + +
    + +
    + +
    +
    + + + +
    +
    + +
    + +
    + +
    +
    +
    +
    + + @gitbucket.core.helper.html.uploadavatar(account) +
    +
    +
    +
    + + Cancel +
    +
    + } +} diff --git a/src/main/twirl/gitbucket/core/admin/usergroup.scala.html b/src/main/twirl/gitbucket/core/admin/usergroup.scala.html new file mode 100644 index 0000000..39b586b --- /dev/null +++ b/src/main/twirl/gitbucket/core/admin/usergroup.scala.html @@ -0,0 +1,135 @@ +@(account: Option[gitbucket.core.model.Account], members: List[gitbucket.core.model.GroupMember])(implicit context: gitbucket.core.controller.Context) +@gitbucket.core.html.main(if(account.isEmpty) "New group" else "Update group"){ + @gitbucket.core.admin.html.menu("users"){ +
    +
    +
    +
    + +
    + +
    + + @if(account.isDefined){ + + } +
    +
    + +
    + +
    + +
    +
    + + +
    +
    + + @gitbucket.core.helper.html.uploadavatar(account) +
    +
    +
    +
    + +
      +
    + @gitbucket.core.helper.html.account("memberName", 200, true, false) + + +
    + +
    +
    +
    +
    +
    + + Cancel +
    +
    + } +} + diff --git a/src/main/twirl/gitbucket/core/admin/userlist.scala.html b/src/main/twirl/gitbucket/core/admin/userlist.scala.html new file mode 100644 index 0000000..71d52c1 --- /dev/null +++ b/src/main/twirl/gitbucket/core/admin/userlist.scala.html @@ -0,0 +1,70 @@ +@(users: List[gitbucket.core.model.Account], members: Map[String, List[String]], includeRemoved: Boolean)(implicit context: gitbucket.core.controller.Context) +@import gitbucket.core.view.helpers +@gitbucket.core.html.main("Manage Users"){ + @gitbucket.core.admin.html.menu("users"){ + + + + @users.map { account => + + + + } +
    +
    + @if(account.isGroupAccount){ + Edit + } else { + Edit + } +
    +
    + @helpers.avatar(account.userName, 20) + @account.userName + @if(account.isGroupAccount){ + (Group) + } else { + @if(account.isAdmin){ + (Administrator) + } else { + (Normal) + } + } + @if(account.isGroupAccount){ + @members(account.userName).map { userName => + @helpers.avatar(userName, 20, tooltip = true) + } + } +
    +
    +
    + @if(!account.isGroupAccount){ + @account.mailAddress + } + @account.url.map { url => + @url + } +
    +
    + Registered: @helpers.datetime(account.registeredDate) + Updated: @helpers.datetime(account.updatedDate) + @if(!account.isGroupAccount){ + Last Login: @account.lastLoginDate.map(helpers.datetime) + } +
    +
    + } +} + diff --git a/src/main/twirl/gitbucket/core/admin/users/group.scala.html b/src/main/twirl/gitbucket/core/admin/users/group.scala.html deleted file mode 100644 index fe4d62b..0000000 --- a/src/main/twirl/gitbucket/core/admin/users/group.scala.html +++ /dev/null @@ -1,140 +0,0 @@ -@(account: Option[gitbucket.core.model.Account], members: List[gitbucket.core.model.GroupMember])(implicit context: gitbucket.core.controller.Context) -@import context._ -@import gitbucket.core.view.helpers._ -@html.main(if(account.isEmpty) "New Group" else "Update Group"){ - @admin.html.menu("users"){ -
    -
    -
    -
    - -
    - -
    - - @if(account.isDefined){ - - } -
    -
    - -
    - -
    - -
    -
    - - -
    -
    - - @helper.html.uploadavatar(account) -
    -
    -
    -
    - -
      -
    - @helper.html.account("memberName", 200) - - -
    - -
    -
    -
    -
    -
    - - Cancel -
    -
    - } -} - diff --git a/src/main/twirl/gitbucket/core/admin/users/list.scala.html b/src/main/twirl/gitbucket/core/admin/users/list.scala.html deleted file mode 100644 index 1c8f17b..0000000 --- a/src/main/twirl/gitbucket/core/admin/users/list.scala.html +++ /dev/null @@ -1,71 +0,0 @@ -@(users: List[gitbucket.core.model.Account], members: Map[String, List[String]], includeRemoved: Boolean)(implicit context: gitbucket.core.controller.Context) -@import context._ -@import gitbucket.core.view.helpers._ -@html.main("Manage Users"){ - @admin.html.menu("users"){ - - - - @users.map { account => - - - - } -
    -
    - @if(account.isGroupAccount){ - Edit - } else { - Edit - } -
    -
    - @avatar(account.userName, 20) - @account.userName - @if(account.isGroupAccount){ - (Group) - } else { - @if(account.isAdmin){ - (Administrator) - } else { - (Normal) - } - } - @if(account.isGroupAccount){ - @members(account.userName).map { userName => - @avatar(userName, 20, tooltip = true) - } - } -
    -
    -
    - @if(!account.isGroupAccount){ - @account.mailAddress - } - @account.url.map { url => - @url - } -
    -
    - Registered: @datetime(account.registeredDate) - Updated: @datetime(account.updatedDate) - @if(!account.isGroupAccount){ - Last Login: @account.lastLoginDate.map(datetime) - } -
    -
    - } -} - \ No newline at end of file diff --git a/src/main/twirl/gitbucket/core/admin/users/user.scala.html b/src/main/twirl/gitbucket/core/admin/users/user.scala.html deleted file mode 100644 index fb54eb5..0000000 --- a/src/main/twirl/gitbucket/core/admin/users/user.scala.html +++ /dev/null @@ -1,83 +0,0 @@ -@(account: Option[gitbucket.core.model.Account])(implicit context: gitbucket.core.controller.Context) -@import context._ -@html.main(if(account.isEmpty) "New User" else "Update User"){ - @admin.html.menu("users"){ -
    -
    -
    -
    - -
    - -
    - - @if(account.isDefined){ - -
    - -
    - } -
    - @if(account.map(_.password.nonEmpty).getOrElse(true)){ -
    - -
    - -
    - -
    - } -
    - -
    - -
    - -
    -
    - -
    - -
    - -
    -
    - - - -
    -
    - -
    - -
    - -
    -
    -
    -
    - - @helper.html.uploadavatar(account) -
    -
    -
    -
    - - Cancel -
    -
    - } -} diff --git a/src/main/twirl/gitbucket/core/dashboard/header.scala.html b/src/main/twirl/gitbucket/core/dashboard/header.scala.html index ea14458..bc174bc 100644 --- a/src/main/twirl/gitbucket/core/dashboard/header.scala.html +++ b/src/main/twirl/gitbucket/core/dashboard/header.scala.html @@ -2,72 +2,61 @@ closedCount: Int, condition: gitbucket.core.service.IssuesService.IssueSearchCondition, groups: List[String])(implicit context: gitbucket.core.controller.Context) -@import context._ -@import gitbucket.core.view.helpers._ - - - - @openCount Open -    - - - @closedCount Closed - - -
    - @helper.html.dropdown("Visibility", flat = true){ +@import gitbucket.core.view.helpers +
    + @gitbucket.core.helper.html.dropdown("Visibility"){
  • - @helper.html.checkicon(condition.visibility == Some("private")) + @gitbucket.core.helper.html.checkicon(condition.visibility == Some("private")) Private repository only
  • - @helper.html.checkicon(condition.visibility == Some("public")) + @gitbucket.core.helper.html.checkicon(condition.visibility == Some("public")) Public repository only
  • } - @helper.html.dropdown("Organization", flat = true){ + @gitbucket.core.helper.html.dropdown("Organization"){ @groups.map { group =>
  • - @helper.html.checkicon(condition.groups.contains(group)) - @avatar(group, 20) @group + @gitbucket.core.helper.html.checkicon(condition.groups.contains(group)) + @helpers.avatar(group, 20) @group
  • } } - @helper.html.dropdown("Sort", flat = true){ + @gitbucket.core.helper.html.dropdown("Sort"){
  • - @helper.html.checkicon(condition.sort == "created" && condition.direction == "desc") Newest + @gitbucket.core.helper.html.checkicon(condition.sort == "created" && condition.direction == "desc") Newest
  • - @helper.html.checkicon(condition.sort == "created" && condition.direction == "asc") Oldest + @gitbucket.core.helper.html.checkicon(condition.sort == "created" && condition.direction == "asc") Oldest
  • - @helper.html.checkicon(condition.sort == "comments" && condition.direction == "desc") Most commented + @gitbucket.core.helper.html.checkicon(condition.sort == "comments" && condition.direction == "desc") Most commented
  • - @helper.html.checkicon(condition.sort == "comments" && condition.direction == "asc") Least commented + @gitbucket.core.helper.html.checkicon(condition.sort == "comments" && condition.direction == "asc") Least commented
  • - @helper.html.checkicon(condition.sort == "updated" && condition.direction == "desc") Recently updated + @gitbucket.core.helper.html.checkicon(condition.sort == "updated" && condition.direction == "desc") Recently updated
  • - @helper.html.checkicon(condition.sort == "updated" && condition.direction == "asc") Least recently updated + @gitbucket.core.helper.html.checkicon(condition.sort == "updated" && condition.direction == "asc") Least recently updated
  • } diff --git a/src/main/twirl/gitbucket/core/dashboard/issues.scala.html b/src/main/twirl/gitbucket/core/dashboard/issues.scala.html index af395e5..f23e576 100644 --- a/src/main/twirl/gitbucket/core/dashboard/issues.scala.html +++ b/src/main/twirl/gitbucket/core/dashboard/issues.scala.html @@ -4,13 +4,15 @@ closedCount: Int, condition: gitbucket.core.service.IssuesService.IssueSearchCondition, filter: String, - groups: List[String])(implicit context: gitbucket.core.controller.Context) -@import context._ -@import gitbucket.core.view.helpers._ -@html.main("Issues"){ - @dashboard.html.tab("issues") -
    - @issuesnavi(filter, "issues", condition) - @issueslist(issues, page, openCount, closedCount, condition, filter, groups) -
    + groups: List[String], + recentRepositories: List[gitbucket.core.service.RepositoryService.RepositoryInfo], + userRepositories: List[gitbucket.core.service.RepositoryService.RepositoryInfo])(implicit context: gitbucket.core.controller.Context) +@gitbucket.core.html.main("Issues"){ + @gitbucket.core.dashboard.html.sidebar(recentRepositories, userRepositories){ + @gitbucket.core.dashboard.html.tab("issues") +
    + @gitbucket.core.dashboard.html.issuesnavi("issues", filter, openCount, closedCount, condition) + @gitbucket.core.dashboard.html.issueslist(issues, page, openCount, closedCount, condition, filter, groups) +
    + } } diff --git a/src/main/twirl/gitbucket/core/dashboard/issueslist.scala.html b/src/main/twirl/gitbucket/core/dashboard/issueslist.scala.html index 6140ba0..7463451 100644 --- a/src/main/twirl/gitbucket/core/dashboard/issueslist.scala.html +++ b/src/main/twirl/gitbucket/core/dashboard/issueslist.scala.html @@ -5,58 +5,63 @@ condition: gitbucket.core.service.IssuesService.IssueSearchCondition, filter: String, groups: List[String])(implicit context: gitbucket.core.controller.Context) -@import context._ -@import gitbucket.core.view.helpers._ +@import gitbucket.core.view.helpers @import gitbucket.core.service.IssuesService @import gitbucket.core.service.IssuesService.IssueInfo - - - - @issues.map { case IssueInfo(issue, labels, milestone, commentCount, commitStatus) => - - - - } + + + + + + + @issues.map { case IssueInfo(issue, labels, milestone, commentCount, commitStatus) => + + + + } + @if(issues.isEmpty){ + + + + } +
    - @dashboard.html.header(openCount, closedCount, condition, groups) -
    - @if(issue.isPullRequest){ - - } else { - - } - @issue.userName/@issue.repositoryName ・ - @if(issue.isPullRequest){ - @issue.title - } else { - @issue.title - } - @gitbucket.core.issues.html.commitstatus(issue, commitStatus) - @labels.map { label => - @label.labelName - } - - @issue.assignedUserName.map { userName => - @avatar(userName, 20, tooltip = true) - } - @if(commentCount > 0){ - - @commentCount - - } else { - - @commentCount - - } - -
    - #@issue.issueId opened by @user(issue.openedUserName, styleClass="username") @datetime(issue.registeredDate) - @milestone.map { milestone => - @milestone - } -
    -
    + @gitbucket.core.dashboard.html.header(openCount, closedCount, condition, groups) +
    + @issue.userName/@issue.repositoryName ・ + @if(issue.isPullRequest){ + @issue.title + } else { + @issue.title + } + @gitbucket.core.issues.html.commitstatus(issue, commitStatus) + @labels.map { label => + @label.labelName + } + + @issue.assignedUserName.map { userName => + @helpers.avatar(userName, 20, tooltip = true) + } + @if(commentCount > 0){ + + @commentCount + + } else { + + @commentCount + + } + +
    + #@issue.issueId opened by @helpers.user(issue.openedUserName, styleClass="username") @helpers.datetime(issue.registeredDate) + @milestone.map { milestone => + @milestone + } +
    +
    + No results matched your search. +
    - @helper.html.paginator(page, (if(condition.state == "open") openCount else closedCount), IssuesService.IssueLimit, 10, condition.toURL) + @gitbucket.core.helper.html.paginator(page, (if(condition.state == "open") openCount else closedCount), IssuesService.IssueLimit, 10, condition.toURL)
    diff --git a/src/main/twirl/gitbucket/core/dashboard/issuesnavi.scala.html b/src/main/twirl/gitbucket/core/dashboard/issuesnavi.scala.html index 5770f82..7504d1a 100644 --- a/src/main/twirl/gitbucket/core/dashboard/issuesnavi.scala.html +++ b/src/main/twirl/gitbucket/core/dashboard/issuesnavi.scala.html @@ -1,22 +1,27 @@ -@(filter: String, - active: String, +@(active: String, + filter: String, + openCount: Int, + closedCount: Int, condition: gitbucket.core.service.IssuesService.IssueSearchCondition)(implicit context: gitbucket.core.controller.Context) -@import context._ -@import gitbucket.core.view.helpers._ - diff --git a/src/main/twirl/gitbucket/core/error.scala.html b/src/main/twirl/gitbucket/core/error.scala.html index 48e5507..3c987d8 100644 --- a/src/main/twirl/gitbucket/core/error.scala.html +++ b/src/main/twirl/gitbucket/core/error.scala.html @@ -1,4 +1,8 @@ @(title: String)(implicit context: gitbucket.core.controller.Context) -@main("Error"){ -

    @title

    +@gitbucket.core.html.main("Error"){ +
    +
    +

    @title

    +
    +
    } \ No newline at end of file diff --git a/src/main/twirl/gitbucket/core/goget.scala.html b/src/main/twirl/gitbucket/core/goget.scala.html new file mode 100644 index 0000000..7afd2d8 --- /dev/null +++ b/src/main/twirl/gitbucket/core/goget.scala.html @@ -0,0 +1,7 @@ +@(repository: gitbucket.core.service.RepositoryService.RepositoryInfo)(implicit context: gitbucket.core.controller.Context) + + + + + + \ No newline at end of file diff --git a/src/main/twirl/gitbucket/core/helper/account.scala.html b/src/main/twirl/gitbucket/core/helper/account.scala.html index 4b62e72..0e14ced 100644 --- a/src/main/twirl/gitbucket/core/helper/account.scala.html +++ b/src/main/twirl/gitbucket/core/helper/account.scala.html @@ -1,11 +1,19 @@ -@(id: String, width: Int)(implicit context: gitbucket.core.controller.Context) -@import context._ - +@(id: String, width: Int, user: Boolean, group: Boolean)(implicit context: gitbucket.core.controller.Context) + + + } +@dropzone(clickable: Boolean, textareaId: Option[String]) = { + url: '@context.path/upload/file/@repository.owner/@repository.name', + maxFilesize: 10, + clickable: @clickable, + acceptedFiles: @Html(FileUtil.mimeTypeWhiteList.mkString("'", ",", "'")), + dictInvalidFileType: 'Unfortunately, we don\'t support that file type. Try again with a PNG, GIF, JPG, DOCX, PPTX, XLSX, TXT, or PDF.', + previewTemplate: "
    \n
    Uploading your files...
    \n
    \n
    ", + success: function(file, id) { + var attachFile = (file.type.match(/image\/.*/) ? '\n![' + file.name.split('.')[0] : '\n[' + file.name) + + '](@context.baseUrl/@repository.owner/@repository.name/_attached/' + id + ')'; + $('#@textareaId').val($('#@textareaId').val() + attachFile); + $(file.previewElement).prevAll('div.dz-preview').addBack().remove(); + } +} diff --git a/src/main/twirl/gitbucket/core/helper/branchcontrol.scala.html b/src/main/twirl/gitbucket/core/helper/branchcontrol.scala.html index f97e166..09e3feb 100644 --- a/src/main/twirl/gitbucket/core/helper/branchcontrol.scala.html +++ b/src/main/twirl/gitbucket/core/helper/branchcontrol.scala.html @@ -1,21 +1,17 @@ @(branch: String = "", repository: gitbucket.core.service.RepositoryService.RepositoryInfo, hasWritePermission: Boolean)(body: Html)(implicit context: gitbucket.core.controller.Context) -@import gitbucket.core.service.RepositoryService -@import context._ -@import gitbucket.core._ -@import gitbucket.core.view.helpers._ -@helper.html.dropdown( +@import gitbucket.core.view.helpers +@gitbucket.core.helper.html.dropdown( value = if(branch.length == 40) branch.substring(0, 10) else branch, - prefix = if(branch.length == 40) "tree" else if(repository.branchList.contains(branch)) "branch" else "tree", - mini = true + prefix = if(branch.length == 40) "tree" else if(repository.branchList.contains(branch)) "branch" else "tree" ) {
  • Switch branches
  • -
  • +
  • @body @if(hasWritePermission) {
  • -
    - - @if(active == name){ - - } else { - - - } - @if(expand){ @label} - @if(expand && count > 0){ -
    @count
    - } -
    -
  • -} - -@sidemenuPlugin(path: String, name: String, label: String, icon: String) = { +@menuitem(path: String, name: String, label: String, icon: String, count: Int = 0) = {
  • -
    - @if(expand){ @label} + @if(path.startsWith("http")){ + + @label @if(count > 0) { @count } + + } else { + + @label @if(count > 0) { @count } + + }
  • } -
    - @helper.html.information(info) - @helper.html.error(error) - @if(repository.commitCount > 0){ -
    -
    - @if(loginAccount.isEmpty){ - Fork +
    + + @if(repository.repository.options.wikiOption != "DISABLE") { + @menuitem("/wiki", "wiki", "Wiki", "book") + } else { + @repository.repository.options.externalWikiUrl.map { externalWikiUrl => + @menuitem(externalWikiUrl, "wiki", "Wiki", "book") + } + } + @if(repository.repository.options.allowFork) { + @menuitem("/network/members", "fork", "Forks", "repo-forked", repository.forkedCount) + } + @if(context.loginAccount.isDefined && (context.loginAccount.get.isAdmin || repository.managers.contains(context.loginAccount.get.userName))){ + @menuitem("/settings", "settings", "Settings", "tools") + } + @gitbucket.core.plugin.PluginRegistry().getRepositoryMenus.map { menu => + @menu(repository, context).map { link => + @menuitem(link.path, link.id, link.label, link.icon.getOrElse("ruby")) + } + } +
    - @if(loginAccount.isDefined && isNoGroup){ -
    - -
    - } - } -
    - @helper.html.repositoryicon(repository, true) - @repository.owner / @repository.name +
    +
    +
    +
    +
    + @gitbucket.core.helper.html.information(info) + @gitbucket.core.helper.html.error(error) +
    + @gitbucket.core.helper.html.repositoryicon(repository, true) + @repository.owner / @repository.name - @defining(repository.repository){ x => - @if(repository.repository.originRepositoryName.isDefined){ -
    - forked from @x.parentUserName/@x.parentRepositoryName + @defining(repository.repository){ x => + @if(repository.repository.originRepositoryName.isDefined){ + + } + @x.description.map { description => +
    @Html(helpers.detectAndRenderLinks(description, repository))
    + } + } +
    - } - } +
    + @body +
    -
    -
    -
    -
      -
    • - @sidemenu("" , "code" , "Code") - @sidemenu("/issues", "issues", "Issues", repository.issueCount) - @sidemenu("/pulls" , "pulls" , "Pull Requests", repository.pullCount) - @sidemenu("/wiki" , "wiki" , "Wiki") - @if(loginAccount.isDefined && (loginAccount.get.isAdmin || repository.managers.contains(loginAccount.get.userName))){ - @sidemenu("/settings", "settings", "Settings") - } -
    • -
    - @if(expand){ -
    - HTTP clone URL -
    - @helper.html.copy("repository-url-copy", repository.httpUrl){ - - } - @if(settings.ssh && loginAccount.isDefined){ -
    - You can clone HTTP or SSH. -
    - } - @id.map { id => - - - } - } -
    -
    - @if(expand){ - @repository.repository.description.map { description => -

    @description

    - } - - } - @body -
    -
    - diff --git a/src/main/twirl/gitbucket/core/pulls/commits.scala.html b/src/main/twirl/gitbucket/core/pulls/commits.scala.html index 43ed945..7138fc8 100644 --- a/src/main/twirl/gitbucket/core/pulls/commits.scala.html +++ b/src/main/twirl/gitbucket/core/pulls/commits.scala.html @@ -1,35 +1,42 @@ @(commits: Seq[Seq[gitbucket.core.util.JGitUtil.CommitInfo]], comments: Option[List[gitbucket.core.model.Comment]] = None, repository: gitbucket.core.service.RepositoryService.RepositoryInfo)(implicit context: gitbucket.core.controller.Context) -@import context._ -@import gitbucket.core.view.helpers._ -@import gitbucket.core.model._ -
    - - @commits.map { day => - - +@import gitbucket.core.view.helpers +
    @date(day.head.commitTime)
    +@commits.map { day => + + + @day.zipWithIndex.map { case (commit, i) => + @if(i != 0){ } + - @day.map { commit => - - - - - - - } } -
    @helpers.date(day.head.commitTime)
    + +
    +
    @helpers.avatarLink(commit, 40)
    +
    + @helpers.link(commit.summary, repository) + @if(commit.description.isDefined){ + ... + } +
    + @if(commit.description.isDefined){ + + } +
    + @if(commit.isDifferentFromAuthor) { + @helpers.user(commit.authorName, commit.authorEmailAddress, "username") + authored @gitbucket.core.helper.html.datetimeago(commit.authorTime) + + } + @helpers.user(commit.committerName, commit.committerEmailAddress, "username") + committed @gitbucket.core.helper.html.datetimeago(commit.commitTime) +
    +
    +
    +
    - @avatar(commit, 20) - @user(commit.authorName, commit.authorEmailAddress, "username") - @commit.shortMessage - @if(comments.isDefined){ - @comments.get.flatMap @{ - case comment: CommitComment => Some(comment) - case other => None - }.count(t => t.commitId == commit.id && !t.pullRequest) - } - - @commit.id.substring(0, 7) -
    -
    +} + diff --git a/src/main/twirl/gitbucket/core/pulls/compare.scala.html b/src/main/twirl/gitbucket/core/pulls/compare.scala.html index bf00b94..06d00ff 100644 --- a/src/main/twirl/gitbucket/core/pulls/compare.scala.html +++ b/src/main/twirl/gitbucket/core/pulls/compare.scala.html @@ -1,4 +1,5 @@ -@(commits: Seq[Seq[gitbucket.core.util.JGitUtil.CommitInfo]], +@(title: String, + commits: Seq[Seq[gitbucket.core.util.JGitUtil.CommitInfo]], diffs: Seq[gitbucket.core.util.JGitUtil.DiffInfo], members: List[(String, String)], comments: List[gitbucket.core.model.Comment], @@ -9,78 +10,88 @@ repository: gitbucket.core.service.RepositoryService.RepositoryInfo, originRepository: gitbucket.core.service.RepositoryService.RepositoryInfo, forkedRepository: gitbucket.core.service.RepositoryService.RepositoryInfo, - hasWritePermission: Boolean)(implicit context: gitbucket.core.controller.Context) -@import context._ -@import gitbucket.core.view.helpers._ -@html.main(s"Pull Requests - ${repository.owner}/${repository.name}", Some(repository)){ - @html.menu("pulls", repository){ + hasOriginWritePermission: Boolean, + collaborators: List[String], + milestones: List[gitbucket.core.model.Milestone], + labels: List[gitbucket.core.model.Label])(implicit context: gitbucket.core.controller.Context) +@import gitbucket.core.view.helpers +@gitbucket.core.html.main(s"Pull requests - ${repository.owner}/${repository.name}", Some(repository)){ + @gitbucket.core.html.menu("pulls", repository){
    -
    - Edit - @originRepository.owner:@originId ... @forkedRepository.owner:@forkedId -
    - - @if(commits.nonEmpty && hasWritePermission){ -
    + @if(commits.nonEmpty && context.loginAccount.isDefined){ +
    Create pull request +    + Discuss and review the changes in this comparison with others.
    -