├── .github
└── workflows
│ └── build.yml
├── .gitignore
├── LICENSE
├── README.md
├── build.sbt
├── gitbucket-ci-plugin_output.png
├── gitbucket-ci-plugin_results.png
├── project
├── build.properties
└── plugins.sbt
└── src
├── main
├── resources
│ ├── gitbucket
│ │ └── ci
│ │ │ └── assets
│ │ │ └── vue.js
│ └── update
│ │ ├── gitbucket-ci_1.0.0.xml
│ │ ├── gitbucket-ci_1.1.0.xml
│ │ ├── gitbucket-ci_1.4.0.xml
│ │ ├── gitbucket-ci_1.5.0.xml
│ │ ├── gitbucket-ci_1.6.0.xml
│ │ └── gitbucket-ci_1.8.0.xml
├── scala
│ ├── Plugin.scala
│ └── io
│ │ └── github
│ │ └── gitbucket
│ │ └── ci
│ │ ├── api
│ │ ├── CIApiBuild.scala
│ │ ├── CIApiSingleBuild.scala
│ │ └── JsonFormat.scala
│ │ ├── controller
│ │ ├── CIApiController.scala
│ │ └── CIController.scala
│ │ ├── hook
│ │ ├── CICommitHook.scala
│ │ ├── CIPullRequestHook.scala
│ │ └── CIRepositoryHook.scala
│ │ ├── manager
│ │ ├── BuildJobThread.scala
│ │ └── BuildManager.scala
│ │ ├── model
│ │ ├── CIConfig.scala
│ │ ├── CIProfile.scala
│ │ ├── CIResult.scala
│ │ └── CISystemConfig.scala
│ │ ├── service
│ │ └── CIService.scala
│ │ └── util
│ │ ├── CIUtils.scala
│ │ └── JobStatus.scala
└── twirl
│ └── gitbucket
│ └── ci
│ ├── config.scala.html
│ ├── guide.scala.html
│ ├── output.scala.html
│ ├── results.scala.html
│ ├── system.scala.html
│ └── workspace.scala.html
└── test
└── scala
└── io
└── github
└── gitbucket
└── ci
└── MigrationSpec.scala
/.github/workflows/build.yml:
--------------------------------------------------------------------------------
1 | name: build
2 |
3 | on: [push, pull_request]
4 |
5 | jobs:
6 | build:
7 | runs-on: ubuntu-latest
8 | strategy:
9 | matrix:
10 | java: [8, 11]
11 | steps:
12 | - uses: actions/checkout@v2
13 | - name: Cache
14 | uses: actions/cache@v2
15 | env:
16 | cache-name: cache-sbt-libs
17 | with:
18 | path: |
19 | ~/.ivy2/cache
20 | ~/.sbt
21 | ~/.coursier
22 | key: build-${{ env.cache-name }}-${{ hashFiles('build.sbt') }}
23 | - name: Set up JDK
24 | uses: actions/setup-java@v1
25 | with:
26 | java-version: ${{ matrix.java }}
27 | - name: Run tests
28 | run: |
29 | git clone https://github.com/gitbucket/gitbucket.git
30 | cd gitbucket
31 | sbt publishLocal
32 | cd ../
33 | sbt test
34 | - name: Assembly
35 | run: sbt assembly
36 | - name: Upload artifacts
37 | uses: actions/upload-artifact@v2
38 | with:
39 | name: gitbucket-gist-plugin-java${{ matrix.java }}-${{ github.sha }}
40 | path: ./target/scala-2.13/*.jar
41 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.class
2 | *.log
3 |
4 | # sbt specific
5 | dist/*
6 | target/
7 | lib_managed/
8 | src_managed/
9 | project/boot/
10 | project/plugins/project/
11 |
12 | # Scala-IDE specific
13 | .scala_dependencies
14 | .classpath
15 | .project
16 | .cache
17 | .settings
18 |
19 | # IntelliJ specific
20 | .idea/
21 | .idea_modules/
22 |
23 | # Ensime
24 | .ensime
25 | .ensime_cache/
26 |
27 | # Metals
28 | .bloop/
29 | .metals/
30 | .vscode/
31 | **/metals.sbt
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | gitbucket-ci-plugin [](https://github.com/takezoe/gitbucket-ci-plugin/actions?query=workflow%3Abuild+branch%3Amaster)
2 | ========
3 | GitBucket plug-in that adds simple CI ability to GitBucket.
4 |
5 | 
6 |
7 | 
8 |
9 | This plug-in allows repository owners to configure build command, and run them at following timing:
10 |
11 | - Push commits to the default branch
12 | - Create a new pull request
13 | - Push additional commits to the pull request branch
14 |
15 | ### Skip and re-run by keywords
16 |
17 | You can skip a build by including specific words in the commit message. Moreover you can also re-run the pull request build by adding a comment including specific words. These words can be set at the build settings.
18 |
19 | ### Variables in build script
20 |
21 | In the build script, following environment variables are available:
22 |
23 | - `CI` (true)
24 | - `HOME` (root of the build directory)
25 | - `CI_BUILD_DIR` (same as HOME)
26 | - `CI_BUILD_NUMBER`
27 | - `CI_BUILD_BRANCH`
28 | - `CI_COMMIT_ID`
29 | - `CI_COMMIT_MESSAGE`
30 | - `CI_REPO_SLUG` ("owner/repo")
31 | - `CI_PULL_REQUEST` (pull request id or "false")
32 | - `CI_PULL_REQUEST_SLUG` ("owner/repo" or "")
33 |
34 | ### Web API
35 |
36 | This plugin has [CircleCI API v1.1](https://circleci.com/docs/api/v1-reference/) compatible Web API. Supported APIs are below:
37 |
38 | - User (`GET /api/circleci/v1.1/me`)
39 | - Recent Builds For a Single Project (`GET /api/circleci/v1.1/project/gitbucket/:owner/:repository`)
40 | - Recent Builds For a Project Branch (`GET /api/circleci/v1.1/project/gitbucket/:owner/:repository/tree/:branch`)
41 | - Single Build (`GET /api/circleci/v1.1/project/gitbucket/:owner/:repository/:buildNum`)
42 | - Retry a Build (`POST /api/circleci/v1.1/project/gitbucket/:owner/:repository/:buildNum/retry`)
43 | - Cancel a Build (`POST /api/circleci/v1.1/project/gitbucket/:owner/:repository/:buildNum/cancel`)
44 | - Trigger a new Build (`POST /api/circleci/v1.1/project/gitbucket/:owner/:repository`)
45 | - Trigger a new Build with a Branch (`POST /api/circleci/v1.1/project/gitbucket/:owner/:repository/tree/:branch`)
46 |
47 | While CircleCI API takes the token via query string, in this plugin, `Authorization` header (application token or basic authentication) is available as same as other [GitBucket API](https://github.com/gitbucket/gitbucket/wiki/API-WebHook).
48 |
49 | ### Cautions
50 |
51 | Note that you must not use this plug-in in public environment because it allows executing any commands on a GitBucket instance. It will be **a serious security hole**.
52 |
53 | In addition, this plug-in is made to just experiment continuous integration on GitBucket easily without complex settings of webhook or Jenkins. It doesn't have flexibility and scalability, and also has a security issue which is mentioned above. Therefore, if you like it and would like to use for your project actually, we recommend to setup Jenkins or other CI tool and move to it.
54 |
55 | ## Compatibility
56 |
57 | Plugin version | GitBucket version
58 | :--------------|:--------------------
59 | 1.11.x | 4.35.x -
60 | 1.10.x | 4.34.x -
61 | 1.9.x | 4.32.x -
62 | 1.8.x | 4.31.x -
63 | 1.7.x | 4.30.x -
64 | 1.6.x - | 4.24.0 -
65 | 1.5.x - | 4.23.1 -
66 | 1.4.x - | 4.23.0
67 | 1.3.x - | 4.19.x -
68 | 1.0.x - 1.2.x | 4.17.x, 4.18.x
69 |
70 | ## Installation
71 |
72 | Download jar file from [the release page](https://github.com/takezoe/gitbucket-ci-plugin/releases) and put into `GITBUCKET_HOME/plugins`.
73 |
74 | ## Build
75 |
76 | Run `sbt assembly` and copy generated `/target/scala-2.13/gitbucket-ci-plugin-x.x.x.jar` to `~/.gitbucket/plugins/` (If the directory does not exist, create it by hand before copying the jar), or just run `sbt install`.
77 |
78 | ## Release Notes
79 |
80 | ### 1.11.0
81 | - Update for GitBucket 4.35.0 compatibility
82 |
83 | ### 1.10.0
84 | - Update for GitBucket 4.34.0 compatibility
85 |
86 | ### 1.9.1
87 | - Run build after a pull request is merged
88 |
89 | ### 1.9.0
90 | - Update for GitBucket 4.32.0 and Scala 2.13.0
91 |
92 | ### 1.8.1
93 | - Bug fix
94 |
95 | ### 1.8.0
96 | - Docker support
97 |
98 | ### 1.7.0
99 | - Update for GitBucket 4.30.x
100 |
101 | ### 1.6.0
102 | - Some CircleCI compatible Web API
103 |
104 | ### 1.5.0
105 |
106 | - Build branches even other than the default branch
107 | - Support the use of an arbitrary file in the git repository as a build script
108 |
109 | ### 1.4.0
110 |
111 | - Max parallel builds and max stored history became configurable
112 |
113 | ### 1.3.0
114 |
115 | - Update for Scalatra 2.6
116 | - Fix skipping pull request build bug
117 |
118 | ### 1.2.0
119 |
120 | - Build workspace browser
121 | - Altered build directories location
122 |
123 | ### 1.1.0
124 |
125 | - Skip build by commit message
126 | - Re-run build by pull request comment
127 | - Supply environment variables in build script
128 |
129 | ### 1.0.1
130 |
131 | - Build status badge
132 | - Fix pull request build bug
133 |
134 | ### 1.0.0
135 |
136 | - First release
137 |
--------------------------------------------------------------------------------
/build.sbt:
--------------------------------------------------------------------------------
1 | name := "gitbucket-ci-plugin"
2 | organization := "io.github.gitbucket"
3 | version := "1.11.0"
4 | scalaVersion := "2.13.7"
5 | gitbucketVersion := "4.36.2"
6 | scalacOptions += "-deprecation"
7 | libraryDependencies ++= Seq(
8 | "org.fusesource.jansi" % "jansi" % "1.18",
9 | "org.scalatest" %% "scalatest" % "3.0.8" % "test",
10 | "com.dimafeng" %% "testcontainers-scala" % "0.38.7" % "test",
11 | "org.testcontainers" % "mysql" % "1.15.1" % "test",
12 | "org.testcontainers" % "postgresql" % "1.15.1" % "test"
13 | )
14 |
15 |
--------------------------------------------------------------------------------
/gitbucket-ci-plugin_output.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/takezoe/gitbucket-ci-plugin/dc51537382b8256af6acd5b7b9c0f2e146693e57/gitbucket-ci-plugin_output.png
--------------------------------------------------------------------------------
/gitbucket-ci-plugin_results.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/takezoe/gitbucket-ci-plugin/dc51537382b8256af6acd5b7b9c0f2e146693e57/gitbucket-ci-plugin_results.png
--------------------------------------------------------------------------------
/project/build.properties:
--------------------------------------------------------------------------------
1 | sbt.version = 1.5.6
2 |
--------------------------------------------------------------------------------
/project/plugins.sbt:
--------------------------------------------------------------------------------
1 | addSbtPlugin("io.github.gitbucket" % "sbt-gitbucket-plugin" % "1.5.1")
2 |
--------------------------------------------------------------------------------
/src/main/resources/update/gitbucket-ci_1.0.0.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/src/main/resources/update/gitbucket-ci_1.1.0.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/src/main/resources/update/gitbucket-ci_1.4.0.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/src/main/resources/update/gitbucket-ci_1.5.0.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/src/main/resources/update/gitbucket-ci_1.6.0.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 | UPDATE CI_RESULT SET QUEUED_TIME = START_TIME, BUILD_SCRIPT = ''
10 |
11 |
12 | UPDATE CI_RESULT SET EXIT_CODE = 0 WHERE STATUS = 'success'
13 |
14 |
15 | UPDATE CI_RESULT SET EXIT_CODE = -1 WHERE STATUS = 'failure'
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/src/main/resources/update/gitbucket-ci_1.8.0.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/src/main/scala/Plugin.scala:
--------------------------------------------------------------------------------
1 | import java.util
2 | import javax.servlet.ServletContext
3 |
4 | import gitbucket.core.controller.Context
5 | import gitbucket.core.plugin._
6 | import gitbucket.core.service.RepositoryService.RepositoryInfo
7 | import gitbucket.core.service.{AccountService, RepositoryService, SystemSettingsService}
8 | import gitbucket.core.util.Directory
9 | import gitbucket.core.model.Profile.profile.blockingApi._
10 | import io.github.gitbucket.ci.controller.{CIApiController, CIController}
11 | import io.github.gitbucket.ci.hook.{CICommitHook, CIPullRequestHook, CIRepositoryHook}
12 | import io.github.gitbucket.ci.manager.BuildManager
13 | import io.github.gitbucket.solidbase.migration.LiquibaseMigration
14 | import io.github.gitbucket.solidbase.model.Version
15 | import java.io.File
16 |
17 | import gitbucket.core.service.SystemSettingsService.SystemSettings
18 | import gitbucket.core.servlet.Database
19 | import io.github.gitbucket.ci.service.CIService
20 | import org.apache.commons.io.FileUtils
21 |
22 | class Plugin extends gitbucket.core.plugin.Plugin with CIService with AccountService with RepositoryService {
23 |
24 | override val pluginId: String = "ci"
25 |
26 | override val pluginName: String = "CI Plugin"
27 |
28 | override val description: String = "This plugin adds simple CI functionality to GitBucket."
29 |
30 | override val versions: List[Version] = List(
31 | new Version("1.0.0",
32 | new LiquibaseMigration("update/gitbucket-ci_1.0.0.xml")),
33 | new Version("1.0.1"),
34 | new Version("1.1.0",
35 | new LiquibaseMigration("update/gitbucket-ci_1.1.0.xml")),
36 | new Version("1.2.0", (moduleId: String, version: String, context: util.Map[String, AnyRef]) => {
37 | // Move repositories/USER/REPO.git/build to repositories/USER/REPO/build
38 | for {
39 | userDir <- {
40 | val dir = new File(Directory.RepositoryHome)
41 | if(dir.exists && dir.isDirectory) dir.listFiles(_.isDirectory).toSeq else Nil
42 | }
43 | repositoryDir <- userDir.listFiles(_.getName.endsWith(".git"))
44 | buildDir <- Seq(new File(repositoryDir, "build")).filter(f => f.exists && f.isDirectory)
45 | } yield {
46 | val userName = userDir.getName
47 | val repositoryName = repositoryDir.getName.replaceFirst("\\.git", "")
48 | val newBuildDir = new java.io.File(Directory.getRepositoryFilesDir(userName, repositoryName), "build")
49 | FileUtils.moveDirectory(buildDir, newBuildDir)
50 | }
51 | }),
52 | new Version("1.2.1"),
53 | new Version("1.3.0"),
54 | new Version("1.4.0",
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")),
58 | new Version("1.6.0",
59 | new LiquibaseMigration("update/gitbucket-ci_1.6.0.xml")),
60 | new Version("1.6.1"),
61 | new Version("1.6.2"),
62 | new Version("1.6.3"),
63 | new Version("1.6.4"),
64 | new Version("1.7.0"),
65 | new Version("1.8.0",
66 | new LiquibaseMigration("update/gitbucket-ci_1.8.0.xml")),
67 | new Version("1.8.1"),
68 | new Version("1.9.0"),
69 | new Version("1.10.0"),
70 | new Version("1.11.0")
71 | )
72 |
73 | override val assetsMappings = Seq("/ci" -> "/gitbucket/ci/assets")
74 | override val controllers = Seq("/*" -> new CIController(), "/api/circleci/v1.1/*" -> new CIApiController())
75 |
76 | override val repositoryMenus = Seq(
77 | (repository: RepositoryInfo, context: Context) => Some(Link("build", "Build", "/build", Some("sync")))
78 | )
79 |
80 | override val repositorySettingTabs = Seq(
81 | (repository: RepositoryInfo, context: Context) => Some(Link("build", "Build", "settings/build"))
82 | )
83 |
84 | override val systemSettingMenus: Seq[(Context) => Option[Link]] = Seq(
85 | (ctx: Context) => Some(Link("build", "Build", "admin/build", Some("gear")))
86 | )
87 |
88 | override val receiveHooks: Seq[ReceiveHook] = Seq(new CICommitHook())
89 | override val repositoryHooks: Seq[RepositoryHook] = Seq(new CIRepositoryHook())
90 | override val pullRequestHooks: Seq[PullRequestHook] = Seq(new CIPullRequestHook())
91 |
92 | override def initialize(registry: PluginRegistry, context: ServletContext, settings: SystemSettings): Unit = {
93 | super.initialize(registry, context, settings)
94 |
95 | Database() withTransaction { implicit session =>
96 | BuildManager.setMaxParallelBuilds(loadCISystemConfig().maxParallelBuilds)
97 | }
98 | }
99 |
100 |
101 | override def shutdown(registry: PluginRegistry, context: ServletContext,
102 | settings: SystemSettingsService.SystemSettings): Unit = {
103 | BuildManager.shutdownBuildManager()
104 | }
105 | }
106 |
--------------------------------------------------------------------------------
/src/main/scala/io/github/gitbucket/ci/api/CIApiBuild.scala:
--------------------------------------------------------------------------------
1 | package io.github.gitbucket.ci.api
2 |
3 | import java.util.Date
4 |
5 | import gitbucket.core.api.ApiPath
6 | import io.github.gitbucket.ci.model.CIResult
7 | import io.github.gitbucket.ci.service.BuildJob
8 | import io.github.gitbucket.ci.util.JobStatus
9 |
10 | case class CIApiBuild(
11 | vcs_url: ApiPath,
12 | build_url: ApiPath,
13 | build_num: Long,
14 | branch: String,
15 | vcs_revision: String,
16 | committer_name: String,
17 | committer_email: String,
18 | subject: String,
19 | body: String,
20 | why: String,
21 | dont_build: Option[String],
22 | queued_at: Date,
23 | start_time: Option[Date],
24 | stop_time: Option[Date],
25 | build_time_millis: Option[Long],
26 | username: String,
27 | reponame: String,
28 | lifecycle: String,
29 | outcome: Option[String],
30 | status: String,
31 | retry_of: Option[String],
32 | previous: Option[CIApiPreviousBuild]
33 | )
34 |
35 | object CIApiBuild {
36 | def apply(job: BuildJob): CIApiBuild = {
37 | CIApiBuild(
38 | vcs_url = ApiPath(s"/git/${job.userName}/${job.repositoryName}"),
39 | build_url = ApiPath(s"/${job.userName}/${job.repositoryName}/build/${job.buildNumber}"),
40 | build_num = job.buildNumber,
41 | branch = job.buildBranch,
42 | vcs_revision = job.sha,
43 | committer_name = job.commitUserName,
44 | committer_email = job.commitMailAddress,
45 | subject = job.commitMessage,
46 | body = "",
47 | why = "gitbucket",
48 | dont_build = None,
49 | queued_at = job.queuedTime,
50 | start_time = job.startTime,
51 | stop_time = None,
52 | build_time_millis = None,
53 | username = job.userName,
54 | reponame = job.repositoryName,
55 | lifecycle = "running",
56 | outcome = None,
57 | status = JobStatus.Running,
58 | retry_of = None,
59 | previous = None
60 | )
61 | }
62 |
63 | def apply(result: CIResult): CIApiBuild = {
64 | CIApiBuild(
65 | vcs_url = ApiPath(s"/git/${result.userName}/${result.repositoryName}"),
66 | build_url = ApiPath(s"/${result.userName}/${result.repositoryName}/build/${result.buildNumber}"),
67 | build_num = result.buildNumber,
68 | branch = result.buildBranch,
69 | vcs_revision = result.sha,
70 | committer_name = result.commitUserName,
71 | committer_email = result.commitMailAddress,
72 | subject = result.commitMessage,
73 | body = "",
74 | why = "gitbucket",
75 | dont_build = None,
76 | queued_at = result.queuedTime,
77 | start_time = Some(result.startTime),
78 | stop_time = Some( result.endTime),
79 | build_time_millis = Some(result.endTime.getTime - result.startTime.getTime),
80 | username = result.userName,
81 | reponame = result.repositoryName,
82 | lifecycle = "finished",
83 | outcome = Some(result.apiStatus),
84 | status = result.apiStatus,
85 | retry_of = None,
86 | previous = None
87 | )
88 | }
89 | }
90 |
91 | case class CIApiPreviousBuild(
92 | status: String,
93 | build_num: Long
94 | )
--------------------------------------------------------------------------------
/src/main/scala/io/github/gitbucket/ci/api/CIApiSingleBuild.scala:
--------------------------------------------------------------------------------
1 | package io.github.gitbucket.ci.api
2 |
3 | import java.util.Date
4 |
5 | import gitbucket.core.api.ApiPath
6 | import io.github.gitbucket.ci.model.CIResult
7 | import io.github.gitbucket.ci.util.JobStatus
8 |
9 | case class CIApiSingleBuild(
10 | vcs_url: ApiPath,
11 | build_url: ApiPath,
12 | build_num: Long,
13 | branch: String,
14 | vcs_revision: String,
15 | committer_name: String,
16 | committer_email: String,
17 | subject: String,
18 | body: String,
19 | why: String,
20 | dont_build: Option[String],
21 | queued_at: Date,
22 | start_time: Option[Date],
23 | stop_time: Option[Date],
24 | build_time_millis: Option[Long],
25 | username: String,
26 | reponame: String,
27 | lifecycle: String,
28 | outcome: Option[String],
29 | status: String,
30 | retry_of: Option[String],
31 | steps: Seq[CIApiSingleBuildStep]
32 | )
33 |
34 | object CIApiSingleBuild {
35 | def apply(result: CIResult): CIApiSingleBuild = {
36 | CIApiSingleBuild(
37 | vcs_url = ApiPath(s"/git/${result.userName}/${result.repositoryName}"),
38 | build_url = ApiPath(s"/${result.userName}/${result.repositoryName}/build/${result.buildNumber}"),
39 | build_num = result.buildNumber,
40 | branch = result.buildBranch,
41 | vcs_revision = result.sha,
42 | committer_name = result.commitUserName,
43 | committer_email = result.commitMailAddress,
44 | subject = result.commitMessage,
45 | body = "",
46 | why = "gitbucket",
47 | dont_build = None,
48 | queued_at = result.queuedTime,
49 | start_time = Some(result.startTime),
50 | stop_time = Some( result.endTime),
51 | build_time_millis = Some(result.endTime.getTime - result.startTime.getTime),
52 | username = result.userName,
53 | reponame = result.repositoryName,
54 | lifecycle = "finished",
55 | outcome = Some(result.apiStatus),
56 | status = result.apiStatus,
57 | retry_of = None,
58 | steps = Seq(
59 | CIApiSingleBuildStep(
60 | name = "build",
61 | actions = Seq(
62 | CIApiSingleBuildStepAction(
63 | bash_command = result.buildScript,
64 | run_time_millis = result.endTime.getTime - result.startTime.getTime,
65 | start_time = result.startTime,
66 | messages = Nil,
67 | step = 1,
68 | exit_code = result.exitCode,
69 | end_time = result.endTime,
70 | index = 0,
71 | status = result.apiStatus,
72 | `type` = "build",
73 | failed = if(result.status == JobStatus.Failure) Some(true) else None
74 | )
75 | )
76 | )
77 | )
78 | )
79 | }
80 | }
81 |
82 | case class CIApiSingleBuildStep(
83 | name: String,
84 | actions: Seq[CIApiSingleBuildStepAction]
85 | )
86 |
87 | case class CIApiSingleBuildStepAction(
88 | bash_command: String,
89 | run_time_millis: Long,
90 | start_time: Date,
91 | messages: Seq[String],
92 | step: Int,
93 | exit_code: Int,
94 | end_time: Date,
95 | index: Int,
96 | status: String,
97 | `type`: String,
98 | failed: Option[Boolean]
99 | )
--------------------------------------------------------------------------------
/src/main/scala/io/github/gitbucket/ci/api/JsonFormat.scala:
--------------------------------------------------------------------------------
1 | package io.github.gitbucket.ci.api
2 |
3 | import java.time._
4 | import java.util.Date
5 |
6 | import gitbucket.core.api.JsonFormat._
7 |
8 | import scala.util.Try
9 | import org.json4s._
10 | import org.json4s.jackson.Serialization
11 |
12 |
13 | object JsonFormat {
14 |
15 | val jsonFormats = Serialization.formats(NoTypeHints).preservingEmptyValues +
16 | // TODO This serializer should define in core JsonFormat
17 | new CustomSerializer[Date](format =>
18 | ({ case JString(s) =>
19 | Try(Date.from(Instant.parse(s))).getOrElse(throw new MappingException("Can't convert " + s + " to Date"))
20 | },
21 | { case x: Date =>
22 | JString(OffsetDateTime.ofInstant(x.toInstant, ZoneId.of("UTC")).format(parserISO))
23 | })
24 | )
25 |
26 | /**
27 | * convert object to json string
28 | */
29 | def apply(obj: AnyRef)(implicit c: Context): String =
30 | Serialization.write(obj)(jsonFormats + apiPathSerializer(c) + sshPathSerializer(c))
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/src/main/scala/io/github/gitbucket/ci/controller/CIApiController.scala:
--------------------------------------------------------------------------------
1 | package io.github.gitbucket.ci.controller
2 |
3 | import gitbucket.core.util.Implicits._
4 | import gitbucket.core.controller.ControllerBase
5 | import gitbucket.core.service.{AccountService, RepositoryService}
6 | import gitbucket.core.util.Directory.getRepositoryDir
7 | import gitbucket.core.util._
8 | import io.github.gitbucket.ci.api.{CIApiBuild, CIApiPreviousBuild, CIApiSingleBuild, JsonFormat}
9 | import io.github.gitbucket.ci.service.CIService
10 | import org.eclipse.jgit.api.Git
11 | import org.scalatra.{BadRequest, Ok}
12 | import scala.util.Using
13 |
14 | class CIApiController extends ControllerBase
15 | with UsersAuthenticator
16 | with ReferrerAuthenticator
17 | with WritableUsersAuthenticator
18 | with AccountService
19 | with RepositoryService
20 | with CIService {
21 |
22 | before("/api/circleci/v1.1/*"){
23 | contentType = formats("json")
24 | request.setAttribute(Keys.Request.APIv3, true)
25 | }
26 |
27 | get("/api/circleci/v1.1/*"){
28 | NotFound()
29 | }
30 |
31 | get("/api/circleci/v1.1/me")(usersOnly {
32 | JsonFormat(Map(
33 | "login" -> context.loginAccount.get.userName,
34 | "basic_email_prefs" -> "smart"
35 | ))
36 | })
37 |
38 | get("/api/circleci/v1.1/project/gitbucket/:owner/:repository")(referrersOnly { repository =>
39 | JsonFormat(getBuilds(repository.owner, repository.name))
40 | })
41 |
42 | get("/api/circleci/v1.1/project/gitbucket/:owner/:repository/tree/:branch")(referrersOnly { repository =>
43 | val branch = params("branch")
44 | if(repository.branchList.contains(branch)){
45 | JsonFormat(getBuilds(repository.owner, repository.name).filter(_.branch == params("branch")))
46 | } else NotFound()
47 | })
48 |
49 | get("/api/circleci/v1.1/project/gitbucket/:owner/:repository/:build_num")(referrersOnly { repository =>
50 | val buildNumber = params("build_num").toInt
51 | getCIResult(repository.owner, repository.name, buildNumber).map { result =>
52 | JsonFormat(CIApiSingleBuild(result))
53 | } getOrElse NotFound()
54 | })
55 |
56 | post("/api/circleci/v1.1/project/gitbucket/:owner/:repository/:build_num/retry")(writableUsersOnly { repository =>
57 | val buildNumber = params("build_num").toInt
58 | loadCIConfig(repository.owner, repository.name).flatMap { config =>
59 | getCIResult(repository.owner, repository.name, buildNumber).map { result =>
60 | runBuild(
61 | userName = result.userName,
62 | repositoryName = result.repositoryName,
63 | buildUserName = result.buildUserName,
64 | buildRepositoryName = result.buildRepositoryName,
65 | buildBranch = result.buildBranch,
66 | sha = result.sha,
67 | commitMessage = result.commitMessage,
68 | commitUserName = result.commitUserName,
69 | commitMailAddress = result.commitMailAddress,
70 | pullRequestId = result.pullRequestId,
71 | buildAuthor = context.loginAccount.get,
72 | config = config
73 | )
74 | Ok()
75 | }
76 | } getOrElse BadRequest()
77 | })
78 |
79 | post("/api/circleci/v1.1/project/gitbucket/:owner/:repository/:build_num/cancel")(writableUsersOnly { repository =>
80 | val buildNumber = params("buildNumber").toInt
81 | cancelBuild(repository.owner, repository.name, buildNumber)
82 | Ok()
83 | })
84 |
85 | post("/api/circleci/v1.1/project/gitbucket/:owner/:repository")(writableUsersOnly { repository =>
86 | loadCIConfig(repository.owner, repository.name).map { config =>
87 | Using.resource(Git.open(getRepositoryDir(repository.owner, repository.name))) { git =>
88 | JGitUtil.getDefaultBranch(git, repository).map { case (objectId, revision) =>
89 | val revCommit = JGitUtil.getRevCommitFromId(git, objectId)
90 | runBuild(
91 | userName = repository.owner,
92 | repositoryName = repository.name,
93 | buildUserName = repository.owner,
94 | buildRepositoryName = repository.name,
95 | buildBranch = revision,
96 | sha = objectId.name,
97 | commitMessage = revCommit.getShortMessage,
98 | commitUserName = revCommit.getCommitterIdent.getName,
99 | commitMailAddress = revCommit.getCommitterIdent.getEmailAddress,
100 | pullRequestId = None,
101 | buildAuthor = context.loginAccount.get,
102 | config = config
103 | )
104 | }
105 | }
106 | Ok()
107 | } getOrElse BadRequest()
108 | })
109 |
110 | post("/api/circleci/v1.1/project/gitbucket/:owner/:repository/tree/:branch")(writableUsersOnly { repository =>
111 | val branch = params("branch")
112 | loadCIConfig(repository.owner, repository.name).map { config =>
113 | Using.resource(Git.open(getRepositoryDir(repository.owner, repository.name))) { git =>
114 | val objectId = git.getRepository.resolve(branch)
115 | val revCommit = JGitUtil.getRevCommitFromId(git, objectId)
116 | runBuild(
117 | userName = repository.owner,
118 | repositoryName = repository.name,
119 | buildUserName = repository.owner,
120 | buildRepositoryName = repository.name,
121 | buildBranch = branch,
122 | sha = objectId.name,
123 | commitMessage = revCommit.getShortMessage,
124 | commitUserName = revCommit.getCommitterIdent.getName,
125 | commitMailAddress = revCommit.getCommitterIdent.getEmailAddress,
126 | pullRequestId = None,
127 | buildAuthor = context.loginAccount.get,
128 | config = config
129 | )
130 | }
131 | Ok()
132 | } getOrElse BadRequest()
133 | })
134 |
135 | private def getBuilds(owner: String, repository: String): Seq[CIApiBuild] = {
136 | val queuedJobs = getQueuedJobs(owner, repository).map { job => CIApiBuild(job) }
137 | val runningJobs = getRunningJobs(owner, repository).map { case (job, _) => CIApiBuild(job) }
138 | val buildResults = getCIResults(owner, repository).map { result => CIApiBuild(result) }
139 | val builds = (queuedJobs ++ runningJobs ++ buildResults).sortBy(_.build_num * -1)
140 |
141 | // Fill previous property
142 | builds.zipWithIndex.map { case (result, i) =>
143 | if(i < builds.size - 1){
144 | val previous = builds(i + 1)
145 | result.copy(previous = Some(CIApiPreviousBuild(
146 | status = previous.status,
147 | build_num = previous.build_num
148 | )))
149 | } else {
150 | result
151 | }
152 | }
153 | }
154 |
155 | }
156 |
--------------------------------------------------------------------------------
/src/main/scala/io/github/gitbucket/ci/controller/CIController.scala:
--------------------------------------------------------------------------------
1 | package io.github.gitbucket.ci.controller
2 |
3 | import java.io.FileInputStream
4 |
5 | import gitbucket.core.controller.ControllerBase
6 | import gitbucket.core.service.RepositoryService.RepositoryInfo
7 | import gitbucket.core.service.{AccountService, RepositoryService}
8 | import gitbucket.core.util.Directory.getRepositoryDir
9 | import gitbucket.core.util._
10 | import gitbucket.core.util.Implicits._
11 | import gitbucket.core.view.helpers.datetimeAgo
12 | import io.github.gitbucket.ci.manager.BuildManager
13 | import io.github.gitbucket.ci.model.{CIConfig, CISystemConfig}
14 | import io.github.gitbucket.ci.service.CIService
15 | import io.github.gitbucket.ci.util.{CIUtils, JobStatus}
16 | import org.scalatra.forms._
17 | import org.apache.commons.io.IOUtils
18 | import org.eclipse.jgit.api.Git
19 | import org.json4s.jackson.Serialization
20 | import org.scalatra.{BadRequest, Ok}
21 |
22 | import scala.util.Using
23 |
24 | object CIController {
25 |
26 | case class ApiJobOutput(
27 | status: String,
28 | output: String
29 | )
30 |
31 | case class ApiJobStatus(
32 | buildNumber: Int,
33 | status: String,
34 | target: String,
35 | targetUrl: String,
36 | sha: String,
37 | message: String,
38 | userName: String,
39 | committer: String,
40 | author: String,
41 | startTime: String,
42 | duration: String
43 | )
44 |
45 | case class BuildConfigForm(
46 | enableBuild: Boolean,
47 | buildType: Option[String],
48 | buildScript: Option[String],
49 | buildFile: Option[String],
50 | dockerfile: Option[String],
51 | composeFile: Option[String],
52 | notification: Boolean,
53 | skipWords: Option[String],
54 | runWords: Option[String]
55 | )
56 |
57 | case class CISystemConfigForm(
58 | maxBuildHistory: Int,
59 | maxParallelBuilds: Int,
60 | enableDocker: Boolean,
61 | dockerCommand: Option[String],
62 | enableDockerCompose: Boolean,
63 | dockerComposeCommand: Option[String]
64 | )
65 |
66 | }
67 |
68 | class CIController extends ControllerBase
69 | with CIService with AccountService with RepositoryService
70 | with ReferrerAuthenticator with WritableUsersAuthenticator with OwnerAuthenticator with AdminAuthenticator {
71 | import CIController._
72 |
73 | val buildConfigForm = mapping(
74 | "enableBuild" -> trim(label("Enable build", boolean())),
75 | "buildType" -> trim(label("Build type", optionalRequiredIfChecked("enableBuild", text()))),
76 | "buildScript" -> trim(label("Build script", optionalRequired(_("buildType") == Seq("script"), text()))),
77 | "buildFile" -> trim(label("Build file", optionalRequired(_("buildType") == Seq("file"), text()))),
78 | "dockerfile" -> trim(label("Dockerfile", optional(text()))),
79 | "composeFile" -> trim(label("docker-compose.yml", optional(text()))),
80 | "notification" -> trim(label("Notification", boolean())),
81 | "skipWords" -> trim(label("Skip words", optional(text()))),
82 | "runWords" -> trim(label("Run words", optional(text())))
83 | )(BuildConfigForm.apply)
84 |
85 | val ciSystemConfigForm = mapping(
86 | "maxBuildHistory" -> trim(label("Max build history", number())),
87 | "maxParallelBuilds" -> trim(label("Max parallel builds", number())),
88 | "enableDocker" -> trim(label("Enable docker", boolean())),
89 | "dockerCommand" -> trim(label("docker command", optional(text()))),
90 | "enableDockerCompose" -> trim(label("Enable docker-compse", boolean())),
91 | "dockerComposeCommand" -> trim(label("docker-compose command", optional(text())))
92 | )(CISystemConfigForm.apply)
93 |
94 | get("/:owner/:repository/build")(referrersOnly { repository =>
95 | if(loadCIConfig(repository.owner, repository.name).isDefined){
96 | gitbucket.ci.html.results(repository,
97 | hasDeveloperRole(repository.owner, repository.name, context.loginAccount),
98 | hasOwnerRole(repository.owner, repository.name, context.loginAccount))
99 | } else {
100 | gitbucket.ci.html.guide(repository,
101 | hasOwnerRole(repository.owner, repository.name, context.loginAccount))
102 | }
103 | })
104 |
105 | get("/:owner/:repository/build/:buildNumber")(referrersOnly { repository =>
106 | val buildNumber = params("buildNumber").toInt
107 |
108 | getRunningJobs(repository.owner, repository.name)
109 | .find { case (job, _) => job.buildNumber == buildNumber }
110 | .map { case (job, _) => //(job.buildNumber, JobStatus.Running)
111 | ApiJobStatus(
112 | buildNumber = job.buildNumber,
113 | status = JobStatus.Running,
114 | target = job.pullRequestId.map("PR #" + _.toString).getOrElse(job.buildBranch),
115 | targetUrl = createTargetUrl(job.buildUserName, job.buildRepositoryName, job.buildBranch, job.pullRequestId, repository),
116 | sha = job.sha,
117 | message = job.commitMessage,
118 | userName = getAccountByMailAddress(job.commitMailAddress).map(_.userName).getOrElse(""),
119 | committer = job.commitUserName,
120 | author = job.buildAuthor.userName,
121 | startTime = job.startTime.map { startTime => datetimeAgo(startTime) }.getOrElse(""),
122 | duration = ""
123 | )
124 | }.orElse {
125 | getCIResults(repository.owner, repository.name)
126 | .find { result => result.buildNumber == buildNumber }
127 | .map { result =>
128 | ApiJobStatus(
129 | buildNumber = result.buildNumber,
130 | status = JobStatus.Running,
131 | target = result.pullRequestId.map("PR #" + _.toString).getOrElse(result.buildBranch),
132 | targetUrl = createTargetUrl(result.buildUserName, result.buildRepositoryName, result.buildBranch, result.pullRequestId, repository),
133 | sha = result.sha,
134 | message = result.commitMessage,
135 | userName = getAccountByMailAddress(result.commitMailAddress).map(_.userName).getOrElse(""),
136 | committer = result.commitUserName,
137 | author = result.buildAuthor,
138 | startTime = datetimeAgo(result.startTime),
139 | duration = s"${((result.endTime.getTime - result.startTime.getTime) / 1000)} sec"
140 | )
141 | }
142 | }.map { status =>
143 | gitbucket.ci.html.output(repository, status,
144 | hasDeveloperRole(repository.owner, repository.name, context.loginAccount))
145 | } getOrElse NotFound()
146 | })
147 |
148 | ajaxGet("/:owner/:repository/build/:buildNumber/output")(referrersOnly { repository =>
149 | val buildNumber = params("buildNumber").toInt
150 |
151 | getRunningJobs(repository.owner, repository.name)
152 | .find { case (job, sb) => job.buildNumber == buildNumber }
153 | .map { case (job, sb) =>
154 | contentType = formats("json")
155 | Serialization.write(ApiJobOutput("running", CIUtils.colorize(sb.toString)))
156 | } orElse {
157 | getCIResults(repository.owner, repository.name)
158 | .find { result => result.buildNumber == buildNumber }
159 | .map { result =>
160 | contentType = formats("json")
161 | Serialization.write(ApiJobOutput(result.status, CIUtils.colorize(getCIResultOutput(result))))
162 | }
163 | } getOrElse NotFound()
164 | })
165 |
166 | ajaxPost("/:owner/:repository/build/run")(writableUsersOnly { repository =>
167 | loadCIConfig(repository.owner, repository.name).map { config =>
168 | Using.resource(Git.open(getRepositoryDir(repository.owner, repository.name))) { git =>
169 | JGitUtil.getDefaultBranch(git, repository).map { case (objectId, revision) =>
170 | val revCommit = JGitUtil.getRevCommitFromId(git, objectId)
171 | runBuild(
172 | userName = repository.owner,
173 | repositoryName = repository.name,
174 | buildUserName = repository.owner,
175 | buildRepositoryName = repository.name,
176 | buildBranch = revision,
177 | sha = objectId.name,
178 | commitMessage = revCommit.getShortMessage,
179 | commitUserName = revCommit.getCommitterIdent.getName,
180 | commitMailAddress = revCommit.getCommitterIdent.getEmailAddress,
181 | pullRequestId = None,
182 | buildAuthor = context.loginAccount.get,
183 | config = config
184 | )
185 | }
186 | }
187 | Ok()
188 | } getOrElse BadRequest()
189 | })
190 |
191 | ajaxPost("/:owner/:repository/build/:buildNumber/restart")(writableUsersOnly { repository =>
192 | val buildNumber = params("buildNumber").toInt
193 | loadCIConfig(repository.owner, repository.name).flatMap { config =>
194 | getCIResult(repository.owner, repository.name, buildNumber).map { result =>
195 | runBuild(
196 | userName = result.userName,
197 | repositoryName = result.repositoryName,
198 | buildUserName = result.buildUserName,
199 | buildRepositoryName = result.buildRepositoryName,
200 | buildBranch = result.buildBranch,
201 | sha = result.sha,
202 | commitMessage = result.commitMessage,
203 | commitUserName = result.commitUserName,
204 | commitMailAddress = result.commitMailAddress,
205 | pullRequestId = result.pullRequestId,
206 | buildAuthor = context.loginAccount.get,
207 | config = config
208 | )
209 | Ok()
210 | }
211 | } getOrElse BadRequest()
212 | })
213 |
214 | ajaxPost("/:owner/:repository/build/:buildNumber/cancel")(writableUsersOnly { repository =>
215 | val buildNumber = params("buildNumber").toInt
216 | cancelBuild(repository.owner, repository.name, buildNumber)
217 | Ok()
218 | })
219 |
220 | get("/:owner/:repository/build/:buildNumber/workspace")(referrersOnly { repository =>
221 | val buildNumber = params("buildNumber").toInt
222 | workspace(repository, buildNumber, "")
223 | })
224 |
225 | get("/:owner/:repository/build/:buildNumber/workspace/*")(referrersOnly { repository =>
226 | val buildNumber = params("buildNumber").toInt
227 | val path = multiParams("splat").headOption.getOrElse("")
228 | workspace(repository, buildNumber, path)
229 | })
230 |
231 | private def workspace(repository: RepositoryInfo, buildNumber: Int, path: String) = {
232 | val buildNumber = params("buildNumber").toInt
233 | val path = multiParams("splat").headOption.getOrElse("")
234 | val file = new java.io.File(CIUtils.getBuildDir(
235 | repository.owner, repository.name, buildNumber),
236 | FileUtil.checkFilename(s"workspace/${path}")
237 | )
238 | if(file.isFile){
239 | contentType = FileUtil.getMimeType(path)
240 | response.setContentLength(file.length.toInt)
241 | Using.resource(new FileInputStream(file)){ in =>
242 | IOUtils.copy(in, response.getOutputStream)
243 | }
244 | } else {
245 | gitbucket.ci.html.workspace(
246 | repository,
247 | buildNumber,
248 | "workspace" +: path.split("/").filter(_.nonEmpty).toSeq,
249 | file.listFiles.toSeq.filterNot(_.getName == ".git").sortWith { (file1, file2) =>
250 | (file1.isDirectory, file2.isDirectory) match {
251 | case (true , false) => true
252 | case (false, true ) => false
253 | case _ => file1.getName.compareTo(file2.getName) < 0
254 | }
255 | }
256 | )
257 | }
258 | }
259 |
260 | private def createTargetUrl(buildUserName: String, buildRepositoryName:String, buildBranch: String,
261 | pullRequestId: Option[Int], repository: RepositoryInfo): String = {
262 | pullRequestId match {
263 | case Some(id) => s"${context.path}/${repository.owner}/${repository.name}/pull/${id}"
264 | case None => s"${context.path}/${buildUserName}/${buildRepositoryName}/tree/${buildBranch}"
265 | }
266 | }
267 |
268 | ajaxGet("/:owner/:repository/build/status")(referrersOnly { repository =>
269 | val queuedJobs = getQueuedJobs(repository.owner, repository.name).map { job =>
270 | ApiJobStatus(
271 | buildNumber = job.buildNumber,
272 | status = JobStatus.Waiting,
273 | target = job.pullRequestId.map("PR #" + _.toString).getOrElse(job.buildBranch),
274 | targetUrl = createTargetUrl(job.buildUserName, job.buildRepositoryName, job.buildBranch, job.pullRequestId, repository),
275 | sha = job.sha,
276 | message = job.commitMessage,
277 | userName = getAccountByMailAddress(job.commitMailAddress).map(_.userName).getOrElse(""),
278 | committer = job.commitUserName,
279 | author = job.buildUserName,
280 | startTime = "",
281 | duration = ""
282 | )
283 | }
284 |
285 | val runningJobs = getRunningJobs(repository.owner, repository.name).map { case (job, _) =>
286 | ApiJobStatus(
287 | buildNumber = job.buildNumber,
288 | status = JobStatus.Running,
289 | target = job.pullRequestId.map("PR #" + _.toString).getOrElse(job.buildBranch),
290 | targetUrl = createTargetUrl(job.buildUserName, job.buildRepositoryName, job.buildBranch, job.pullRequestId, repository),
291 | sha = job.sha,
292 | message = job.commitMessage,
293 | userName = getAccountByMailAddress(job.commitMailAddress).map(_.userName).getOrElse(""),
294 | committer = job.commitUserName,
295 | author = job.buildUserName,
296 | startTime = job.startTime.map { startTime => datetimeAgo(startTime) }.getOrElse(""),
297 | duration = ""
298 | )
299 | }
300 |
301 | val finishedJobs = getCIResults(repository.owner, repository.name).map { result =>
302 | ApiJobStatus(
303 | buildNumber = result.buildNumber,
304 | status = result.status,
305 | target = result.pullRequestId.map("PR #" + _.toString).getOrElse(result.buildBranch),
306 | targetUrl = createTargetUrl(result.buildUserName, result.buildRepositoryName, result.buildBranch, result.pullRequestId, repository),
307 | sha = result.sha,
308 | message = result.commitMessage,
309 | userName = getAccountByMailAddress(result.commitMailAddress).map(_.userName).getOrElse(""),
310 | committer = result.commitUserName,
311 | author = result.buildUserName,
312 | startTime = datetimeAgo(result.startTime),
313 | duration = s"${((result.endTime.getTime - result.startTime.getTime) / 1000)} sec"
314 | )
315 | }
316 |
317 | contentType = formats("json")
318 | Serialization.write((queuedJobs ++ runningJobs ++ finishedJobs).sortBy(_.buildNumber * -1))(jsonFormats)
319 | })
320 |
321 | get("/:owner/:repository/settings/build")(ownerOnly { repository =>
322 | gitbucket.ci.html.config(repository, loadCIConfig(repository.owner, repository.name), loadCISystemConfig(), flash.get("info"))
323 | })
324 |
325 | post("/:owner/:repository/settings/build", buildConfigForm)(ownerOnly { (form, repository) =>
326 | if(form.enableBuild){
327 | val buildType = form.buildType.getOrElse("script")
328 | saveCIConfig(repository.owner, repository.name, Some(
329 | CIConfig(
330 | repository.owner,
331 | repository.name,
332 | buildType,
333 | (buildType match {
334 | case "script" => form.buildScript.getOrElse("")
335 | case "file" => form.buildFile.getOrElse("")
336 | case "docker" => form.dockerfile.getOrElse("")
337 | case "docker-compose" => form.composeFile.getOrElse("")
338 | case _ => ""
339 | }),
340 | form.notification,
341 | form.skipWords,
342 | form.runWords
343 | )
344 | ))
345 | } else {
346 | saveCIConfig(repository.owner, repository.name, None)
347 | }
348 | flash.update("info", "Build configuration has been updated.")
349 | redirect(s"/${repository.owner}/${repository.name}/settings/build")
350 | })
351 |
352 | get("/:owner/:repository/build/:branch/badge.svg")(referrersOnly { repository =>
353 | contentType = "image/svg+xml"
354 | getLatestCIStatus(repository.owner, repository.name, params("branch")) match {
355 | case "success" => """"""
356 | case "failure" => """"""
357 | case "waiting" => """"""
358 | case "running" => """"""
359 | case _ => """"""
360 | }
361 | })
362 |
363 | get("/admin/build")(adminOnly {
364 | gitbucket.ci.html.system(loadCISystemConfig())
365 | })
366 |
367 | post("/admin/build", ciSystemConfigForm)(adminOnly { form =>
368 | saveCISystemConfig(CISystemConfig(
369 | maxBuildHistory = form.maxBuildHistory,
370 | maxParallelBuilds = form.maxParallelBuilds,
371 | enableDocker = form.enableDocker,
372 | dockerCommand = form.dockerCommand,
373 | enableDockerCompose = form.enableDockerCompose,
374 | dockerComposeCommand = form.dockerComposeCommand
375 | ))
376 | BuildManager.setMaxParallelBuilds(form.maxParallelBuilds)
377 | redirect("/admin/build")
378 | })
379 |
380 | }
381 |
382 |
--------------------------------------------------------------------------------
/src/main/scala/io/github/gitbucket/ci/hook/CICommitHook.scala:
--------------------------------------------------------------------------------
1 | package io.github.gitbucket.ci.hook
2 |
3 | import gitbucket.core.plugin.ReceiveHook
4 | import gitbucket.core.model.Profile._
5 | import gitbucket.core.service._
6 | import gitbucket.core.util.Directory.getRepositoryDir
7 | import gitbucket.core.util.JGitUtil
8 | import io.github.gitbucket.ci.model.CIConfig
9 | import io.github.gitbucket.ci.service.CIService
10 | import org.eclipse.jgit.api.Git
11 | import org.eclipse.jgit.transport.{ReceiveCommand, ReceivePack}
12 | import profile.blockingApi._
13 | import scala.util.Using
14 |
15 | class CICommitHook extends ReceiveHook
16 | with CIService with RepositoryService with AccountService with CommitStatusService with SystemSettingsService {
17 |
18 | override def postReceive(owner: String, repository: String, receivePack: ReceivePack,
19 | command: ReceiveCommand, pusher: String, mergePullRequest: Boolean)(implicit session: Session): Unit = {
20 | val branch = command.getRefName.stripPrefix("refs/heads/")
21 | if(branch != command.getRefName && command.getType != ReceiveCommand.Type.DELETE){
22 | getRepository(owner, repository).foreach { repositoryInfo =>
23 | Using.resource(Git.open(getRepositoryDir(owner, repository))) { git =>
24 | val sha = command.getNewId.name
25 | val revCommit = JGitUtil.getRevCommitFromId(git, command.getNewId)
26 |
27 | loadCIConfig(owner, repository).foreach { buildConfig =>
28 | if(buildConfig.skipWordsSeq.find(revCommit.getFullMessage.contains).isEmpty){
29 | runBuild(
30 | userName = owner,
31 | repositoryName = repository,
32 | buildUserName = owner,
33 | buildRepositoryName = repository,
34 | buildBranch = branch,
35 | sha = sha,
36 | commitMessage = revCommit.getShortMessage,
37 | commitUserName = revCommit.getCommitterIdent.getName,
38 | commitMailAddress = revCommit.getCommitterIdent.getEmailAddress,
39 | pullRequestId = None,
40 | pusher = pusher,
41 | config = buildConfig
42 | )
43 | }
44 | }
45 |
46 | for {
47 | (pullreq, issue) <- PullRequests
48 | .join(Issues).on { (t1, t2) => t1.byPrimaryKey(t2.userName, t2.repositoryName, t2.issueId) }
49 | .filter { case (t1, t2) =>
50 | (t1.requestUserName === owner.bind) && (t1.requestRepositoryName === repository.bind) &&
51 | (t1.requestBranch === branch.bind) && (t2.closed === false.bind)
52 | }
53 | .list
54 | buildConfig <- loadCIConfig(pullreq.userName, pullreq.repositoryName)
55 | } yield {
56 | if(buildConfig.skipWordsSeq.find(revCommit.getFullMessage.contains).isEmpty){
57 | runBuild(
58 | userName = pullreq.userName,
59 | repositoryName = pullreq.repositoryName,
60 | buildUserName = owner,
61 | buildRepositoryName = repository,
62 | buildBranch = branch,
63 | sha = sha,
64 | commitMessage = revCommit.getShortMessage,
65 | commitUserName = revCommit.getCommitterIdent.getName,
66 | commitMailAddress = revCommit.getCommitterIdent.getEmailAddress,
67 | pullRequestId = Some(pullreq.issueId),
68 | pusher = pusher,
69 | config = buildConfig
70 | )
71 | }
72 | }
73 | }
74 | }
75 | }
76 | }
77 |
78 | private def runBuild(userName: String, repositoryName: String, buildUserName: String, buildRepositoryName: String,
79 | buildBranch: String, sha: String, commitMessage: String, commitUserName: String, commitMailAddress: String,
80 | pullRequestId: Option[Int], pusher: String, config: CIConfig)(implicit session: Session): Unit = {
81 | getAccountByUserName(pusher).foreach { pusherAccount =>
82 | runBuild(
83 | userName = userName,
84 | repositoryName = repositoryName,
85 | buildUserName = buildUserName,
86 | buildRepositoryName = buildRepositoryName,
87 | buildBranch = buildBranch,
88 | sha = sha,
89 | commitMessage = commitMessage,
90 | commitUserName = commitUserName,
91 | commitMailAddress = commitMailAddress,
92 | pullRequestId = pullRequestId,
93 | buildAuthor = pusherAccount,
94 | config = config
95 | )
96 | }
97 | }
98 |
99 | }
100 |
--------------------------------------------------------------------------------
/src/main/scala/io/github/gitbucket/ci/hook/CIPullRequestHook.scala:
--------------------------------------------------------------------------------
1 | package io.github.gitbucket.ci.hook
2 |
3 | import gitbucket.core.controller.Context
4 | import gitbucket.core.model.Issue
5 | import gitbucket.core.plugin.PullRequestHook
6 | import gitbucket.core.service.RepositoryService.RepositoryInfo
7 | import gitbucket.core.model.Profile._
8 | import gitbucket.core.service._
9 | import gitbucket.core.util.Directory.getRepositoryDir
10 | import gitbucket.core.util.JGitUtil
11 | import io.github.gitbucket.ci.service.CIService
12 | import org.eclipse.jgit.api.Git
13 | import profile.api._
14 | import scala.util.Using
15 |
16 | class CIPullRequestHook extends PullRequestHook
17 | with PullRequestService with IssuesService with CommitsService with AccountService with WebHookService
18 | with WebHookPullRequestService with WebHookPullRequestReviewCommentService with ActivityService with MergeService
19 | with RepositoryService with LabelsService with PrioritiesService with MilestonesService with CIService
20 | with RequestCache {
21 |
22 | private def runBuildWith(issue: Issue, repository: RepositoryInfo, isMergeRequest: Boolean)(implicit session: Session, context: Context): Unit = {
23 | if(issue.isPullRequest){
24 | for {
25 | (_, pullreq) <- getPullRequest(issue.userName, issue.repositoryName, issue.issueId)
26 | buildAuthor <- context.loginAccount
27 | buildConfig <- loadCIConfig(pullreq.userName, pullreq.repositoryName)
28 | } yield {
29 | val revCommit = Using.resource(Git.open(getRepositoryDir(pullreq.requestUserName, pullreq.requestRepositoryName))) { git =>
30 | val objectId = git.getRepository.resolve(pullreq.commitIdTo)
31 | JGitUtil.getRevCommitFromId(git, objectId)
32 | }
33 | runBuild(
34 | userName = pullreq.userName,
35 | repositoryName = pullreq.repositoryName,
36 | buildUserName = pullreq.requestUserName,
37 | buildRepositoryName = pullreq.requestRepositoryName,
38 | buildBranch = isMergeRequest match {
39 | case true => pullreq.branch
40 | case false => pullreq.requestBranch
41 | },
42 | sha = isMergeRequest match {
43 | case true => Using.resource(Git.open(getRepositoryDir(pullreq.userName, pullreq.repositoryName))) { git =>
44 | val objectId = git.getRepository.resolve(pullreq.branch)
45 | objectId.name
46 | }
47 | case false => pullreq.commitIdTo
48 | },
49 | commitMessage = revCommit.getShortMessage,
50 | commitUserName = revCommit.getCommitterIdent.getName,
51 | commitMailAddress = revCommit.getCommitterIdent.getEmailAddress,
52 | pullRequestId = Some(pullreq.issueId),
53 | buildAuthor = buildAuthor,
54 | config = buildConfig
55 | )
56 | }
57 | }
58 | }
59 |
60 | override def created(issue: Issue, repository: RepositoryInfo)(implicit session: Session, context: Context): Unit =
61 | runBuildWith(issue, repository, false)
62 |
63 | override def merged(issue: Issue, repository: RepositoryInfo)(implicit session: Session, context: Context): Unit =
64 | runBuildWith(issue, repository, true)
65 |
66 |
67 | override def addedComment(commentId: Int, content: String, issue: Issue, repository: RepositoryInfo)
68 | (implicit session: Session, context: Context): Unit = {
69 | if(issue.isPullRequest){
70 | for {
71 | (_, pullreq) <- getPullRequest(issue.userName, issue.repositoryName, issue.issueId)
72 | buildAuthor <- context.loginAccount
73 | buildConfig <- loadCIConfig(pullreq.userName, pullreq.repositoryName)
74 | } yield {
75 | if(!buildConfig.runWordsSeq.find(content.contains).isEmpty){
76 | val revCommit = Using.resource(Git.open(getRepositoryDir(pullreq.requestUserName, pullreq.requestRepositoryName))) { git =>
77 | val objectId = git.getRepository.resolve(pullreq.commitIdTo)
78 | JGitUtil.getRevCommitFromId(git, objectId)
79 | }
80 | runBuild(
81 | userName = pullreq.userName,
82 | repositoryName = pullreq.repositoryName,
83 | buildUserName = pullreq.requestUserName,
84 | buildRepositoryName = pullreq.requestRepositoryName,
85 | buildBranch = pullreq.requestBranch,
86 | sha = pullreq.commitIdTo,
87 | commitMessage = revCommit.getShortMessage,
88 | commitUserName = revCommit.getCommitterIdent.getName,
89 | commitMailAddress = revCommit.getCommitterIdent.getEmailAddress,
90 | pullRequestId = Some(pullreq.issueId),
91 | buildAuthor = buildAuthor,
92 | config = buildConfig
93 | )
94 | }
95 | }
96 | }
97 | }
98 |
99 | }
100 |
--------------------------------------------------------------------------------
/src/main/scala/io/github/gitbucket/ci/hook/CIRepositoryHook.scala:
--------------------------------------------------------------------------------
1 | package io.github.gitbucket.ci.hook
2 |
3 | import gitbucket.core.plugin.RepositoryHook
4 | import io.github.gitbucket.ci.model.Profile._
5 | import profile.blockingApi._
6 |
7 | class CIRepositoryHook extends RepositoryHook {
8 |
9 | override def deleted(owner: String, repository: String)(implicit session: Session): Unit = {
10 | CIConfigs.filter { t => (t.userName === owner.bind) && (t.repositoryName === repository.bind) }.delete
11 | CIResults.filter { t => (t.userName === owner.bind) && (t.repositoryName === repository.bind) }.delete
12 | }
13 |
14 | override def renamed(owner: String, repository: String, newRepository: String)(implicit session: Session): Unit = {
15 | CIConfigs.filter { t => (t.userName === owner.bind) && (t.repositoryName === repository.bind) }.list.foreach { config =>
16 | CIConfigs += config.copy(repositoryName = newRepository)
17 | }
18 | CIResults.filter { t => (t.userName === owner.bind) && (t.repositoryName === repository.bind) }.list.foreach { result =>
19 | CIResults += result.copy(repositoryName = newRepository)
20 | }
21 | deleted(owner, repository)
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/scala/io/github/gitbucket/ci/manager/BuildJobThread.scala:
--------------------------------------------------------------------------------
1 | package io.github.gitbucket.ci.manager
2 |
3 | import java.io.File
4 | import java.util.concurrent.LinkedBlockingQueue
5 | import java.util.concurrent.atomic.{AtomicBoolean, AtomicReference}
6 |
7 | import gitbucket.core.model.CommitState
8 | import gitbucket.core.model.Profile.profile.blockingApi._
9 | import gitbucket.core.service.SystemSettingsService.SystemSettings
10 | import gitbucket.core.service.{AccountService, CommitStatusService, RepositoryService, SystemSettingsService}
11 | import gitbucket.core.servlet.Database
12 | import gitbucket.core.util.Directory.getRepositoryDir
13 | import gitbucket.core.util.Mailer
14 | import io.github.gitbucket.ci.model.CIResult
15 | import io.github.gitbucket.ci.service._
16 | import io.github.gitbucket.ci.util.{CIUtils, JobStatus}
17 | import io.github.gitbucket.markedj.{Marked, Options}
18 | import org.apache.commons.io.FileUtils
19 | import org.apache.commons.lang3.exception.ExceptionUtils
20 | import org.eclipse.jgit.api.Git
21 | import org.slf4j.LoggerFactory
22 |
23 | import scala.sys.process.{Process, ProcessLogger}
24 | import scala.util.control.ControlThrowable
25 | import scala.util.Using
26 |
27 |
28 | class BuildJobThread(queue: LinkedBlockingQueue[BuildJob], threads: LinkedBlockingQueue[BuildJobThread]) extends Thread
29 | with CommitStatusService with AccountService with RepositoryService with CIService with SystemSettingsService {
30 |
31 | private val logger = LoggerFactory.getLogger(classOf[BuildJobThread])
32 |
33 | val cancelled = new AtomicReference[Boolean](false)
34 | val runningProcess = new AtomicReference[Option[Process]](None)
35 | val runningJob = new AtomicReference[Option[BuildJob]](None)
36 | val sb = new StringBuffer()
37 | val continue = new AtomicBoolean(true)
38 |
39 | override def run(): Unit = {
40 | logger.info("Start BuildJobThread-" + this.getId)
41 | try {
42 | while(continue.get()){
43 | runBuild(queue.take())
44 | }
45 | } catch {
46 | case _: InterruptedException => cancel()
47 | }
48 | threads.remove(this)
49 | logger.info("Stop BuildJobThread-" + this.getId)
50 | }
51 |
52 | private def initState(job: Option[BuildJob]): Unit = {
53 | cancelled.set(false)
54 | runningProcess.set(None)
55 | runningJob.set(job)
56 | sb.setLength(0)
57 | }
58 |
59 | private def runBuild(job: BuildJob): Unit = {
60 | val startTime = new java.util.Date()
61 | initState(Some(job.copy(startTime = Some(startTime))))
62 |
63 | val settings = loadSystemSettings()
64 | val targetUrl = settings.baseUrl.map { baseUrl =>
65 | s"${baseUrl}/${job.userName}/${job.repositoryName}/build/${job.buildNumber}"
66 | }
67 |
68 | val systemCIConfig = Database() withTransaction { implicit session =>
69 | createCommitStatus(
70 | userName = job.userName,
71 | repositoryName = job.repositoryName,
72 | sha = job.sha,
73 | context = CIUtils.ContextName,
74 | state = CommitState.PENDING,
75 | targetUrl = targetUrl,
76 | description = None,
77 | now = new java.util.Date(),
78 | creator = job.buildAuthor // TODO right??
79 | )
80 | loadCISystemConfig()
81 | }
82 | val dockerCommand = systemCIConfig.dockerCommand.getOrElse("docker")
83 | val dockerComposeCommand = systemCIConfig.dockerComposeCommand.getOrElse("docker-compose")
84 |
85 | try {
86 | val exitValue = try {
87 | val buildDir = CIUtils.getBuildDir(job.userName, job.repositoryName, job.buildNumber)
88 | val dir = new File(buildDir, "workspace")
89 | if (dir.exists()) {
90 | FileUtils.deleteDirectory(dir)
91 | }
92 |
93 | if(cancelled.get() == true){
94 | throw new BuildJobCancelException()
95 | }
96 |
97 | sb.append(s"git clone ${job.buildUserName}/${job.buildRepositoryName}\n")
98 |
99 | // git clone
100 | Using.resource(Git.cloneRepository()
101 | .setURI(getRepositoryDir(job.buildUserName, job.buildRepositoryName).toURI.toString)
102 | .setDirectory(dir).call()) { git =>
103 |
104 | if(cancelled.get() == true){
105 | throw new BuildJobCancelException()
106 | }
107 |
108 | sb.append(s"git checkout ${job.sha}\n")
109 |
110 | // git checkout
111 | git.checkout().setName(job.sha).call()
112 |
113 | if(cancelled.get() == true){
114 | throw new BuildJobCancelException()
115 | }
116 |
117 | job.config.buildType match {
118 | case "script" =>
119 | runScriptJob(job, buildDir, dir)
120 | case "file" =>
121 | runFileJob(job, buildDir, dir)
122 | case "docker" =>
123 | if(systemCIConfig.enableDocker){
124 | runDockerJob(job, buildDir, dir, dockerCommand)
125 | }else{
126 | throw new RuntimeException("Docker job is disabled.")
127 | }
128 | case "docker-compose" =>
129 | if(systemCIConfig.enableDockerCompose){
130 | runDockerComposeJob(job, buildDir, dir, dockerComposeCommand)
131 | }else{
132 | throw new RuntimeException("Docker compose job is disabled.")
133 | }
134 | case _ =>
135 | throw new MatchError("Invalid build type: " + job.config.buildType)
136 | }
137 | }
138 | } catch {
139 | case e: Exception => {
140 | sb.append(ExceptionUtils.getStackTrace(e))
141 | logger.error(s"${job.userName}/${job.repositoryName} #${job.buildNumber}", e)
142 | -1
143 | }
144 | case _: ControlThrowable =>
145 | -1
146 | }
147 |
148 | val endTime = new java.util.Date()
149 |
150 | // Create or update commit status
151 | Database() withTransaction { implicit session =>
152 | saveCIResult(
153 | CIResult(
154 | userName = job.userName,
155 | repositoryName = job.repositoryName,
156 | buildUserName = job.buildUserName,
157 | buildRepositoryName = job.buildRepositoryName,
158 | buildNumber = job.buildNumber,
159 | buildBranch = job.buildBranch,
160 | sha = job.sha,
161 | commitMessage = job.commitMessage,
162 | commitUserName = job.commitUserName,
163 | commitMailAddress = job.commitMailAddress,
164 | pullRequestId = job.pullRequestId,
165 | queuedTime = job.queuedTime,
166 | startTime = startTime,
167 | endTime = endTime,
168 | exitCode = exitValue,
169 | status = if(exitValue == 0) JobStatus.Success else JobStatus.Failure,
170 | buildAuthor = job.buildAuthor.userName,
171 | buildScript = job.config.buildScript
172 | ),
173 | sb.toString,
174 | loadCISystemConfig()
175 | )
176 |
177 | createCommitStatus(
178 | userName = job.userName,
179 | repositoryName = job.repositoryName,
180 | sha = job.sha,
181 | context = CIUtils.ContextName,
182 | state = if(exitValue == 0) CommitState.SUCCESS else CommitState.FAILURE,
183 | targetUrl = targetUrl,
184 | description = None,
185 | now = endTime,
186 | creator = job.buildAuthor // TODO right??
187 | )
188 |
189 | // Send email
190 | if(job.config.notification && settings.useSMTP && exitValue != 0){
191 | val committer = getAccountByMailAddress(job.commitMailAddress, false).map(_.mailAddress).toSeq
192 | val collaborators = getCollaboratorUserNames(job.userName, job.repositoryName).flatMap { userName =>
193 | getAccountByUserName(userName).map(_.mailAddress)
194 | }
195 |
196 | val subject = createMailSubject(job)
197 | val markdown = createMailContent(job, settings, targetUrl)
198 | val html = markdown2html(markdown)
199 |
200 | val mailer = new Mailer(settings)
201 |
202 | val recipients = (committer ++ collaborators).distinct
203 | recipients.foreach { to =>
204 | mailer.send(to, subject, markdown, Some(html))
205 | }
206 | }
207 | }
208 |
209 | logger.info("Build number: " + job.buildNumber)
210 | logger.info("Total: " + (endTime.getTime - startTime.getTime) + " msec")
211 | logger.info("Finish build with exit code: " + exitValue)
212 |
213 | } finally {
214 | initState(None)
215 | }
216 | }
217 |
218 | private def runProcess(job: BuildJob, buildDir: File, workspaceDir: File, command: String): Int = {
219 | val process = Process(command, workspaceDir,
220 | "CI" -> "true",
221 | "HOME" -> buildDir.getAbsolutePath,
222 | "CI_BUILD_DIR" -> buildDir.getAbsolutePath,
223 | "CI_BUILD_NUMBER" -> job.buildNumber.toString,
224 | "CI_BUILD_BRANCH" -> job.buildBranch,
225 | "CI_COMMIT_ID" -> job.sha,
226 | "CI_COMMIT_MESSAGE" -> job.commitMessage,
227 | "CI_REPO_SLUG" -> s"${job.userName}/${job.repositoryName}",
228 | "CI_PULL_REQUEST" -> job.pullRequestId.map(_.toString).getOrElse("false"),
229 | "CI_PULL_REQUEST_SLUG" -> (if (job.pullRequestId.isDefined) s"${job.buildUserName}/${job.buildRepositoryName}" else "")
230 | ).run(new BuildProcessLogger(sb))
231 | runningProcess.set(Some(process))
232 |
233 | while (process.isAlive()) {
234 | Thread.sleep(1000)
235 | }
236 |
237 | val exitValue = process.exitValue()
238 | if(exitValue != 0){
239 | sb.append(s"EXIT CODE: ${exitValue}\n")
240 | }
241 | exitValue
242 | }
243 |
244 | private def runScriptJob(job: BuildJob, buildDir: File, workspaceDir: File): Int = {
245 | // run script
246 | val command = prepareBuildScript(buildDir, job.config.buildScript)
247 | runProcess(job, buildDir, workspaceDir, command)
248 | }
249 |
250 | private def runFileJob(job: BuildJob, buildDir: File, workspaceDir: File): Int = {
251 | // run script
252 | val command = prepareBuildFile(buildDir, job.config.buildScript)
253 | runProcess(job, buildDir, workspaceDir, command)
254 | }
255 |
256 | private def runDockerJob(job: BuildJob, buildDir: File, workspaceDir: File, dockerCommand: String): Int = {
257 | val tagName = s"gitbucket-ci/${job.buildUserName}/${job.buildRepositoryName}:${job.sha.substring(0, 7)}"
258 | val containerName = s"${job.buildUserName}-${job.buildRepositoryName}-${job.buildNumber}"
259 | val dockerfile = if(job.config.buildScript.nonEmpty){job.config.buildScript}else{"Dockerfile"}
260 |
261 | val buildContainerCommand = s"${dockerCommand} build -f ${dockerfile} -t ${tagName} ${workspaceDir.getAbsolutePath}"
262 | val runContainerCommand = s"${dockerCommand} run --rm --name ${containerName} ${tagName}"
263 |
264 | sb.append(s"${buildContainerCommand}\n")
265 | val buildResult = runProcess(job, buildDir, workspaceDir, buildContainerCommand)
266 | if (buildResult == 0){
267 | sb.append(s"${runContainerCommand}\n")
268 | val exitCode = runProcess(job, buildDir, workspaceDir, runContainerCommand)
269 |
270 | val imageId = Process(s"""${dockerCommand} images --format {{.ID}} ${tagName}""").!!.stripLineEnd
271 | val rmImageCommand = s"${dockerCommand} rmi --force ${imageId}"
272 | sb.append(s"$rmImageCommand\n")
273 | runProcess(job, buildDir, workspaceDir, rmImageCommand)
274 |
275 | exitCode
276 | }else{
277 | buildResult
278 | }
279 | }
280 |
281 | private def runDockerComposeJob(job: BuildJob, buildDir: File, workspaceDir: File, composeCommand: String): Int = {
282 | val composeFile = if(job.config.buildScript.nonEmpty){job.config.buildScript}else{"docker-compose.yml"}
283 | val containerName = s"gitbucket_ci_${job.buildUserName}_${job.buildRepositoryName}_${job.buildNumber}"
284 |
285 | val buildCommand = s"${composeCommand} -f ${composeFile} build"
286 | // TODO: specify service name (currently consider as "ci")
287 | val runCommand = s"${composeCommand} -f ${composeFile} run --name ${containerName} -T --rm ci"
288 | val downCommand = s"${composeCommand} -f ${composeFile} down --rmi all"
289 |
290 | sb.append(s"${buildCommand}\n")
291 | val buildResult = runProcess(job, buildDir, workspaceDir, buildCommand)
292 | if(buildCommand != 0){
293 | sb.append(s"${runCommand}\n")
294 | val exitCode = runProcess(job, buildDir, workspaceDir, runCommand)
295 | runProcess(job, buildDir, workspaceDir, downCommand)
296 |
297 | exitCode
298 | }else{
299 | buildResult
300 | }
301 | }
302 |
303 | private def createMailSubject(job: BuildJob): String = {
304 | val sb = new StringBuilder()
305 |
306 | sb.append(s"[${job.userName}/${job.repositoryName}] Build #${job.buildNumber} failed ")
307 | job.pullRequestId match {
308 | case Some(id) =>
309 | sb.append(s"(PR #${id})")
310 | case None =>
311 | sb.append(s"(${job.buildBranch})")
312 | }
313 |
314 | sb.toString
315 | }
316 |
317 | private def createMailContent(job: BuildJob, settings: SystemSettings, targetUrl: Option[String]): String = {
318 | val sb = new StringBuilder()
319 |
320 | settings.baseUrl match {
321 | case Some(baseUrl) =>
322 | sb.append(s"[${job.sha.substring(0, 7)}](${baseUrl}/${job.userName}/${job.repositoryName}/commit/${job.sha}) by ${job.commitUserName}\n")
323 | case None =>
324 | sb.append(s"${job.sha.substring(0, 7)} by ${job.commitUserName}\n")
325 | }
326 | sb.append(job.commitMessage)
327 | sb.append("\n")
328 |
329 | targetUrl.foreach { url =>
330 | sb.append("\n")
331 | sb.append("----\n")
332 | sb.append(s"[View it on GitBucket](${url})\n")
333 | }
334 |
335 | sb.toString
336 | }
337 |
338 | private def markdown2html(markdown: String): String = {
339 | val options = new Options()
340 | options.setBreaks(true)
341 | Marked.marked(markdown, options)
342 | }
343 |
344 | def cancel(): Unit = {
345 | cancelled.set(true)
346 | runningProcess.get.foreach(_.destroy())
347 | }
348 |
349 | private def prepareBuildScript(buildDir: File, buildScript: String): String = {
350 | if(CIUtils.isWindows){
351 | val buildFile = new File(buildDir, "build.bat")
352 | FileUtils.write(buildFile, buildScript, "UTF-8")
353 | buildFile.setExecutable(true)
354 | buildFile.getAbsolutePath
355 | } else {
356 | val buildFile = new File(buildDir, "build.sh")
357 | FileUtils.write(buildFile, "#!/bin/sh\n" + buildScript.replaceAll("\r\n", "\n"), "UTF-8")
358 | buildFile.setExecutable(true)
359 | "../build.sh"
360 | }
361 | }
362 |
363 | private def prepareBuildFile(buildDir: File, buildScript: String): String = {
364 | if(CIUtils.isWindows){
365 | val dir = new File(buildDir, "workspace")
366 | new File(dir, buildScript).getAbsolutePath
367 | } else {
368 | "./" + buildScript
369 | }
370 | }
371 | }
372 |
373 | /**
374 | * Used to abort build job immediately in BuildJobThread.
375 | */
376 | private class BuildJobCancelException extends ControlThrowable
377 |
378 | /**
379 | * Used to capture output of the build process.
380 | */
381 | private class BuildProcessLogger(sb: StringBuffer) extends ProcessLogger {
382 |
383 | override def err(s: => String): Unit = {
384 | sb.append(s + "\n")
385 | }
386 |
387 | override def out(s: => String): Unit = {
388 | sb.append(s + "\n")
389 | }
390 |
391 | override def buffer[T](f: => T): T = ???
392 |
393 | }
394 |
--------------------------------------------------------------------------------
/src/main/scala/io/github/gitbucket/ci/manager/BuildManager.scala:
--------------------------------------------------------------------------------
1 | package io.github.gitbucket.ci.manager
2 |
3 | import java.util.concurrent.LinkedBlockingQueue
4 | import io.github.gitbucket.ci.service.BuildJob
5 |
6 | object BuildManager {
7 |
8 | val queue = new LinkedBlockingQueue[BuildJob]()
9 | val threads = new LinkedBlockingQueue[BuildJobThread]()
10 |
11 | def queueBuildJob(job: BuildJob): Unit = {
12 | queue.add(job)
13 | }
14 |
15 | def shutdownBuildManager(): Unit = {
16 | threads.forEach(_.interrupt())
17 | }
18 |
19 | def setMaxParallelBuilds(maxParallelBuilds: Int): Unit = {
20 | if(maxParallelBuilds > threads.size){
21 | for(_ <- 1 to maxParallelBuilds - threads.size){
22 | val thread = new BuildJobThread(queue, threads)
23 | threads.add(thread)
24 | thread.start()
25 | }
26 | } else if (maxParallelBuilds < threads.size){
27 | val i = threads.iterator()
28 | for(_ <- 1 to threads.size - maxParallelBuilds){
29 | val thread = i.next()
30 | if(thread.runningJob.get.isEmpty){
31 | thread.interrupt()
32 | } else {
33 | thread.continue.set(false)
34 | }
35 | }
36 | }
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/src/main/scala/io/github/gitbucket/ci/model/CIConfig.scala:
--------------------------------------------------------------------------------
1 | package io.github.gitbucket.ci.model
2 |
3 | import scala.language.postfixOps
4 |
5 | trait CIConfigComponent { self: gitbucket.core.model.Profile =>
6 | import profile.api._
7 | import self._
8 |
9 | lazy val CIConfigs = TableQuery[CIConfigs]
10 |
11 | class CIConfigs(tag: Tag) extends Table[CIConfig](tag, "CI_CONFIG") {
12 | val userName = column[String]("USER_NAME", O PrimaryKey)
13 | val repositoryName = column[String]("REPOSITORY_NAME")
14 | val buildType = column[String]("BUILD_TYPE")
15 | val buildScript = column[String]("BUILD_SCRIPT")
16 | val notification = column[Boolean]("NOTIFICATION")
17 | val skipWords = column[String]("SKIP_WORDS")
18 | val runWords = column[String]("RUN_WORDS")
19 | def * = (userName, repositoryName, buildType, buildScript, notification, skipWords.?, runWords.?) <> (CIConfig.tupled, CIConfig.unapply)
20 | }
21 | }
22 |
23 | case class CIConfig(
24 | userName: String,
25 | repositoryName: String,
26 | buildType: String,
27 | buildScript: String,
28 | notification: Boolean,
29 | skipWords: Option[String],
30 | runWords: Option[String]
31 | ){
32 | lazy val skipWordsSeq: Seq[String] = skipWords.map(_.split(",").map(_.trim).toSeq).getOrElse(Nil)
33 | lazy val runWordsSeq: Seq[String] = runWords.map(_.split(",").map(_.trim).toSeq).getOrElse(Nil)
34 | }
35 |
--------------------------------------------------------------------------------
/src/main/scala/io/github/gitbucket/ci/model/CIProfile.scala:
--------------------------------------------------------------------------------
1 | package io.github.gitbucket.ci.model
2 |
3 | import gitbucket.core.model._
4 |
5 | object Profile extends CoreProfile
6 | with CIConfigComponent with CIResultComponent with CISystemConfigComponent
7 |
--------------------------------------------------------------------------------
/src/main/scala/io/github/gitbucket/ci/model/CIResult.scala:
--------------------------------------------------------------------------------
1 | package io.github.gitbucket.ci.model
2 |
3 | import io.github.gitbucket.ci.util.JobStatus
4 | import scala.language.postfixOps
5 |
6 | trait CIResultComponent { self: gitbucket.core.model.Profile =>
7 | import profile.api._
8 | import self._
9 |
10 | lazy val CIResults = TableQuery[CIResults]
11 |
12 | class CIResults(tag: Tag) extends Table[CIResult](tag, "CI_RESULT") {
13 | val userName = column[String]("USER_NAME", O PrimaryKey)
14 | val repositoryName = column[String]("REPOSITORY_NAME")
15 | val buildUserName = column[String]("BUILD_USER_NAME")
16 | val buildRepositoryName = column[String]("BUILD_REPOSITORY_NAME")
17 | val buildNumber = column[Int]("BUILD_NUMBER")
18 | val buildBranch = column[String]("BUILD_BRANCH")
19 | val sha = column[String]("SHA")
20 | val commitMessage = column[String]("COMMIT_MESSAGE")
21 | val commitUserName = column[String]("COMMIT_USER_NAME")
22 | val commitMailAddress = column[String]("COMMIT_MAIL_ADDRESS")
23 | val pullRequestId = column[Int]("PULL_REQUEST_ID")
24 | val queuedTime = column[java.util.Date]("QUEUED_TIME")
25 | val startTime = column[java.util.Date]("START_TIME")
26 | val endTime = column[java.util.Date]("END_TIME")
27 | val exitCode = column[Int]("EXIT_CODE")
28 | val status = column[String]("STATUS")
29 | val buildAuthor = column[String]("BUILD_AUTHOR")
30 | val buildScript = column[String]("BUILD_SCRIPT")
31 | def * = (userName, repositoryName, buildUserName, buildRepositoryName, buildNumber, buildBranch, sha, commitMessage, commitUserName, commitMailAddress, pullRequestId.?, queuedTime, startTime, endTime, exitCode, status, buildAuthor, buildScript) <> (CIResult.tupled, CIResult.unapply)
32 | }
33 | }
34 |
35 | case class CIResult(
36 | userName: String,
37 | repositoryName: String,
38 | buildUserName: String,
39 | buildRepositoryName: String,
40 | buildNumber: Int,
41 | buildBranch: String,
42 | sha: String,
43 | commitMessage: String,
44 | commitUserName: String,
45 | commitMailAddress: String,
46 | pullRequestId: Option[Int],
47 | queuedTime: java.util.Date,
48 | startTime: java.util.Date,
49 | endTime: java.util.Date,
50 | exitCode: Int,
51 | status: String,
52 | buildAuthor: String,
53 | buildScript: String
54 | ){
55 | lazy val apiStatus = if(status == JobStatus.Success) "success" else "failed"
56 | }
57 |
--------------------------------------------------------------------------------
/src/main/scala/io/github/gitbucket/ci/model/CISystemConfig.scala:
--------------------------------------------------------------------------------
1 | package io.github.gitbucket.ci.model
2 |
3 | trait CISystemConfigComponent { self: gitbucket.core.model.Profile =>
4 | import profile.api._
5 | import self._
6 |
7 | lazy val CISystemConfigs = TableQuery[CISystemConfigs]
8 |
9 | class CISystemConfigs(tag: Tag) extends Table[CISystemConfig](tag, "CI_SYSTEM_CONFIG") {
10 | val maxBuildHistory = column[Int]("MAX_BUILD_HISTORY")
11 | val maxParallelBuilds = column[Int]("MAX_PARALLEL_BUILDS")
12 | val enableDocker = column[Boolean]("ENABLE_DOCKER")
13 | val dockerCommand = column[String]("DOCKER_COMMAND")
14 | val enableDockerCompose = column[Boolean]("ENABLE_DOCKER_COMPOSE")
15 | val dockerComposeCommand = column[String]("DOCKER_COMPOSE_COMMAND")
16 | def * = (maxBuildHistory, maxParallelBuilds, enableDocker, dockerCommand.?, enableDockerCompose, dockerComposeCommand.?) <> (CISystemConfig.tupled, CISystemConfig.unapply)
17 | }
18 | }
19 |
20 | case class CISystemConfig(
21 | maxBuildHistory: Int,
22 | maxParallelBuilds: Int,
23 | enableDocker: Boolean,
24 | dockerCommand: Option[String],
25 | enableDockerCompose: Boolean,
26 | dockerComposeCommand: Option[String]
27 | )
28 |
--------------------------------------------------------------------------------
/src/main/scala/io/github/gitbucket/ci/service/CIService.scala:
--------------------------------------------------------------------------------
1 | package io.github.gitbucket.ci.service
2 |
3 | import gitbucket.core.model.Account
4 | import io.github.gitbucket.ci.manager.BuildManager
5 | import io.github.gitbucket.ci.model._
6 | import io.github.gitbucket.ci.model.Profile._
7 | import gitbucket.core.model.Profile.profile.blockingApi._
8 | import gitbucket.core.service.{AccountService, RepositoryService}
9 | import io.github.gitbucket.ci.util.CIUtils
10 | import org.apache.commons.io.FileUtils
11 | import scala.jdk.CollectionConverters._
12 |
13 | case class BuildJob(
14 | userName: String,
15 | repositoryName: String,
16 | buildUserName: String,
17 | buildRepositoryName: String,
18 | buildNumber: Int,
19 | buildBranch: String,
20 | sha: String,
21 | commitMessage: String,
22 | commitUserName: String,
23 | commitMailAddress: String,
24 | pullRequestId: Option[Int],
25 | queuedTime: java.util.Date,
26 | startTime: Option[java.util.Date],
27 | buildAuthor: Account,
28 | config: CIConfig
29 | )
30 |
31 | object BuildNumberGenerator extends CIService with AccountService with RepositoryService {
32 |
33 | private val map = new scala.collection.mutable.HashMap[(String, String), Int]()
34 |
35 | def generateBuildNumber(userName: String, repositoryName: String)(implicit s: Session): Int = synchronized {
36 | val buildNumber = map.get((userName, repositoryName)).map(_ + 1).getOrElse {
37 | (getCIResults(userName, repositoryName).map(_.buildNumber) match {
38 | case Nil => 0
39 | case seq => seq.max
40 | }) + 1
41 | }
42 |
43 | map.put((userName, repositoryName), buildNumber)
44 |
45 | buildNumber
46 | }
47 |
48 | }
49 |
50 | trait CIService { self: AccountService with RepositoryService =>
51 |
52 | def saveCISystemConfig(config: CISystemConfig)(implicit s: Session): Unit = {
53 | CISystemConfigs.map { t =>
54 | (t.maxBuildHistory, t.maxParallelBuilds, t.enableDocker, t.dockerCommand.?, t.enableDockerCompose, t.dockerComposeCommand.?)
55 | }.update((config.maxBuildHistory, config.maxParallelBuilds, config.enableDocker, config.dockerCommand, config.enableDockerCompose, config.dockerComposeCommand))
56 | }
57 |
58 | def loadCISystemConfig()(implicit s: Session): CISystemConfig = {
59 | CISystemConfigs.first
60 | }
61 |
62 | def saveCIConfig(userName: String, repositoryName: String, config: Option[CIConfig])(implicit s: Session): Unit = {
63 | CIConfigs.filter { t =>
64 | (t.userName === userName.bind) && (t.repositoryName === repositoryName.bind)
65 | }.delete
66 |
67 | config.foreach { config => CIConfigs += config }
68 | }
69 |
70 | def loadCIConfig(userName: String, repositoryName: String)(implicit s: Session): Option[CIConfig] = {
71 | CIConfigs.filter { t =>
72 | (t.userName === userName.bind) && (t.repositoryName === repositoryName.bind)
73 | }.firstOption
74 | }
75 |
76 | def getCIResults(userName: String, repositoryName: String)(implicit s: Session): Seq[CIResult] = {
77 | CIResults.filter { t =>
78 | (t.userName === userName.bind) && (t.repositoryName === repositoryName.bind)
79 | }.list
80 | }
81 |
82 | def getCIResult(userName: String, repositoryName: String, buildNumber: Int)(implicit s: Session): Option[CIResult] = {
83 | CIResults.filter { t =>
84 | (t.userName === userName.bind) && (t.repositoryName === repositoryName.bind) && (t.buildNumber === buildNumber.bind)
85 | }.firstOption
86 | }
87 |
88 | def getLatestCIStatus(userName: String, repositoryName: String, branchName: String)(implicit s: Session): String = {
89 | if (BuildManager.queue.iterator.asScala.exists{ job =>
90 | job.userName == userName && job.repositoryName == repositoryName && job.buildBranch == branchName
91 | }){
92 | "waiting"
93 | } else if ( BuildManager.threads.asScala.exists{ thread =>
94 | thread.runningJob.get.exists { job =>
95 | job.userName == userName && job.repositoryName == repositoryName && job.buildBranch == branchName
96 | }
97 | }){
98 | "running"
99 | } else {
100 | CIResults.filter { t =>
101 | (t.userName === userName.bind) && (t.repositoryName === repositoryName.bind) && (t.buildBranch === branchName.bind)
102 | }.sortBy(_.buildNumber.desc).map{_.status}.firstOption.getOrElse("uknown")
103 | }
104 | }
105 |
106 | def runBuild(userName: String, repositoryName: String, buildUserName: String, buildRepositoryName: String,
107 | buildBranch: String, sha: String, commitMessage: String, commitUserName: String, commitMailAddress: String,
108 | pullRequestId: Option[Int], buildAuthor: Account, config: CIConfig)(implicit s: Session): Unit = {
109 | BuildManager.queueBuildJob(BuildJob(
110 | userName = userName,
111 | repositoryName = repositoryName,
112 | buildUserName = buildUserName,
113 | buildRepositoryName = buildRepositoryName,
114 | buildNumber = BuildNumberGenerator.generateBuildNumber(userName, repositoryName),
115 | buildBranch = buildBranch,
116 | sha = sha,
117 | commitMessage = commitMessage,
118 | commitUserName = commitUserName,
119 | commitMailAddress = commitMailAddress,
120 | pullRequestId = pullRequestId,
121 | queuedTime = new java.util.Date(),
122 | startTime = None,
123 | buildAuthor = buildAuthor,
124 | config = config
125 | ))
126 | }
127 |
128 | def cancelBuild(userName: String, repositoryName: String, buildNumber: Int): Unit = {
129 | BuildManager.threads.asScala.find { thread =>
130 | thread.runningJob.get.exists { job =>
131 | job.userName == userName && job.repositoryName == repositoryName && job.buildNumber == buildNumber
132 | }
133 | }.foreach { thread =>
134 | thread.cancel()
135 | }
136 | }
137 |
138 | def getRunningJobs(userName: String, repositoryName: String): Seq[(BuildJob, StringBuffer)] = {
139 | BuildManager.threads.asScala
140 | .map { thread => (thread, thread.runningJob.get) }
141 | .collect { case (thread, Some(job)) if(job.userName == userName && job.repositoryName == repositoryName) =>
142 | (job, thread.sb)
143 | }
144 | .toSeq
145 | }
146 |
147 | def getQueuedJobs(userName: String, repositoryName: String): Seq[BuildJob] = {
148 | BuildManager.queue.iterator.asScala.filter { job =>
149 | job.userName == userName && job.repositoryName == repositoryName
150 | }.toSeq
151 | }
152 |
153 | def saveCIResult(result: CIResult, output: String, systemConfig: CISystemConfig)(implicit s: Session): Unit = {
154 | // Delete older results
155 | val results = getCIResults(result.userName, result.repositoryName).sortBy(_.buildNumber)
156 | if (results.length >= systemConfig.maxBuildHistory){
157 | results.take(results.length - systemConfig.maxBuildHistory + 1).foreach { result =>
158 | // Delete from database
159 | CIResults.filter { t =>
160 | (t.userName === result.userName.bind) &&
161 | (t.repositoryName === result.repositoryName.bind) &&
162 | (t.buildNumber === result.buildNumber.bind)
163 | }.delete
164 |
165 | // Delete files
166 | val buildDir = CIUtils.getBuildDir(result.userName, result.repositoryName, result.buildNumber)
167 | if(buildDir.exists){
168 | FileUtils.deleteQuietly(buildDir)
169 | }
170 | }
171 | }
172 |
173 | // Insert new result
174 | CIResults += result
175 |
176 | // Save result output as file
177 | val buildDir = CIUtils.getBuildDir(result.userName, result.repositoryName, result.buildNumber)
178 | if(!buildDir.exists){
179 | buildDir.mkdirs()
180 | }
181 | FileUtils.write(new java.io.File(buildDir, "output"), output, "UTF-8")
182 | }
183 |
184 | def getCIResultOutput(result: CIResult): String = {
185 | val buildDir = CIUtils.getBuildDir(result.userName, result.repositoryName, result.buildNumber)
186 | val file = new java.io.File(buildDir, "output")
187 | if(file.exists){
188 | FileUtils.readFileToString(file, "UTF-8")
189 | } else ""
190 | }
191 |
192 | // @deprecated("Use RepositoryService#hasOwnerRole instead.", "1.0.0")
193 | // def hasOwnerRole(owner: String, repository: String, loginAccount: Option[Account])(implicit s: Session): Boolean = {
194 | // loginAccount match {
195 | // case Some(a) if(a.isAdmin) => true
196 | // case Some(a) if(a.userName == owner) => true
197 | // case Some(a) if(getGroupMembers(owner).exists(_.userName == a.userName)) => true
198 | // case Some(a) if(getCollaboratorUserNames(owner, repository, Seq(Role.ADMIN)).contains(a.userName)) => true
199 | // case _ => false
200 | // }
201 | // }
202 |
203 | }
204 |
--------------------------------------------------------------------------------
/src/main/scala/io/github/gitbucket/ci/util/CIUtils.scala:
--------------------------------------------------------------------------------
1 | package io.github.gitbucket.ci.util
2 |
3 | import java.io.{ByteArrayOutputStream, File}
4 |
5 | import gitbucket.core.util.Directory
6 | import org.fusesource.jansi.HtmlAnsiOutputStream
7 |
8 | import scala.util.Using
9 |
10 | object CIUtils {
11 |
12 | val ContextName = "gitbucket-ci"
13 |
14 | def getBuildDir(userName: String, repositoryName: String, buildNumber: Int): File = {
15 | val dir = Directory.getRepositoryFilesDir(userName, repositoryName)
16 | new java.io.File(dir, s"build/${buildNumber}")
17 | }
18 |
19 | def colorize(text: String) = {
20 | Using.resource(new ByteArrayOutputStream()){ os =>
21 | Using.resource(new HtmlAnsiOutputStream(os)){ hos =>
22 | hos.write(text.getBytes("UTF-8"))
23 | }
24 | new String(os.toByteArray, "UTF-8")
25 | }
26 | }
27 |
28 | def isWindows: Boolean = File.separatorChar == '\\'
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/scala/io/github/gitbucket/ci/util/JobStatus.scala:
--------------------------------------------------------------------------------
1 | package io.github.gitbucket.ci.util
2 |
3 | object JobStatus {
4 |
5 | val Running = "running"
6 | val Waiting = "waiting"
7 | val Success = "success"
8 | val Failure = "failure"
9 |
10 | }
11 |
--------------------------------------------------------------------------------
/src/main/twirl/gitbucket/ci/config.scala.html:
--------------------------------------------------------------------------------
1 | @(repository: gitbucket.core.service.RepositoryService.RepositoryInfo,
2 | config: Option[io.github.gitbucket.ci.model.CIConfig],
3 | systemConfig: io.github.gitbucket.ci.model.CISystemConfig,
4 | info: Option[Any])(implicit context: gitbucket.core.controller.Context)
5 | @import gitbucket.core.view.helpers._
6 | @gitbucket.core.html.main(s"Build - ${repository.owner}/${repository.name}", Some(repository)) {
7 | @gitbucket.core.html.menu("settings", repository){
8 | @gitbucket.core.settings.html.menu("build", repository){
9 | @gitbucket.core.helper.html.information(info)
10 |
82 | }
83 | }
84 | }
85 |
116 |
--------------------------------------------------------------------------------
/src/main/twirl/gitbucket/ci/guide.scala.html:
--------------------------------------------------------------------------------
1 | @(repository: gitbucket.core.service.RepositoryService.RepositoryInfo,
2 | isOwner: Boolean)(implicit context: gitbucket.core.controller.Context)
3 | @import gitbucket.core.view.helpers._
4 | @gitbucket.core.html.main(s"Build - ${repository.owner}/${repository.name}", Some(repository)) {
5 | @gitbucket.core.html.menu("build", repository) {
6 | No build configuration for this repository.
7 | @if(isOwner){
8 | You can configure build here.
9 | }
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/src/main/twirl/gitbucket/ci/output.scala.html:
--------------------------------------------------------------------------------
1 | @(repository: gitbucket.core.service.RepositoryService.RepositoryInfo,
2 | status: io.github.gitbucket.ci.controller.CIController.ApiJobStatus,
3 | isEditable: Boolean)(implicit context: gitbucket.core.controller.Context)
4 | @import gitbucket.core.view.helpers._
5 | @import io.github.gitbucket.ci.util.JobStatus
6 | @gitbucket.core.html.main(s"Build #${status.buildNumber} - ${repository.owner}/${repository.name}", Some(repository)) {
7 | @gitbucket.core.html.menu("build", repository) {
8 |
9 |
10 |
11 |
20 |
21 |
22 |
23 | #@{status.buildNumber}
24 | by @user(userName = status.author, styleClass="username strong") at @status.startTime
25 | @if(status.status == JobStatus.Success){
26 | Success
27 | }
28 | @if(status.status == JobStatus.Failure){
29 | Failure
30 | }
31 | @if(status.status == JobStatus.Running){
32 | Success
33 | Failure
34 | Running
35 | }
36 | |
37 |
38 | @status.target
39 | @status.sha.substring(0, 7)
40 | |
41 |
42 | @status.message
43 | @if(status.userName.nonEmpty) {
44 | @user(userName = status.userName, styleClass = "username strong")
45 | } else {
46 | @status.committer
47 | }
48 | |
49 |
50 | @status.duration
51 | Browse files »
52 | |
53 |
54 |
55 |
56 |
Loading...
57 |
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/src/main/twirl/gitbucket/ci/results.scala.html:
--------------------------------------------------------------------------------
1 | @(repository: gitbucket.core.service.RepositoryService.RepositoryInfo,
2 | isEditable: Boolean, isOwner: Boolean)(implicit context: gitbucket.core.controller.Context)
3 | @import gitbucket.core.view.helpers._
4 | @import io.github.gitbucket.ci.util.JobStatus
5 | @gitbucket.core.html.main(s"Build - ${repository.owner}/${repository.name}", Some(repository)) {
6 | @gitbucket.core.html.menu("build", repository) {
7 |
8 |
9 |
10 | @defining(s"${url(repository)}/build/${repository.repository.defaultBranch}/badge.svg") { url =>
11 |
12 |
You can add status badge by this URL: @url
or this link.
13 |
14 | }
15 |
16 |
17 |
18 | @if(isEditable){
19 |
20 |
21 | @if(isOwner){
22 |
23 | }
24 |
25 | }
26 | Build history
27 |
28 |
55 |
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/src/main/twirl/gitbucket/ci/system.scala.html:
--------------------------------------------------------------------------------
1 | @(systemConfig: io.github.gitbucket.ci.model.CISystemConfig)(implicit context: gitbucket.core.controller.Context)
2 | @gitbucket.core.html.main("Build") {
3 | @gitbucket.core.admin.html.menu("build") {
4 |
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/src/main/twirl/gitbucket/ci/workspace.scala.html:
--------------------------------------------------------------------------------
1 | @(repository: gitbucket.core.service.RepositoryService.RepositoryInfo,
2 | buildNumber: Int, pathList: Seq[String], files: Seq[java.io.File])(implicit context: gitbucket.core.controller.Context)
3 | @import gitbucket.core.view.helpers._
4 | @gitbucket.core.html.main(s"Build - ${repository.owner}/${repository.name}", Some(repository)) {
5 | @gitbucket.core.html.menu("build", repository) {
6 |
7 |
Build history >
#@{buildNumber} >
8 | @pathList.zipWithIndex.map { case (path, i) =>
9 | @if(i == pathList.size - 1){
10 | @path /
11 | } else {
12 |
@path /
13 | }
14 | }
15 |
16 |
17 | @files.map { file =>
18 | @if(file.isDirectory){
19 |
20 |
21 |
22 | @file.getName
23 | |
24 |
25 | } else {
26 |
27 |
28 |
29 | @file.getName
30 | |
31 |
32 | }
33 | }
34 |
35 | }
36 | }
--------------------------------------------------------------------------------
/src/test/scala/io/github/gitbucket/ci/MigrationSpec.scala:
--------------------------------------------------------------------------------
1 | package io.github.gitbucket.ci
2 |
3 | import java.sql.DriverManager
4 |
5 | import com.dimafeng.testcontainers.{MySQLContainer, PostgreSQLContainer}
6 | import io.github.gitbucket.solidbase.Solidbase
7 | import io.github.gitbucket.solidbase.model.Module
8 | import liquibase.database.core.{H2Database, MySQLDatabase, PostgresDatabase}
9 | import org.junit.runner.Description
10 | import org.scalatest.{FunSuite, Tag}
11 | import scala.jdk.CollectionConverters._
12 | import org.testcontainers.utility.DockerImageName
13 |
14 | object ExternalDBTest extends Tag("ExternalDBTest")
15 |
16 | class MigrationSpec extends FunSuite {
17 |
18 | val plugin = Class.forName("Plugin").newInstance().asInstanceOf[gitbucket.core.plugin.Plugin]
19 |
20 | test("Migration H2") {
21 | new Solidbase().migrate(
22 | DriverManager.getConnection("jdbc:h2:mem:test", "sa", "sa"),
23 | Thread.currentThread().getContextClassLoader(),
24 | new H2Database(),
25 | new Module(plugin.pluginId, plugin.versions.asJava)
26 | )
27 | }
28 |
29 | implicit private val suiteDescription = Description.createSuiteDescription(getClass)
30 |
31 | Seq("8.0", "5.7").foreach { tag =>
32 | test(s"Migration MySQL $tag", ExternalDBTest) {
33 | val container = new MySQLContainer() {
34 | override val container = new org.testcontainers.containers.MySQLContainer(s"mysql:$tag") {
35 | override def getDriverClassName = "org.mariadb.jdbc.Driver"
36 | }
37 | // TODO https://github.com/testcontainers/testcontainers-java/issues/736
38 | container.withCommand("mysqld --default-authentication-plugin=mysql_native_password")
39 | }
40 | container.start()
41 | try {
42 | new Solidbase().migrate(
43 | DriverManager.getConnection(s"${container.jdbcUrl}?useSSL=false", container.username, container.password),
44 | Thread.currentThread().getContextClassLoader(),
45 | new MySQLDatabase(),
46 | new Module(plugin.pluginId, plugin.versions.asJava)
47 | )
48 | } finally {
49 | container.stop()
50 | }
51 | }
52 | }
53 |
54 | Seq("11", "10").foreach { tag =>
55 | test(s"Migration PostgreSQL $tag", ExternalDBTest) {
56 | val container = PostgreSQLContainer(DockerImageName.parse(s"postgres:$tag"))
57 |
58 | container.start()
59 | try {
60 | new Solidbase().migrate(
61 | DriverManager.getConnection(container.jdbcUrl, container.username, container.password),
62 | Thread.currentThread().getContextClassLoader(),
63 | new PostgresDatabase(),
64 | new Module(plugin.pluginId, plugin.versions.asJava)
65 | )
66 | } finally {
67 | container.stop()
68 | }
69 | }
70 | }
71 |
72 | }
73 |
--------------------------------------------------------------------------------