Skip to content

Commit f8da672

Browse files
committed
[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 <[email protected]>
1 parent cf1c57b commit f8da672

File tree

4 files changed

+130
-48
lines changed

4 files changed

+130
-48
lines changed

routers/web/repo/pull.go

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -115,21 +115,21 @@ func getRepository(ctx *context.Context, repoID int64) *repo_model.Repository {
115115
return repo
116116
}
117117

118-
func getForkRepository(ctx *context.Context) *repo_model.Repository {
119-
forkRepo := getRepository(ctx, ctx.ParamsInt64(":repoid"))
120-
if ctx.Written() {
121-
return nil
118+
func updateForkRepositoryInContext(ctx *context.Context, forkRepo *repo_model.Repository) bool {
119+
if forkRepo == nil {
120+
ctx.NotFound("No repository in context", nil)
121+
return false
122122
}
123123

124124
if forkRepo.IsEmpty {
125125
log.Trace("Empty repository %-v", forkRepo)
126-
ctx.NotFound("getForkRepository", nil)
127-
return nil
126+
ctx.NotFound("updateForkRepositoryInContext", nil)
127+
return false
128128
}
129129

130130
if err := forkRepo.LoadOwner(ctx); err != nil {
131131
ctx.ServerError("LoadOwner", err)
132-
return nil
132+
return false
133133
}
134134

135135
ctx.Data["repo_name"] = forkRepo.Name
@@ -142,7 +142,7 @@ func getForkRepository(ctx *context.Context) *repo_model.Repository {
142142
ownedOrgs, err := organization.GetOrgsCanCreateRepoByUserID(ctx, ctx.Doer.ID)
143143
if err != nil {
144144
ctx.ServerError("GetOrgsCanCreateRepoByUserID", err)
145-
return nil
145+
return false
146146
}
147147
var orgs []*organization.Organization
148148
for _, org := range ownedOrgs {
@@ -170,7 +170,7 @@ func getForkRepository(ctx *context.Context) *repo_model.Repository {
170170
traverseParentRepo, err = repo_model.GetRepositoryByID(ctx, traverseParentRepo.ForkID)
171171
if err != nil {
172172
ctx.ServerError("GetRepositoryByID", err)
173-
return nil
173+
return false
174174
}
175175
}
176176

@@ -184,7 +184,7 @@ func getForkRepository(ctx *context.Context) *repo_model.Repository {
184184
} else {
185185
ctx.Data["CanForkRepo"] = false
186186
ctx.Flash.Error(ctx.Tr("repo.fork_no_valid_owners"), true)
187-
return nil
187+
return false
188188
}
189189

190190
branches, err := git_model.FindBranchNames(ctx, git_model.FindBranchOptions{
@@ -198,14 +198,19 @@ func getForkRepository(ctx *context.Context) *repo_model.Repository {
198198
})
199199
if err != nil {
200200
ctx.ServerError("FindBranchNames", err)
201-
return nil
201+
return false
202202
}
203203
ctx.Data["Branches"] = append([]string{ctx.Repo.Repository.DefaultBranch}, branches...)
204204

205-
return forkRepo
205+
return true
206+
}
207+
208+
// ForkByID redirects (with 301 Moved Permanently) to the repository's `/fork` page
209+
func ForkByID(ctx *context.Context) {
210+
ctx.Redirect(ctx.Repo.Repository.Link()+"/fork", http.StatusMovedPermanently)
206211
}
207212

208-
// Fork render repository fork page
213+
// Fork renders the repository fork page
209214
func Fork(ctx *context.Context) {
210215
ctx.Data["Title"] = ctx.Tr("new_fork")
211216

@@ -217,8 +222,7 @@ func Fork(ctx *context.Context) {
217222
ctx.Flash.Error(msg, true)
218223
}
219224

220-
getForkRepository(ctx)
221-
if ctx.Written() {
225+
if !updateForkRepositoryInContext(ctx, ctx.Repo.Repository) {
222226
return
223227
}
224228

@@ -236,8 +240,8 @@ func ForkPost(ctx *context.Context) {
236240
return
237241
}
238242

239-
forkRepo := getForkRepository(ctx)
240-
if ctx.Written() {
243+
forkRepo := ctx.Repo.Repository
244+
if !updateForkRepositoryInContext(ctx, forkRepo) {
241245
return
242246
}
243247

routers/web/web.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -967,10 +967,7 @@ func registerRoutes(m *web.Route) {
967967
m.Post("/create", web.Bind(forms.CreateRepoForm{}), repo.CreatePost)
968968
m.Get("/migrate", repo.Migrate)
969969
m.Post("/migrate", web.Bind(forms.MigrateRepoForm{}), repo.MigratePost)
970-
m.Group("/fork", func() {
971-
m.Combo("/{repoid}").Get(repo.Fork).
972-
Post(web.Bind(forms.CreateRepoForm{}), repo.ForkPost)
973-
}, context.RepoIDAssignment(), context.UnitTypes(), reqRepoCodeReader)
970+
m.Get("/fork/{repoid}", context.RepoIDAssignment(), context.UnitTypes(), reqRepoCodeReader, repo.ForkByID)
974971
m.Get("/search", repo.SearchRepo)
975972
}, reqSignIn)
976973

@@ -1148,6 +1145,8 @@ func registerRoutes(m *web.Route) {
11481145

11491146
// Grouping for those endpoints that do require authentication
11501147
m.Group("/{username}/{reponame}", func() {
1148+
m.Combo("/fork", reqRepoCodeReader).Get(repo.Fork).
1149+
Post(web.Bind(forms.CreateRepoForm{}), repo.ForkPost)
11511150
m.Group("/issues", func() {
11521151
m.Group("/new", func() {
11531152
m.Combo("").Get(context.RepoRef(), repo.NewIssue).

templates/repo/header.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
{{/*else is not required here, because the button shouldn't link to any site if you can't create a fork*/}}
8383
{{end}}
8484
{{else if not $.UserAndOrgForks}}
85-
href="{{AppSubUrl}}/repo/fork/{{.ID}}"
85+
href="{{$.RepoLink}}/fork"
8686
{{else}}
8787
data-modal="#fork-repo-modal"
8888
{{end}}
@@ -103,7 +103,7 @@
103103
</div>
104104
{{if $.CanSignedUserFork}}
105105
<div class="divider"></div>
106-
<a href="{{AppSubUrl}}/repo/fork/{{.ID}}">{{ctx.Locale.Tr "repo.fork_to_different_account"}}</a>
106+
<a href="{{$.RepoLink}}/fork">{{ctx.Locale.Tr "repo.fork_to_different_account"}}</a>
107107
{{end}}
108108
</div>
109109
</div>

tests/integration/repo_fork_test.go

Lines changed: 104 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,36 @@ import (
77
"fmt"
88
"net/http"
99
"net/http/httptest"
10+
"net/url"
1011
"testing"
1112

13+
"code.gitea.io/gitea/models/db"
14+
repo_model "code.gitea.io/gitea/models/repo"
1215
"code.gitea.io/gitea/models/unittest"
1316
user_model "code.gitea.io/gitea/models/user"
17+
repo_service "code.gitea.io/gitea/services/repository"
1418
"code.gitea.io/gitea/tests"
1519

1620
"github.com/stretchr/testify/assert"
1721
)
1822

1923
func testRepoFork(t *testing.T, session *TestSession, ownerName, repoName, forkOwnerName, forkRepoName string) *httptest.ResponseRecorder {
24+
t.Helper()
25+
2026
forkOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: forkOwnerName})
2127

2228
// Step0: check the existence of the to-fork repo
2329
req := NewRequestf(t, "GET", "/%s/%s", forkOwnerName, forkRepoName)
2430
session.MakeRequest(t, req, http.StatusNotFound)
2531

26-
// Step1: go to the main page of repo
27-
req = NewRequestf(t, "GET", "/%s/%s", ownerName, repoName)
32+
// Step1: visit the /fork page
33+
forkURL := fmt.Sprintf("/%s/%s/fork", ownerName, repoName)
34+
req = NewRequest(t, "GET", forkURL)
2835
resp := session.MakeRequest(t, req, http.StatusOK)
2936

30-
// Step2: click the fork button
37+
// Step2: fill the form of the forking
3138
htmlDoc := NewHTMLParser(t, resp.Body)
32-
link, exists := htmlDoc.doc.Find("a.ui.button[href^=\"/repo/fork/\"]").Attr("href")
33-
assert.True(t, exists, "The template has changed")
34-
req = NewRequest(t, "GET", link)
35-
resp = session.MakeRequest(t, req, http.StatusOK)
36-
37-
// Step3: fill the form of the forking
38-
htmlDoc = NewHTMLParser(t, resp.Body)
39-
link, exists = htmlDoc.doc.Find("form.ui.form[action^=\"/repo/fork/\"]").Attr("action")
39+
link, exists := htmlDoc.doc.Find(fmt.Sprintf("form.ui.form[action=\"%s\"]", forkURL)).Attr("action")
4040
assert.True(t, exists, "The template has changed")
4141
_, exists = htmlDoc.doc.Find(fmt.Sprintf(".owner.dropdown .item[data-value=\"%d\"]", forkOwner.ID)).Attr("data-value")
4242
assert.True(t, exists, fmt.Sprintf("Fork owner '%s' is not present in select box", forkOwnerName))
@@ -47,29 +47,108 @@ func testRepoFork(t *testing.T, session *TestSession, ownerName, repoName, forkO
4747
})
4848
session.MakeRequest(t, req, http.StatusSeeOther)
4949

50-
// Step4: check the existence of the forked repo
50+
// Step3: check the existence of the forked repo
5151
req = NewRequestf(t, "GET", "/%s/%s", forkOwnerName, forkRepoName)
5252
resp = session.MakeRequest(t, req, http.StatusOK)
5353

5454
return resp
5555
}
5656

57+
func testRepoForkLegacyRedirect(t *testing.T, session *TestSession, ownerName, repoName string) {
58+
t.Helper()
59+
60+
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: ownerName})
61+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: owner.ID, Name: repoName})
62+
63+
// Visit the /repo/fork/:id url
64+
req := NewRequestf(t, "GET", "/repo/fork/%d", repo.ID)
65+
resp := session.MakeRequest(t, req, http.StatusMovedPermanently)
66+
67+
assert.Equal(t, repo.Link()+"/fork", resp.Header().Get("Location"))
68+
}
69+
5770
func TestRepoFork(t *testing.T) {
58-
defer tests.PrepareTestEnv(t)()
59-
session := loginUser(t, "user1")
60-
testRepoFork(t, session, "user2", "repo1", "user1", "repo1")
71+
onGiteaRun(t, func(t *testing.T, u *url.URL) {
72+
user5 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user5"})
73+
session := loginUser(t, user5.Name)
74+
75+
t.Run("by name", func(t *testing.T) {
76+
defer tests.PrintCurrentTest(t)()
77+
defer func() {
78+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: user5.ID, Name: "repo1"})
79+
repo_service.DeleteRepository(db.DefaultContext, user5, repo, false)
80+
}()
81+
testRepoFork(t, session, "user2", "repo1", "user5", "repo1")
82+
})
83+
84+
t.Run("legacy redirect", func(t *testing.T) {
85+
defer tests.PrintCurrentTest(t)()
86+
testRepoForkLegacyRedirect(t, session, "user2", "repo1")
87+
88+
t.Run("private 404", func(t *testing.T) {
89+
defer tests.PrintCurrentTest(t)()
90+
91+
// Make sure the repo we try to fork is private
92+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 31, IsPrivate: true})
93+
94+
// user5 does not have access to user2/repo20
95+
req := NewRequestf(t, "GET", "/repo/fork/%d", repo.ID) // user2/repo20
96+
session.MakeRequest(t, req, http.StatusNotFound)
97+
})
98+
t.Run("authenticated private redirect", func(t *testing.T) {
99+
defer tests.PrintCurrentTest(t)()
100+
101+
// Make sure the repo we try to fork is private
102+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 31, IsPrivate: true})
103+
104+
// user1 has access to user2/repo20
105+
session := loginUser(t, "user1")
106+
req := NewRequestf(t, "GET", "/repo/fork/%d", repo.ID) // user2/repo20
107+
session.MakeRequest(t, req, http.StatusMovedPermanently)
108+
})
109+
t.Run("no code unit", func(t *testing.T) {
110+
defer tests.PrintCurrentTest(t)()
111+
112+
// Make sure the repo we try to fork is private.
113+
// We're also choosing user15/big_test_private_2, becase it has the Code unit disabled.
114+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 20, IsPrivate: true})
115+
116+
// user1, even though an admin, can't fork a repo without a code unit.
117+
session := loginUser(t, "user1")
118+
req := NewRequestf(t, "GET", "/repo/fork/%d", repo.ID) // user15/big_test_private_2
119+
session.MakeRequest(t, req, http.StatusNotFound)
120+
})
121+
})
122+
})
61123
}
62124

