Skip to content

Commit 48c101a

Browse files
jeblairlunny
authored andcommitted
Create a repo redirect when transferring ownership (#6210) (#6211)
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 <[email protected]>
1 parent 525f033 commit 48c101a

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

modules/context/repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ func RedirectToRepo(ctx *Context, redirectRepoID int64) {
204204
redirectPath := strings.Replace(
205205
ctx.Req.URL.Path,
206206
fmt.Sprintf("%s/%s", ownerName, previousRepoName),
207-
fmt.Sprintf("%s/%s", ownerName, repo.Name),
207+
fmt.Sprintf("%s/%s", repo.MustOwnerName(), repo.Name),
208208
1,
209209
)
210210
ctx.Redirect(redirectPath)

routers/repo/setting.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
321321
return
322322
}
323323

324+
oldOwnerID := ctx.Repo.Owner.ID
324325
if err = models.TransferOwnership(ctx.User, newOwner, repo); err != nil {
325326
if models.IsErrRepoAlreadyExist(err) {
326327
ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), tplSettingsOptions, nil)
@@ -329,6 +330,13 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
329330
}
330331
return
331332
}
333+
334+
err = models.NewRepoRedirect(oldOwnerID, repo.ID, repo.Name, repo.Name)
335+
if err != nil {
336+
ctx.ServerError("NewRepoRedirect", err)
337+
return
338+
}
339+
332340
log.Trace("Repository transferred: %s/%s -> %s", ctx.Repo.Owner.Name, repo.Name, newOwner)
333341
ctx.Flash.Success(ctx.Tr("repo.settings.transfer_succeed"))
334342
ctx.Redirect(setting.AppSubURL + "/" + newOwner + "/" + repo.Name)

0 commit comments

Comments
 (0)