Skip to content

Commit

Permalink
Create a repo redirect when transferring ownership (#6210) (#6211)
Browse files Browse the repository at this point in the history
When transferring ownership of a repo to a different user/org,
create a repo redirect that points to the new location in the same
way that is done when a repo is renamed.

Signed-off-by: James E. Blair <jeblair@redhat.com>
  • Loading branch information
jeblair authored and lunny committed Feb 28, 2019
1 parent 525f033 commit 48c101a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion modules/context/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func RedirectToRepo(ctx *Context, redirectRepoID int64) {
redirectPath := strings.Replace(
ctx.Req.URL.Path,
fmt.Sprintf("%s/%s", ownerName, previousRepoName),
fmt.Sprintf("%s/%s", ownerName, repo.Name),
fmt.Sprintf("%s/%s", repo.MustOwnerName(), repo.Name),
1,
)
ctx.Redirect(redirectPath)
Expand Down
8 changes: 8 additions & 0 deletions routers/repo/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
return
}

oldOwnerID := ctx.Repo.Owner.ID
if err = models.TransferOwnership(ctx.User, newOwner, repo); err != nil {
if models.IsErrRepoAlreadyExist(err) {
ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), tplSettingsOptions, nil)
Expand All @@ -329,6 +330,13 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
}
return
}

err = models.NewRepoRedirect(oldOwnerID, repo.ID, repo.Name, repo.Name)
if err != nil {
ctx.ServerError("NewRepoRedirect", err)
return
}

log.Trace("Repository transferred: %s/%s -> %s", ctx.Repo.Owner.Name, repo.Name, newOwner)
ctx.Flash.Success(ctx.Tr("repo.settings.transfer_succeed"))
ctx.Redirect(setting.AppSubURL + "/" + newOwner + "/" + repo.Name)
Expand Down

0 comments on commit 48c101a

Please sign in to comment.