Skip to content

Commit 91e83ca

Browse files
committed
internal: add support for path@main
Support requests for path@main in addition to path@master. For golang/go#41312 Change-Id: Ie7665fbb3906e366c784a9cc592ffe37ef347671 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/274244 Trust: Julie Qiu <[email protected]> Reviewed-by: Jonathan Amsterdam <[email protected]>
1 parent 360dfb4 commit 91e83ca

File tree

8 files changed

+32
-21
lines changed

8 files changed

+32
-21
lines changed

internal/discovery.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,19 @@ const (
2020
// proxy client.
2121
LatestVersion = "latest"
2222

23-
// MasterVersion signifies the version at master.
24-
MasterVersion = "master"
25-
2623
// UnknownModulePath signifies that the module path for a given package
2724
// path is ambiguous or not known. This is because requests to the
2825
// frontend can come in the form of <import-path>[@<version>], and it is
2926
// not clear which part of the import-path is the module path.
3027
UnknownModulePath = "unknownModulePath"
3128
)
3229

30+
// DefaultBranches are default branches that are supported by pkgsite.
31+
var DefaultBranches = map[string]bool{
32+
"main": true,
33+
"master": true,
34+
}
35+
3336
// ModuleInfo holds metadata associated with a module.
3437
type ModuleInfo struct {
3538
ModulePath string

internal/frontend/fetch_test.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ func TestFetch(t *testing.T) {
4444
{
4545
name: "path at master package is in module root",
4646
fullPath: testModulePath,
47-
version: internal.MasterVersion,
47+
version: "master",
48+
},
49+
{
50+
name: "path at main package is in module root",
51+
fullPath: testModulePath,
52+
version: "main",
4853
},
4954
{
5055
name: "path at latest package is in module root",
@@ -64,7 +69,12 @@ func TestFetch(t *testing.T) {
6469
{
6570
name: "directory at master package is not in module root",
6671
fullPath: testModulePath + "/bar",
67-
version: internal.MasterVersion,
72+
version: "master",
73+
},
74+
{
75+
name: "directory at main package is not in module root",
76+
fullPath: testModulePath + "/bar",
77+
version: "main",
6878
},
6979
} {
7080
t.Run(test.name, func(t *testing.T) {

internal/frontend/unit.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (s *Server) serveUnitPage(ctx context.Context, w http.ResponseWriter, r *ht
9595
}
9696

9797
recordVersionTypeMetric(ctx, info.requestedVersion)
98-
if info.requestedVersion == internal.MasterVersion {
98+
if _, ok := internal.DefaultBranches[info.requestedVersion]; ok {
9999
// Since path@master is a moving target, we don't want it to be stale.
100100
// As a result, we enqueue every request of path@master to the frontend
101101
// task queue, which will initiate a fetch request depending on the
@@ -106,7 +106,7 @@ func (s *Server) serveUnitPage(ctx context.Context, w http.ResponseWriter, r *ht
106106
go func() {
107107
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
108108
defer cancel()
109-
if _, err := s.queue.ScheduleFetch(ctx, info.modulePath, internal.MasterVersion, ""); err != nil {
109+
if _, err := s.queue.ScheduleFetch(ctx, info.modulePath, info.requestedVersion, ""); err != nil {
110110
log.Errorf(ctx, "serveDetails(%q): %v", r.URL.Path, err)
111111
}
112112
}()

internal/frontend/urlinfo.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,10 @@ func checkExcluded(ctx context.Context, ds internal.DataSource, fullPath string)
170170

171171
// isSupportedVersion reports whether the version is supported by the frontend.
172172
func isSupportedVersion(fullPath, requestedVersion string) bool {
173-
if stdlib.Contains(fullPath) && requestedVersion == internal.MasterVersion {
174-
return false
175-
}
176-
if requestedVersion == internal.LatestVersion || semver.IsValid(requestedVersion) {
177-
return true
173+
if _, ok := internal.DefaultBranches[requestedVersion]; ok {
174+
return !stdlib.Contains(fullPath)
178175
}
179-
return requestedVersion == internal.MasterVersion
176+
return requestedVersion == internal.LatestVersion || semver.IsValid(requestedVersion)
180177
}
181178

182179
func setExperimentsFromQueryParam(ctx context.Context, r *http.Request) context.Context {

internal/frontend/urlinfo_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,12 @@ func TestIsSupportedVersion(t *testing.T) {
188188
{sample.ModulePath, "v1.2.bad", false},
189189
{sample.ModulePath, "latest", true},
190190
{sample.ModulePath, "master", true},
191+
{sample.ModulePath, "main", true},
191192
{"net/http", "v1.2.3", true}, // isSupportedVersion expects the goTag is already converted to semver
192193
{"net/http", "v1.2.3.bad", false},
193194
{"net/http", "latest", true},
194195
{"net/http", "master", false},
196+
{"net/http", "main", false},
195197
}
196198
for _, test := range tests {
197199
got := isSupportedVersion(test.path, test.version)

internal/postgres/path_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func TestGetUnitMeta(t *testing.T) {
4343
}
4444
requested := m.Version
4545
if testModule.isMaster {
46-
requested = internal.MasterVersion
46+
requested = "master"
4747
}
4848
if err := testDB.UpsertVersionMap(ctx, &internal.VersionMap{
4949
ModulePath: m.ModulePath,
@@ -224,7 +224,7 @@ func TestGetUnitMetaBypass(t *testing.T) {
224224
}
225225
requested := m.Version
226226
if testModule.isMaster {
227-
requested = internal.MasterVersion
227+
requested = "master"
228228
}
229229
if err := bypassDB.UpsertVersionMap(ctx, &internal.VersionMap{
230230
ModulePath: m.ModulePath,

internal/postgres/unit.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,9 @@ func (db *DB) GetUnitMeta(ctx context.Context, fullPath, requestedModulePath, re
4747
if requestedModulePath != internal.UnknownModulePath {
4848
query = query.Where(squirrel.Eq{"m.module_path": requestedModulePath})
4949
}
50-
switch requestedVersion {
51-
case internal.LatestVersion:
52-
case internal.MasterVersion:
53-
query = query.Join("version_map vm ON m.id = vm.module_id").Where("vm.requested_version = 'master'")
54-
default:
50+
if _, ok := internal.DefaultBranches[requestedVersion]; ok {
51+
query = query.Join("version_map vm ON m.id = vm.module_id").Where("vm.requested_version = ? ", requestedVersion)
52+
} else if requestedVersion != internal.LatestVersion {
5553
query = query.Where(squirrel.Eq{"version": requestedVersion})
5654
}
5755
var (

internal/proxy/server.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,9 @@ func (s *Server) AddModule(m *Module) {
118118
s.handleList(m.ModulePath)
119119
s.handleLatest(m.ModulePath, fmt.Sprintf("/%s/@latest", m.ModulePath))
120120
// TODO(https://golang.org/issue/39985): Add endpoint for handling
121-
// master version.
121+
// master and main versions.
122122
s.handleLatest(m.ModulePath, fmt.Sprintf("/%s/@v/master.info", m.ModulePath))
123+
s.handleLatest(m.ModulePath, fmt.Sprintf("/%s/@v/main.info", m.ModulePath))
123124
}
124125
s.handleInfo(m.ModulePath, m.Version)
125126
s.handleMod(m)

0 commit comments

Comments
 (0)