cmd/coordinator: truncate strings more safely
These strings should always be set, so slicing them to 8 bytes should
be fine, but apparently that's not true and the status page is
crashing when rendering the template.
Change-Id: I11b47675ca272a2e6adc6bbc496fe715e4c8c5aa
Reviewed-on: https://go-review.googlesource.com/c/build/+/205604
Reviewed-by: Carlos Amedee <carlos@golang.org>
diff --git a/cmd/coordinator/coordinator.go b/cmd/coordinator/coordinator.go
index 637a75f..894a3ee 100644
--- a/cmd/coordinator/coordinator.go
+++ b/cmd/coordinator/coordinator.go
@@ -3518,6 +3518,13 @@
func (st *buildStatus) HTMLStatusLine() template.HTML { return st.htmlStatusLine(true) }
func (st *buildStatus) HTMLStatusLine_done() template.HTML { return st.htmlStatusLine(false) }
+func strSliceTo(s string, n int) string {
+ if len(s) <= n {
+ return s
+ }
+ return s[:n]
+}
+
func (st *buildStatus) htmlStatusLine(full bool) template.HTML {
if st == nil {
return "[nil]"
@@ -3527,17 +3534,27 @@
urlPrefix := "https://go-review.googlesource.com/#/q/"
+ if st.Rev == "" {
+ log.Printf("warning: st.Rev is empty")
+ }
+
var buf bytes.Buffer
fmt.Fprintf(&buf, "<a href='https://github.com/golang/go/wiki/DashboardBuilders'>%s</a> rev <a href='%s%s'>%s</a>",
- st.Name, urlPrefix, st.Rev, st.Rev[:8])
+ st.Name, urlPrefix, st.Rev, strSliceTo(st.Rev, 8))
if st.IsSubrepo() {
+ if st.SubRev == "" {
+ log.Printf("warning: st.SubRev is empty on subrepo")
+ }
fmt.Fprintf(&buf, " (sub-repo %s rev <a href='%s%s'>%s</a>)",
- st.SubName, urlPrefix, st.SubRev, st.SubRev[:8])
+ st.SubName, urlPrefix, st.SubRev, strSliceTo(st.SubRev, 8))
}
if ts := st.trySet; ts != nil {
+ if ts.ChangeID == "" {
+ log.Printf("warning: ts.ChangeID is empty")
+ }
fmt.Fprintf(&buf, " (<a href='/try?commit=%v'>trybot set</a> for <a href='https://go-review.googlesource.com/#/q/%s'>%s</a>)",
- ts.Commit[:8],
- ts.ChangeTriple(), ts.ChangeID[:8])
+ strSliceTo(ts.Commit, 8),
+ ts.ChangeTriple(), strSliceTo(ts.ChangeID, 8))
}
var state string