Skip to content

Commit f90f495

Browse files
committed
Merge remote-tracking branch 'upstream/main'
* upstream/main: Use the type RefName for all the needed places and fix pull mirror sync bugs (go-gitea#24634) [skip ci] Updated translations via Crowdin Add Link to Stars and Forks Page to Repo List (go-gitea#24931) Fix documentation ref (go-gitea#24934) Add Chinese docs for help, contribution and development (go-gitea#24925) Merge two translation contributing documents (go-gitea#23939) Add the ability to pin Issues (go-gitea#24406) Use `shared/issueicon` template in projects (go-gitea#24922) Allow skipping forks and mirrors from being indexed (go-gitea#23187) Add Chinese documentations for Actions (go-gitea#24902) # Conflicts: # templates/repo/issue/view_content/sidebar.tmpl
2 parents 0f56349 + f9cfd6c commit f90f495

File tree

103 files changed

+3613
-484
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+3613
-484
lines changed

.github/workflows/pull-compliance-docs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ jobs:
2222
node-version: 20
2323
- run: make deps-frontend
2424
- run: make lint-md
25+
- run: make docs # test if build could succeed

cmd/hook.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ Gitea or set your environment appropriately.`, "")
201201

202202
oldCommitIDs := make([]string, hookBatchSize)
203203
newCommitIDs := make([]string, hookBatchSize)
204-
refFullNames := make([]string, hookBatchSize)
204+
refFullNames := make([]git.RefName, hookBatchSize)
205205
count := 0
206206
total := 0
207207
lastline := 0
@@ -236,14 +236,14 @@ Gitea or set your environment appropriately.`, "")
236236

237237
oldCommitID := string(fields[0])
238238
newCommitID := string(fields[1])
239-
refFullName := string(fields[2])
239+
refFullName := git.RefName(fields[2])
240240
total++
241241
lastline++
242242

243243
// If the ref is a branch or tag, check if it's protected
244244
// if supportProcReceive all ref should be checked because
245245
// permission check was delayed
246-
if supportProcReceive || strings.HasPrefix(refFullName, git.BranchPrefix) || strings.HasPrefix(refFullName, git.TagPrefix) {
246+
if supportProcReceive || refFullName.IsBranch() || refFullName.IsTag() {
247247
oldCommitIDs[count] = oldCommitID
248248
newCommitIDs[count] = newCommitID
249249
refFullNames[count] = refFullName
@@ -351,7 +351,7 @@ Gitea or set your environment appropriately.`, "")
351351
}
352352
oldCommitIDs := make([]string, hookBatchSize)
353353
newCommitIDs := make([]string, hookBatchSize)
354-
refFullNames := make([]string, hookBatchSize)
354+
refFullNames := make([]git.RefName, hookBatchSize)
355355
count := 0
356356
total := 0
357357
wasEmpty := false
@@ -373,7 +373,7 @@ Gitea or set your environment appropriately.`, "")
373373
fmt.Fprintf(out, ".")
374374
oldCommitIDs[count] = string(fields[0])
375375
newCommitIDs[count] = string(fields[1])
376-
refFullNames[count] = string(fields[2])
376+
refFullNames[count] = git.RefName(fields[2])
377377
if refFullNames[count] == git.BranchPrefix+"master" && newCommitIDs[count] != git.EmptySHA && count == total {
378378
masterPushed = true
379379
}
@@ -575,7 +575,7 @@ Gitea or set your environment appropriately.`, "")
575575
}
576576
hookOptions.OldCommitIDs = make([]string, 0, hookBatchSize)
577577
hookOptions.NewCommitIDs = make([]string, 0, hookBatchSize)
578-
hookOptions.RefFullNames = make([]string, 0, hookBatchSize)
578+
hookOptions.RefFullNames = make([]git.RefName, 0, hookBatchSize)
579579

580580
for {
581581
// note: pktLineTypeUnknow means pktLineTypeFlush and pktLineTypeData all allowed
@@ -593,7 +593,7 @@ Gitea or set your environment appropriately.`, "")
593593
}
594594
hookOptions.OldCommitIDs = append(hookOptions.OldCommitIDs, t[0])
595595
hookOptions.NewCommitIDs = append(hookOptions.NewCommitIDs, t[1])
596-
hookOptions.RefFullNames = append(hookOptions.RefFullNames, t[2])
596+
hookOptions.RefFullNames = append(hookOptions.RefFullNames, git.RefName(t[2]))
597597
}
598598

