Redirects for renamed repos (#807)

* Redirects for renamed repos

* Remove unused phrase from locales
This commit is contained in:
Ethan Koenig 2017-02-05 09:35:03 -05:00 committed by Lunny Xiao
parent e86d935175
commit 027591a3a5
29 changed files with 127 additions and 27 deletions

View file

@ -133,6 +133,26 @@ func earlyResponseForGoGetMeta(ctx *Context) {
})))
}
// RedirectToRepo redirect to a differently-named repository
func RedirectToRepo(ctx *Context, redirectRepoID int64) {
ownerName := ctx.Params(":username")
previousRepoName := ctx.Params(":reponame")
repo, err := models.GetRepositoryByID(redirectRepoID)
if err != nil {
ctx.Handle(500, "GetRepositoryByID", err)
return
}
redirectPath := strings.Replace(
ctx.Req.URL.Path,
fmt.Sprintf("%s/%s", ownerName, previousRepoName),
fmt.Sprintf("%s/%s", ownerName, repo.Name),
1,
)
ctx.Redirect(redirectPath)
}
// RepoAssignment returns a macaron to handle repository assignment
func RepoAssignment(args ...bool) macaron.Handler {
return func(ctx *Context) {
@ -176,11 +196,18 @@ func RepoAssignment(args ...bool) macaron.Handler {
repo, err := models.GetRepositoryByName(owner.ID, repoName)
if err != nil {
if models.IsErrRepoNotExist(err) {
if ctx.Query("go-get") == "1" {
earlyResponseForGoGetMeta(ctx)
return
redirectRepoID, err := models.LookupRepoRedirect(owner.ID, repoName)
if err == nil {
RedirectToRepo(ctx, redirectRepoID)
} else if models.IsErrRepoRedirectNotExist(err) {
if ctx.Query("go-get") == "1" {
earlyResponseForGoGetMeta(ctx)
return
}
ctx.Handle(404, "GetRepositoryByName", err)
} else {
ctx.Handle(500, "LookupRepoRedirect", err)
}
ctx.Handle(404, "GetRepositoryByName", err)
} else {
ctx.Handle(500, "GetRepositoryByName", err)
}