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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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.11 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.12; 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 - but 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 CSS
  108. To generate the CSS, you will need [Node.js](https://nodejs.org/) 8.0 or greater with npm. At present we use [less](http://lesscss.org/) and [postcss](https://postcss.org) to generate our CSS. Do **not** edit the files in `public/css` directly, as they are generated from `lessc` from the files in `public/less`.
  109. Edit files in `public/less`, run the linter, regenerate the CSS and commit all changed files:
  110. ```bash
  111. make css
  112. ```
  113. ### Updating JS
  114. To run the JavaScript linter you will need [Node.js](https://nodejs.org/) 8.0 or greater with npm. Edit files in `public/js` and run the linter:
  115. ```bash
  116. make js
  117. ```
  118. ### Updating the API
  119. When creating new API routes or modifying existing API routes, you **MUST**
  120. update and/or create [Swagger](https://swagger.io/docs/specification/2-0/what-is-swagger/)
  121. documentation for these using [go-swagger](https://goswagger.io/) comments.
  122. The structure of these comments is described in the [specification](https://goswagger.io/use/spec.html#annotation-syntax).
  123. If you want more information about the Swagger structure, you can look at the
  124. [Swagger 2.0 Documentation](https://swagger.io/docs/specification/2-0/basic-structure/)
  125. 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)
  126. You should be careful not to break the API for downstream users which depend
  127. on a stable API. In general, this means additions are acceptable, but deletions
  128. or fundamental changes to the API will be rejected.
  129. Once you have created or changed an API endpoint, please regenerate the Swagger
  130. documentation using:
  131. ```bash
  132. make generate-swagger
  133. ```
  134. You should validate your generated Swagger file and spell-check it with:
  135. ```bash
  136. make swagger-validate mispell-check
  137. ```
  138. You should commit the changed swagger JSON file. The continous integration
  139. server will check that this has been done using:
  140. ```bash
  141. make swagger-check
  142. ```
  143. **Note**: Please note you should use the Swagger 2.0 documentation, not the
  144. OpenAPI 3 documentation.
  145. ### Creating new configuration options
  146. When creating new configuration options, it is not enough to add them to the
  147. `modules/setting` files. You should add information to `custom/conf/app.ini`
  148. and to the
  149. <a href='{{< relref "doc/advanced/config-cheat-sheet.en-us.md" >}}'>configuration cheat sheet</a>
  150. found in `docs/content/doc/advanced/config-cheat-sheet.en-us.md`
  151. ### Changing the logo
  152. When changing the Gitea logo SVG, you will need to run and commit the results
  153. of:
  154. ```bash
  155. make generate-images
  156. ```
  157. This will create the necessary Gitea favicon and others.
  158. ### Database Migrations
  159. If you make breaking changes to any of the database persisted structs in the
  160. `models/` directory, you will need to make a new migration. These can be found
  161. in `models/migrations/`. You can ensure that your migrations work for the main
  162. database types using:
  163. ```bash
  164. make test-sqlite-migration # with sqlite switched for the appropriate database
  165. ```
  166. ## Testing
  167. There are two types of test run by Gitea: Unit tests and Integration Tests.
  168. ```bash
  169. TAGS="bindata sqlite sqlite_unlock_notify" make test # Runs the unit tests
  170. ```
  171. Unit tests will not and cannot completely test Gitea alone. Therefore, we
  172. have written integration tests; however, these are database dependent.
  173. ```bash
  174. TAGS="bindata sqlite sqlite_unlock_notify" make generate build test-sqlite
  175. ```
  176. will run the integration tests in an sqlite environment. Other database tests
  177. are available but may need adjustment to the local environment.
  178. Look at
  179. [`integrations/README.md`](https://github.com/go-gitea/gitea/blob/master/integrations/README.md)
  180. for more information and how to run a single test.
  181. Our continuous integration will test the code passes its unit tests and that
  182. all supported databases will pass integration test in a Docker environment.
  183. Migration from several recent versions of Gitea will also be tested.
  184. Please submit your PR with additional tests and integration tests as
  185. appropriate.
  186. ## Documentation for the website
  187. Documentation for the website is found in `docs/`. If you change this you
  188. can test your changes to ensure that they pass continuous integration using:
  189. ```bash
  190. cd "$GOPATH/src/code.gitea.io/gitea/docs"
  191. make trans-copy clean build
  192. ```
  193. You will require a copy of [Hugo](https://gohugo.io/) to run this task. Please
  194. note: this may generate a number of untracked git objects, which will need to
  195. be cleaned up.
  196. ## Visual Studio Code
  197. A `launch.json` and `tasks.json` are provided within `contrib/ide/vscode` for
  198. Visual Studio Code. Look at
  199. [`contrib/ide/README.md`](https://github.com/go-gitea/gitea/blob/master/contrib/ide/README.md)
  200. for more information.
  201. ## Submitting PRs
  202. Once you're happy with your changes, push them up and open a pull request. It
  203. is recommended that you allow Gitea Managers and Owners to modify your PR
  204. branches as we will need to update it to master before merging and/or may be
  205. able to help fix issues directly.
  206. Any PR requires two approvals from the Gitea maintainers and needs to pass the
  207. continous integration. Take a look at our
  208. [`CONTRIBUTING.md`](https://github.com/go-gitea/gitea/blob/master/CONTRIBUTING.md)
  209. document.
  210. If you need more help pop on to [Discord](https://discord.gg/gitea) #Develop
  211. and chat there.
  212. That's it! You are ready to hack on Gitea.