599599
hookOptions.GitPushOptions = make(map[string]string)
@@ -640,15 +640,15 @@ Gitea or set your environment appropriately.`, "")
640640

641641
for _, rs := range resp.Results {
642642
if len(rs.Err) > 0 {
643-
err = writeDataPktLine(ctx, os.Stdout, []byte("ng "+rs.OriginalRef+" "+rs.Err))
643+
err = writeDataPktLine(ctx, os.Stdout, []byte("ng "+rs.OriginalRef.String()+" "+rs.Err))
644644
if err != nil {
645645
return err
646646
}
647647
continue
648648
}
649649

650650
if rs.IsNotMatched {
651-
err = writeDataPktLine(ctx, os.Stdout, []byte("ok "+rs.OriginalRef))
651+
err = writeDataPktLine(ctx, os.Stdout, []byte("ok "+rs.OriginalRef.String()))
652652
if err != nil {
653653
return err
654654
}

custom/conf/app.example.ini

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,9 @@ LEVEL = Info
10481048
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
10491049
;; List of reasons why a Pull Request or Issue can be locked
10501050
;LOCK_REASONS = Too heated,Off-topic,Resolved,Spam
1051+
;; Maximum number of pinned Issues
1052+
;; Set to 0 to disable pinning Issues
1053+
;MAX_PINNED = 3
10511054

10521055
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
10531056
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -1362,6 +1365,10 @@ LEVEL = Info
13621365
;; repo indexer by default disabled, since it uses a lot of disk space
13631366
;REPO_INDEXER_ENABLED = false
13641367
;;
1368+
;; repo indexer units, the items to index, could be `sources`, `forks`, `mirrors`, `templates` or any combination of them separated by a comma.
1369+
;; If empty then it defaults to `sources` only, as if you'd like to disable fully please see REPO_INDEXER_ENABLED.
1370+
;REPO_INDEXER_REPO_TYPES = sources,forks,mirrors,templates
1371+
;;
13651372
;; Code search engine type, could be `bleve` or `elasticsearch`.
13661373
;REPO_INDEXER_TYPE = bleve
13671374
;;

docs/content/doc/administration/config-cheat-sheet.en-us.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ In addition there is _`StaticRootPath`_ which can be set as a built-in at build
141141
### Repository - Issue (`repository.issue`)
142142

143143
- `LOCK_REASONS`: **Too heated,Off-topic,Resolved,Spam**: A list of reasons why a Pull Request or Issue can be locked
144+
- `MAX_PINNED`: **3**: Maximum number of pinned Issues. Set to 0 to disable pinning Issues.
144145

145146
### Repository - Upload (`repository.upload`)
146147

@@ -465,6 +466,7 @@ relation to port exhaustion.
465466
- `ISSUE_INDEXER_PATH`: **indexers/issues.bleve**: Index file used for issue search; available when ISSUE_INDEXER_TYPE is bleve and elasticsearch. Relative paths will be made absolute against _`AppWorkPath`_.
466467

467468
- `REPO_INDEXER_ENABLED`: **false**: Enables code search (uses a lot of disk space, about 6 times more than the repository size).
469+
- `REPO_INDEXER_REPO_TYPES`: **sources,forks,mirrors,templates**: Repo indexer units. The items to index could be `sources`, `forks`, `mirrors`, `templates` or any combination of them separated by a comma. If empty then it defaults to `sources` only, as if you'd like to disable fully please see `REPO_INDEXER_ENABLED`.
468470
- `REPO_INDEXER_TYPE`: **bleve**: Code search engine type, could be `bleve` or `elasticsearch`.
469471
- `REPO_INDEXER_PATH`: **indexers/repos.bleve**: Index file used for code search.
470472
- `REPO_INDEXER_CONN_STR`: ****: Code indexer connection string, available when `REPO_INDEXER_TYPE` is elasticsearch. i.e. http://elastic:changeme@localhost:9200

docs/content/doc/contributing/guidelines-backend.en-us.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ To maintain understandable code and avoid circular dependencies it is important
3737
- `cmd`: All Gitea actual sub commands includes web, doctor, serv, hooks, admin and etc. `web` will start the web service. `serv` and `hooks` will be invoked by Git or OpenSSH. Other sub commands could help to maintain Gitea.
3838
- `tests`: Common test utility functions
3939
- `tests/integration`: Integration tests, to test back-end regressions
40-
- `tests/e2e`: E2e tests, to test test front-end <> back-end compatibility and visual regressions.
40+
- `tests/e2e`: E2e tests, to test front-end and back-end compatibility and visual regressions.
4141
- `models`: Contains the data structures used by xorm to construct database tables. It also contains functions to query and update the database. Dependencies to other Gitea code should be avoided. You can make exceptions in cases such as logging.
4242
- `models/db`: Basic database operations. All other `models/xxx` packages should depend on this package. The `GetEngine` function should only be invoked from `models/`.
4343
- `models/fixtures`: Sample data used in unit tests and integration tests. One `yml` file means one table which will be loaded into database when beginning the tests.
@@ -72,7 +72,6 @@ So services must be allowed to create a database transaction. Here is some examp
7272
// services/repository/repository.go
7373
func CreateXXXX() error {
7474
return db.WithTx(func(ctx context.Context) error {
75-
e := db.GetEngine(ctx)
7675
// do something, if err is returned, it will rollback automatically
7776
if err := issues.UpdateIssue(ctx, repoID); err != nil {
7877
// ...
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
---
2+
date: "2023-05-25T23:41:00+08:00"
3+
title: "后端开发指南"
4+
slug: "guidelines-backend"
5+
weight: 20
6+
toc: false
7+
draft: false
8+
aliases:
9+
- /zh-cn/guidelines-backend
10+
menu:
11+
sidebar:
12+
parent: "contributing"
13+
name: "后端开发指南"
14+
weight: 20
15+
identifier: "guidelines-backend"
16+
---
17+
18+
# 后端开发指南
19+
20+
**目录**
21+
22+
{{< toc >}}
23+
24+
## 背景
25+
26+
Gitea使用Golang作为后端编程语言。它使用了许多第三方包,并且自己也编写了一些包。
27+
例如,Gitea使用[Chi](https://github.com/go-chi/chi)作为基本的Web框架。[Xorm](https://xorm.io)是一个用于与数据库交互的ORM框架。
28+
因此,管理这些包非常重要。在开始编写后端代码之前,请参考以下准则。
29+
30+
## 包设计准则
31+
32+
### 包列表
33+
34+
为了保持易于理解的代码并避免循环依赖,拥有良好的代码结构是很重要的。Gitea后端分为以下几个部分:
35+
36+
- `build`:帮助构建Gitea的脚本。
37+
- `cmd`:包含所有Gitea的实际子命令,包括web、doctor、serv、hooks、admin等。`web`将启动Web服务。`serv``hooks`将被Git或OpenSSH调用。其他子命令可以帮助维护Gitea。
38+
- `tests`:常用的测试函数
39+
- `tests/integration`:集成测试,用于测试后端回归。
40+
- `tests/e2e`:端到端测试,用于测试前端和后端的兼容性和视觉回归。
41+
- `models`:包含由xorm用于构建数据库表的数据结构。它还包含查询和更新数据库的函数。应避免与其他Gitea代码的依赖关系。在某些情况下,比如日志记录时可以例外。
42+
- `models/db`:基本的数据库操作。所有其他`models/xxx`包都应依赖于此包。`GetEngine`函数只能从models/中调用。
43+
- `models/fixtures`:单元测试和集成测试中使用的示例数据。一个`yml`文件表示一个将在测试开始时加载到数据库中的表。
44+
- `models/migrations`:存储不同版本之间的数据库迁移。修改数据库结构的PR**必须**包含一个迁移步骤。
45+
- `modules`:在Gitea中处理特定功能的不同模块。工作正在进行中:其中一些模块应该移到`services`中,特别是那些依赖于models的模块,因为它们依赖于数据库。
46+
- `modules/setting`:存储从ini文件中读取的所有系统配置,并在各处引用。但是在可能的情况下,应将其作为函数参数使用。
47+
- `modules/git`:用于与`Git`命令行或Gogit包交互的包。
48+
- `public`:编译后的前端文件(JavaScript、图像、CSS等)
49+
- `routers`:处理服务器请求。由于它使用其他Gitea包来处理请求,因此其他包(models、modules或services)不能依赖于routers。
50+
- `routers/api`:包含`/api/v1`相关路由,用于处理RESTful API请求。
51+
- `routers/install`:只能在系统处于安装模式(INSTALL_LOCK=false)时响应。
52+
- `routers/private`:仅由内部子命令调用,特别是`serv``hooks`
53+
- `routers/web`:处理来自Web浏览器或Git SMART HTTP协议的HTTP请求。
54+
- `services`:用于常见路由操作或命令执行的支持函数。使用`models``modules`来处理请求。
55+
- `templates`:用于生成HTML输出的Golang模板。
56+
57+
### 包依赖关系
58+
59+
由于Golang不支持导入循环,我们必须仔细决定包之间的依赖关系。这些包之间有一些级别。以下是理想的包依赖关系方向。
60+
61+
`cmd` -> `routers` -> `services` -> `models` -> `modules`
62+
63+
从左到右,左侧的包可以依赖于右侧的包,但右侧的包不能依赖于左侧的包。在同一级别的子包中,可以根据该级别的规则进行依赖。
64+
65+
**注意事项**
66+
67+
为什么我们需要在`models`之外使用数据库事务?以及如何使用?
68+
某些操作在数据库记录插入/更新/删除失败时应该允许回滚。
69+
因此,服务必须能够创建数据库事务。以下是一些示例:
70+
71+
```go
72+
// services/repository/repository.go
73+
func CreateXXXX() error {
74+
return db.WithTx(func(ctx context.Context) error {
75+
// do something, if err is returned, it will rollback automatically
76+
if err := issues.UpdateIssue(ctx, repoID); err != nil {
77+
// ...
78+
return err
79+
}
80+
// ...
81+
return nil
82+
})
83+
}
84+
```
85+
86+
`services`**不应该**直接使用`db.GetEngine(ctx)`,而是应该在`models/`下编写一个函数。
87+
如果该函数将在事务中使用,请将`context.Context`作为函数的第一个参数。
88+
89+
```go
90+
// models/issues/issue.go
91+
func UpdateIssue(ctx context.Context, repoID int64) error {
92+
e := db.GetEngine(ctx)
93+
94+
// ...
95+
}
96+
```
97+
98+
### 包名称
99+
100+
对于顶层包,请使用复数作为包名,例如`services``models`,对于子包,请使用单数,例如`services/user``models/repository`
101+
102+
### 导入别名
103+
104+
由于有一些使用相同包名的包,例如`modules/user``models/user``services/user`,当这些包在一个Go文件中被导入时,很难知道我们使用的是哪个包以及它是变量名还是导入名。因此,我们始终建议使用导入别名。为了与常见的驼峰命名法的包变量区分开,建议使用**snake_case**作为导入别名的命名规则。
105+
例如:`import user_service "code.gitea.io/gitea/services/user"`
106+
107+
### 重要注意事项
108+
109+
- 永远不要写成`x.Update(exemplar)`,而没有明确的`WHERE`子句:
110+
- 这将导致表中的所有行都被使用exemplar的非零值进行更新,包括ID。
111+
- 通常应该写成`x.ID(id).Update(exemplar)`
112+
- 如果在迁移过程中使用`x.Insert(exemplar)`向表中插入记录,而ID是预设的:
113+
- 对于MSSQL变体,你将需要执行``SET IDENTITY_INSERT `table` ON``(否则迁移将失败)
114+
- 对于PostgreSQL,你还需要更新ID序列,否则迁移将悄无声息地通过,但后续的插入将失败:
115+
``SELECT setval('table_name_id_seq', COALESCE((SELECT MAX(id)+1 FROM `table_name`), 1), false)``
116+
117+
### 未来的任务
118+
119+
目前,我们正在进行一些重构,以完成以下任务:
120+
121+
- 纠正不符合规则的代码。
122+
- `models`中的文件太多了,所以我们正在将其中的一些移动到子包`models/xxx`中。
123+
- 由于它们依赖于`models`,因此应将某些`modules`子包移动到`services`中。

docs/content/doc/contributing/guidelines-frontend.en-us.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
date: "2021-10-13T16:00:00+02:00"
33
title: "Guidelines for Frontend Development"
44
slug: "guidelines-frontend"
5-
weight: 20
5+
weight: 30
66
toc: false
77
draft: false
88
aliases:
@@ -11,7 +11,7 @@ menu:
1111
sidebar:
1212
parent: "contributing"
1313
name: "Guidelines for Frontend"
14-
weight: 20
14+
weight: 30
1515
identifier: "guidelines-frontend"
1616
---
1717

0 commit comments

Comments
 (0)