mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-05-21 09:21:15 +00:00
[FEAT]: Allow forking without a repo ID
Forking a repository via the web UI currently requires visiting a `/repo/fork/{{repoid}}` URL. This makes it cumbersome to create a link that starts a fork, because the repository ID is only available via the API. While it *is* possible to create a link, doing so requires extra steps. To make it easier to have a "Fork me!"-style links, introduce the `/{username}/{repo}/fork` route, which will start the forking process based on the repository in context instead. The old `/repo/fork/{repoid}` route (with a `GET` request) will remain there for the sake of backwards compatibility, but will redirect to the new URL instead. It's `POST` handler is removed. Tests that used the old route are updated to use the new one, and new tests are introduced to exercise the redirect. Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
This commit is contained in:
parent
cf1c57b681
commit
f8da672307
4 changed files with 130 additions and 48 deletions
|
@ -115,21 +115,21 @@ func getRepository(ctx *context.Context, repoID int64) *repo_model.Repository {
|
|||
return repo
|
||||
}
|
||||
|
||||
func getForkRepository(ctx *context.Context) *repo_model.Repository {
|
||||
forkRepo := getRepository(ctx, ctx.ParamsInt64(":repoid"))
|
||||
if ctx.Written() {
|
||||
return nil
|
||||
func updateForkRepositoryInContext(ctx *context.Context, forkRepo *repo_model.Repository) bool {
|
||||
if forkRepo == nil {
|
||||
ctx.NotFound("No repository in context", nil)
|
||||
return false
|
||||
}
|
||||
|
||||
if forkRepo.IsEmpty {
|
||||
log.Trace("Empty repository %-v", forkRepo)
|
||||
ctx.NotFound("getForkRepository", nil)
|
||||
return nil
|
||||
ctx.NotFound("updateForkRepositoryInContext", nil)
|
||||
return false
|
||||
}
|
||||
|
||||
if err := forkRepo.LoadOwner(ctx); err != nil {
|
||||
ctx.ServerError("LoadOwner", err)
|
||||
return nil
|
||||
return false
|
||||
}
|
||||
|
||||
ctx.Data["repo_name"] = forkRepo.Name
|
||||
|
@ -142,7 +142,7 @@ func getForkRepository(ctx *context.Context) *repo_model.Repository {
|
|||
ownedOrgs, err := organization.GetOrgsCanCreateRepoByUserID(ctx, ctx.Doer.ID)
|
||||
if err != nil {
|
||||
ctx.ServerError("GetOrgsCanCreateRepoByUserID", err)
|
||||
return nil
|
||||
return false
|
||||
}
|
||||
var orgs []*organization.Organization
|
||||
for _, org := range ownedOrgs {
|
||||
|
@ -170,7 +170,7 @@ func getForkRepository(ctx *context.Context) *repo_model.Repository {
|
|||
traverseParentRepo, err = repo_model.GetRepositoryByID(ctx, traverseParentRepo.ForkID)
|
||||
if err != nil {
|
||||
ctx.ServerError("GetRepositoryByID", err)
|
||||
return nil
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -184,7 +184,7 @@ func getForkRepository(ctx *context.Context) *repo_model.Repository {
|
|||
} else {
|
||||
ctx.Data["CanForkRepo"] = false
|
||||
ctx.Flash.Error(ctx.Tr("repo.fork_no_valid_owners"), true)
|
||||
return nil
|
||||
return false
|
||||
}
|
||||
|
||||
branches, err := git_model.FindBranchNames(ctx, git_model.FindBranchOptions{
|
||||
|
@ -198,14 +198,19 @@ func getForkRepository(ctx *context.Context) *repo_model.Repository {
|
|||
})
|
||||
if err != nil {
|
||||
ctx.ServerError("FindBranchNames", err)
|
||||
return nil
|
||||
return false
|
||||
}
|
||||
ctx.Data["Branches"] = append([]string{ctx.Repo.Repository.DefaultBranch}, branches...)
|
||||
|
||||
return forkRepo
|
||||
return true
|
||||
}
|
||||
|
||||
// Fork render repository fork page
|
||||
// ForkByID redirects (with 301 Moved Permanently) to the repository's `/fork` page
|
||||
func ForkByID(ctx *context.Context) {
|
||||
ctx.Redirect(ctx.Repo.Repository.Link()+"/fork", http.StatusMovedPermanently)
|
||||
}
|
||||
|
||||
// Fork renders the repository fork page
|
||||
func Fork(ctx *context.Context) {
|
||||
ctx.Data["Title"] = ctx.Tr("new_fork")
|
||||
|
||||
|
@ -217,8 +222,7 @@ func Fork(ctx *context.Context) {
|
|||
ctx.Flash.Error(msg, true)
|
||||
}
|
||||
|
||||
getForkRepository(ctx)
|
||||
if ctx.Written() {
|
||||
if !updateForkRepositoryInContext(ctx, ctx.Repo.Repository) {
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -236,8 +240,8 @@ func ForkPost(ctx *context.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
forkRepo := getForkRepository(ctx)
|
||||
if ctx.Written() {
|
||||
forkRepo := ctx.Repo.Repository
|
||||
if !updateForkRepositoryInContext(ctx, forkRepo) {
|
||||
return
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue