cmd/coordinator: support running TryBots on x/ repo branches

This change fixes a bug that was preventing TryBots from running on
branches of the x/ repositories, other than "master" and
"release-branch.go1.N". For now, we only run TryBots with Go at tip.

Fixes golang/go#37512

Change-Id: I99f11f570252d5612ef11ecaa3335c4478cda730
Reviewed-on: https://go-review.googlesource.com/c/build/+/227397
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
diff --git a/cmd/coordinator/coordinator.go b/cmd/coordinator/coordinator.go
index 357f36a..709337f 100644
--- a/cmd/coordinator/coordinator.go
+++ b/cmd/coordinator/coordinator.go
@@ -1164,6 +1164,7 @@
 // Must hold statusMu.
 func newTrySet(work *apipb.GerritTryWorkItem) *trySet {
 	key := tryWorkItemKey(work)
+	// TODO(golang.org/issue/38303): compute goBranch value better
 	goBranch := key.Branch // assume same as repo's branch for now
 
 	builders := dashboard.TryBuildersForProject(key.Project, key.Branch, goBranch)
@@ -1203,6 +1204,10 @@
 	// do subrepos. Any GoCommit values past the first are for older
 	// release branches, but we use a limited subset of builders for those.
 	var goRev string
+	if len(work.GoCommit) > 0 {
+		// By default, use the first GoCommit, which represents Go tip (master branch).
+		goRev = work.GoCommit[0]
+	}
 	for i, branch := range work.GoBranch {
 		if branch == work.Branch {
 			goRev = work.GoCommit[i]
diff --git a/cmd/coordinator/coordinator_test.go b/cmd/coordinator/coordinator_test.go
index 4d725df..f7eff92 100644
--- a/cmd/coordinator/coordinator_test.go
+++ b/cmd/coordinator/coordinator_test.go
@@ -202,6 +202,30 @@
 	}
 }
 
+// Tests that TryBots run on branches of the x/ repositories, other than
+// "master" and "release-branch.go1.N". See golang.org/issue/37512.
+func TestXRepoBranches(t *testing.T) {
+	testingKnobSkipBuilds = true
+
+	work := &apipb.GerritTryWorkItem{
+		Project:   "tools",
+		Branch:    "gopls-release-branch.0.4",
+		ChangeId:  "Ica799fcf117bf607c0c59f41b08a78552339dc53",
+		Commit:    "6af4ce83c61d0f3e616b410b53b51982798c4d73",
+		GoVersion: []*apipb.MajorMinor{{1, 15}},
+		GoCommit:  []string{"74d6de03fd7db2c6faa7794620a9bcf0c4f018f2"},
+		GoBranch:  []string{"master"},
+	}
+	ts := newTrySet(work)
+	for i, bs := range ts.builds {
+		v := bs.NameAndBranch()
+		t.Logf("build[%d]: %s", i, v)
+	}
+	if len(ts.builds) < 3 {
+		t.Fatalf("expected at least 3 builders, got %v", len(ts.builds))
+	}
+}
+
 func TestFindWork(t *testing.T) {
 	if testing.Short() {
 		t.Skip("skipping in short mode")