Skip to content

Commit 940090f

Browse files
committed
add setting tab page
1 parent 178e89d commit 940090f

File tree

5 files changed

+108
-3
lines changed

5 files changed

+108
-3
lines changed

build.sbt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ name := "pages-plugin"
33
scalaVersion := "2.12.0"
44
scalacOptions := Seq("-unchecked", "-deprecation", "-encoding", "utf8", "-feature")
55

6+
lazy val root = (project in file(".")).enablePlugins(SbtTwirl)
7+
68
resolvers ++= Seq(
79
Resolver.jcenterRepo,
810
Resolver.mavenLocal
911
)
1012

1113
libraryDependencies ++= Seq(
1214
"io.github.gitbucket" %% "gitbucket" % "4.11.0",
13-
"javax.servlet" % "javax.servlet-api" % "3.1.0"
15+
"javax.servlet" % "javax.servlet-api" % "3.1.0",
16+
"com.typesafe.play" %% "twirl-compiler" % "1.3.0"
1417
)
1518

19+
useJCenter := true

project/plugins.sbt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
addSbtPlugin("org.scalariform" % "sbt-scalariform" % "1.6.0")
22
addSbtPlugin("net.virtual-void" % "sbt-dependency-graph" % "0.8.2")
33
addSbtPlugin("com.dwijnand" % "sbt-dynver" % "1.1.1")
4+
addSbtPlugin("com.typesafe.sbt" % "sbt-twirl" % "1.3.0")

src/main/scala/Plugin.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import gitbucket.core.controller.Context
12
import gitbucket.core.controller.ControllerBase
3+
import gitbucket.core.plugin.Link
4+
import gitbucket.core.service.RepositoryService.RepositoryInfo
25
import gitbucket.plugin.pages.{ PagesController, PagesHook }
36
import io.github.gitbucket.solidbase.migration.{ SqlMigration, LiquibaseMigration }
47
import io.github.gitbucket.solidbase.model.Version
@@ -29,6 +32,10 @@ class Plugin extends gitbucket.core.plugin.Plugin {
2932
"/*" -> new PagesController
3033
)
3134

35+
override val repositorySettingTabs = Seq(
36+
(repository: RepositoryInfo, context: Context) => Some(Link("pages", "Pages", s"settings/pages"))
37+
)
38+
3239
override val repositoryHooks = Seq(
3340
new PagesHook
3441
)

src/main/scala/gitbucket/plugin/pages/pages.scala

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,37 @@ import gitbucket.core.controller.ControllerBase
44
import gitbucket.core.service.{ AccountService, RepositoryService }
55
import gitbucket.core.util.Implicits._
66
import gitbucket.core.util.SyntaxSugars._
7-
import gitbucket.core.util.{ Directory, JGitUtil, ReferrerAuthenticator }
7+
import gitbucket.core.util.{ Directory, JGitUtil, OwnerAuthenticator, ReferrerAuthenticator }
8+
import gitbucket.pages.html
89
import gitbucket.plugin.model.PageSourceType
910
import gitbucket.plugin.service.PagesService
11+
import io.github.gitbucket.scalatra.forms._
1012
import org.eclipse.jgit.api.Git
1113
import org.eclipse.jgit.lib.ObjectId
1214
import org.eclipse.jgit.revwalk.RevCommit
15+
import org.scalatra.i18n.Messages
1316

1417
import scala.annotation.tailrec
1518
import scala.language.implicitConversions
1619

1720
class PagesController
1821
extends PagesControllerBase
1922
with AccountService
23+
with OwnerAuthenticator
2024
with PagesService
2125
with RepositoryService
2226
with ReferrerAuthenticator
2327

2428
trait PagesControllerBase extends ControllerBase {
25-
self: AccountService with RepositoryService with PagesService with ReferrerAuthenticator =>
29+
self: AccountService with RepositoryService with PagesService with ReferrerAuthenticator with OwnerAuthenticator =>
30+
31+
case class OptionsForm(source: PageSourceType)
32+
33+
val optionsForm = mapping(
34+
"source" -> trim(label("Pages Source", text(required, pagesOption)))
35+
)(
36+
(source) => OptionsForm(PageSourceType.valueOf(source))
37+
)
2638

2739
val PAGES_BRANCHES = List("gb-pages", "gh-pages")
2840

@@ -85,6 +97,20 @@ trait PagesControllerBase extends ControllerBase {
8597
redirect(s"/${repository.owner}/${repository.name}/pages/")
8698
})
8799

