Fix invalid repo urls after change username (#10150)

* Fix invalid repo urls after change username

* Update user.go

Co-authored-by: Lauris BH <lauris@nix.lv>
Co-authored-by: Antoine GIRARD <sapk@users.noreply.github.com>
This commit is contained in:
mavenbline 2020-02-08 17:59:40 +03:00 committed by GitHub
parent 7d8a2d05cf
commit 74a4a1e17f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 1 deletions

View File

@ -1044,13 +1044,23 @@ func ChangeUserName(u *User, newUserName string) (err error) {
} else if isExist { } else if isExist {
return ErrUserAlreadyExist{newUserName} return ErrUserAlreadyExist{newUserName}
} }
sess := x.NewSession()
defer sess.Close()
if err = sess.Begin(); err != nil {
return err
}
if _, err = sess.Exec("UPDATE `repository` SET owner_name=? WHERE owner_name=?", newUserName, u.Name); err != nil {
return fmt.Errorf("Change repo owner name: %v", err)
}
// Do not fail if directory does not exist // Do not fail if directory does not exist
if err = os.Rename(UserPath(u.Name), UserPath(newUserName)); err != nil && !os.IsNotExist(err) { if err = os.Rename(UserPath(u.Name), UserPath(newUserName)); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("Rename user directory: %v", err) return fmt.Errorf("Rename user directory: %v", err)
} }
return nil return sess.Commit()
} }
// checkDupEmail checks whether there are the same email with the user // checkDupEmail checks whether there are the same email with the user