analysis: linkify labels when possible

Change-Id: I18ee1d1eab704c628dd476ee3aa7dc90e729106d
Reviewed-on: https://go-review.googlesource.com/43053
Run-TryBot: Quentin Smith <quentin@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
diff --git a/analysis/app/compare.go b/analysis/app/compare.go
index 37d09d7..497df49 100644
--- a/analysis/app/compare.go
+++ b/analysis/app/compare.go
@@ -133,6 +133,25 @@
 	return add + " | " + query
 }
 
+// linkify returns a link related to the label's value. If no such link exists, it returns an empty string.
+// For example, "cl: 1234" is linked to golang.org/cl/1234.
+// string is used as the return type and not template.URL so that html/template will validate the scheme.
+func linkify(labels benchfmt.Labels, label string) string {
+	switch label {
+	case "cl", "commit":
+		return "https://golang.org/cl/" + template.URLQueryEscaper(labels[label])
+	case "ps":
+		// TODO(quentin): Figure out how to link to a particular patch set on Gerrit.
+		return ""
+	case "repo":
+		return labels["repo"]
+	case "try":
+		// TODO(quentin): Return link to farmer once farmer has permalinks.
+		return ""
+	}
+	return ""
+}
+
 // compare handles queries that require comparison of the groups in the query.
 func (a *App) compare(w http.ResponseWriter, r *http.Request) {
 	ctx := requestContext(r)
@@ -152,6 +171,7 @@
 
 	t, err := template.New("main").Funcs(template.FuncMap{
 		"addToQuery": addToQuery,
+		"linkify":    linkify,
 	}).Parse(string(tmpl))
 	if err != nil {
 		http.Error(w, err.Error(), 500)
diff --git a/analysis/appengine/template/compare.html b/analysis/appengine/template/compare.html
index 6ba7c28..846bae7 100644
--- a/analysis/appengine/template/compare.html
+++ b/analysis/appengine/template/compare.html
@@ -103,14 +103,14 @@
         <p>{{.}}</p>
         {{else}}
           <table id="labels">
-            {{with .CommonLabels}}
+            {{with $cl := .CommonLabels}}
               <tbody>
                 <tr>
                   <th>label</th><th>common value</th>
                 </tr>
                 {{range $label, $value := .}}
                   <tr>
-                    <th class="label">{{$label}}</th><td>{{$value}}</td>
+                    <th class="label">{{$label}}</th><td>{{with $href := linkify $cl $label}}<a href="{{$href}}" rel="nofollow">{{$value}}</a>{{else}}{{$value}}{{end}}</td>
                   </tr>
                 {{end}}
               </tbody>