maintner: add unused code for parsing Gerrit NoteDB info, sum label votes

Change-Id: I7208c1ed6cda42fb5ad2d13374bd34304c87644c
Reviewed-on: https://go-review.googlesource.com/51912
Reviewed-by: Kevin Burke <kev@inburke.com>
diff --git a/maintner/gerrit_test.go b/maintner/gerrit_test.go
index c44dbaa..65dffd2 100644
--- a/maintner/gerrit_test.go
+++ b/maintner/gerrit_test.go
@@ -86,10 +86,12 @@
 }
 
 var messageTests = []struct {
-	in  string
-	out string
+	in      string
+	want    string
+	wantNil bool
 }{
-	{`Update patch set 1
+	{
+		in: `Update patch set 1
 
 Patch Set 1: Code-Review+2
 
@@ -98,7 +100,65 @@
 Patch-set: 1
 Reviewer: Quentin Smith <13020@62eb7196-b449-3ce5-99f1-c037f21e1705>
 Label: Code-Review=+2
-`, "Patch Set 1: Code-Review+2\n\nJust to confirm, \"go test\" will consider an empty test file to be passing?"},
+`,
+		want: "Patch Set 1: Code-Review+2\n\nJust to confirm, \"go test\" will consider an empty test file to be passing?",
+	},
+	{
+		in: `Create change
+                
+Uploaded patch set 1: Run-TryBot+1.
+
+Patch-set: 1
+Change-id: I1e0035ffba986c3551479d5742809e43da5e7c73
+Subject: runtime: fall back to small mmaps if we fail to grow reservation
+Branch: refs/heads/master
+Status: new
+Topic:
+Commit: 8776f8d725c001456037e8888a72885d46cd6744
+Tag: autogenerated:gerrit:newPatchSet
+Groups: 8776f8d725c001456037e8888a72885d46cd6744
+Reviewer: Keith Randall <5200@62eb7196-b449-3ce5-99f1-c037f21e1705>
+Reviewer: Rick Hudson <5186@62eb7196-b449-3ce5-99f1-c037f21e1705>
+Reviewer: Austin Clements <5167@62eb7196-b449-3ce5-99f1-c037f21e1705>
+Label: Run-TryBot=+1    
+Private: false  
+Work-in-progress: false
+`,
+		want: "Uploaded patch set 1: Run-TryBot+1.",
+	},
+	{
+		in: `Uploaded patch set 1.
+        
+Patch-set: 1
+`,
+		wantNil: true,
+	},
+	{
+		in: `Create change
+    
+Uploaded patch set 1.
+    
+Patch-set: 1
+Change-id: I3799148a111f1ab6bfee24c9e03e6ebbf9e9595b
+Subject: net: make error messages consistent for invalid ports
+Branch: refs/heads/master
+Commit: 8a7de7048dc194d5e6f761add433b915beebb2e0
+Groups: 8a7de7048dc194d5e6f761add433b915beebb2e0
+`,
+		wantNil: true,
+	},
+	{
+		in: `Create patch set 7
+
+Uploaded patch set 7.: Commit message was updated 
+
+Patch-set: 7
+Subject: cmd/vet: -lostcancel: check for discarded result of context.WithCancel
+Commit: 5487cc78ea332c7b49d43ef5955211387aca73bb
+Groups: 5487cc78ea332c7b49d43ef5955211387aca73bb
+`,
+		wantNil: true,
+	},
 }
 
 func TestGetGerritMessage(t *testing.T) {
@@ -106,21 +166,32 @@
 	c.EnableLeaderMode(new(dummyMutationLogger), "/fake/dir")
 	c.TrackGerrit("go.googlesource.com/build")
 	gp := c.gerrit.projects["go.googlesource.com/build"]
-	for _, tt := range messageTests {
+	for i, tt := range messageTests {
 		gc := &GitCommit{
 			Msg:        tt.in,
 			CommitTime: time.Now().UTC(),
 		}
 		msg := gp.getGerritMessage(gc)
+		if msg == nil != tt.wantNil {
+			if tt.wantNil {
+				t.Errorf("%d. getGerritMessage returned item; want nil")
+			} else {
+				t.Errorf("%d. getGerritMessage = nil; want a message")
+			}
+			continue
+		}
+		if msg == nil {
+			continue
+		}
 		// just checking these get copied through appropriately
 		if msg.Version != 1 {
-			t.Errorf("getGerritMessage: want Version 1, got %d", msg.Version)
+			t.Errorf("%d. getGerritMessage: want Version 1, got %d", i, msg.Version)
 		}
 		if msg.Date.IsZero() {
-			t.Errorf("getGerritMessage: expected Date to be non-zero, got zero")
+			t.Errorf("%d. getGerritMessage: expected Date to be non-zero, got zero", i)
 		}
-		if msg.Message != tt.out {
-			t.Errorf("getGerritMessage: want %q, got %q", tt.out, msg.Message)
+		if msg.Message != tt.want {
+			t.Errorf("%d. getGerritMessage = %q; want %q", i, msg.Message, tt.want)
 		}
 	}
 }
@@ -168,33 +239,76 @@
 
 func TestLineValue(t *testing.T) {
 	tests := []struct {
-		all, prefix, want string
+		all, prefix, want, wantRest string
 	}{
 		{
-			all:    "foo:  value ",
-			prefix: "foo:",
-			want:   "value",
+			all:      "foo:  value ",
+			prefix:   "foo:",
+			want:     "value",
+			wantRest: "",
 		},
 		{
-			all:    "foo:  value\n",
-			prefix: "foo:",
-			want:   "value",
+			all:      "foo:  value\n",
+			prefix:   "foo:",
+			want:     "value",
+			wantRest: "",
 		},
 		{
-			all:    "bar: other\nfoo:  value\n",
-			prefix: "foo:",
-			want:   "value",
+			all:      "bar: other\nfoo:  value\n",
+			prefix:   "foo:",
+			want:     "value",
+			wantRest: "",
 		},
 		{
-			all:    "notfoo: other\nfoo:  value\n",
-			prefix: "foo:",
-			want:   "value",
+			all:      "notfoo: other\nfoo:  value\n",
+			prefix:   "foo:",
+			want:     "value",
+			wantRest: "",
+		},
+		{
+			all:      "Foo: bar\nLabel: Vote=+1\nLabel: Vote=+2\n",
+			prefix:   "Label: ",
+			want:     "Vote=+1",
+			wantRest: "Label: Vote=+2\n",
+		},
+		{
+			all:      "Label: Vote=+2\n",
+			prefix:   "Label: ",
+			want:     "Vote=+2",
+			wantRest: "",
 		},
 	}
 	for _, tt := range tests {
-		got := lineValue(tt.all, tt.prefix)
+		got, gotRest := lineValue(tt.all, tt.prefix)
 		if got != tt.want {
-			t.Errorf("lineValue(%q, %q) = %q; want %q", tt.all, tt.prefix, got, tt.want)
+			t.Errorf("lineValue(%q, %q) returned value %q; want %q", tt.all, tt.prefix, got, tt.want)
+		}
+		if gotRest != tt.wantRest {
+			t.Errorf("lineValue(%q, %q) returned rest %q; want %q", tt.all, tt.prefix, gotRest, tt.wantRest)
+		}
+	}
+}
+
+func TestParseGerritLabelValue(t *testing.T) {
+	tests := []struct {
+		in        string
+		wantLabel string
+		wantValue int8
+		wantWhose string
+	}{
+		{"Run-TryBot=+1", "Run-TryBot", 1, ""},
+		{"-Run-TryBot", "-Run-TryBot", 0, ""},
+		{"-TryBot-Result Gobot Gobot <5976@62eb7196-b449-3ce5-99f1-c037f21e1705>", "-TryBot-Result", 0, "5976@62eb7196-b449-3ce5-99f1-c037f21e1705"},
+		{"Run-TryBot=+1 Brad Fitzpatrick <5065@62eb7196-b449-3ce5-99f1-c037f21e1705>", "Run-TryBot", 1, "5065@62eb7196-b449-3ce5-99f1-c037f21e1705"},
+		{"TryBot-Result=-1 Gobot Gobot <5976@62eb7196-b449-3ce5-99f1-c037f21e1705>", "TryBot-Result", -1, "5976@62eb7196-b449-3ce5-99f1-c037f21e1705"},
+	}
+	for _, tt := range tests {
+		label, value, whose := parseGerritLabelValue(tt.in)
+		if label != tt.wantLabel || value != tt.wantValue || whose != tt.wantWhose {
+			t.Errorf("parseGerritLabelValue(%q) = %q, %v, %q; want %q, %v, %q",
+				tt.in,
+				label, value, whose,
+				tt.wantLabel, tt.wantValue, tt.wantWhose)
 		}
 	}
 }