// Copyright 2020-2025 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

package buf.alpha.registry.v1alpha1;

import "google/protobuf/timestamp.proto";

message RepositoryBranch {
  // primary key, unique.
  // branches are based on labels, so when a branch is pushed to, the ID will point to the
  // updated entry in the labels table.
  string id = 1;
  // The name of the repository branch.
  string name = 2;
  // The name of the latest commit on the branch.
  string latest_commit_name = 3;
  // is_main_branch denotes whether this branch is considered the main branch of the repository.
  bool is_main_branch = 4;
  // The last update time of the branch.
  google.protobuf.Timestamp last_update_time = 5;
  // The ID of the user who updated the branch.
  string last_update_user_id = 6;
  // The author name of the most recent associated git commit of the branch. May be an empty string
  // if no commit in the branch history contains any associated git commit.
  string last_update_git_author_name = 7;
  // The git commit hash of the most recent associated git commit of the branch. May be an empty
  // string if no commit in the branch history contains any associated git commit.
  string last_update_git_commit_hash = 8;
}

// RepositoryBranchService is the Repository branch service.
service RepositoryBranchService {
  // GetRepositoryBranch gets a repository branch associated with a Repository by name
  rpc GetRepositoryBranch(GetRepositoryBranchRequest) returns (GetRepositoryBranchResponse) {
    option idempotency_level = NO_SIDE_EFFECTS;
  }
  // ListRepositoryBranches lists the repository branches associated with a Repository.
  rpc ListRepositoryBranches(ListRepositoryBranchesRequest) returns (ListRepositoryBranchesResponse) {
    option idempotency_level = NO_SIDE_EFFECTS;
  }
  // ListRepositoryBranchesByReference lists the repository branches associated with a Repository,
  // filtered by a reference.
  rpc ListRepositoryBranchesByReference(ListRepositoryBranchesByReferenceRequest) returns (ListRepositoryBranchesByReferenceResponse) {
    option idempotency_level = NO_SIDE_EFFECTS;
  }
  // GetCurrentDefaultBranch returns the branch that is mapped to the repository's `default_branch` field.
  rpc GetCurrentDefaultBranch(GetCurrentDefaultBranchRequest) returns (GetCurrentDefaultBranchResponse) {
    option idempotency_level = NO_SIDE_EFFECTS;
  }
}

message GetRepositoryBranchRequest {
  // The ID of the repository for which the branch is requested.
  string repository_id = 1;
  // The name of the branch to get.
  string name = 2;
}

message GetRepositoryBranchResponse {
  RepositoryBranch branch = 1;
}

message ListRepositoryBranchesRequest {
  // The ID of the repository whose branches should be listed.
  string repository_id = 1;
  uint32 page_size = 2;
  // The first page is returned if this is empty.
  string page_token = 3;
}

message ListRepositoryBranchesResponse {
  repeated RepositoryBranch repository_branches = 1;
  // There are no more pages if this is empty.
  string next_page_token = 2;
}

message ListRepositoryBranchesByReferenceRequest {
  // The ID of the repository whose branches should be listed.
  string repository_id = 1;
  uint32 page_size = 2;
  // The first page is returned if this is empty.
  string page_token = 3;
  // The reference by which to filter the returned branches.
  oneof reference {
    // The name of a BSR commit. This will filter the branches to only those that include the commit.
    string commit_name = 4;
    // The hash of a VCS commit. This will filter the branches to only those that include the commit
    // associated with the VCS hash.
    string vcs_commit_hash = 5;
  }
}

message ListRepositoryBranchesByReferenceResponse {
  repeated RepositoryBranch repository_branches = 1;
  // There are no more pages if this is empty.
  string next_page_token = 2;
}
message GetCurrentDefaultBranchRequest {
  // The ID of the repository whose current default branch is returned.
  string repository_id = 1;
}

message GetCurrentDefaultBranchResponse {
  RepositoryBranch current_default_branch = 1;
}
