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

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