cros-publish: upload bug_id to RDB

Bug: b:322496630
Test: unit tests
Change-Id: I0496ee2768d0163983f9ead7d21f2056ef289824
Reviewed-on: https://chromium-review.googlesource.com/c/infra/infra/+/5475084
Commit-Queue: Zhihui Xie <zhihuixie@google.com>
Reviewed-by: Jason Kusuma <jkusuma@google.com>
Cr-Commit-Position: refs/heads/main@{#64832}
diff --git a/go/src/infra/cros/cmd/cros_test_runner/internal/commands/rdb_publish_upload_cmd.go b/go/src/infra/cros/cmd/cros_test_runner/internal/commands/rdb_publish_upload_cmd.go
index 5b06159..913497b 100644
--- a/go/src/infra/cros/cmd/cros_test_runner/internal/commands/rdb_publish_upload_cmd.go
+++ b/go/src/infra/cros/cmd/cros_test_runner/internal/commands/rdb_publish_upload_cmd.go
@@ -193,7 +193,11 @@
 	populateSecondaryExecutionInfo(ctx, testInv, sk, botDims, build)
 
 	// Scheduling metadata
-	populateSchedulingMetadata(ctx, testInv, build.GetTags())
+	bbTags := build.GetTags()
+	populateSchedulingMetadata(ctx, testInv, bbTags)
+
+	// Project tracker metadata
+	populateProjectTrackerMetadata(ctx, testInv, bbTags)
 }
 
 // getPrimaryDut get the primary Dut if exists. Otherwise, return nil.
@@ -532,6 +536,24 @@
 	}
 }
 
+// populateProjectTrackerMetadata populates project tracker metadata.
+func populateProjectTrackerMetadata(
+	ctx context.Context,
+	testInv *artifactpb.TestInvocation,
+	tags []*buildbucketpb.StringPair) {
+	projectTrackerMetadata := &artifactpb.ProjectTrackerMetadata{}
+	testInv.ProjectTrackerMetadata = projectTrackerMetadata
+
+	for _, tag := range tags {
+		if tag.GetKey() == "bug_id" {
+			projectTrackerMetadata.BugId = tag.GetValue()
+
+			// Remove the break if adding more fields whitin the loop.
+			break
+		}
+	}
+}
+
 // populateTestRunsInfo populates test runs info.
 func populateTestRunsInfo(
 	ctx context.Context,
diff --git a/go/src/infra/cros/cmd/cros_test_runner/internal/commands/rdb_publish_upload_cmd_test.go b/go/src/infra/cros/cmd/cros_test_runner/internal/commands/rdb_publish_upload_cmd_test.go
index d9bd937..104cf13 100644
--- a/go/src/infra/cros/cmd/cros_test_runner/internal/commands/rdb_publish_upload_cmd_test.go
+++ b/go/src/infra/cros/cmd/cros_test_runner/internal/commands/rdb_publish_upload_cmd_test.go
@@ -294,8 +294,12 @@
 						"qs_account":        "unmanaged_p2",
 						"parent_task_id":    "parentId1",
 						"branch-trigger":    "DEV",
+						"bug_id":            "1234",
 					},
 				},
+				ProjectTrackerMetadata: &artifactpb.ProjectTrackerMetadata{
+					BugId: "1234",
+				},
 			},
 			TestRuns: []*artifactpb.TestRun{
 				{
@@ -356,6 +360,7 @@
 				{Key: "qs_account", Value: "unmanaged_p2"},
 				{Key: "parent_task_id", Value: "parentId1"},
 				{Key: "branch-trigger", Value: "DEV"},
+				{Key: "bug_id", Value: "1234"},
 			},
 			CreateTime: createTime,
 			Infra: &bbpb.BuildInfra{Swarming: &bbpb.BuildInfra_Swarming{