Skip to content

Commit 3dc6af3

Browse files
Fix creation of Organization repos by Users with max created personal repos (#11183)
* Fix creation of Org repos Fix #9269 * Change variable name to appease linter * Update PR with suggestions Add a note for user.CanCreateRepo() about failure assumptions Change repo.create help message Co-authored-by: guillep2k <[email protected]>
1 parent 28e5e7f commit 3dc6af3

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

models/user.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ func (u *User) MaxCreationLimit() int {
279279
}
280280

281281
// CanCreateRepo returns if user login can create a repository
282+
// NOTE: functions calling this assume a failure due to repository count limit; if new checks are added, those functions should be revised
282283
func (u *User) CanCreateRepo() bool {
283284
if u.IsAdmin {
284285
return true

routers/repo/repo.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ func checkContextUser(ctx *context.Context, uid int64) *models.User {
6161
ctx.ServerError("GetOrgsCanCreateRepoByUserID", err)
6262
return nil
6363
}
64-
ctx.Data["Orgs"] = orgs
6564

6665
// Not equal means current user is an organization.
6766
if uid == ctx.User.ID || uid == 0 {
@@ -84,6 +83,14 @@ func checkContextUser(ctx *context.Context, uid int64) *models.User {
8483
return nil
8584
}
8685
if !ctx.User.IsAdmin {
86+
orgsAvailable := []*models.User{}
87+
for i := 0; i < len(orgs); i++ {
88+
if orgs[i].CanCreateRepo() {
89+
orgsAvailable = append(orgsAvailable, orgs[i])
90+
}
91+
}
92+
ctx.Data["Orgs"] = orgsAvailable
93+
8794
canCreate, err := org.CanCreateOrgRepo(ctx.User.ID)
8895
if err != nil {
8996
ctx.ServerError("CanCreateOrgRepo", err)
@@ -92,6 +99,8 @@ func checkContextUser(ctx *context.Context, uid int64) *models.User {
9299
ctx.Error(403)
93100
return nil
94101
}
102+
} else {
103+
ctx.Data["Orgs"] = orgs
95104
}
96105
return org
97106
}
@@ -111,10 +120,6 @@ func getRepoPrivate(ctx *context.Context) bool {
111120

112121
// Create render creating repository page
113122
func Create(ctx *context.Context) {
114-
if !ctx.User.CanCreateRepo() {
115-
ctx.RenderWithErr(ctx.Tr("repo.form.reach_limit_of_creation", ctx.User.MaxCreationLimit()), tplCreate, nil)
116-
}
117-
118123
ctx.Data["Title"] = ctx.Tr("new_repo")
119124

120125
// Give default value for template to render.
@@ -142,7 +147,11 @@ func Create(ctx *context.Context) {
142147
}
143148
}
144149

145-
ctx.HTML(200, tplCreate)
150+
if !ctx.User.CanCreateRepo() {
151+
ctx.RenderWithErr(ctx.Tr("repo.form.reach_limit_of_creation", ctx.User.MaxCreationLimit()), tplCreate, nil)
152+
} else {
153+
ctx.HTML(200, tplCreate)
154+
}
146155
}
147156

148157
func handleCreateError(ctx *context.Context, owner *models.User, err error, name string, tpl base.TplName, form interface{}) {

templates/repo/create.tmpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
{{end}}
3232
</div>
3333
</div>
34+
<span class="help">Some organizations may not show up in the dropdown due to a maximum repository count limit</span>
3435
</div>
3536

3637
<div class="inline required field {{if .Err_RepoName}}error{{end}}">

0 commit comments

Comments
 (0)