Skip to content

Commit 59f37ce

Browse files
authored
Merge branch 'main' into agit/update
2 parents d4f2379 + 6410c34 commit 59f37ce

Some content is hidden

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

49 files changed

+740
-601
lines changed

models/fixtures/webhook.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,31 @@
2222
content_type: 1 # json
2323
events: '{"push_only":false,"send_everything":false,"choose_events":false,"events":{"create":false,"push":true,"pull_request":true}}'
2424
is_active: true
25+
2526
-
2627
id: 4
2728
repo_id: 2
2829
url: www.example.com/url4
2930
content_type: 1 # json
3031
events: '{"push_only":true,"branch_filter":"{master,feature*}"}'
3132
is_active: true
33+
34+
-
35+
id: 5
36+
repo_id: 0
37+
owner_id: 0
38+
url: www.example.com/url5
39+
content_type: 1 # json
40+
events: '{"push_only":true,"branch_filter":"{master,feature*}"}'
41+
is_active: true
42+
is_system_webhook: true
43+
44+
-
45+
id: 6
46+
repo_id: 0
47+
owner_id: 0
48+
url: www.example.com/url6
49+
content_type: 1 # json
50+
events: '{"push_only":true,"branch_filter":"{master,feature*}"}'
51+
is_active: true
52+
is_system_webhook: false

models/perm/access/repo_permission.go

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,14 @@ func (p *Permission) LogString() string {
175175
return fmt.Sprintf(format, args...)
176176
}
177177

