зеркало из https://github.com/golang/pkgsite.git
internal/{postgres,frontend}: support modules with zero commit times
It's valid to have a module with a zero commit time (as reported by the proxy .info endpoint). That can happen if the module is served from a vanity URL with a go-import meta tag of type `mod`. Process those modules and display them with a publish date of "unknown." Fixes golang/go#48952 Change-Id: I8efdebfa27c8b6ed68c183bac77d87ee715f8262 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/356129 Trust: Jonathan Amsterdam <jba@google.com> Run-TryBot: Jonathan Amsterdam <jba@google.com> TryBot-Result: kokoro <noreply+kokoro@google.com> Reviewed-by: Jamal Carvalho <jamal@golang.org>
This commit is contained in:
Родитель
d5bd14bda0
Коммит
76f929b8d0
|
@ -97,8 +97,11 @@ func effectiveName(pkgPath, pkgName string) string {
|
|||
}
|
||||
|
||||
// absoluteTime takes a date and returns returns a human-readable,
|
||||
// date with the format mmm d, yyyy:
|
||||
// date with the format mmm d, yyyy.
|
||||
func absoluteTime(date time.Time) string {
|
||||
if date.IsZero() {
|
||||
return "unknown"
|
||||
}
|
||||
// Convert to UTC because that is how the date is represented in the DB.
|
||||
// (The pgx driver returns local times.) Example: if a date is stored
|
||||
// as Jan 30 at midnight, then the local NYC time is on Jan 29, and this
|
||||
|
|
|
@ -103,6 +103,11 @@ func TestAbsoluteTime(t *testing.T) {
|
|||
date: now.Add(time.Hour * 24 * -5),
|
||||
absoluteTime: now.Add(time.Hour * 24 * -5).Format("Jan _2, 2006"),
|
||||
},
|
||||
{
|
||||
name: "zero time",
|
||||
date: time.Time{},
|
||||
absoluteTime: "unknown",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
|
|
|
@ -341,7 +341,7 @@ func TestNewSearchResult(t *testing.T) {
|
|||
t.Run(test.name, func(t *testing.T) {
|
||||
pr := message.NewPrinter(test.tag)
|
||||
got := newSearchResult(&test.in, false, pr)
|
||||
test.want.CommitTime = "Jan 1, 0001"
|
||||
test.want.CommitTime = "unknown"
|
||||
if diff := cmp.Diff(&test.want, got); diff != "" {
|
||||
t.Errorf("mimatch (-want, +got):\n%s", diff)
|
||||
}
|
||||
|
@ -425,6 +425,11 @@ func TestElapsedTime(t *testing.T) {
|
|||
date: now.Add(time.Hour * 24 * -14),
|
||||
elapsedTime: now.Add(time.Hour * 24 * -14).Format("Jan _2, 2006"),
|
||||
},
|
||||
{
|
||||
name: "zero",
|
||||
date: time.Time{},
|
||||
elapsedTime: "unknown",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
|
|
|
@ -180,9 +180,13 @@ func buildVersionDetails(ctx context.Context, currentModulePath string,
|
|||
Major: major,
|
||||
Incompatible: version.IsIncompatible(mi.Version),
|
||||
}
|
||||
commitTime := "date unknown"
|
||||
if !mi.CommitTime.IsZero() {
|
||||
commitTime = absoluteTime(mi.CommitTime)
|
||||
}
|
||||
vs := &VersionSummary{
|
||||
Link: linkify(mi),
|
||||
CommitTime: absoluteTime(mi.CommitTime),
|
||||
CommitTime: commitTime,
|
||||
Version: linkVersion(mi.ModulePath, mi.Version, mi.Version),
|
||||
IsMinor: isMinor(mi.Version),
|
||||
Retracted: mi.Retracted,
|
||||
|
|
|
@ -44,10 +44,6 @@ func (db *DB) InsertModule(ctx context.Context, m *internal.Module, lmv *interna
|
|||
if err := validateModule(m); err != nil {
|
||||
return false, err
|
||||
}
|
||||
// The proxy accepts modules with zero commit times, but they are bad.
|
||||
if m.CommitTime.IsZero() {
|
||||
return false, fmt.Errorf("empty commit time: %w", derrors.BadModule)
|
||||
}
|
||||
// Compare existing data from the database, and the module to be
|
||||
// inserted. Rows that currently exist should not be missing from the
|
||||
// new module. We want to be sure that we will overwrite every row that
|
||||
|
|
|
@ -77,6 +77,14 @@ func TestInsertModule(t *testing.T) {
|
|||
}(),
|
||||
goMod: "module " + sample.ModulePath + " // Deprecated: use v2",
|
||||
},
|
||||
{
|
||||
name: "zero commit time",
|
||||
module: func() *internal.Module {
|
||||
v := sample.DefaultModule()
|
||||
v.CommitTime = time.Time{}
|
||||
return v
|
||||
}(),
|
||||
},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
testDB, release := acquire(t)
|
||||
|
@ -259,17 +267,6 @@ func TestInsertModuleErrors(t *testing.T) {
|
|||
wantModulePath: sample.ModulePath,
|
||||
wantWriteErr: derrors.DBModuleInsertInvalid,
|
||||
},
|
||||
{
|
||||
name: "empty commit time",
|
||||
module: func() *internal.Module {
|
||||
v := sample.DefaultModule()
|
||||
v.CommitTime = time.Time{}
|
||||
return v
|
||||
}(),
|
||||
wantVersion: sample.VersionString,
|
||||
wantModulePath: sample.ModulePath,
|
||||
wantWriteErr: derrors.BadModule,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
|
|
Загрузка…
Ссылка в новой задаче