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

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