code.gitea.io/gitea@v1.22.3/docs/content/development/hacking-on-gitea.en-us.md (about) 1 --- 2 date: "2016-12-01T16:00:00+02:00" 3 title: "Hacking on Gitea" 4 slug: "hacking-on-gitea" 5 sidebar_position: 10 6 toc: false 7 draft: false 8 aliases: 9 - /en-us/hacking-on-gitea 10 menu: 11 sidebar: 12 parent: "development" 13 name: "Hacking on Gitea" 14 sidebar_position: 10 15 identifier: "hacking-on-gitea" 16 --- 17 18 # Hacking on Gitea 19 20 ## Quickstart 21 22 To get a quick working development environment you could use Gitpod. 23 24 [](https://gitpod.io/#https://github.com/go-gitea/gitea) 25 26 ## Installing go 27 28 You should [install go](https://go.dev/doc/install) and set up your go 29 environment correctly. 30 31 Next, [install Node.js with npm](https://nodejs.org/en/download/) which is 32 required to build the JavaScript and CSS files. The minimum supported Node.js 33 version is @minNodeVersion@ and the latest LTS version is recommended. 34 35 **Note**: When executing make tasks that require external tools, like 36 `make watch-backend`, Gitea will automatically download and build these as 37 necessary. To be able to use these you must have the `"$GOPATH"/bin` directory 38 on the executable path. If you don't add the go bin directory to the 39 executable path you will have to manage this yourself. 40 41 **Note 2**: Go version @minGoVersion@ or higher is required. 42 Gitea uses `gofmt` to format source code. However, the results of 43 `gofmt` can differ by the version of `go`. Therefore it is 44 recommended to install the version of Go that our continuous integration is 45 running. As of last update, the Go version should be @goVersion@. 46 47 To lint the template files, ensure [Python](https://www.python.org/) and 48 [Poetry](https://python-poetry.org/) are installed. 49 50 ## Installing Make 51 52 Gitea makes heavy use of Make to automate tasks and improve development. This 53 guide covers how to install Make. 54 55 ### On Linux 56 57 Install with the package manager. 58 59 On Ubuntu/Debian: 60 61 ```bash 62 sudo apt-get install make 63 ``` 64 65 On Fedora/RHEL/CentOS: 66 67 ```bash 68 sudo yum install make 69 ``` 70 71 ### On Windows 72 73 One of these three distributions of Make will run on Windows: 74 75 - [Single binary build](http://www.equation.com/servlet/equation.cmd?fa=make). Copy somewhere and add to `PATH`. 76 - [32-bits version](http://www.equation.com/ftpdir/make/32/make.exe) 77 - [64-bits version](http://www.equation.com/ftpdir/make/64/make.exe) 78 - [MinGW-w64](https://www.mingw-w64.org) / [MSYS2](https://www.msys2.org/). 79 - MSYS2 is a collection of tools and libraries providing you with an easy-to-use environment for building, installing and running native Windows software, it includes MinGW-w64. 80 - In MingGW-w64, the binary is called `mingw32-make.exe` instead of `make.exe`. Add the `bin` folder to `PATH`. 81 - In MSYS2, you can use `make` directly. See [MSYS2 Porting](https://www.msys2.org/wiki/Porting/). 82 - To compile Gitea with CGO_ENABLED (eg: SQLite3), you might need to use [tdm-gcc](https://jmeubank.github.io/tdm-gcc/) instead of MSYS2 gcc, because MSYS2 gcc headers lack some Windows-only CRT functions like `_beginthread`. 83 - [Chocolatey package](https://chocolatey.org/packages/make). Run `choco install make` 84 85 **Note**: If you are attempting to build using make with Windows Command Prompt, you may run into issues. The above prompts (Git bash, or MinGW) are recommended, however if you only have command prompt (or potentially PowerShell) you can set environment variables using the [set](https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/set_1) command, e.g. `set TAGS=bindata`. 86 87 ## Downloading and cloning the Gitea source code 88 89 The recommended method of obtaining the source code is by using `git clone`. 90 91 ```bash 92 git clone https://github.com/go-gitea/gitea 93 ``` 94 95 (Since the advent of go modules, it is no longer necessary to build go projects 96 from within the `$GOPATH`, hence the `go get` approach is no longer recommended.) 97 98 ## Forking Gitea 99 100 Download the main Gitea source code as above. Then, fork the 101 [Gitea repository](https://github.com/go-gitea/gitea) on GitHub, 102 and either switch the git remote origin for your fork or add your fork as another remote: 103 104 ```bash 105 # Rename original Gitea origin to upstream 106 git remote rename origin upstream 107 git remote add origin "git@github.com:$GITHUB_USERNAME/gitea.git" 108 git fetch --all --prune 109 ``` 110 111 or: 112 113 ```bash 114 # Add new remote for our fork 115 git remote add "$FORK_NAME" "git@github.com:$GITHUB_USERNAME/gitea.git" 116 git fetch --all --prune 117 ``` 118 119 To be able to create pull requests, the forked repository should be added as a remote 120 to the Gitea sources. Otherwise, changes can't be pushed. 121 122 ## Building Gitea (Basic) 123 124 Take a look at our 125 [instructions](installation/from-source.md) 126 for [building from source](installation/from-source.md). 127 128 The simplest recommended way to build from source is: 129 130 ```bash 131 TAGS="bindata sqlite sqlite_unlock_notify" make build 132 ``` 133 134 The `build` target will execute both `frontend` and `backend` sub-targets. If the `bindata` tag is present, the frontend files will be compiled into the binary. It is recommended to leave out the tag when doing frontend development so that changes will be reflected. 135 136 See `make help` for all available `make` targets. Also see [`.drone.yml`](https://github.com/go-gitea/gitea/blob/main/.drone.yml) to see how our continuous integration works. 137 138 ## Building continuously 139 140 To run and continuously rebuild when source files change: 141 142 ```bash 143 # for both frontend and backend 144 make watch 145 146 # or: watch frontend files (html/js/css) only 147 make watch-frontend 148 149 # or: watch backend files (go) only 150 make watch-backend 151 ``` 152 153 On macOS, watching all backend source files may hit the default open files limit which can be increased via `ulimit -n 12288` for the current shell or in your shell startup file for all future shells. 154 155 ### Formatting, code analysis and spell check 156 157 Our continuous integration will reject PRs that fail the code linters (including format check, code analysis and spell check). 158 159 You should format your code: 160 161 ```bash 162 make fmt 163 ``` 164 165 and lint the source code: 166 167 ```bash 168 # lint both frontend and backend code 169 make lint 170 # lint only backend code 171 make lint-backend 172 ``` 173 174 **Note**: The results of `gofmt` are dependent on the version of `go` present. 175 You should run the same version of go that is on the continuous integration 176 server as mentioned above. 177 178 ### Working on JS and CSS 179 180 Frontend development should follow [Guidelines for Frontend Development](contributing/guidelines-frontend.md) 181 182 To build with frontend resources, either use the `watch-frontend` target mentioned above or just build once: 183 184 ```bash 185 make build && ./gitea 186 ``` 187 188 Before committing, make sure the linters pass: 189 190 ```bash 191 make lint-frontend 192 ``` 193 194 ### Configuring local ElasticSearch instance 195 196 Start local ElasticSearch instance using docker: 197 198 ```sh 199 mkdir -p $(pwd)/data/elasticsearch 200 sudo chown -R 1000:1000 $(pwd)/data/elasticsearch 201 docker run --rm --memory="4g" -p 127.0.0.1:9200:9200 -p 127.0.0.1:9300:9300 -e "discovery.type=single-node" -v "$(pwd)/data/elasticsearch:/usr/share/elasticsearch/data" docker.elastic.co/elasticsearch/elasticsearch:7.16.3 202 ``` 203 204 Configure `app.ini`: 205 206 ```ini 207 [indexer] 208 ISSUE_INDEXER_TYPE = elasticsearch 209 ISSUE_INDEXER_CONN_STR = http://elastic:changeme@localhost:9200 210 REPO_INDEXER_ENABLED = true 211 REPO_INDEXER_TYPE = elasticsearch 212 REPO_INDEXER_CONN_STR = http://elastic:changeme@localhost:9200 213 ``` 214 215 ### Building and adding SVGs 216 217 SVG icons are built using the `make svg` target which compiles the icon sources into the output directory `public/assets/img/svg`. Custom icons can be added in the `web_src/svg` directory. 218 219 ### Building the Logo 220 221 The PNG and SVG versions of the Gitea logo are built from a single SVG source file `assets/logo.svg` using the `TAGS="gitea" make generate-images` target. To run it, Node.js and npm must be available. 222 223 The same process can also be used to generate custom logo PNGs from a SVG source file by updating `assets/logo.svg` and running `make generate-images`. Omitting the `gitea` tag will update only the user-designated logo files. 224 225 ### Updating the API 226 227 When creating new API routes or modifying existing API routes, you **MUST** 228 update and/or create [Swagger](https://swagger.io/docs/specification/2-0/what-is-swagger/) 229 documentation for these using [go-swagger](https://goswagger.io/) comments. 230 The structure of these comments is described in the [specification](https://goswagger.io/use/spec.html#annotation-syntax). 231 If you want more information about the Swagger structure, you can look at the 232 [Swagger 2.0 Documentation](https://swagger.io/docs/specification/2-0/basic-structure/) 233 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) 234 235 You should be careful not to break the API for downstream users which depend 236 on a stable API. In general, this means additions are acceptable, but deletions 237 or fundamental changes to the API will be rejected. 238 239 Once you have created or changed an API endpoint, please regenerate the Swagger 240 documentation using: 241 242 ```bash 243 make generate-swagger 244 ``` 245 246 You should validate your generated Swagger file: 247 248 ```bash 249 make swagger-validate 250 ``` 251 252 You should commit the changed swagger JSON file. The continuous integration 253 server will check that this has been done using: 254 255 ```bash 256 make swagger-check 257 ``` 258 259 **Note**: Please note you should use the Swagger 2.0 documentation, not the 260 OpenAPI 3 documentation. 261 262 ### Creating new configuration options 263 264 When creating new configuration options, it is not enough to add them to the 265 `modules/setting` files. You should add information to `custom/conf/app.ini` 266 and to the 267 [configuration cheat sheet](administration/config-cheat-sheet.md) 268 found in `docs/content/doc/administer/config-cheat-sheet.en-us.md` 269 270 ### Changing the logo 271 272 When changing the Gitea logo SVG, you will need to run and commit the results 273 of: 274 275 ```bash 276 make generate-images 277 ``` 278 279 This will create the necessary Gitea favicon and others. 280 281 ### Database Migrations 282 283 If you make breaking changes to any of the database persisted structs in the 284 `models/` directory, you will need to make a new migration. These can be found 285 in `models/migrations/`. You can ensure that your migrations work for the main 286 database types using: 287 288 ```bash 289 make test-sqlite-migration # with SQLite switched for the appropriate database 290 ``` 291 292 ## Testing 293 294 There are two types of test run by Gitea: Unit tests and Integration Tests. 295 296 ### Unit Tests 297 298 Unit tests are covered by `*_test.go` in `go test` system. 299 You can set the environment variable `GITEA_UNIT_TESTS_LOG_SQL=1` to display all SQL statements when running the tests in verbose mode (i.e. when `GOTESTFLAGS=-v` is set). 300 301 ```bash 302 TAGS="bindata sqlite sqlite_unlock_notify" make test # Runs the unit tests 303 ``` 304 305 ### Integration Tests 306 307 Unit tests will not and cannot completely test Gitea alone. Therefore, we 308 have written integration tests; however, these are database dependent. 309 310 ```bash 311 TAGS="bindata sqlite sqlite_unlock_notify" make build test-sqlite 312 ``` 313 314 will run the integration tests in an SQLite environment. Integration tests 315 require `git lfs` to be installed. Other database tests are available but 316 may need adjustment to the local environment. 317 318 Take a look at [`tests/integration/README.md`](https://github.com/go-gitea/gitea/blob/main/tests/integration/README.md) 319 for more information and how to run a single test. 320 321 ### Testing for a PR 322 323 Our continuous integration will test the code passes its unit tests and that 324 all supported databases will pass integration test in a Docker environment. 325 Migration from several recent versions of Gitea will also be tested. 326 327 Please submit your PR with additional tests and integration tests as 328 appropriate. 329 330 ## Documentation for the website 331 332 Documentation for the website is found in `docs/`. If you change this you 333 can test your changes to ensure that they pass continuous integration using: 334 335 ```bash 336 make lint-md 337 ``` 338 339 ## Visual Studio Code 340 341 A `launch.json` and `tasks.json` are provided within `contrib/ide/vscode` for 342 Visual Studio Code. Look at 343 [`contrib/ide/README.md`](https://github.com/go-gitea/gitea/blob/main/contrib/ide/README.md) 344 for more information. 345 346 ## GoLand 347 348 Clicking the `Run Application` arrow on the function `func main()` in `/main.go` 349 can quickly start a debuggable Gitea instance. 350 351 The `Output Directory` in `Run/Debug Configuration` MUST be set to the 352 gitea project directory (which contains `main.go` and `go.mod`), 353 otherwise, the started instance's working directory is a GoLand's temporary directory 354 and prevents Gitea from loading dynamic resources (eg: templates) in a development environment. 355 356 To run unit tests with SQLite in GoLand, set `-tags sqlite,sqlite_unlock_notify` 357 in `Go tool arguments` of `Run/Debug Configuration`. 358 359 ## Submitting PRs 360 361 Once you're happy with your changes, push them up and open a pull request. It 362 is recommended that you allow Gitea Managers and Owners to modify your PR 363 branches as we will need to update it to main before merging and/or may be 364 able to help fix issues directly. 365 366 Any PR requires two approvals from the Gitea maintainers and needs to pass the 367 continuous integration. Take a look at our 368 [`CONTRIBUTING.md`](https://github.com/go-gitea/gitea/blob/main/CONTRIBUTING.md) 369 document. 370 371 If you need more help pop on to [Discord](https://discord.gg/gitea) #Develop 372 and chat there. 373 374 That's it! You are ready to hack on Gitea.