Skip to content

Commit

Permalink
internal: create GetLatestMajorVersion function in DataSource
Browse files Browse the repository at this point in the history
This change creates a function in the DataSource interface that
retrieves the latest major version of a module path. This includes a
function in the proxydatasource package.

Updates golang/go#37765

Change-Id: I29505c1d7df51dd9bef8228e73bbedeea9771347
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/250797
Reviewed-by: Julie Qiu <julie@golang.org>
Run-TryBot: Julie Qiu <julie@golang.org>
  • Loading branch information
Miguel Acero committed Aug 28, 2020
1 parent ddade37 commit cd2ea52
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
2 changes: 2 additions & 0 deletions internal/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type DataSource interface {
GetLicenses(ctx context.Context, fullPath, modulePath, resolvedVersion string) ([]*licenses.License, error)
// GetPathInfo returns information about a path.
GetPathInfo(ctx context.Context, path, inModulePath, inVersion string) (outModulePath, outVersion string, isPackage bool, err error)
// GetLatestMajorVersion returns the latest major version of a module path.
GetLatestMajorVersion(ctx context.Context, seriesPath string) (_ string, err error)

// TODO(golang/go#39629): Deprecate these methods.
//
Expand Down
27 changes: 27 additions & 0 deletions internal/proxydatasource/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,30 @@ func (ds *DataSource) directoryFromVersion(ctx context.Context, fullPath, module
}
return nil, fmt.Errorf("%q missing from module %s: %w", fullPath, m.ModulePath, derrors.NotFound)
}

// GetLatestMajorVersion finds the latest major version of a modulePath that
// is found in the proxy by iterating through vN versions.
func (ds *DataSource) GetLatestMajorVersion(ctx context.Context, seriesPath string) (_ string, err error) {
// We are checking if the series path is valid so that we can forward the error if not.
_, err = ds.proxyClient.GetInfo(ctx, seriesPath, internal.LatestVersion)
if err != nil {
return "", err
}
const startVersion = 2
// We start checking versions from "/v2", since v1 and v0 versions don't
// have a major version at the end of the modulepath.
for v := startVersion; ; v++ {
query := fmt.Sprintf("%s/v%d", seriesPath, v)

_, err := ds.proxyClient.GetInfo(ctx, query, internal.LatestVersion)
if errors.Is(err, derrors.NotFound) {
if v == 2 {
return "", nil
}
return fmt.Sprintf("/v%d", v-1), nil
}
if err != nil {
return "", err
}
}
}
55 changes: 55 additions & 0 deletions internal/proxydatasource/datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,3 +432,58 @@ func TestDataSource_Bypass(t *testing.T) {
})
}
}

func TestDataSource_GetLatestMajorVersion(t *testing.T) {
t.Helper()
testModules := []*proxy.Module{
{
ModulePath: "foo.com/bar",
},
{
ModulePath: "foo.com/bar/v2",
},
{
ModulePath: "foo.com/bar/v3",
},
{
ModulePath: "bar.com/foo",
},
}
client, teardownProxy := proxy.SetupTestClient(t, testModules)
defer teardownProxy()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
ds := New(client)

for _, test := range []struct {
seriesPath string
wantVersion string
wantErr error
}{
{
seriesPath: "foo.com/bar",
wantVersion: "/v3",
},
{
seriesPath: "bar.com/foo",
wantVersion: "",
},
{
seriesPath: "boo.com/far",
wantErr: derrors.NotFound,
},
} {
gotVersion, err := ds.GetLatestMajorVersion(ctx, test.seriesPath)
if err != nil {
if test.wantErr == nil {
t.Fatalf("got unexpected error %v", err)
}
if !errors.Is(err, test.wantErr) {
t.Errorf("got err = %v, want Is(%v)", err, test.wantErr)
}
}
if gotVersion != test.wantVersion {
t.Errorf("GetLatestMajorVersion(%v) = %v, want %v", test.seriesPath, gotVersion, test.wantVersion)
}
}
}

0 comments on commit cd2ea52

Please sign in to comment.