Skip to content

Fix case change in ownernames #16045

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -1350,6 +1350,26 @@ func UpdateRepository(repo *Repository, visibilityChanged bool) (err error) {
return sess.Commit()
}

// UpdateRepositoryOwnerNames updates repository owner_names (this should only be used when the ownerName has changed case)
func UpdateRepositoryOwnerNames(ownerID int64, ownerName string) error {
if ownerID == 0 {
return nil
}
sess := x.NewSession()
defer sess.Close()
if err := sess.Begin(); err != nil {
return err
}

if _, err := sess.Where("owner_id = ?", ownerID).Cols("owner_name").Update(&Repository{
OwnerName: ownerName,
}); err != nil {
return err
}

return sess.Commit()
}

// UpdateRepositoryUpdatedTime updates a repository's updated time
func UpdateRepositoryUpdatedTime(repoID int64, updateTime time.Time) error {
_, err := x.Exec("UPDATE repository SET updated_unix = ? WHERE id = ?", updateTime.Unix(), repoID)
Expand Down
9 changes: 9 additions & 0 deletions routers/org/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func SettingsPost(ctx *context.Context) {
}

org := ctx.Org.Organization
nameChanged := org.Name != form.Name

// Check if organization name has been changed.
if org.LowerName != strings.ToLower(form.Name) {
Expand All @@ -75,7 +76,9 @@ func SettingsPost(ctx *context.Context) {
// reset ctx.org.OrgLink with new name
ctx.Org.OrgLink = setting.AppSubURL + "/org/" + form.Name
log.Trace("Organization name changed: %s -> %s", org.Name, form.Name)
nameChanged = false
}

// In case it's just a case change.
org.Name = form.Name
org.LowerName = strings.ToLower(form.Name)
Expand Down Expand Up @@ -105,11 +108,17 @@ func SettingsPost(ctx *context.Context) {
return
}
for _, repo := range org.Repos {
repo.OwnerName = org.Name
if err := models.UpdateRepository(repo, true); err != nil {
ctx.ServerError("UpdateRepository", err)
return
}
}
} else if nameChanged {
if err := models.UpdateRepositoryOwnerNames(org.ID, org.Name); err != nil {
ctx.ServerError("UpdateRepository", err)
return
}
}

log.Trace("Organization setting updated: %s", org.Name)
Expand Down
8 changes: 7 additions & 1 deletion routers/user/setting/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,13 @@ func HandleUsernameChange(ctx *context.Context, user *models.User, newName strin
}
return err
}
log.Trace("User name changed: %s -> %s", user.Name, newName)
} else {
if err := models.UpdateRepositoryOwnerNames(user.ID, newName); err != nil {
ctx.ServerError("UpdateRepository", err)
return err
}
}
log.Trace("User name changed: %s -> %s", user.Name, newName)
return nil
}

Expand All @@ -85,6 +90,7 @@ func ProfilePost(ctx *context.Context) {
}

if len(form.Name) != 0 && ctx.User.Name != form.Name {
log.Debug("Changing name for %s to %s", ctx.User.Name, form.Name)
if err := HandleUsernameChange(ctx, ctx.User, form.Name); err != nil {
ctx.Redirect(setting.AppSubURL + "/user/settings")
return
Expand Down