63125
func TestRepoForkToOrg(t *testing.T) {
64-
defer tests.PrepareTestEnv(t)()
65-
session := loginUser(t, "user2")
66-
testRepoFork(t, session, "user2", "repo1", "org3", "repo1")
126+
onGiteaRun(t, func(t *testing.T, u *url.URL) {
127+
session := loginUser(t, "user2")
128+
org3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "org3"})
67129

68-
// Check that no more forking is allowed as user2 owns repository
69-
// and org3 organization that owner user2 is also now has forked this repository
70-
req := NewRequest(t, "GET", "/user2/repo1")
71-
resp := session.MakeRequest(t, req, http.StatusOK)
72-
htmlDoc := NewHTMLParser(t, resp.Body)
73-
_, exists := htmlDoc.doc.Find("a.ui.button[href^=\"/repo/fork/\"]").Attr("href")
74-
assert.False(t, exists, "Forking should not be allowed anymore")
130+
t.Run("by name", func(t *testing.T) {
131+
defer tests.PrintCurrentTest(t)()
132+
defer func() {
133+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: org3.ID, Name: "repo1"})
134+
repo_service.DeleteRepository(db.DefaultContext, org3, repo, false)
135+
}()
136+
137+
testRepoFork(t, session, "user2", "repo1", "org3", "repo1")
138+
139+
// Check that no more forking is allowed as user2 owns repository
140+
// and org3 organization that owner user2 is also now has forked this repository
141+
req := NewRequest(t, "GET", "/user2/repo1")
142+
resp := session.MakeRequest(t, req, http.StatusOK)
143+
htmlDoc := NewHTMLParser(t, resp.Body)
144+
_, exists := htmlDoc.doc.Find("a.ui.button[href^=\"/fork\"]").Attr("href")
145+
assert.False(t, exists, "Forking should not be allowed anymore")
146+
})
147+
148+
t.Run("legacy redirect", func(t *testing.T) {
149+
defer tests.PrintCurrentTest(t)()
150+
151+
testRepoForkLegacyRedirect(t, session, "user2", "repo1")
152+
})
153+
})
75154
}

0 commit comments

Comments
 (0)