Skip to content

Commit 038e1db

Browse files
authored
Return go-get info on subdirs (#15642)
This PR is an alternative to #15628 and makes the go get handler a handler. Fix #15625 Close #15628 Signed-off-by: Andrew Thornton <[email protected]>
1 parent 4a84022 commit 038e1db

File tree

3 files changed

+123
-65
lines changed

3 files changed

+123
-65
lines changed

integrations/goget_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package integrations
6+
7+
import (
8+
"fmt"
9+
"net/http"
10+
"testing"
11+
12+
"code.gitea.io/gitea/modules/setting"
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
func TestGoGet(t *testing.T) {
17+
defer prepareTestEnv(t)()
18+
19+
req := NewRequest(t, "GET", "/blah/glah/plah?go-get=1")
20+
resp := MakeRequest(t, req, http.StatusOK)
21+
22+
expected := fmt.Sprintf(`<!doctype html>
23+
<html>
24+
<head>
25+
<meta name="go-import" content="%[1]s:%[2]s/blah/glah git %[3]sblah/glah.git">
26+
<meta name="go-source" content="%[1]s:%[2]s/blah/glah _ %[3]sblah/glah/src/branch/master{/dir} %[3]sblah/glah/src/branch/master{/dir}/{file}#L{line}">
27+
</head>
28+
<body>
29+
go get --insecure %[1]s:%[2]s/blah/glah
30+
</body>
31+
</html>
32+
`, setting.Domain, setting.HTTPPort, setting.AppURL)
33+
34+
assert.Equal(t, expected, resp.Body.String())
35+
}

routers/routes/goget.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package routes
6+
7+
import (
8+
"net/http"
9+
"net/url"
10+
"path"
11+
"strings"
12+
13+
"code.gitea.io/gitea/models"
14+
"code.gitea.io/gitea/modules/context"
15+
"code.gitea.io/gitea/modules/setting"
16+
"code.gitea.io/gitea/modules/util"
17+
"github.com/unknwon/com"
18+
)
19+
20+
func goGet(ctx *context.Context) {
21+
if ctx.Req.Method != "GET" || ctx.Query("go-get") != "1" || len(ctx.Req.URL.Query()) > 1 {
22+
return
23+
}
24+
25+
parts := strings.SplitN(ctx.Req.URL.EscapedPath(), "/", 4)
26+
27+
if len(parts) < 3 {
28+
return
29+
}
30+
31+
ownerName := parts[1]
32+
repoName := parts[2]
33+
34+
// Quick responses appropriate go-get meta with status 200
35+
// regardless of if user have access to the repository,
36+
// or the repository does not exist at all.
37+
// This is particular a workaround for "go get" command which does not respect
38+
// .netrc file.
39+
40+
trimmedRepoName := strings.TrimSuffix(repoName, ".git")
41+
42+
if ownerName == "" || trimmedRepoName == "" {
43+
_, _ = ctx.Write([]byte(`<!doctype html>
44+
<html>
45+
<body>
46+
invalid import path
47+
</body>
48+
</html>
49+
`))
50+
ctx.Status(400)
51+
return
52+
}
53+
branchName := setting.Repository.DefaultBranch
54+
55+
repo, err := models.GetRepositoryByOwnerAndName(ownerName, repoName)
56+
if err == nil && len(repo.DefaultBranch) > 0 {
57+
branchName = repo.DefaultBranch
58+
}
59+
prefix := setting.AppURL + path.Join(url.PathEscape(ownerName), url.PathEscape(repoName), "src", "branch", util.PathEscapeSegments(branchName))
60+
61+
appURL, _ := url.Parse(setting.AppURL)
62+
63+
insecure := ""
64+
if appURL.Scheme == string(setting.HTTP) {
65+
insecure = "--insecure "
66+
}
67+
ctx.Header().Set("Content-Type", "text/html")
68+
ctx.Status(http.StatusOK)
69+
_, _ = ctx.Write([]byte(com.Expand(`<!doctype html>
70+
<html>
71+
<head>
72+
<meta name="go-import" content="{GoGetImport} git {CloneLink}">
73+
<meta name="go-source" content="{GoGetImport} _ {GoDocDirectory} {GoDocFile}">
74+
</head>
75+
<body>
76+
go get {Insecure}{GoGetImport}
77+
</body>
78+
</html>
79+
`, map[string]string{
80+
"GoGetImport": context.ComposeGoGetImport(ownerName, trimmedRepoName),
81+
"CloneLink": models.ComposeHTTPSCloneURL(ownerName, repoName),
82+
"GoDocDirectory": prefix + "{/dir}",
83+
"GoDocFile": prefix + "{/dir}/{file}#L{line}",
84+
"Insecure": insecure,
85+
})))
86+
}

routers/routes/web.go

Lines changed: 2 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"encoding/gob"
99
"fmt"
1010
"net/http"
11-
"net/url"
1211
"os"
1312
"path"
1413
"strings"
@@ -22,7 +21,6 @@ import (
2221
"code.gitea.io/gitea/modules/setting"
2322
"code.gitea.io/gitea/modules/storage"
2423
"code.gitea.io/gitea/modules/templates"
25-
"code.gitea.io/gitea/modules/util"
2624
"code.gitea.io/gitea/modules/validation"
2725
"code.gitea.io/gitea/modules/web"
2826
"code.gitea.io/gitea/routers"
@@ -51,7 +49,6 @@ import (
5149
"github.com/go-chi/cors"
5250
"github.com/prometheus/client_golang/prometheus"
5351
"github.com/tstranex/u2f"
54-
"github.com/unknwon/com"
5552
)
5653

5754
const (
@@ -228,6 +225,7 @@ func WebRoutes() *web.Route {
228225
// TODO: These really seem like things that could be folded into Contexter or as helper functions
229226
common = append(common, user.GetNotificationCount)
230227
common = append(common, repo.GetActiveStopwatch)
228+
common = append(common, goGet)
231229

232230
others := web.NewRoute()
233231
for _, middle := range common {
@@ -239,67 +237,6 @@ func WebRoutes() *web.Route {
239237
return routes
240238
}
241239

242-
func goGet(ctx *context.Context) {
243-
if ctx.Query("go-get") != "1" {
244-
return
245-
}
246-
247-
// Quick responses appropriate go-get meta with status 200
248-
// regardless of if user have access to the repository,
249-
// or the repository does not exist at all.
250-
// This is particular a workaround for "go get" command which does not respect
251-
// .netrc file.
252-
253-
ownerName := ctx.Params(":username")
254-
repoName := ctx.Params(":reponame")
255-
trimmedRepoName := strings.TrimSuffix(repoName, ".git")
256-
257-
if ownerName == "" || trimmedRepoName == "" {
258-
_, _ = ctx.Write([]byte(`<!doctype html>
259-
<html>
260-
<body>
261-
invalid import path
262-
</body>
263-
</html>
264-
`))
265-
ctx.Status(400)
266-
return
267-
}
268-
branchName := setting.Repository.DefaultBranch
269-
270-
repo, err := models.GetRepositoryByOwnerAndName(ownerName, repoName)
271-
if err == nil && len(repo.DefaultBranch) > 0 {
272-
branchName = repo.DefaultBranch
273-
}
274-
prefix := setting.AppURL + path.Join(url.PathEscape(ownerName), url.PathEscape(repoName), "src", "branch", util.PathEscapeSegments(branchName))
275-
276-
appURL, _ := url.Parse(setting.AppURL)
277-
278-
insecure := ""
279-
if appURL.Scheme == string(setting.HTTP) {
280-
insecure = "--insecure "
281-
}
282-
ctx.Header().Set("Content-Type", "text/html")
283-
ctx.Status(http.StatusOK)
284-
_, _ = ctx.Write([]byte(com.Expand(`<!doctype html>
285-
<html>
286-
<head>
287-
<meta name="go-import" content="{GoGetImport} git {CloneLink}">
288-
<meta name="go-source" content="{GoGetImport} _ {GoDocDirectory} {GoDocFile}">
289-
</head>
290-
<body>
291-
go get {Insecure}{GoGetImport}
292-
</body>
293-
</html>
294-
`, map[string]string{
295-
"GoGetImport": context.ComposeGoGetImport(ownerName, trimmedRepoName),
296-
"CloneLink": models.ComposeHTTPSCloneURL(ownerName, repoName),
297-
"GoDocDirectory": prefix + "{/dir}",
298-
"GoDocFile": prefix + "{/dir}/{file}#L{line}",
299-
"Insecure": insecure,
300-
})))
301-
}
302-
303240
// RegisterRoutes register routes
304241
func RegisterRoutes(m *web.Route) {
305242
reqSignIn := context.Toggle(&context.ToggleOptions{SignInRequired: true})
@@ -1104,7 +1041,7 @@ func RegisterRoutes(m *web.Route) {
11041041
m.Group("/{username}", func() {
11051042
m.Group("/{reponame}", func() {
11061043
m.Get("", repo.SetEditorconfigIfExists, repo.Home)
1107-
}, goGet, ignSignIn, context.RepoAssignment, context.RepoRef(), context.UnitTypes())
1044+
}, ignSignIn, context.RepoAssignment, context.RepoRef(), context.UnitTypes())
11081045

11091046
m.Group("/{reponame}", func() {
11101047
m.Group("/info/lfs", func() {

0 commit comments

Comments
 (0)