internal/fetch: put ModuleInfo into each unit

Install the full ModuleInfo from the parent module into each
unit. Previously this wasn't necessary in fetch because
Unit.ModuleInfo was populated when reading from the DB. But it's
necessary now that we can serve directly from a fetched module.

For golang/go#47982

Change-Id: I0b0462148f7623ccbbbe7d4ca3bf2409e2107023
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/347931
Trust: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Julie Qiu <julie@golang.org>
diff --git a/internal/fetch/fetch.go b/internal/fetch/fetch.go
index c8e7483..60158a7 100644
--- a/internal/fetch/fetch.go
+++ b/internal/fetch/fetch.go
@@ -213,17 +213,18 @@
 	if err != nil {
 		return nil, nil, err
 	}
+	minfo := internal.ModuleInfo{
+		ModulePath:        modulePath,
+		Version:           resolvedVersion,
+		CommitTime:        commitTime,
+		IsRedistributable: d.ModuleIsRedistributable(),
+		SourceInfo:        sourceInfo,
+		// HasGoMod is populated by the caller.
+	}
 	return &internal.Module{
-		ModuleInfo: internal.ModuleInfo{
-			ModulePath:        modulePath,
-			Version:           resolvedVersion,
-			CommitTime:        commitTime,
-			IsRedistributable: d.ModuleIsRedistributable(),
-			SourceInfo:        sourceInfo,
-			// HasGoMod is populated by the caller.
-		},
-		Licenses: allLicenses,
-		Units:    moduleUnits(modulePath, resolvedVersion, packages, readmes, d),
+		ModuleInfo: minfo,
+		Licenses:   allLicenses,
+		Units:      moduleUnits(modulePath, minfo, packages, readmes, d),
 	}, packageVersionStates, nil
 }
 
diff --git a/internal/fetch/fetch_test.go b/internal/fetch/fetch_test.go
index cb6c5a1..a5a4b3d 100644
--- a/internal/fetch/fetch_test.go
+++ b/internal/fetch/fetch_test.go
@@ -135,11 +135,10 @@
 						opts = append(opts,
 							[]cmp.Option{
 								// Pre specified for all modules
-								cmpopts.IgnoreFields(internal.Module{}, "SourceInfo"),
+								cmpopts.IgnoreFields(internal.ModuleInfo{}, "SourceInfo", "CommitTime"),
 								cmpopts.IgnoreFields(internal.Module{}, "Version"),
 								cmpopts.IgnoreFields(FetchResult{}, "RequestedVersion"),
 								cmpopts.IgnoreFields(FetchResult{}, "ResolvedVersion"),
-								cmpopts.IgnoreFields(internal.Module{}, "CommitTime"),
 							}...)
 					}
 					opts = append(opts, sample.LicenseCmpOpts...)
diff --git a/internal/fetch/helper_test.go b/internal/fetch/helper_test.go
index 5eb0a39..ccb7e5d 100644
--- a/internal/fetch/helper_test.go
+++ b/internal/fetch/helper_test.go
@@ -119,7 +119,10 @@
 		}
 	} else {
 		for _, u := range fr.Module.Units {
-			u.UnitMeta.Version = fr.Module.Version
+			// Copy all of ModuleInfo except HasGoMod.
+			h := u.UnitMeta.ModuleInfo.HasGoMod
+			u.UnitMeta.ModuleInfo = fr.Module.ModuleInfo
+			u.UnitMeta.HasGoMod = h
 		}
 		for _, pvs := range fr.PackageVersionStates {
 			pvs.Version = fr.Module.Version
diff --git a/internal/fetch/unit.go b/internal/fetch/unit.go
index 29de7c1..a6da5d4 100644
--- a/internal/fetch/unit.go
+++ b/internal/fetch/unit.go
@@ -15,7 +15,7 @@
 
 // moduleUnits returns all of the units in a given module, along
 // with the contents for those units.
-func moduleUnits(modulePath, version string,
+func moduleUnits(modulePath string, minfo internal.ModuleInfo,
 	pkgs []*goPackage,
 	readmes []*internal.Readme,
 	d *licenses.Detector) []*internal.Unit {
@@ -49,11 +49,7 @@
 		}
 		dir := &internal.Unit{
 			UnitMeta: internal.UnitMeta{
-				ModuleInfo: internal.ModuleInfo{
-					ModulePath:        modulePath,
-					Version:           version,
-					IsRedistributable: d.ModuleIsRedistributable(),
-				},
+				ModuleInfo:        minfo,
 				Path:              dirPath,
 				IsRedistributable: isRedist,
 				Licenses:          meta,