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.

295 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. See `make help` for all available `make` tasks. Also see [`.drone.yml`](https://github.com/go-gitea/gitea/blob/master/.drone.yml) to see how our continuous integration works.
  73. ### Formatting, code analysis and spell check
  74. Our continuous integration will reject PRs that are not properly formatted, fail
  75. code analysis or spell check.
  76. You should format your code with `go fmt` using:
  77. ```bash
  78. make fmt
  79. ```
  80. and can test whether your changes would match the results with:
  81. ```bash
  82. make fmt-check # which runs make fmt internally
  83. ```
  84. **Note**: The results of `go fmt` are dependent on the version of `go` present.
  85. You should run the same version of go that is on the continuous integration
  86. server as mentioned above. `make fmt-check` will only check if your `go` would
  87. format differently - this may be different from the CI server version.
  88. You should run revive, vet and spell-check on the code with:
  89. ```bash
  90. make revive vet misspell-check
  91. ```
  92. ### Working on JS and CSS
  93. For simple changes, edit files in `web_src`, run the build and start the server to test:
  94. ```bash
  95. make build && ./gitea
  96. ```
  97. `make build` runs both `make frontend` and `make backend` which can be run individually as well as long as the `bindata` tag is not used (which compiles frontend files into the binary).
  98. For more involved changes use the `watch-frontend` task to continuously rebuild files when their sources change. The `bindata` tag must be absent. First, build and run the backend:
  99. ```bash
  100. make backend && ./gitea
  101. ```
  102. With the backend running, open another terminal and run:
  103. ```bash
  104. make watch-frontend
  105. ```
  106. Before committing, make sure the linters pass:
  107. ```bash
  108. make lint-frontend
  109. ```
  110. Note: When working on frontend code, set `USE_SERVICE_WORKER` to `false` in `app.ini` to prevent undesirable caching of frontend assets.
  111. ### Building and adding SVGs
  112. 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.
  113. ### Building the Logo
  114. 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.
  115. ### Updating the API
  116. When creating new API routes or modifying existing API routes, you **MUST**
  117. update and/or create [Swagger](https://swagger.io/docs/specification/2-0/what-is-swagger/)
  118. documentation for these using [go-swagger](https://goswagger.io/) comments.
  119. The structure of these comments is described in the [specification](https://goswagger.io/use/spec.html#annotation-syntax).
  120. If you want more information about the Swagger structure, you can look at the
  121. [Swagger 2.0 Documentation](https://swagger.io/docs/specification/2-0/basic-structure/)
  122. 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)
  123. You should be careful not to break the API for downstream users which depend
  124. on a stable API. In general, this means additions are acceptable, but deletions
  125. or fundamental changes to the API will be rejected.
  126. Once you have created or changed an API endpoint, please regenerate the Swagger
  127. documentation using:
  128. ```bash
  129. make generate-swagger
  130. ```
  131. You should validate your generated Swagger file and spell-check it with:
  132. ```bash
  133. make swagger-validate misspell-check
  134. ```
  135. You should commit the changed swagger JSON file. The continous integration
  136. server will check that this has been done using:
  137. ```bash
  138. make swagger-check
  139. ```
  140. **Note**: Please note you should use the Swagger 2.0 documentation, not the
  141. OpenAPI 3 documentation.
  142. ### Creating new configuration options
  143. When creating new configuration options, it is not enough to add them to the
  144. `modules/setting` files. You should add information to `custom/conf/app.ini`
  145. and to the
  146. <a href='{{< relref "doc/advanced/config-cheat-sheet.en-us.md" >}}'>configuration cheat sheet</a>
  147. found in `docs/content/doc/advanced/config-cheat-sheet.en-us.md`
  148. ### Changing the logo
  149. When changing the Gitea logo SVG, you will need to run and commit the results
  150. of:
  151. ```bash
  152. make generate-images
  153. ```
  154. This will create the necessary Gitea favicon and others.
  155. ### Database Migrations
  156. If you make breaking changes to any of the database persisted structs in the
  157. `models/` directory, you will need to make a new migration. These can be found
  158. in `models/migrations/`. You can ensure that your migrations work for the main
  159. database types using:
  160. ```bash
  161. make test-sqlite-migration # with sqlite switched for the appropriate database
  162. ```
  163. ## Testing
  164. There are two types of test run by Gitea: Unit tests and Integration Tests.
  165. ```bash
  166. TAGS="bindata sqlite sqlite_unlock_notify" make test # Runs the unit tests
  167. ```
  168. Unit tests will not and cannot completely test Gitea alone. Therefore, we
  169. have written integration tests; however, these are database dependent.
  170. ```bash
  171. TAGS="bindata sqlite sqlite_unlock_notify" make build test-sqlite
  172. ```
  173. will run the integration tests in an sqlite environment. Integration tests
  174. require `git lfs` to be installed. Other database tests are available but
  175. may need adjustment to the local environment.
  176. Look at
  177. [`integrations/README.md`](https://github.com/go-gitea/gitea/blob/master/integrations/README.md)
  178. for more information and how to run a single test.
  179. Our continuous integration will test the code passes its unit tests and that
  180. all supported databases will pass integration test in a Docker environment.
  181. Migration from several recent versions of Gitea will also be tested.
  182. Please submit your PR with additional tests and integration tests as
  183. appropriate.
  184. ## Documentation for the website
  185. Documentation for the website is found in `docs/`. If you change this you
  186. can test your changes to ensure that they pass continuous integration using:
  187. ```bash
  188. # from the docs directory within Gitea
  189. make trans-copy clean build
  190. ```
  191. You will require a copy of [Hugo](https://gohugo.io/) to run this task. Please
  192. note: this may generate a number of untracked git objects, which will need to
  193. be cleaned up.
  194. ## Visual Studio Code
  195. A `launch.json` and `tasks.json` are provided within `contrib/ide/vscode` for
  196. Visual Studio Code. Look at
  197. [`contrib/ide/README.md`](https://github.com/go-gitea/gitea/blob/master/contrib/ide/README.md)
  198. for more information.
  199. ## Submitting PRs
  200. Once you're happy with your changes, push them up and open a pull request. It
  201. is recommended that you allow Gitea Managers and Owners to modify your PR
  202. branches as we will need to update it to master before merging and/or may be
  203. able to help fix issues directly.
  204. Any PR requires two approvals from the Gitea maintainers and needs to pass the
  205. continous integration. Take a look at our
  206. [`CONTRIBUTING.md`](https://github.com/go-gitea/gitea/blob/master/CONTRIBUTING.md)
  207. document.
  208. If you need more help pop on to [Discord](https://discord.gg/gitea) #Develop
  209. and chat there.
  210. That's it! You are ready to hack on Gitea.