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.

297 lines
10 KiB

  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: "advanced"
  11. name: "Hacking on Gitea"
  12. weight: 10
  13. identifier: "hacking-on-gitea"
  14. ---
  15. # Hacking on Gitea
  16. ## Installing go and setting the GOPATH
  17. You should [install go](https://golang.org/doc/install) and set up your go
  18. environment correctly. In particular, it is recommended to set the `$GOPATH`
  19. environment variable and to add the go bin directory or directories
  20. `${GOPATH//://bin:}/bin` to the `$PATH`. See the Go wiki entry for
  21. [GOPATH](https://github.com/golang/go/wiki/GOPATH).
  22. Next, [install Node.js with npm](https://nodejs.org/en/download/) which is
  23. required to build the JavaScript and CSS files. The minimum supported Node.js
  24. version is {{< min-node-version >}} and the latest LTS version is recommended.
  25. You will also need make.
  26. <a href='{{< relref "doc/advanced/make.en-us.md" >}}'>(See here how to get Make)</a>
  27. **Note**: When executing make tasks that require external tools, like
  28. `make misspell-check`, Gitea will automatically download and build these as
  29. necessary. To be able to use these you must have the `"$GOPATH"/bin` directory
  30. on the executable path. If you don't add the go bin directory to the
  31. executable path you will have to manage this yourself.
  32. **Note 2**: Go version {{< min-go-version >}} or higher is required; however, it is important
  33. to note that our continuous integration will check that the formatting of the
  34. source code is not changed by `gofmt` using `make fmt-check`. Unfortunately,
  35. the results of `gofmt` can differ by the version of `go`. It is therefore
  36. recommended to install the version of Go that our continuous integration is
  37. running. As of last update, it should be Go version {{< go-version >}}.
  38. ## Downloading and cloning the Gitea source code
  39. The recommended method of obtaining the source code is by using `git clone`.
  40. ```bash
  41. git clone https://github.com/go-gitea/gitea
  42. ```
  43. (Since the advent of go modules, it is no longer necessary to build go projects
  44. from within the `$GOPATH`, hence the `go get` approach is no longer recommended.)
  45. ## Forking Gitea
  46. Download the master Gitea source code as above. Then, fork the
  47. [Gitea repository](https://github.com/go-gitea/gitea) on GitHub,
  48. and either switch the git remote origin for your fork or add your fork as another remote:
  49. ```bash
  50. # Rename original Gitea origin to upstream
  51. git remote rename origin upstream
  52. git remote add origin "git@github.com:$GITHUB_USERNAME/gitea.git"
  53. git fetch --all --prune
  54. ```
  55. or:
  56. ```bash
  57. # Add new remote for our fork
  58. git remote add "$FORK_NAME" "git@github.com:$GITHUB_USERNAME/gitea.git"
  59. git fetch --all --prune
  60. ```
  61. To be able to create pull requests, the forked repository should be added as a remote
  62. to the Gitea sources. Otherwise, changes can't be pushed.
  63. ## Building Gitea (Basic)
  64. Take a look at our
  65. <a href='{{< relref "doc/installation/from-source.en-us.md" >}}'>instructions</a>
  66. for <a href='{{< relref "doc/installation/from-source.en-us.md" >}}'>building
  67. from source</a>.
  68. The simplest recommended way to build from source is:
  69. ```bash
  70. TAGS="bindata sqlite sqlite_unlock_notify" make build
  71. ```
  72. 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.
  73. See `make help` for all available `make` targets. Also see [`.drone.yml`](https://github.com/go-gitea/gitea/blob/master/.drone.yml) to see how our continuous integration works.
  74. ## Building continuously
  75. Both the `frontend` and `backend` targets can be ran continuously when source files change:
  76. ````bash
  77. # in your first terminal
  78. make watch-backend
  79. # in your second terminal
  80. make watch-frontend
  81. ````
  82. 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.
  83. ### Formatting, code analysis and spell check
  84. Our continuous integration will reject PRs that are not properly formatted, fail
  85. code analysis or spell check.
  86. You should format your code with `go fmt` using:
  87. ```bash
  88. make fmt
  89. ```
  90. and can test whether your changes would match the results with:
  91. ```bash
  92. make fmt-check # which runs make fmt internally
  93. ```
  94. **Note**: The results of `go fmt` are dependent on the version of `go` present.
  95. You should run the same version of go that is on the continuous integration
  96. server as mentioned above. `make fmt-check` will only check if your `go` would
  97. format differently - this may be different from the CI server version.
  98. You should run revive, vet and spell-check on the code with:
  99. ```bash
  100. make revive vet misspell-check
  101. ```
  102. ### Working on JS and CSS
  103. Either use the `watch-frontend` target mentioned above or just build once:
  104. ```bash
  105. make build && ./gitea
  106. ```
  107. Before committing, make sure the linters pass:
  108. ```bash
  109. make lint-frontend
  110. ```
  111. Note: When working on frontend code, set `USE_SERVICE_WORKER` to `false` in `app.ini` to prevent undesirable caching of frontend assets.
  112. ### Building and adding SVGs
  113. 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.
  114. ### Building the Logo
  115. The PNG versions of the logo are built from a single SVG source file `assets/logo.svg` using the `make generate-images` target. To run it, Node.js and npm must be available. The same process can also be used to generate a custom logo PNGs from a SVG source file. It's possible to remove parts of the SVG logo for the favicon build by adding a `detail-remove` class to the SVG nodes to be removed.
  116. ### Updating the API
  117. When creating new API routes or modifying existing API routes, you **MUST**
  118. update and/or create [Swagger](https://swagger.io/docs/specification/2-0/what-is-swagger/)
  119. documentation for these using [go-swagger](https://goswagger.io/) comments.
  120. The structure of these comments is described in the [specification](https://goswagger.io/use/spec.html#annotation-syntax).
  121. If you want more information about the Swagger structure, you can look at the
  122. [Swagger 2.0 Documentation](https://swagger.io/docs/specification/2-0/basic-structure/)
  123. 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)
  124. You should be careful not to break the API for downstream users which depend
  125. on a stable API. In general, this means additions are acceptable, but deletions
  126. or fundamental changes to the API will be rejected.
  127. Once you have created or changed an API endpoint, please regenerate the Swagger
  128. documentation using:
  129. ```bash
  130. make generate-swagger
  131. ```
  132. You should validate your generated Swagger file and spell-check it with:
  133. ```bash
  134. make swagger-validate misspell-check
  135. ```
  136. You should commit the changed swagger JSON file. The continous integration
  137. server will check that this has been done using:
  138. ```bash
  139. make swagger-check
  140. ```
  141. **Note**: Please note you should use the Swagger 2.0 documentation, not the
  142. OpenAPI 3 documentation.
  143. ### Creating new configuration options
  144. When creating new configuration options, it is not enough to add them to the
  145. `modules/setting` files. You should add information to `custom/conf/app.ini`
  146. and to the
  147. <a href='{{< relref "doc/advanced/config-cheat-sheet.en-us.md" >}}'>configuration cheat sheet</a>
  148. found in `docs/content/doc/advanced/config-cheat-sheet.en-us.md`
  149. ### Changing the logo
  150. When changing the Gitea logo SVG, you will need to run and commit the results
  151. of:
  152. ```bash
  153. make generate-images
  154. ```
  155. This will create the necessary Gitea favicon and others.
  156. ### Database Migrations
  157. If you make breaking changes to any of the database persisted structs in the
  158. `models/` directory, you will need to make a new migration. These can be found
  159. in `models/migrations/`. You can ensure that your migrations work for the main
  160. database types using:
  161. ```bash
  162. make test-sqlite-migration # with sqlite switched for the appropriate database
  163. ```
  164. ## Testing
  165. There are two types of test run by Gitea: Unit tests and Integration Tests.
  166. ```bash
  167. TAGS="bindata sqlite sqlite_unlock_notify" make test # Runs the unit tests
  168. ```
  169. Unit tests will not and cannot completely test Gitea alone. Therefore, we
  170. have written integration tests; however, these are database dependent.
  171. ```bash
  172. TAGS="bindata sqlite sqlite_unlock_notify" make build test-sqlite
  173. ```
  174. will run the integration tests in an sqlite environment. Integration tests
  175. require `git lfs` to be installed. Other database tests are available but
  176. may need adjustment to the local environment.
  177. Look at
  178. [`integrations/README.md`](https://github.com/go-gitea/gitea/blob/master/integrations/README.md)
  179. for more information and how to run a single test.
  180. Our continuous integration will test the code passes its unit tests and that
  181. all supported databases will pass integration test in a Docker environment.
  182. Migration from several recent versions of Gitea will also be tested.
  183. Please submit your PR with additional tests and integration tests as
  184. appropriate.
  185. ## Documentation for the website
  186. Documentation for the website is found in `docs/`. If you change this you
  187. can test your changes to ensure that they pass continuous integration using:
  188. ```bash
  189. # from the docs directory within Gitea
  190. make trans-copy clean build
  191. ```
  192. You will require a copy of [Hugo](https://gohugo.io/) to run this task. Please
  193. note: this may generate a number of untracked git objects, which will need to
  194. be cleaned up.
  195. ## Visual Studio Code
  196. A `launch.json` and `tasks.json` are provided within `contrib/ide/vscode` for
  197. Visual Studio Code. Look at
  198. [`contrib/ide/README.md`](https://github.com/go-gitea/gitea/blob/master/contrib/ide/README.md)
  199. for more information.
  200. ## Submitting PRs
  201. Once you're happy with your changes, push them up and open a pull request. It
  202. is recommended that you allow Gitea Managers and Owners to modify your PR
  203. branches as we will need to update it to master before merging and/or may be
  204. able to help fix issues directly.
  205. Any PR requires two approvals from the Gitea maintainers and needs to pass the
  206. continous integration. Take a look at our
  207. [`CONTRIBUTING.md`](https://github.com/go-gitea/gitea/blob/master/CONTRIBUTING.md)
  208. document.
  209. If you need more help pop on to [Discord](https://discord.gg/gitea) #Develop
  210. and chat there.
  211. That's it! You are ready to hack on Gitea.