maintner: fix github issue reference parsing, add tests

Change-Id: I5c23e17cb873dbb3ad1c7e5f319e87a5e053e26b
Reviewed-on: https://go-review.googlesource.com/41309
Reviewed-by: Kevin Burke <kev@inburke.com>
diff --git a/maintner/github.go b/maintner/github.go
index 5dd4f55..ad4b7e8 100644
--- a/maintner/github.go
+++ b/maintner/github.go
@@ -163,6 +163,8 @@
 	Number int32       // GitHubIssue.Number
 }
 
+func (r GitHubIssueRef) String() string { return fmt.Sprintf("%s#%d", r.Repo.ID(), r.Number) }
+
 // GitHubIssue represents a github issue.
 // This is maintner's in-memory representation. It differs slightly
 // from the API's *github.Issue type, notably in the lack of pointers
@@ -1949,7 +1951,7 @@
 	return gr
 }
 
-var rxReferences = regexp.MustCompile(`\b(?:([\w\-]+)/([\w\-]+))?\#(\d+)\b`)
+var rxReferences = regexp.MustCompile(`(?:\b([\w\-]+)/([\w\-]+))?\#(\d+)\b`)
 
 func (c *Corpus) parseGithubRefs(gerritProj string, commitMsg string) []GitHubIssueRef {
 	// Use of rxReferences by itself caused this function to take 20% of the CPU time.
diff --git a/maintner/github_test.go b/maintner/github_test.go
index c89754c..6684e03 100644
--- a/maintner/github_test.go
+++ b/maintner/github_test.go
@@ -560,3 +560,30 @@
 	}
 	return tp
 }
+
+func TestParseGithubRefs(t *testing.T) {
+	tests := []struct {
+		gerritProj string // "go.googlesource.com/go", etc
+		msg        string
+		want       []string
+	}{
+		{"go.googlesource.com/go", "\nFixes #1234\n", []string{"golang/go#1234"}},
+		{"go.googlesource.com/go", "Fixes #1234\n", []string{"golang/go#1234"}},
+		{"go.googlesource.com/go", "Fixes #1234", []string{"golang/go#1234"}},
+		{"go.googlesource.com/go", "Fixes golang/go#1234", []string{"golang/go#1234"}},
+		{"go.googlesource.com/go", "Fixes golang/go#1234\n", []string{"golang/go#1234"}},
+		{"go.googlesource.com/go", "Fixes golang/go#1234.", []string{"golang/go#1234"}},
+		{"go.googlesource.com/net", "Fixes golang/go#1234.", []string{"golang/go#1234"}},
+		{"go.googlesource.com/net", "Fixes #1234", nil},
+	}
+	for _, tt := range tests {
+		c := new(Corpus)
+		var got []string
+		for _, ref := range c.parseGithubRefs(tt.gerritProj, tt.msg) {
+			got = append(got, ref.String())
+		}
+		if !reflect.DeepEqual(got, tt.want) {
+			t.Errorf("parseGithubRefs(%q, %q) = %q; want %q", tt.gerritProj, tt.msg, got, tt.want)
+		}
+	}
+}