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.

296 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 Images
  114. To build the images, ImageMagick, `inkscape` and `zopflipng` binaries must be available in
  115. your `PATH` to run `make generate-images`.
  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.