You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hacking-on-gitea.en-us.md 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. ---
  2. date: "2016-12-01T16:00:00+02:00"
  3. title: "Hacking on Gitea"
  4. slug: "hacking-on-gitea"
  5. weight: 10
  6. toc: false
  7. draft: false
  8. menu:
  9. sidebar:
  10. parent: "developers"
  11. name: "Hacking on Gitea"
  12. weight: 10
  13. identifier: "hacking-on-gitea"
  14. ---
  15. # Hacking on Gitea
  16. **Table of Contents**
  17. {{< toc >}}
  18. ## Installing go
  19. You should [install go](https://golang.org/doc/install) and set up your go
  20. environment correctly.
  21. Next, [install Node.js with npm](https://nodejs.org/en/download/) which is
  22. required to build the JavaScript and CSS files. The minimum supported Node.js
  23. version is {{< min-node-version >}} and the latest LTS version is recommended.
  24. **Note**: When executing make tasks that require external tools, like
  25. `make watch-backend`, Gitea will automatically download and build these as
  26. necessary. To be able to use these you must have the `"$GOPATH"/bin` directory
  27. on the executable path. If you don't add the go bin directory to the
  28. executable path you will have to manage this yourself.
  29. **Note 2**: Go version {{< min-go-version >}} or higher is required.
  30. Gitea uses `gofmt` to format source code. However, the results of
  31. `gofmt` can differ by the version of `go`. Therefore it is
  32. recommended to install the version of Go that our continuous integration is
  33. running. As of last update, the Go version should be {{< go-version >}}.
  34. ## Installing Make
  35. Gitea makes heavy use of Make to automate tasks and improve development. This
  36. guide covers how to install Make.
  37. ### On Linux
  38. Install with the package manager.
  39. On Ubuntu/Debian:
  40. ```bash
  41. sudo apt-get install make
  42. ```
  43. On Fedora/RHEL/CentOS:
  44. ```bash
  45. sudo yum install make
  46. ```
  47. ### On Windows
  48. One of these three distributions of Make will run on Windows:
  49. - [Single binary build](http://www.equation.com/servlet/equation.cmd?fa=make). Copy somewhere and add to `PATH`.
  50. - [32-bits version](http://www.equation.com/ftpdir/make/32/make.exe)
  51. - [64-bits version](http://www.equation.com/ftpdir/make/64/make.exe)
  52. - [MinGW-w64](https://www.mingw-w64.org) / [MSYS2](https://www.msys2.org/).
  53. - MSYS2 is a collection of tools and libraries providing you with an easy-to-use environment for building, installing and running native Windows software, it includes MinGW-w64.
  54. - In MingGW-w64, the binary is called `mingw32-make.exe` instead of `make.exe`. Add the `bin` folder to `PATH`.
  55. - In MSYS2, you can use `make` directly. See [MSYS2 Porting](https://www.msys2.org/wiki/Porting/).
  56. - To compile Gitea with CGO_ENABLED (eg: SQLite3), you might need to use [tdm-gcc](https://jmeubank.github.io/tdm-gcc/) instead of MSYS2 gcc, because MSYS2 gcc headers lack some Windows-only CRT functions like `_beginthread`.
  57. - [Chocolatey package](https://chocolatey.org/packages/make). Run `choco install make`
  58. **Note**: If you are attempting to build using make with Windows Command Prompt, you may run into issues. The above prompts (Git bash, or MinGW) are recommended, however if you only have command prompt (or potentially PowerShell) you can set environment variables using the [set](https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/set_1) command, e.g. `set TAGS=bindata`.
  59. ## Downloading and cloning the Gitea source code
  60. The recommended method of obtaining the source code is by using `git clone`.
  61. ```bash
  62. git clone https://github.com/go-gitea/gitea
  63. ```
  64. (Since the advent of go modules, it is no longer necessary to build go projects
  65. from within the `$GOPATH`, hence the `go get` approach is no longer recommended.)
  66. ## Forking Gitea
  67. Download the main Gitea source code as above. Then, fork the
  68. [Gitea repository](https://github.com/go-gitea/gitea) on GitHub,
  69. and either switch the git remote origin for your fork or add your fork as another remote:
  70. ```bash
  71. # Rename original Gitea origin to upstream
  72. git remote rename origin upstream
  73. git remote add origin "git@github.com:$GITHUB_USERNAME/gitea.git"
  74. git fetch --all --prune
  75. ```
  76. or:
  77. ```bash
  78. # Add new remote for our fork
  79. git remote add "$FORK_NAME" "git@github.com:$GITHUB_USERNAME/gitea.git"
  80. git fetch --all --prune
  81. ```
  82. To be able to create pull requests, the forked repository should be added as a remote
  83. to the Gitea sources. Otherwise, changes can't be pushed.
  84. ## Building Gitea (Basic)
  85. Take a look at our
  86. <a href='{{< relref "doc/installation/from-source.en-us.md" >}}'>instructions</a>
  87. for <a href='{{< relref "doc/installation/from-source.en-us.md" >}}'>building
  88. from source</a>.
  89. The simplest recommended way to build from source is:
  90. ```bash
  91. TAGS="bindata sqlite sqlite_unlock_notify" make build
  92. ```
  93. The `build` target will execute both `frontend` and `backend` sub-targets. If the `bindata` tag is present, the frontend files will be compiled into the binary. It is recommended to leave out the tag when doing frontend development so that changes will be reflected.
  94. See `make help` for all available `make` targets. Also see [`.drone.yml`](https://github.com/go-gitea/gitea/blob/main/.drone.yml) to see how our continuous integration works.
  95. ## Building continuously
  96. To run and continuously rebuild when source files change:
  97. ```bash
  98. # for both frontend and backend
  99. make watch
  100. # or: watch frontend files (html/js/css) only
  101. make watch-frontend
  102. # or: watch backend files (go) only
  103. make watch-backend
  104. ```
  105. On macOS, watching all backend source files may hit the default open files limit which can be increased via `ulimit -n 12288` for the current shell or in your shell startup file for all future shells.
  106. ### Formatting, code analysis and spell check
  107. Our continuous integration will reject PRs that fail the code linters (including format check, code analysis and spell check).
  108. You should format your code:
  109. ```bash
  110. make fmt
  111. ```
  112. and lint the source code:
  113. ```bash
  114. # lint both frontend and backend code
  115. make lint
  116. # lint only backend code
  117. make lint-backend
  118. ```
  119. **Note**: The results of `gofmt` are dependent on the version of `go` present.
  120. You should run the same version of go that is on the continuous integration
  121. server as mentioned above.
  122. ### Working on JS and CSS
  123. Frontend development should follow [Guidelines for Frontend Development](./guidelines-frontend.md)
  124. To build with frontend resources, either use the `watch-frontend` target mentioned above or just build once:
  125. ```bash
  126. make build && ./gitea
  127. ```
  128. Before committing, make sure the linters pass:
  129. ```bash
  130. make lint-frontend
  131. ```
  132. ### Configuring local ElasticSearch instance
  133. Start local ElasticSearch instance using docker:
  134. ```sh
  135. mkdir -p $(pwd)/data/elasticsearch
  136. sudo chown -R 1000:1000 $(pwd)/data/elasticsearch
  137. docker run --rm --memory="4g" -p 127.0.0.1:9200:9200 -p 127.0.0.1:9300:9300 -e "discovery.type=single-node" -v "$(pwd)/data/elasticsearch:/usr/share/elasticsearch/data" docker.elastic.co/elasticsearch/elasticsearch:7.16.3
  138. ```
  139. Configure `app.ini`:
  140. ```ini
  141. [indexer]
  142. ISSUE_INDEXER_TYPE = elasticsearch
  143. ISSUE_INDEXER_CONN_STR = http://elastic:changeme@localhost:9200
  144. REPO_INDEXER_ENABLED = true
  145. REPO_INDEXER_TYPE = elasticsearch
  146. REPO_INDEXER_CONN_STR = http://elastic:changeme@localhost:9200
  147. ```
  148. ### Building and adding SVGs
  149. SVG icons are built using the `make svg` target which compiles the icon sources defined in `build/generate-svg.js` into the output directory `public/img/svg`. Custom icons can be added in the `web_src/svg` directory.
  150. ### Building the Logo
  151. The PNG and SVG versions of the Gitea logo are built from a single SVG source file `assets/logo.svg` using the `TAGS="gitea" make generate-images` target. To run it, Node.js and npm must be available.
  152. The same process can also be used to generate custom logo PNGs from a SVG source file by updating `assets/logo.svg` and running `make generate-images`. Omitting the `gitea` tag will update only the user-designated logo files.
  153. ### Updating the API
  154. When creating new API routes or modifying existing API routes, you **MUST**
  155. update and/or create [Swagger](https://swagger.io/docs/specification/2-0/what-is-swagger/)
  156. documentation for these using [go-swagger](https://goswagger.io/) comments.
  157. The structure of these comments is described in the [specification](https://goswagger.io/use/spec.html#annotation-syntax).
  158. If you want more information about the Swagger structure, you can look at the
  159. [Swagger 2.0 Documentation](https://swagger.io/docs/specification/2-0/basic-structure/)
  160. or compare with a previous PR adding a new API endpoint, e.g. [PR #5483](https://github.com/go-gitea/gitea/pull/5843/files#diff-2e0a7b644cf31e1c8ef7d76b444fe3aaR20)
  161. You should be careful not to break the API for downstream users which depend
  162. on a stable API. In general, this means additions are acceptable, but deletions
  163. or fundamental changes to the API will be rejected.
  164. Once you have created or changed an API endpoint, please regenerate the Swagger
  165. documentation using:
  166. ```bash
  167. make generate-swagger
  168. ```
  169. You should validate your generated Swagger file and spell-check it with:
  170. ```bash
  171. make swagger-validate misspell-check
  172. ```
  173. You should commit the changed swagger JSON file. The continuous integration
  174. server will check that this has been done using:
  175. ```bash
  176. make swagger-check
  177. ```
  178. **Note**: Please note you should use the Swagger 2.0 documentation, not the
  179. OpenAPI 3 documentation.
  180. ### Creating new configuration options
  181. When creating new configuration options, it is not enough to add them to the
  182. `modules/setting` files. You should add information to `custom/conf/app.ini`
  183. and to the
  184. <a href='{{< relref "doc/advanced/config-cheat-sheet.en-us.md" >}}'>configuration cheat sheet</a>
  185. found in `docs/content/doc/advanced/config-cheat-sheet.en-us.md`
  186. ### Changing the logo
  187. When changing the Gitea logo SVG, you will need to run and commit the results
  188. of:
  189. ```bash
  190. make generate-images
  191. ```
  192. This will create the necessary Gitea favicon and others.
  193. ### Database Migrations
  194. If you make breaking changes to any of the database persisted structs in the
  195. `models/` directory, you will need to make a new migration. These can be found
  196. in `models/migrations/`. You can ensure that your migrations work for the main
  197. database types using:
  198. ```bash
  199. make test-sqlite-migration # with SQLite switched for the appropriate database
  200. ```
  201. ## Testing
  202. There are two types of test run by Gitea: Unit tests and Integration Tests.
  203. ### Unit Tests
  204. Unit tests are covered by `*_test.go` in `go test` system.
  205. You can set the environment variable `GITEA_UNIT_TESTS_LOG_SQL=1` to display all SQL statements when running the tests in verbose mode (i.e. when `GOTESTFLAGS=-v` is set).
  206. ```bash
  207. TAGS="bindata sqlite sqlite_unlock_notify" make test # Runs the unit tests
  208. ```
  209. ### Integration Tests
  210. Unit tests will not and cannot completely test Gitea alone. Therefore, we
  211. have written integration tests; however, these are database dependent.
  212. ```bash
  213. TAGS="bindata sqlite sqlite_unlock_notify" make build test-sqlite
  214. ```
  215. will run the integration tests in an SQLite environment. Integration tests
  216. require `git lfs` to be installed. Other database tests are available but
  217. may need adjustment to the local environment.
  218. Take a look at [`integrations/README.md`](https://github.com/go-gitea/gitea/blob/main/integrations/README.md)
  219. for more information and how to run a single test.
  220. ### Testing for a PR
  221. Our continuous integration will test the code passes its unit tests and that
  222. all supported databases will pass integration test in a Docker environment.
  223. Migration from several recent versions of Gitea will also be tested.
  224. Please submit your PR with additional tests and integration tests as
  225. appropriate.
  226. ## Documentation for the website
  227. Documentation for the website is found in `docs/`. If you change this you
  228. can test your changes to ensure that they pass continuous integration using:
  229. ```bash
  230. # from the docs directory within Gitea
  231. make trans-copy clean build
  232. ```
  233. You will require a copy of [Hugo](https://gohugo.io/) to run this task. Please
  234. note: this may generate a number of untracked Git objects, which will need to
  235. be cleaned up.
  236. ## Visual Studio Code
  237. A `launch.json` and `tasks.json` are provided within `contrib/ide/vscode` for
  238. Visual Studio Code. Look at
  239. [`contrib/ide/README.md`](https://github.com/go-gitea/gitea/blob/main/contrib/ide/README.md)
  240. for more information.
  241. ## GoLand
  242. Clicking the `Run Application` arrow on the function `func main()` in `/main.go`
  243. can quickly start a debuggable Gitea instance.
  244. The `Output Directory` in `Run/Debug Configuration` MUST be set to the
  245. gitea project directory (which contains `main.go` and `go.mod`),
  246. otherwise, the started instance's working directory is a GoLand's temporary directory
  247. and prevents Gitea from loading dynamic resources (eg: templates) in a development environment.
  248. To run unit tests with SQLite in GoLand, set `-tags sqlite,sqlite_unlock_notify`
  249. in `Go tool arguments` of `Run/Debug Configuration`.
  250. ## Submitting PRs
  251. Once you're happy with your changes, push them up and open a pull request. It
  252. is recommended that you allow Gitea Managers and Owners to modify your PR
  253. branches as we will need to update it to main before merging and/or may be
  254. able to help fix issues directly.
  255. Any PR requires two approvals from the Gitea maintainers and needs to pass the
  256. continuous integration. Take a look at our
  257. [`CONTRIBUTING.md`](https://github.com/go-gitea/gitea/blob/main/CONTRIBUTING.md)
  258. document.
  259. If you need more help pop on to [Discord](https://discord.gg/gitea) #Develop
  260. and chat there.
  261. That's it! You are ready to hack on Gitea.