Skip to content

Commit d941db9

Browse files
authored
Merge pull request #56 from takezoe/build-file-in-repos
Makes possible to use a file in the git repository to build
2 parents 7e02f73 + 7bb27a3 commit d941db9

File tree

7 files changed

+102
-26
lines changed

7 files changed

+102
-26
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ In addition, this plug-in is made to just experiment continuous integration on G
3535

3636
Plugin version | GitBucket version
3737
:--------------|:--------------------
38-
1.4.x - | 4.23.x -
38+
1.5.x - | 4.23.1 -
39+
1.4.x - | 4.23.0
3940
1.3.x - | 4.19.x -
4041
1.0.x - 1.2.x | 4.17.x, 4.18.x
4142

@@ -49,6 +50,11 @@ Run `sbt assembly` and copy generated `/target/scala-2.12/gitbucket-ci-plugin-as
4950

5051
## Release Notes
5152

53+
### 1.5.0
54+
55+
- Build branches even other than the default branch
56+
- Support the use of an arbitrary file in the git repository as a build script
57+
5258
### 1.4.0
5359

5460
- Max parallel builds and max stored history became configurable
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<changeSet>
3+
<addColumn tableName="CI_CONFIG">
4+
<column name="BUILD_TYPE" type="varchar(200)" nullable="true"/>
5+
</addColumn>
6+
<update tableName="CI_CONFIG">
7+
<column name="BUILD_TYPE" value="script"/>
8+
</update>
9+
</changeSet>

src/main/scala/Plugin.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ class Plugin extends gitbucket.core.plugin.Plugin with CIService with AccountSer
5252
new Version("1.2.1"),
5353
new Version("1.3.0"),
5454
new Version("1.4.0",
55-
new LiquibaseMigration("update/gitbucket-ci_1.4.0.xml"))
55+
new LiquibaseMigration("update/gitbucket-ci_1.4.0.xml")),
56+
new Version("1.5.0",
57+
new LiquibaseMigration("update/gitbucket-ci_1.5.0.xml"))
5658
)
5759

5860
override val assetsMappings = Seq("/ci" -> "/gitbucket/ci/assets")

src/main/scala/io/github/gitbucket/ci/controller/CIController.scala

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ object CIController {
4343

4444
case class BuildConfigForm(
4545
enableBuild: Boolean,
46+
buildType: Option[String],
4647
buildScript: Option[String],
48+
buildFile: Option[String],
4749
notification: Boolean,
4850
skipWords: Option[String],
4951
runWords: Option[String]
@@ -63,7 +65,9 @@ class CIController extends ControllerBase
6365

6466
val buildConfigForm = mapping(
6567
"enableBuild" -> trim(label("Enable build", boolean())),
66-
"buildScript" -> trim(label("Build script", optional(text()))),
68+
"buildType" -> trim(label("Build type", optionalRequiredIfChecked("enableBuild", text()))),
69+
"buildScript" -> trim(label("Build script", optionalRequired(_("buildType") == Seq("script"), text()))),
70+
"buildFile" -> trim(label("Build file", optionalRequired(_("buildType") == Seq("file"), text()))),
6771
"notification" -> trim(label("Notification", boolean())),
6872
"skipWords" -> trim(label("Skip words", optional(text()))),
6973
"runWords" -> trim(label("Run words", optional(text())))
@@ -293,12 +297,17 @@ class CIController extends ControllerBase
293297

294298
post("/:owner/:repository/settings/build", buildConfigForm)(ownerOnly { (form, repository) =>
295299
if(form.enableBuild){
296-
// TODO buildScript is required!!
300+
val buildType = form.buildType.getOrElse("script")
297301
saveCIConfig(repository.owner, repository.name, Some(
298302
CIConfig(
299303
repository.owner,
300304
repository.name,
301-
form.buildScript.getOrElse(""),
305+
buildType,
306+
(buildType match {
307+
case "script" => form.buildScript.getOrElse("")
308+
case "file" => form.buildFile.getOrElse("")
309+
case _ => ""
310+
}),
302311
form.notification,
303312
form.skipWords,
304313
form.runWords

src/main/scala/io/github/gitbucket/ci/manager/BuildJobThread.scala

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class BuildJobThread(queue: LinkedBlockingQueue[BuildJob], threads: LinkedBlocki
112112
}
113113

114114
// run script
115-
val command = prepareBuildScript(buildDir, job.config.buildScript)
115+
val command = prepareBuildScript(buildDir, job.config.buildType, job.config.buildScript)
116116
val process = Process(command, dir,
117117
"CI" -> "true",
118118
"HOME" -> buildDir.getAbsolutePath,
@@ -256,17 +256,29 @@ class BuildJobThread(queue: LinkedBlockingQueue[BuildJob], threads: LinkedBlocki
256256
runningProcess.get.foreach(_.destroy())
257257
}
258258

259-
private def prepareBuildScript(buildDir: File, buildScript: String): String = {
260-
if(CIUtils.isWindows){
261-
val buildFile = new File(buildDir, "build.bat")
262-
FileUtils.write(buildFile, buildScript, "UTF-8")
263-
buildFile.setExecutable(true)
264-
buildFile.getAbsolutePath
265-
} else {
266-
val buildFile = new File(buildDir, "build.sh")
267-
FileUtils.write(buildFile, "#!/bin/sh\n" + buildScript.replaceAll("\r\n", "\n"), "UTF-8")
268-
buildFile.setExecutable(true)
269-
"../build.sh"
259+
private def prepareBuildScript(buildDir: File, buildType: String, buildScript: String): String = {
260+
buildType match {
261+
case "script" =>
262+
if(CIUtils.isWindows){
263+
val buildFile = new File(buildDir, "build.bat")
264+
FileUtils.write(buildFile, buildScript, "UTF-8")
265+
buildFile.setExecutable(true)
266+
buildFile.getAbsolutePath
267+
} else {
268+
val buildFile = new File(buildDir, "build.sh")
269+
FileUtils.write(buildFile, "#!/bin/sh\n" + buildScript.replaceAll("\r\n", "\n"), "UTF-8")
270+
buildFile.setExecutable(true)
271+
"../build.sh"
272+
}
273+
case "file" =>
274+
if(CIUtils.isWindows){
275+
val dir = new File(buildDir, "workspace")
276+
new File(dir, buildScript).getAbsolutePath
277+
} else {
278+
"./" + buildScript
279+
}
280+
case _ =>
281+
throw new MatchError("Invalid build type: " + buildType)
270282
}
271283
}
272284

src/main/scala/io/github/gitbucket/ci/model/CIConfig.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@ trait CIConfigComponent { self: gitbucket.core.model.Profile =>
99
class CIConfigs(tag: Tag) extends Table[CIConfig](tag, "CI_CONFIG") {
1010
val userName = column[String]("USER_NAME", O PrimaryKey)
1111
val repositoryName = column[String]("REPOSITORY_NAME")
12+
val buildType = column[String]("BUILD_TYPE")
1213
val buildScript = column[String]("BUILD_SCRIPT")
1314
val notification = column[Boolean]("NOTIFICATION")
1415
val skipWords = column[String]("SKIP_WORDS")
1516
val runWords = column[String]("RUN_WORDS")
16-
def * = (userName, repositoryName, buildScript, notification, skipWords.?, runWords.?) <> (CIConfig.tupled, CIConfig.unapply)
17+
def * = (userName, repositoryName, buildType, buildScript, notification, skipWords.?, runWords.?) <> (CIConfig.tupled, CIConfig.unapply)
1718
}
1819
}
1920

2021
case class CIConfig(
2122
userName: String,
2223
repositoryName: String,
24+
buildType: String,
2325
buildScript: String,
2426
notification: Boolean,
2527
skipWords: Option[String],

src/main/twirl/gitbucket/ci/config.scala.html

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
@gitbucket.core.html.menu("settings", repository){
77
@gitbucket.core.settings.html.menu("build", repository){
88
@gitbucket.core.helper.html.information(info)
9-
<form action="@url(repository)/settings/build" method="POST" validate="true">
9+
<form action="@url(repository)/settings/build" method="POST" validate="true" autocomplete="off">
1010
<div class="panel panel-default">
1111
<div class="panel-heading strong">Build</div>
1212
<div class="panel-body">
@@ -17,10 +17,25 @@
1717
</label>
1818
</fieldset>
1919
<div id="buildSettings">
20-
<label>Build script</label>
21-
<fieldset>
22-
<textarea id="buildScript" name="buildScript" class="form-control" style="height: 100px;">@config.map(_.buildScript)</textarea>
23-
</fieldset>
20+
<fielset class="form-group">
21+
<div>
22+
<span id="error-buildType" class="error"></span>
23+
</div>
24+
<label>
25+
<input type="radio" name="buildType" value="script" @if(config.exists(_.buildType == "script") || config.isEmpty){checked}>
26+
Build script
27+
<span class="normal muted">(Put arbitrary scripts to build this project)</span>
28+
</label>
29+
<textarea id="buildScript" name="buildScript" class="form-control" style="height: 100px;">@config.filter(_.buildType == "script").map(_.buildScript)</textarea>
30+
<span id="error-buildScript" class="error"></span>
31+
<label>
32+
<input type="radio" name="buildType" value="file" @if(config.exists(_.buildType == "file")){checked}>
33+
Build file
34+
<span class="normal muted">(Specify a script file in your repository to build this project)</span>
35+
</label>
36+
<input id="buildFile" name="buildFile" class="form-control" value="@config.filter(_.buildType == "file").map(_.buildScript)">
37+
<span id="error-buildFile" class="error"></span>
38+
</fielset>
2439
<fieldset class="form-group">
2540
<label class="checkbox" for="notification">
2641
<input type="checkbox" id="notification" name="notification"@if(config.exists(_.notification)){ checked}/>
@@ -50,8 +65,29 @@
5065
}
5166
<script>
5267
$(function(){
53-
$('#enableBuild').change(function(){
54-
$('#buildSettings textarea, #buildSettings input').prop('disabled', !$(this).prop('checked'));
55-
}).change();
68+
$('#enableBuild').change(updateStatus);
69+
$('input[name=buildType]').change(updateStatus);
70+
71+
updateStatus();
5672
});
73+
74+
function updateStatus(){
75+
console.log($('#enableBuild').prop('checked'));
76+
if($('#enableBuild').prop('checked')){
77+
// Enables radio buttons
78+
$('#buildSettings input[type=radio]').prop('disabled', false);
79+
// Once disables both fields
80+
$('textarea[name=buildScript], input[name=buildFile]').prop('disabled', true);
81+
// Enables a field if corresponding radio button has been checked
82+
var buildType = $('input[name=buildType]:checked').val();
83+
if(buildType == 'script'){
84+
$('textarea[name=buildScript]').prop('disabled', false);
85+
} else if(buildType == 'file'){
86+
$('input[name=buildFile]').prop('disabled', false);
87+
}
88+
} else {
89+
// Disables all fields
90+
$('#buildSettings textarea, #buildSettings input').prop('disabled', true);
91+
}
92+
}
5793
</script>

0 commit comments

Comments
 (0)