internal/frontend: drop docs after all/all

If there is more than one Documentation in a unit but the first is
all/all, ignore the others.

Also, consolidate doc mods into one function.

Change-Id: I9ec7551d39a4e68e8a4a1483f928d288d77e1e55
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/305989
Trust: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Julie Qiu <julie@golang.org>
diff --git a/internal/frontend/main.go b/internal/frontend/main.go
index 8fca419..dd10b8f 100644
--- a/internal/frontend/main.go
+++ b/internal/frontend/main.go
@@ -135,6 +135,8 @@
 		buildContexts      []internal.BuildContext
 	)
 
+	unit.Documentation = cleanDocumentation(unit.Documentation)
+
 	doc := internal.DocumentationForBuildContext(unit.Documentation, bc)
 	versionToNameToUnitSymbol := map[string]map[string]*internal.UnitSymbol{}
 	if experiment.IsActive(ctx, internal.ExperimentSymbolHistoryMainPage) {
@@ -158,26 +160,9 @@
 		synopsis = doc.Synopsis
 		goos = doc.GOOS
 		goarch = doc.GOARCH
-		// If there is only one Documentation and it is linux/amd64, then
-		// make it all/all.
-		//
-		// This is temporary, until the next reprocessing. It assumes a unit
-		// with a single linux/amd64 actually has only one build context,
-		// and hasn't been reprocessed to have all/all.
-		//
-		// The only effect of this is to prevent "GOOS=linux, GOARCH=amd64" from
-		// appearing at the bottom of the doc. That is wrong in the (rather
-		// unlikely) case that the package truly only has doc for linux/amd64,
-		// but the bug is only cosmetic.
-		if len(unit.Documentation) == 1 && goos == "linux" && goarch == "amd64" {
-			goos = internal.All
-			goarch = internal.All
-		} else {
-			for _, v := range unit.Documentation {
-				buildContexts = append(buildContexts, v.BuildContext())
-			}
+		for _, v := range unit.Documentation {
+			buildContexts = append(buildContexts, v.BuildContext())
 		}
-
 		end := middleware.ElapsedStat(ctx, "DecodePackage")
 		docPkg, err := godoc.DecodePackage(doc.Source)
 		end()
@@ -255,6 +240,30 @@
 	}, nil
 }
 
+func cleanDocumentation(docs []*internal.Documentation) []*internal.Documentation {
+	// If there is more than one row but the first is all/all, ignore the others.
+	// Should never happen;  temporary fix until the DB is cleaned up.
+	if len(docs) > 1 && docs[0].BuildContext() == internal.BuildContextAll {
+		return docs[:1]
+	}
+	// If there is only one Documentation and it is linux/amd64, then
+	// make it all/all.
+	//
+	// This is temporary, until the next reprocessing. It assumes a unit
+	// with a single linux/amd64 actually has only one build context,
+	// and hasn't been reprocessed to have all/all.
+	//
+	// The only effect of this is to prevent "GOOS=linux, GOARCH=amd64" from
+	// appearing at the bottom of the doc. That is wrong in the (rather
+	// unlikely) case that the package truly only has doc for linux/amd64,
+	// but the bug is only cosmetic.
+	if len(docs) == 1 && docs[0].GOOS == "linux" && docs[0].GOARCH == "amd64" {
+		docs[0].GOOS = internal.All
+		docs[0].GOARCH = internal.All
+	}
+	return docs
+}
+
 // readmeContent renders the readme to html and collects the headings
 // into an outline.
 func readmeContent(ctx context.Context, u *internal.Unit) (_ *Readme, err error) {