100+
get("/:owner/:repository/settings/pages")(ownerOnly { repository =>
101+
val source = getPageOptions(repository.owner, repository.name) match {
102+
case Some(p) => p.source
103+
case None => PageSourceType.GH_PAGES
104+
}
105+
html.options(repository, source, flash.get("info"))
106+
})
107+
108+
post("/:owner/:repository/settings/pages", optionsForm)(ownerOnly { (form, repository) =>
109+
updatePageOptions(repository.owner, repository.name, form.source)
110+
flash += "info" -> "Pages source saved"
111+
redirect(s"/${repository.owner}/${repository.name}/settings/pages")
112+
})
113+
88114
@tailrec
89115
final def resolveBranch(git: Git, names: List[String]): Option[ObjectId] = {
90116
names match {
@@ -117,5 +143,12 @@ trait PagesControllerBase extends ControllerBase {
117143

118144
def isRoot(path: String) = path == ""
119145

146+
private def pagesOption: Constraint = new Constraint() {
147+
override def validate(name: String, value: String, messages: Messages): Option[String] =
148+
PageSourceType.valueOpt(value) match {
149+
case Some(_) => None
150+
case None => Some("Pages source is invalid.")
151+
}
152+
}
120153
}
121154

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
@import gitbucket.plugin.model.PageSourceType
2+
@(repository: gitbucket.core.service.RepositoryService.RepositoryInfo,
3+
source: PageSourceType,
4+
info: Option[Any])(implicit context: gitbucket.core.controller.Context)
5+
@import gitbucket.core.view.helpers
6+
@gitbucket.core.html.main("Pages", Some(repository)){
7+
@gitbucket.core.html.menu("settings", repository){
8+
@gitbucket.core.settings.html.menu("pages", repository){
9+
@gitbucket.core.helper.html.information(info)
10+
<form id="form" method="post" action="@helpers.url(repository)/settings/pages" validate="true">
11+
<div class="panel panel-default">
12+
<div class="panel-heading strong">Source</div>
13+
<div class="panel-body">
14+
<fieldset class="form-group">
15+
<div class="radio">
16+
<label>
17+
<input type="radio" name="source" value="@PageSourceType.GH_PAGES.code" @if(source == PageSourceType.GH_PAGES) { checked="checked" }>
18+
<span class="strong">gh-pages branch</span><br>
19+
<div class="normal muted">
20+
Use the <code>gh-pages</code> branch for Pages.
21+
</div>
22+
</label>
23+
</div>
24+
<div class="radio">
25+
<label>
26+
<input type="radio" name="source" value="@PageSourceType.MASTER.code" @if(source == PageSourceType.MASTER) { checked="checked" }>
27+
<span class="strong">master branch</span><br>
28+
<div class="normal muted">
29+
Use the <code>master</code> branch for Pages.
30+
</div>
31+
</label>
32+
</div>
33+
<div class="radio">
34+
<label>
35+
<input type="radio" name="source" value="@PageSourceType.MASTER_DOCS.code" @if(source == PageSourceType.MASTER_DOCS) { checked="checked" }>
36+
<span class="strong">master branch /docs folder</span><br>
37+
<div class="normal muted">
38+
Use only the <code>/docs</code> folder for Pages.
39+
</div>
40+
</label>
41+
</div>
42+
<div class="radio">
43+
<label>
44+
<input type="radio" name="source" value="@PageSourceType.NONE.code" @if(source == PageSourceType.NONE) { checked="checked" }>
45+
<span class="strong">None</span><br>
46+
<div class="normal muted">
47+
Disable Pages.
48+
</div>
49+
</label>
50+
</div>
51+
</fieldset>
52+
</div>
53+
</div>
54+
<div class="align-right" style="margin-top: 20px;">
55+
<input type="submit" class="btn btn-success" value="Apply changes"/>
56+
</div>
57+
</form>
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)