178-
func applyEveryoneRepoPermission(user *user_model.User, perm *Permission) {
178+
func finalProcessRepoUnitPermission(user *user_model.User, perm *Permission) {
179179
if user == nil || user.ID <= 0 {
180+
// for anonymous access, it could be:
181+
// AccessMode is None or Read, units has repo units, unitModes is nil
180182
return
181183
}
184+
185+
// apply everyone access permissions
182186
for _, u := range perm.units {
183187
if u.EveryoneAccessMode >= perm_model.AccessModeRead && u.EveryoneAccessMode > perm.everyoneAccessMode[u.Type] {
184188
if perm.everyoneAccessMode == nil {
@@ -187,17 +191,40 @@ func applyEveryoneRepoPermission(user *user_model.User, perm *Permission) {
187191
perm.everyoneAccessMode[u.Type] = u.EveryoneAccessMode
188192
}
189193
}
194+
195+
if perm.unitsMode == nil {
196+
// if unitsMode is not set, then it means that the default p.AccessMode applies to all units
197+
return
198+
}
199+
200+
// remove no permission units
201+
origPermUnits := perm.units
202+
perm.units = make([]*repo_model.RepoUnit, 0, len(perm.units))
203+
for _, u := range origPermUnits {
204+
shouldKeep := false
205+
for t := range perm.unitsMode {
206+
if shouldKeep = u.Type == t; shouldKeep {
207+
break
208+
}
209+
}
210+
for t := range perm.everyoneAccessMode {
211+
if shouldKeep = shouldKeep || u.Type == t; shouldKeep {
212+
break
213+
}
214+
}
215+
if shouldKeep {
216+
perm.units = append(perm.units, u)
217+
}
218+
}
190219
}
191220

192221
// GetUserRepoPermission returns the user permissions to the repository
193222
func GetUserRepoPermission(ctx context.Context, repo *repo_model.Repository, user *user_model.User) (perm Permission, err error) {
194223
defer func() {
195224
if err == nil {
196-
applyEveryoneRepoPermission(user, &perm)
197-
}
198-
if log.IsTrace() {
199-
log.Trace("Permission Loaded for user %-v in repo %-v, permissions: %-+v", user, repo, perm)
225+
finalProcessRepoUnitPermission(user, &perm)
200226
}
227+
log.Trace("Permission Loaded for user %-v in repo %-v, permissions: %-+v", user, repo, perm)
201228
}()
202229

203230
if err = repo.LoadUnits(ctx); err != nil {
@@ -294,16 +321,6 @@ func GetUserRepoPermission(ctx context.Context, repo *repo_model.Repository, use
294321
}
295322
}
296323

297-
// remove no permission units
298-
perm.units = make([]*repo_model.RepoUnit, 0, len(repo.Units))
299-
for t := range perm.unitsMode {
300-
for _, u := range repo.Units {
301-
if u.Type == t {
302-
perm.units = append(perm.units, u)
303-
}
304-
}
305-
}
306-
307324
return perm, err
308325
}
309326

models/perm/access/repo_permission_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func TestApplyEveryoneRepoPermission(t *testing.T) {
5050
{Type: unit.TypeWiki, EveryoneAccessMode: perm_model.AccessModeRead},
5151
},
5252
}
53-
applyEveryoneRepoPermission(nil, &perm)
53+
finalProcessRepoUnitPermission(nil, &perm)
5454
assert.False(t, perm.CanRead(unit.TypeWiki))
5555

5656
perm = Permission{
@@ -59,7 +59,7 @@ func TestApplyEveryoneRepoPermission(t *testing.T) {
5959
{Type: unit.TypeWiki, EveryoneAccessMode: perm_model.AccessModeRead},
6060
},
6161
}
62-
applyEveryoneRepoPermission(&user_model.User{ID: 0}, &perm)
62+
finalProcessRepoUnitPermission(&user_model.User{ID: 0}, &perm)
6363
assert.False(t, perm.CanRead(unit.TypeWiki))
6464

6565
perm = Permission{
@@ -68,7 +68,7 @@ func TestApplyEveryoneRepoPermission(t *testing.T) {
6868
{Type: unit.TypeWiki, EveryoneAccessMode: perm_model.AccessModeRead},
6969
},
7070
}
71-
applyEveryoneRepoPermission(&user_model.User{ID: 1}, &perm)
71+
finalProcessRepoUnitPermission(&user_model.User{ID: 1}, &perm)
7272
assert.True(t, perm.CanRead(unit.TypeWiki))
7373

7474
perm = Permission{
@@ -77,20 +77,22 @@ func TestApplyEveryoneRepoPermission(t *testing.T) {
7777
{Type: unit.TypeWiki, EveryoneAccessMode: perm_model.AccessModeRead},
7878
},
7979
}
80-
applyEveryoneRepoPermission(&user_model.User{ID: 1}, &perm)
80+
finalProcessRepoUnitPermission(&user_model.User{ID: 1}, &perm)
8181
// it should work the same as "EveryoneAccessMode: none" because the default AccessMode should be applied to units
8282
assert.True(t, perm.CanWrite(unit.TypeWiki))
8383

8484
perm = Permission{
8585
units: []*repo_model.RepoUnit{
86+
{Type: unit.TypeCode}, // will be removed
8687
{Type: unit.TypeWiki, EveryoneAccessMode: perm_model.AccessModeRead},
8788
},
8889
unitsMode: map[unit.Type]perm_model.AccessMode{
8990
unit.TypeWiki: perm_model.AccessModeWrite,
9091
},
9192
}
92-
applyEveryoneRepoPermission(&user_model.User{ID: 1}, &perm)
93+
finalProcessRepoUnitPermission(&user_model.User{ID: 1}, &perm)
9394
assert.True(t, perm.CanWrite(unit.TypeWiki))
95+
assert.Len(t, perm.units, 1)
9496
}
9597

9698
func TestUnitAccessMode(t *testing.T) {

models/repo/license.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func UpdateRepoLicenses(ctx context.Context, repo *Repository, commitID string,
5454
for _, o := range oldLicenses {
5555
// Update already existing license
5656
if o.License == license {
57+
o.CommitID = commitID
5758
if _, err := db.GetEngine(ctx).ID(o.ID).Cols("`commit_id`").Update(o); err != nil {
5859
return err
5960
}

models/unittest/fscopy.go

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,13 @@ import (
1111
"code.gitea.io/gitea/modules/util"
1212
)
1313

14-
// Copy copies file from source to target path.
15-
func Copy(src, dest string) error {
16-
// Gather file information to set back later.
17-
si, err := os.Lstat(src)
18-
if err != nil {
19-
return err
20-
}
21-
22-
// Handle symbolic link.
23-
if si.Mode()&os.ModeSymlink != 0 {
24-
target, err := os.Readlink(src)
25-
if err != nil {
26-
return err
27-
}
28-
// NOTE: os.Chmod and os.Chtimes don't recognize symbolic link,
29-
// which will lead "no such file or directory" error.
30-
return os.Symlink(target, dest)
31-
}
32-
33-
return util.CopyFile(src, dest)
34-
}
35-
36-
// Sync synchronizes the two files. This is skipped if both files
14+
// SyncFile synchronizes the two files. This is skipped if both files
3715
// exist and the size, modtime, and mode match.
38-
func Sync(srcPath, destPath string) error {
16+
func SyncFile(srcPath, destPath string) error {
3917
dest, err := os.Stat(destPath)
4018
if err != nil {
4119
if os.IsNotExist(err) {
42-
return Copy(srcPath, destPath)
20+
return util.CopyFile(srcPath, destPath)
4321
}
4422
return err
4523
}
@@ -55,7 +33,7 @@ func Sync(srcPath, destPath string) error {
5533
return nil
5634
}
5735

58-
return Copy(srcPath, destPath)
36+
return util.CopyFile(srcPath, destPath)
5937
}
6038

6139
// SyncDirs synchronizes files recursively from source to target directory.
@@ -66,23 +44,31 @@ func SyncDirs(srcPath, destPath string) error {
6644
return err
6745
}
6846

47+
// the keep file is used to keep the directory in a git repository, it doesn't need to be synced
48+
// and go-git doesn't work with the ".keep" file (it would report errors like "ref is empty")
49+
const keepFile = ".keep"
50+
6951
// find and delete all untracked files
7052
destFiles, err := util.ListDirRecursively(destPath, &util.ListDirOptions{IncludeDir: true})
7153
if err != nil {
7254
return err
7355
}
7456
for _, destFile := range destFiles {
7557
destFilePath := filepath.Join(destPath, destFile)
58+
shouldRemove := filepath.Base(destFilePath) == keepFile
7659
if _, err = os.Stat(filepath.Join(srcPath, destFile)); err != nil {
7760
if os.IsNotExist(err) {
78-
// if src file does not exist, remove dest file
79-
if err = os.RemoveAll(destFilePath); err != nil {
80-
return err
81-
}
61+
shouldRemove = true
8262
} else {
8363
return err
8464
}
8565
}
66+
// if src file does not exist, remove dest file
67+
if shouldRemove {
68+
if err = os.RemoveAll(destFilePath); err != nil {
69+
return err
70+
}
71+
}
8672
}
8773

8874
// sync src files to dest
@@ -95,8 +81,8 @@ func SyncDirs(srcPath, destPath string) error {
9581
// util.ListDirRecursively appends a slash to the directory name
9682
if strings.HasSuffix(srcFile, "/") {
9783
err = os.MkdirAll(destFilePath, os.ModePerm)
98-
} else {
99-
err = Sync(filepath.Join(srcPath, srcFile), destFilePath)
84+
} else if filepath.Base(destFilePath) != keepFile {
85+
err = SyncFile(filepath.Join(srcPath, srcFile), destFilePath)
10086
}
10187
if err != nil {
10288
return err

models/webhook/webhook_system.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@ import (
1111
"code.gitea.io/gitea/modules/optional"
1212
)
1313

14+
// GetSystemOrDefaultWebhooks returns webhooks by given argument or all if argument is missing.
15+
func GetSystemOrDefaultWebhooks(ctx context.Context, isSystemWebhook optional.Option[bool]) ([]*Webhook, error) {
16+
webhooks := make([]*Webhook, 0, 5)
17+
if !isSystemWebhook.Has() {
18+
return webhooks, db.GetEngine(ctx).Where("repo_id=? AND owner_id=?", 0, 0).
19+
Find(&webhooks)
20+
}
21+
22+
return webhooks, db.GetEngine(ctx).
23+
Where("repo_id=? AND owner_id=? AND is_system_webhook=?", 0, 0, isSystemWebhook.Value()).
24+
Find(&webhooks)
25+
}
26+
1427
// GetDefaultWebhooks returns all admin-default webhooks.
1528
func GetDefaultWebhooks(ctx context.Context) ([]*Webhook, error) {
1629
webhooks := make([]*Webhook, 0, 5)

models/webhook/webhook_system_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2017 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package webhook
5+
6+
import (
7+
"testing"
8+
9+
"code.gitea.io/gitea/models/db"
10+
"code.gitea.io/gitea/models/unittest"
11+
"code.gitea.io/gitea/modules/optional"
12+
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
func TestGetSystemOrDefaultWebhooks(t *testing.T) {
17+
assert.NoError(t, unittest.PrepareTestDatabase())
18+
19+
hooks, err := GetSystemOrDefaultWebhooks(db.DefaultContext, optional.None[bool]())
20+
assert.NoError(t, err)
21+
if assert.Len(t, hooks, 2) {
22+
assert.Equal(t, int64(5), hooks[0].ID)
23+
assert.Equal(t, int64(6), hooks[1].ID)
24+
}
25+
26+
hooks, err = GetSystemOrDefaultWebhooks(db.DefaultContext, optional.Some(true))
27+
assert.NoError(t, err)
28+
if assert.Len(t, hooks, 1) {
29+
assert.Equal(t, int64(5), hooks[0].ID)
30+
}
31+
32+
hooks, err = GetSystemOrDefaultWebhooks(db.DefaultContext, optional.Some(false))
33+
assert.NoError(t, err)
34+
if assert.Len(t, hooks, 1) {
35+
assert.Equal(t, int64(6), hooks[0].ID)
36+
}
37+
}

options/locale/locale_cs-CZ.ini

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,9 +1115,6 @@ blame.ignore_revs=Ignorování revizí v <a href="%s">.git-blame-ignorerevs</a>.
11151115
blame.ignore_revs.failed=Nepodařilo se ignorovat revize v <a href="%s">.git-blame-ignore-revs</a>.
11161116
user_search_tooltip=Zobrazí maximálně 30 uživatelů
11171117

1118-
tree_path_not_found_commit=Cesta %[1]s v commitu %[2]s neexistuje
1119-
tree_path_not_found_branch=Cesta %[1]s ve větvi %[2]s neexistuje
1120-
tree_path_not_found_tag=Cesta %[1]s ve značce %[2]s neexistuje
11211118

11221119
transfer.accept=Přijmout převod
11231120
transfer.accept_desc=Převést do „%s“

options/locale/locale_de-DE.ini

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,9 +1111,6 @@ blame.ignore_revs=Revisionen in <a href="%s">.git-blame-ignore-revs</a> werden i
11111111
blame.ignore_revs.failed=Fehler beim Ignorieren der Revisionen in <a href="%s">.git-blame-ignore-revs</a>.
11121112
user_search_tooltip=Zeigt maximal 30 Benutzer
11131113
1114-
tree_path_not_found_commit=Pfad %[1]s existiert nicht in Commit%[2]s
1115-
tree_path_not_found_branch=Pfad %[1]s existiert nicht in Branch %[2]s
1116-
tree_path_not_found_tag=Pfad %[1]s existiert nicht in Tag %[2]s
11171114
11181115
transfer.accept=Übertragung Akzeptieren
11191116
transfer.accept_desc=`Übertragung nach "%s"`

options/locale/locale_el-GR.ini

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -992,9 +992,6 @@ blame_prior=Προβολή ευθύνης πριν από αυτή την αλλ
992992
blame.ignore_revs=Αγνόηση των αναθεωρήσεων στο <a href="%s">.git-blame-ignore-revs</a>. Πατήστε <a href="%s">εδώ</a> για να το παρακάμψετε και να δείτε την κανονική προβολή ευθυνών.
993993
blame.ignore_revs.failed=Αποτυχία αγνόησης των αναθεωρήσεων στο <a href="%s">.git-blame-ignore-revs</a>.
994994

995-
tree_path_not_found_commit=Η διαδρομή %[1]s δεν υπάρχει στην υποβολή %[2]s
996-
tree_path_not_found_branch=Η διαδρομή %[1]s δεν υπάρχει στον κλάδο %[2]s
997-
tree_path_not_found_tag=Η διαδρομή %[1]s δεν υπάρχει στην ετικέτα %[2]s
998995

999996
transfer.accept=Αποδοχή Μεταφοράς
1000997
transfer.reject=Απόρριψη Μεταφοράς

0 commit comments

Comments
 (0)