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 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 10 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 1.11 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. At the time of writing this is Go version 1.12; however, this can be
  38. checked by looking at the
  39. [master `.drone.yml`](https://github.com/go-gitea/gitea/blob/master/.drone.yml)
  40. (At the time of writing
  41. [line 67](https://github.com/go-gitea/gitea/blob/8917d66571a95f3da232a0c27bc1300210d10fde/.drone.yml#L67)
  42. is the relevant line - but this may change.)
  43. ## Downloading and cloning the Gitea source code
  44. Go is quite opinionated about where it expects its source code, and simply
  45. cloning the Gitea repository to an arbitrary path is likely to lead to
  46. problems - the fixing of which is out of scope for this document. Further, some
  47. internal packages are referenced using their respective GitHub URL and at
  48. present we use `vendor/` directories.
  49. The recommended method of obtaining the source code is by using the `go get` command:
  50. ```bash
  51. go get -d code.gitea.io/gitea
  52. cd "$GOPATH/src/code.gitea.io/gitea"
  53. ```
  54. This will clone the Gitea source code to: `"$GOPATH/src/code.gitea.io/gitea"`, or if `$GOPATH`
  55. is not set `"$HOME/go/src/code.gitea.io/gitea"`.
  56. ## Forking Gitea
  57. As stated above, you cannot clone Gitea to an arbitrary path. Download the master Gitea source
  58. code as above. Then, fork the [Gitea repository](https://github.com/go-gitea/gitea) on GitHub,
  59. and either switch the git remote origin for your fork or add your fork as another remote:
  60. ```bash
  61. # Rename original Gitea origin to upstream
  62. cd "$GOPATH/src/code.gitea.io/gitea"
  63. git remote rename origin upstream
  64. git remote add origin "git@github.com:$GITHUB_USERNAME/gitea.git"
  65. git fetch --all --prune
  66. ```
  67. or:
  68. ```bash
  69. # Add new remote for our fork
  70. cd "$GOPATH/src/code.gitea.io/gitea"
  71. git remote add "$FORK_NAME" "git@github.com:$GITHUB_USERNAME/gitea.git"
  72. git fetch --all --prune
  73. ```
  74. To be able to create pull requests, the forked repository should be added as a remote
  75. to the Gitea sources. Otherwise, changes can't be pushed.
  76. ## Building Gitea (Basic)
  77. Take a look at our
  78. <a href='{{< relref "doc/installation/from-source.en-us.md" >}}'>instructions</a>
  79. for <a href='{{< relref "doc/installation/from-source.en-us.md" >}}'>building
  80. from source</a>.
  81. The simplest recommended way to build from source is:
  82. ```bash
  83. TAGS="bindata sqlite sqlite_unlock_notify" make build
  84. ```
  85. However, there are a number of additional make tasks you should be aware of.
  86. These are documented below but you can look at our
  87. [`Makefile`](https://github.com/go-gitea/gitea/blob/master/Makefile) for more,
  88. and look at our
  89. [`.drone.yml`](https://github.com/go-gitea/gitea/blob/master/.drone.yml) to see
  90. how our continuous integration works.
  91. ### Formatting, code analysis and spell check
  92. Our continous integration will reject PRs that are not properly formatted, fail
  93. code analysis or spell check.
  94. You should format your code with `go fmt` using:
  95. ```bash
  96. make fmt
  97. ```
  98. and can test whether your changes would match the results with:
  99. ```bash
  100. make fmt-check # which runs make fmt internally
  101. ```
  102. **Note**: The results of `go fmt` are dependent on the version of `go` present.
  103. You should run the same version of go that is on the continuous integration
  104. server as mentioned above. `make fmt-check` will only check if your `go` would
  105. format differently - this may be different from the CI server version.
  106. You should run revive, vet and spell-check on the code with:
  107. ```bash
  108. make revive vet misspell-check
  109. ```
  110. ### Working on JS and CSS
  111. Edit files in `web_src` and run the linter and build the files in `public`:
  112. ```bash
  113. make webpack
  114. ```
  115. Note: When working on frontend code, it is advisable to set `USE_SERVICE_WORKER` to `false` in `app.ini` which will prevent undesirable caching of frontend assets.
  116. ### Building Images
  117. To build the images, ImageMagick, `inkscape` and `zopflipng` binaries must be available in
  118. your `PATH` to run `make generate-images`.
  119. ### Updating the API
  120. When creating new API routes or modifying existing API routes, you **MUST**
  121. update and/or create [Swagger](https://swagger.io/docs/specification/2-0/what-is-swagger/)
  122. documentation for these using [go-swagger](https://goswagger.io/) comments.
  123. The structure of these comments is described in the [specification](https://goswagger.io/use/spec.html#annotation-syntax).
  124. If you want more information about the Swagger structure, you can look at the
  125. [Swagger 2.0 Documentation](https://swagger.io/docs/specification/2-0/basic-structure/)
  126. 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)
  127. You should be careful not to break the API for downstream users which depend
  128. on a stable API. In general, this means additions are acceptable, but deletions
  129. or fundamental changes to the API will be rejected.
  130. Once you have created or changed an API endpoint, please regenerate the Swagger
  131. documentation using:
  132. ```bash
  133. make generate-swagger
  134. ```
  135. You should validate your generated Swagger file and spell-check it with:
  136. ```bash
  137. make swagger-validate misspell-check
  138. ```
  139. You should commit the changed swagger JSON file. The continous integration
  140. server will check that this has been done using:
  141. ```bash
  142. make swagger-check
  143. ```
  144. **Note**: Please note you should use the Swagger 2.0 documentation, not the
  145. OpenAPI 3 documentation.
  146. ### Creating new configuration options
  147. When creating new configuration options, it is not enough to add them to the
  148. `modules/setting` files. You should add information to `custom/conf/app.ini`
  149. and to the
  150. <a href='{{< relref "doc/advanced/config-cheat-sheet.en-us.md" >}}'>configuration cheat sheet</a>
  151. found in `docs/content/doc/advanced/config-cheat-sheet.en-us.md`
  152. ### Changing the logo
  153. When changing the Gitea logo SVG, you will need to run and commit the results
  154. of:
  155. ```bash
  156. make generate-images
  157. ```
  158. This will create the necessary Gitea favicon and others.
  159. ### Database Migrations
  160. If you make breaking changes to any of the database persisted structs in the
  161. `models/` directory, you will need to make a new migration. These can be found
  162. in `models/migrations/`. You can ensure that your migrations work for the main
  163. database types using:
  164. ```bash
  165. make test-sqlite-migration # with sqlite switched for the appropriate database
  166. ```
  167. ## Testing
  168. There are two types of test run by Gitea: Unit tests and Integration Tests.
  169. ```bash
  170. TAGS="bindata sqlite sqlite_unlock_notify" make test # Runs the unit tests
  171. ```
  172. Unit tests will not and cannot completely test Gitea alone. Therefore, we
  173. have written integration tests; however, these are database dependent.
  174. ```bash
  175. TAGS="bindata sqlite sqlite_unlock_notify" make build test-sqlite
  176. ```
  177. will run the integration tests in an sqlite environment. Other database tests
  178. are available but may need adjustment to the local environment.
  179. Look at
  180. [`integrations/README.md`](https://github.com/go-gitea/gitea/blob/master/integrations/README.md)
  181. for more information and how to run a single test.
  182. Our continuous integration will test the code passes its unit tests and that
  183. all supported databases will pass integration test in a Docker environment.
  184. Migration from several recent versions of Gitea will also be tested.
  185. Please submit your PR with additional tests and integration tests as
  186. appropriate.
  187. ## Documentation for the website
  188. Documentation for the website is found in `docs/`. If you change this you
  189. can test your changes to ensure that they pass continuous integration using:
  190. ```bash
  191. cd "$GOPATH/src/code.gitea.io/gitea/docs"
  192. make trans-copy clean build
  193. ```
  194. You will require a copy of [Hugo](https://gohugo.io/) to run this task. Please
  195. note: this may generate a number of untracked git objects, which will need to
  196. be cleaned up.
  197. ## Visual Studio Code
  198. A `launch.json` and `tasks.json` are provided within `contrib/ide/vscode` for
  199. Visual Studio Code. Look at
  200. [`contrib/ide/README.md`](https://github.com/go-gitea/gitea/blob/master/contrib/ide/README.md)
  201. for more information.
  202. ## Submitting PRs
  203. Once you're happy with your changes, push them up and open a pull request. It
  204. is recommended that you allow Gitea Managers and Owners to modify your PR
  205. branches as we will need to update it to master before merging and/or may be
  206. able to help fix issues directly.
  207. Any PR requires two approvals from the Gitea maintainers and needs to pass the
  208. continous integration. Take a look at our
  209. [`CONTRIBUTING.md`](https://github.com/go-gitea/gitea/blob/master/CONTRIBUTING.md)
  210. document.
  211. If you need more help pop on to [Discord](https://discord.gg/gitea) #Develop
  212. and chat there.
  213. That's it! You are ready to hack on Gitea.