syntax = "proto3";

import "proto/context.proto";
import "proto/api/v1/common.proto";

package target;

// A single "Target" that is part of a build.
message Target {
  // The id of this target.
  // For example: "TS12345".
  string id = 1;

  // The label of the target.
  // For example: "//server/test:foo"
  string label = 2;

  // The type of the target rule.
  // For example: java_binary
  string rule_type = 3;

  // The (enum) type of this target.
  // For example: APPLICATION, BINARY, TEST.
  api.v1.TargetType target_type = 4;

  // The (enum) size of this target.
  // For example: SMALL, MEDIUM, ENORMOUS.
  api.v1.TestSize test_size = 5;
}

message TargetStatus {
  // The invocation identifier itself.
  string invocation_id = 1;

  // The commit SHA that this invocation was for.
  // For example: "e6a712c7c15b87ea772e13468fdbf78ecf3ed43d"
  string commit_sha = 2;

  // The aggregate status of the target. Targets can be run multiple times by
  // bazel which computes an "aggregate" enum status, like PASSED, FAILED, or
  // FLAKY.
  api.v1.Status status = 3;

  // When this target started and its duration.
  // Note: The target's start time is when the test is run and it's different
  // from invocation_created_at_usec when the target is cached.
  api.v1.Timing timing = 4;

  // When the invocation was created.
  int64 invocation_created_at_usec = 5;
}

message TargetHistory {
  // The target that was run.
  Target target = 1;

  // The git repo the build was for.
  // For example: "buildbuddy-io/buildbuddy"
  string repo_url = 2;

  // A list of target statuses run across a range of invocations / commits.
  // If multiple targets were run at the same commit, the latest run will be
  // returned.
  repeated TargetStatus target_status = 3;
}

// NB: TargetQuery params apply to both invocations and their child targets. For
// example, filtering to role: "CI" and target_type: TEST will only return
// invocations that were run via CI and within each of those only targets of
// type TEST.
message TargetQuery {
  // The search parameters in this query will be ANDed when performing a
  // query -- so if a client specifies both "user" and "host", all results
  // returned must match both fields.

  // The unix-user who performed the build.
  string user = 1;

  // The host this build was executed on.
  string host = 2;

  // The git repo the build was for.
  string repo_url = 3;

  // The commit sha used for the build.
  string commit_sha = 4;

  // The role played by the build. Ex: "CI"
  string role = 5;

  // The type of target to return.
  // For example: TEST.
  api.v1.TargetType target_type = 6;

  // The git branch the build was for.
  string branch_name = 7;
}

message GetTargetRequest {
  // The request context.
  context.RequestContext request_context = 1;

  // The filters to apply to this query. Required.
  // When server_side_pagination = true, only repo_url takes effect.
  TargetQuery query = 2;

  // Return records that were run *after* this timestamp.
  // Deprecated when server_side_pagination = true.
  int64 start_time_usec = 3;

  // Return records that were run *before* this timestamp.
  // Deprecated when server_side_pagination = true.
  int64 end_time_usec = 4;

  // This boolean is used to roll out server side pagination.
  bool server_side_pagination = 5;

  // The pagination token. If unset, the server returns the first page of
  // the result.
  string page_token = 6;
}

message GetTargetResponse {
  // The response context.
  context.ResponseContext response_context = 1;

  // The targets and statuses that matched the query, ordered by
  // the time they were executed, descending.
  repeated TargetHistory invocation_targets = 2;

  // Indicates if the server had to truncate results because of size. If true,
  // the client should fetch additional time ranges working backwards from the
  // oldest timestamp returned.
  bool truncated_results = 3;

  // The pagination token to retrieve the next page of results.
  string next_page_token = 4;
}
