Skip to content

Commit 10cc6b9

Browse files
authored
Merge branch 'main' into feat/publish-ghcr-io
2 parents 9807d69 + fd7c364 commit 10cc6b9

Some content is hidden

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

47 files changed

+1257
-1078
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/assets/*.json linguist-generated
55
/public/assets/img/svg/*.svg linguist-generated
66
/templates/swagger/v1_json.tmpl linguist-generated
7+
/options/fileicon/** linguist-generated
78
/vendor/** -text -eol linguist-vendored
89
/web_src/js/vendor/** -text -eol linguist-vendored
910
Dockerfile.* linguist-language=Dockerfile

models/git/branch.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,11 @@ func GetDeletedBranchByID(ctx context.Context, repoID, branchID int64) (*Branch,
235235
return &branch, nil
236236
}
237237

238+
func DeleteRepoBranches(ctx context.Context, repoID int64) error {
239+
_, err := db.GetEngine(ctx).Where("repo_id=?", repoID).Delete(new(Branch))
240+
return err
241+
}
242+
238243
func DeleteBranches(ctx context.Context, repoID, doerID int64, branchIDs []int64) error {
239244
return db.WithTx(ctx, func(ctx context.Context) error {
240245
branches := make([]*Branch, 0, len(branchIDs))

models/issues/issue_update.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
"code.gitea.io/gitea/models/db"
1313
"code.gitea.io/gitea/models/organization"
14-
"code.gitea.io/gitea/models/perm"
1514
access_model "code.gitea.io/gitea/models/perm/access"
1615
project_model "code.gitea.io/gitea/models/project"
1716
repo_model "code.gitea.io/gitea/models/repo"
@@ -612,7 +611,7 @@ func ResolveIssueMentionsByVisibility(ctx context.Context, issue *Issue, doer *u
612611
unittype = unit.TypePullRequests
613612
}
614613
for _, team := range teams {
615-
if team.AccessMode >= perm.AccessModeAdmin {
614+
if team.HasAdminAccess() {
616615
checked = append(checked, team.ID)
617616
resolved[issue.Repo.Owner.LowerName+"/"+team.LowerName] = true
618617
continue

models/organization/org.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,6 @@ func (org *Organization) HomeLink() string {
178178
return org.AsUser().HomeLink()
179179
}
180180

181-
// CanCreateRepo returns if user login can create a repository
182-
// NOTE: functions calling this assume a failure due to repository count limit; if new checks are added, those functions should be revised
183-
func (org *Organization) CanCreateRepo() bool {
184-
return org.AsUser().CanCreateRepo()
185-
}
186-
187181
// FindOrgMembersOpts represensts find org members conditions
188182
type FindOrgMembersOpts struct {
189183
db.ListOptions

models/organization/org_user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func IsOrganizationAdmin(ctx context.Context, orgID, uid int64) (bool, error) {
7878
return false, err
7979
}
8080
for _, t := range teams {
81-
if t.AccessMode >= perm.AccessModeAdmin {
81+
if t.HasAdminAccess() {
8282
return true, nil
8383
}
8484
}

models/organization/team.go

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func (t *Team) LoadUnits(ctx context.Context) (err error) {
113113

114114
// GetUnitNames returns the team units names
115115
func (t *Team) GetUnitNames() (res []string) {
116-
if t.AccessMode >= perm.AccessModeAdmin {
116+
if t.HasAdminAccess() {
117117
return unit.AllUnitKeyNames()
118118
}
119119

@@ -126,7 +126,7 @@ func (t *Team) GetUnitNames() (res []string) {
126126
// GetUnitsMap returns the team units permissions
127127
func (t *Team) GetUnitsMap() map[string]string {
128128
m := make(map[string]string)
129-
if t.AccessMode >= perm.AccessModeAdmin {
129+
if t.HasAdminAccess() {
130130
for _, u := range unit.Units {
131131
m[u.NameKey] = t.AccessMode.ToString()
132132
}
@@ -153,6 +153,10 @@ func (t *Team) IsMember(ctx context.Context, userID int64) bool {
153153
return isMember
154154
}
155155

156+
func (t *Team) HasAdminAccess() bool {
157+
return t.AccessMode >= perm.AccessModeAdmin
158+
}
159+
156160
// LoadMembers returns paginated members in team of organization.
157161
func (t *Team) LoadMembers(ctx context.Context) (err error) {
158162
t.Members, err = GetTeamMembers(ctx, &SearchMembersOptions{
@@ -238,22 +242,6 @@ func GetTeamByID(ctx context.Context, teamID int64) (*Team, error) {
238242
return t, nil
239243
}
240244

241-
// GetTeamNamesByID returns team's lower name from a list of team ids.
242-
func GetTeamNamesByID(ctx context.Context, teamIDs []int64) ([]string, error) {
243-
if len(teamIDs) == 0 {
244-
return []string{}, nil
245-
}
246-
247-
var teamNames []string
248-
err := db.GetEngine(ctx).Table("team").
249-
Select("lower_name").
250-
In("id", teamIDs).
251-
Asc("name").
252-
Find(&teamNames)
253-
254-
return teamNames, err
255-
}
256-
257245
// IncrTeamRepoNum increases the number of repos for the given team by 1
258246
func IncrTeamRepoNum(ctx context.Context, teamID int64) error {
259247
_, err := db.GetEngine(ctx).Incr("num_repos").ID(teamID).Update(new(Team))

models/perm/access/repo_permission.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ func GetUserRepoPermission(ctx context.Context, repo *repo_model.Repository, use
331331

332332
// if user in an owner team
333333
for _, team := range teams {
334-
if team.AccessMode >= perm_model.AccessModeAdmin {
334+
if team.HasAdminAccess() {
335335
perm.AccessMode = perm_model.AccessModeOwner
336336
perm.unitsMode = nil
337337
return perm, nil
@@ -399,7 +399,7 @@ func IsUserRepoAdmin(ctx context.Context, repo *repo_model.Repository, user *use
399399
}
400400

401401
for _, team := range teams {
402-
if team.AccessMode >= perm_model.AccessModeAdmin {
402+
if team.HasAdminAccess() {
403403
return true, nil
404404
}
405405
}

models/repo/release.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,3 +558,8 @@ func FindTagsByCommitIDs(ctx context.Context, repoID int64, commitIDs ...string)
558558
}
559559
return res, nil
560560
}
561+
562+
func DeleteRepoReleases(ctx context.Context, repoID int64) error {
563+
_, err := db.GetEngine(ctx).Where("repo_id = ?", repoID).Delete(new(Release))
564+
return err
565+
}

models/repo/update.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,31 +111,31 @@ func (err ErrRepoFilesAlreadyExist) Unwrap() error {
111111
return util.ErrAlreadyExist
112112
}
113113

114-
// CheckCreateRepository check if could created a repository
115-
func CheckCreateRepository(ctx context.Context, doer, u *user_model.User, name string, overwriteOrAdopt bool) error {
116-
if !doer.CanCreateRepo() {
117-
return ErrReachLimitOfRepo{u.MaxRepoCreation}
114+
// CheckCreateRepository check if doer could create a repository in new owner
115+
func CheckCreateRepository(ctx context.Context, doer, owner *user_model.User, name string, overwriteOrAdopt bool) error {
116+
if !doer.CanCreateRepoIn(owner) {
117+
return ErrReachLimitOfRepo{owner.MaxRepoCreation}
118118
}
119119

120120
if err := IsUsableRepoName(name); err != nil {
121121
return err
122122
}
123123

124-
has, err := IsRepositoryModelOrDirExist(ctx, u, name)
124+
has, err := IsRepositoryModelOrDirExist(ctx, owner, name)
125125
if err != nil {
126126
return fmt.Errorf("IsRepositoryExist: %w", err)
127127
} else if has {
128-
return ErrRepoAlreadyExist{u.Name, name}
128+
return ErrRepoAlreadyExist{owner.Name, name}
129129
}
130130

131-
repoPath := RepoPath(u.Name, name)
131+
repoPath := RepoPath(owner.Name, name)
132132
isExist, err := util.IsExist(repoPath)
133133
if err != nil {
134134
log.Error("Unable to check if %s exists. Error: %v", repoPath, err)
135135
return err
136136
}
137137
if !overwriteOrAdopt && isExist {
138-
return ErrRepoFilesAlreadyExist{u.Name, name}
138+
return ErrRepoFilesAlreadyExist{owner.Name, name}
139139
}
140140
return nil
141141
}

models/unit/unit.go

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,21 @@ type Type int
2020

2121
// Enumerate all the unit types
2222
const (
23-
TypeInvalid Type = iota // 0 invalid
24-
TypeCode // 1 code
25-
TypeIssues // 2 issues
26-
TypePullRequests // 3 PRs
27-
TypeReleases // 4 Releases
28-
TypeWiki // 5 Wiki
29-
TypeExternalWiki // 6 ExternalWiki
30-
TypeExternalTracker // 7 ExternalTracker
31-
TypeProjects // 8 Projects
32-
TypePackages // 9 Packages
33-
TypeActions // 10 Actions
23+
TypeInvalid Type = iota // 0 invalid
24+
25+
TypeCode // 1 code
26+
TypeIssues // 2 issues
27+
TypePullRequests // 3 PRs
28+
TypeReleases // 4 Releases
29+
TypeWiki // 5 Wiki
30+
TypeExternalWiki // 6 ExternalWiki
31+
TypeExternalTracker // 7 ExternalTracker
32+
TypeProjects // 8 Projects
33+
TypePackages // 9 Packages
34+
TypeActions // 10 Actions
35+
36+
// FIXME: TEAM-UNIT-PERMISSION: the team unit "admin" permission's design is not right, when a new unit is added in the future,
37+
// admin team won't inherit the correct admin permission for the new unit, need to have a complete fix before adding any new unit.
3438
)
3539

3640
// Value returns integer value for unit type (used by template)
@@ -380,20 +384,3 @@ func AllUnitKeyNames() []string {
380384
}
381385
return res
382386
}
383-
384-
// MinUnitAccessMode returns the minial permission of the permission map
385-
func MinUnitAccessMode(unitsMap map[Type]perm.AccessMode) perm.AccessMode {
386-
res := perm.AccessModeNone
387-
for t, mode := range unitsMap {
388-
// Don't allow `TypeExternal{Tracker,Wiki}` to influence this as they can only be set to READ perms.
389-
if t == TypeExternalTracker || t == TypeExternalWiki {
390-
continue
391-
}
392-
393-
// get the minial permission great than AccessModeNone except all are AccessModeNone
394-
if mode > perm.AccessModeNone && (res == perm.AccessModeNone || mode < res) {
395-
res = mode
396-
}
397-
}
398-
return res
399-
}

models/user/user.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -247,19 +247,20 @@ func (u *User) MaxCreationLimit() int {
247247
return u.MaxRepoCreation
248248
}
249249

250-
// CanCreateRepo returns if user login can create a repository
251-
// NOTE: functions calling this assume a failure due to repository count limit; if new checks are added, those functions should be revised
252-
func (u *User) CanCreateRepo() bool {
250+
// CanCreateRepoIn checks whether the doer(u) can create a repository in the owner
251+
// NOTE: functions calling this assume a failure due to repository count limit; it ONLY checks the repo number LIMIT, if new checks are added, those functions should be revised
252+
func (u *User) CanCreateRepoIn(owner *User) bool {
253253
if u.IsAdmin {
254254
return true
255255
}
256-
if u.MaxRepoCreation <= -1 {
257-
if setting.Repository.MaxCreationLimit <= -1 {
256+
const noLimit = -1
257+
if owner.MaxRepoCreation == noLimit {
258+
if setting.Repository.MaxCreationLimit == noLimit {
258259
return true
259260
}
260-
return u.NumRepos < setting.Repository.MaxCreationLimit
261+
return owner.NumRepos < setting.Repository.MaxCreationLimit
261262
}
262-
return u.NumRepos < u.MaxRepoCreation
263+
return owner.NumRepos < owner.MaxRepoCreation
263264
}
264265

265266
// CanCreateOrganization returns true if user can create organisation.
@@ -272,13 +273,12 @@ func (u *User) CanEditGitHook() bool {
272273
return !setting.DisableGitHooks && (u.IsAdmin || u.AllowGitHook)
273274
}
274275

275-
// CanForkRepo returns if user login can fork a repository
276-
// It checks especially that the user can create repos, and potentially more
277-
func (u *User) CanForkRepo() bool {
276+
// CanForkRepoIn ONLY checks repository count limit
277+
func (u *User) CanForkRepoIn(owner *User) bool {
278278
if setting.Repository.AllowForkWithoutMaximumLimit {
279279
return true
280280
}
281-
return u.CanCreateRepo()
281+
return u.CanCreateRepoIn(owner)
282282
}
283283

284284
// CanImportLocal returns true if user can migrate repository by local path.

models/user/user_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"code.gitea.io/gitea/modules/optional"
2020
"code.gitea.io/gitea/modules/setting"
2121
"code.gitea.io/gitea/modules/structs"
22+
"code.gitea.io/gitea/modules/test"
2223
"code.gitea.io/gitea/modules/timeutil"
2324

2425
"github.com/stretchr/testify/assert"
@@ -616,3 +617,37 @@ func TestGetInactiveUsers(t *testing.T) {
616617
assert.NoError(t, err)
617618
assert.Empty(t, users)
618619
}
620+
621+
func TestCanCreateRepo(t *testing.T) {
622+
defer test.MockVariableValue(&setting.Repository.MaxCreationLimit)()
623+
const noLimit = -1
624+
doerNormal := &user_model.User{}
625+
doerAdmin := &user_model.User{IsAdmin: true}
626+
t.Run("NoGlobalLimit", func(t *testing.T) {
627+
setting.Repository.MaxCreationLimit = noLimit
628+
629+
assert.True(t, doerNormal.CanCreateRepoIn(&user_model.User{NumRepos: 10, MaxRepoCreation: noLimit}))
630+
assert.False(t, doerNormal.CanCreateRepoIn(&user_model.User{NumRepos: 10, MaxRepoCreation: 0}))
631+
assert.True(t, doerNormal.CanCreateRepoIn(&user_model.User{NumRepos: 10, MaxRepoCreation: 100}))
632+
633+
assert.True(t, doerAdmin.CanCreateRepoIn(&user_model.User{NumRepos: 10, MaxRepoCreation: noLimit}))
634+
assert.True(t, doerAdmin.CanCreateRepoIn(&user_model.User{NumRepos: 10, MaxRepoCreation: 0}))
635+
assert.True(t, doerAdmin.CanCreateRepoIn(&user_model.User{NumRepos: 10, MaxRepoCreation: 100}))
636+
})
637+
638+
t.Run("GlobalLimit50", func(t *testing.T) {
639+
setting.Repository.MaxCreationLimit = 50
640+
641+
assert.True(t, doerNormal.CanCreateRepoIn(&user_model.User{NumRepos: 10, MaxRepoCreation: noLimit}))
642+
assert.False(t, doerNormal.CanCreateRepoIn(&user_model.User{NumRepos: 60, MaxRepoCreation: noLimit})) // limited by global limit
643+
assert.False(t, doerNormal.CanCreateRepoIn(&user_model.User{NumRepos: 10, MaxRepoCreation: 0}))
644+
assert.True(t, doerNormal.CanCreateRepoIn(&user_model.User{NumRepos: 10, MaxRepoCreation: 100}))
645+
assert.True(t, doerNormal.CanCreateRepoIn(&user_model.User{NumRepos: 60, MaxRepoCreation: 100}))
646+
647+
assert.True(t, doerAdmin.CanCreateRepoIn(&user_model.User{NumRepos: 10, MaxRepoCreation: noLimit}))
648+
assert.True(t, doerAdmin.CanCreateRepoIn(&user_model.User{NumRepos: 60, MaxRepoCreation: noLimit}))
649+
assert.True(t, doerAdmin.CanCreateRepoIn(&user_model.User{NumRepos: 10, MaxRepoCreation: 0}))
650+
assert.True(t, doerAdmin.CanCreateRepoIn(&user_model.User{NumRepos: 10, MaxRepoCreation: 100}))
651+
assert.True(t, doerAdmin.CanCreateRepoIn(&user_model.User{NumRepos: 60, MaxRepoCreation: 100}))
652+
})
653+
}

modules/repository/init.go

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@ import (
1111
"strings"
1212

1313
issues_model "code.gitea.io/gitea/models/issues"
14-
repo_model "code.gitea.io/gitea/models/repo"
15-
"code.gitea.io/gitea/modules/git"
16-
"code.gitea.io/gitea/modules/gitrepo"
1714
"code.gitea.io/gitea/modules/label"
18-
"code.gitea.io/gitea/modules/log"
1915
"code.gitea.io/gitea/modules/options"
2016
"code.gitea.io/gitea/modules/setting"
2117
"code.gitea.io/gitea/modules/util"
@@ -121,29 +117,6 @@ func LoadRepoConfig() error {
121117
return nil
122118
}
123119

124-
func CheckInitRepository(ctx context.Context, repo *repo_model.Repository) (err error) {
125-
// Somehow the directory could exist.
126-
isExist, err := gitrepo.IsRepositoryExist(ctx, repo)
127-
if err != nil {
128-
log.Error("Unable to check if %s exists. Error: %v", repo.FullName(), err)
129-
return err
130-
}
131-
if isExist {
132-
return repo_model.ErrRepoFilesAlreadyExist{
133-
Uname: repo.OwnerName,
134-
Name: repo.Name,
135-
}
136-
}
137-
138-
// Init git bare new repository.
139-
if err = git.InitRepository(ctx, repo.RepoPath(), true, repo.ObjectFormatName); err != nil {
140-
return fmt.Errorf("git.InitRepository: %w", err)
141-
} else if err = gitrepo.CreateDelegateHooks(ctx, repo); err != nil {
142-
return fmt.Errorf("createDelegateHooks: %w", err)
143-
}
144-
return nil
145-
}
146-
147120
// InitializeLabels adds a label set to a repository using a template
148121
func InitializeLabels(ctx context.Context, id int64, labelTemplate string, isOrg bool) error {
149122
list, err := LoadTemplateLabelsByDisplayName(labelTemplate)

0 commit comments

Comments
 (0)