dmitri.shuralyov.com/website/gido/...

Correctly map import path to its issue title prefix.

Previously, the import path was used directly as an issue title prefix.
This works for most import paths, but not for golang.org/x/... ones.
We need to map those to x/... prefixes.

Also simplify code in ParsePrefixedTitle.
dmitshur committed 1 year ago commit f560975cbd839c686d89160b5653737171c439a6
main.go
@@ -395,15 +395,14 @@ func (h *handler) ServeIssues(w http.ResponseWriter, req *http.Request, pkg stri
 	heading := htmlg.NodeComponent{
 		Type: html.ElementNode, Data: atom.H2.String(),
 		Attr:       []html.Attribute{{Key: atom.Style.String(), Val: "margin-top: 30px;"}},
 		FirstChild: htmlg.Text(pkg),
 	}
-	title := pkg + ": "
 	if pkg == otherPackages {
 		heading.Data, heading.FirstChild = atom.H3.String(), htmlg.Text("Other Go Issues")
-		title = ""
 	}
+	title := ImportPathToFullPrefix(pkg)
 	newIssue := htmlg.NodeComponent{
 		Type: html.ElementNode, Data: atom.Div.String(),
 		Attr:       []html.Attribute{{Key: atom.Style.String(), Val: "text-align: right;"}},
 		FirstChild: htmlg.A("New Issue", "https://golang.org/issue/new?title="+url.QueryEscape(title)),
 	}
service.go
@@ -244,16 +244,32 @@ func ParsePrefixedTitle(prefixedTitle string) (paths []string, title string) {
 	}
 	paths = strings.Split(prefix, ",")
 	for i := range paths {
 		paths[i] = strings.TrimSpace(paths[i])
 		if strings.HasPrefix(paths[i], "x/") { // Map "x/..." to "golang.org/x/...".
-			paths[i] = "golang.org/x/" + paths[i][len("x/"):]
+			paths[i] = "golang.org/" + paths[i]
 		}
 	}
 	return paths, title
 }
 
+// ImportPathToFullPrefix returns the an issue title prefix (including ": ") for the given import path.
+// If path equals to otherPackages, an empty prefix is returned.
+func ImportPathToFullPrefix(path string) string {
+	switch {
+	default:
+		// Use all other import paths directly as prefix.
+		return path + ": "
+	case strings.HasPrefix(path, "golang.org/x/") || path == "golang.org/x":
+		// Map "golang.org/x/..." to "x/...".
+		return path[len("golang.org/"):] + ": "
+	case path == otherPackages:
+		// Empty prefix for otherPackages.
+		return ""
+	}
+}
+
 // ghState converts a GitHub issue state into a issues.State.
 func ghState(issue *maintner.GitHubIssue) issues.State {
 	switch issue.Closed {
 	case false:
 		return issues.OpenState
service_test.go
@@ -46,5 +46,35 @@ func TestParsePrefixedTitle(t *testing.T) {
 		if gotTitle != tc.wantTitle {
 			t.Errorf("got title: %q, want: %q", gotTitle, tc.wantTitle)
 		}
 	}
 }
+
+func TestImportPathToFullPrefix(t *testing.T) {
+	tests := []struct {
+		in   string
+		want string
+	}{
+		{
+			in:   "net/http",
+			want: "net/http: ",
+		},
+		{
+			in:   "golang.org/x/build/cmd/releasebot",
+			want: "x/build/cmd/releasebot: ",
+		},
+		{
+			in:   "golang.org/x",
+			want: "x: ",
+		},
+		{
+			in:   "other",
+			want: "",
+		},
+	}
+	for _, tc := range tests {
+		got := gido.ImportPathToFullPrefix(tc.in)
+		if got != tc.want {
+			t.Errorf("got prefix: %q, want: %q", got, tc.want)
+		}
+	}
+}