// 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 "buf/alpha/registry/v1alpha1/repository_tag.proto";
import "google/protobuf/timestamp.proto";

message RepositoryCommit {
  // primary key, unique, immutable
  string id = 1;
  // immutable
  google.protobuf.Timestamp create_time = 2;
  // The digest of the commit.
  string digest = 3;
  // The name of the commit.
  // This is what is referenced by users.
  // Unique, immutable.
  string name = 4;
  // The branch on which this commit was created.
  string branch = 5;
  reserved 6;
  reserved "commit_sequence_id";
  // The username of the user who authored this commit.
  string author = 7;
  // The tags associated with this commit
  repeated buf.alpha.registry.v1alpha1.RepositoryTag tags = 8;
  // The commit's draft name, if it is a draft commit.
  string draft_name = 9;
  // spdx_license_id is the license of the commit, based on the
  // license file pushed, which should be one of the identifier defined in
  // https://spdx.org/licenses, and will be not set if the license file is not
  // presented or cannot be classified into a known license.
  string spdx_license_id = 10;
  // The manifest digest of the commit.
  string manifest_digest = 11;
  // Number of tags associated with the commit.
  int64 tag_count = 12;
  // Number of git commits with associated with the BSR commit.
  int64 git_commits_count = 13;
  // The b5 digest of the commit.
  string b5_digest = 14;
}

// RepositoryCommitService is the Repository commit service.
service RepositoryCommitService {
  // ListRepositoryCommitsByBranch lists the repository commits associated
  // with a repository branch on a repository, ordered by their create time.
  rpc ListRepositoryCommitsByBranch(ListRepositoryCommitsByBranchRequest) returns (ListRepositoryCommitsByBranchResponse) {
    // ListRepositoryCommitsByBranch has been deprecated, use
    // ListRepositoryCommitsByReference instead.
    option deprecated = true;
    option idempotency_level = NO_SIDE_EFFECTS;
  }

  // ListRepositoryCommitsByReference returns repository commits up-to and including
  // the provided reference.
  rpc ListRepositoryCommitsByReference(ListRepositoryCommitsByReferenceRequest) returns (ListRepositoryCommitsByReferenceResponse) {
    option idempotency_level = NO_SIDE_EFFECTS;
  }
  // GetRepositoryCommitByReference returns the repository commit matching
  // the provided reference, if it exists.
  rpc GetRepositoryCommitByReference(GetRepositoryCommitByReferenceRequest) returns (GetRepositoryCommitByReferenceResponse) {
    option idempotency_level = NO_SIDE_EFFECTS;
  }
  // ListRepositoryDraftCommits lists draft commits in a repository.
  rpc ListRepositoryDraftCommits(ListRepositoryDraftCommitsRequest) returns (ListRepositoryDraftCommitsResponse) {
    option idempotency_level = NO_SIDE_EFFECTS;
  }
  // DeleteRepositoryDraftCommit deletes a draft.
  rpc DeleteRepositoryDraftCommit(DeleteRepositoryDraftCommitRequest) returns (DeleteRepositoryDraftCommitResponse) {
    option idempotency_level = IDEMPOTENT;
  }
}

message ListRepositoryCommitsByBranchRequest {
  // The owner of the repository which the repository branch belongs to.
  string repository_owner = 1;
  // The name of the repository which the repository branch belongs to.
  string repository_name = 2;
  // The name of the repository branch whose commits should be listed.
  string repository_branch_name = 3;
  uint32 page_size = 4;
  // For now this is the id of he last commit returned in the previous page.
  // This is directly set by the frontend when listing track history starting with a
  // specific commit. If we page_token to be anything other than a commit id
  // we need to change the frontend as well.
  string page_token = 5;
  bool reverse = 6;
}

message ListRepositoryCommitsByBranchResponse {
  repeated RepositoryCommit repository_commits = 1;
  string next_page_token = 2;
}

message ListRepositoryCommitsByReferenceRequest {
  // The owner of the repository which the repository reference belongs to.
  string repository_owner = 1;
  // The name of the repository which the repository reference belongs to.
  string repository_name = 2;
  // The optional reference used to resolve repository commits. Can be a tag or commit.
  // If unspecified, will use the repository's default_branch.
  string reference = 3;
  uint32 page_size = 4;
  string page_token = 5;
  bool reverse = 6;
}

message ListRepositoryCommitsByReferenceResponse {
  repeated RepositoryCommit repository_commits = 1;
  string next_page_token = 2;
}

message GetRepositoryCommitByReferenceRequest {
  // The owner of the repository which the reference belongs to.
  string repository_owner = 1;
  // The name of the repository which the reference belongs to.
  string repository_name = 2;
  // The optional reference that should be resolved to a commit. Can be a tag or commit.
  // If unspecified, will use the repository's default_branch.
  string reference = 3;
}

message GetRepositoryCommitByReferenceResponse {
  RepositoryCommit repository_commit = 1;
}

message ListRepositoryDraftCommitsRequest {
  // The owner of the repository which the repository branch belongs to.
  string repository_owner = 1;
  // The name of the repository which the repository branch belongs to.
  string repository_name = 2;
  uint32 page_size = 3;
  string page_token = 4;
  // By default, draft commits are ordered by last update time descending.
  // Reverse orders them ascending.
  bool reverse = 5;
}

message ListRepositoryDraftCommitsResponse {
  // Each commit will have draft_name set.
  repeated RepositoryCommit repository_commits = 1;
  string next_page_token = 2;
}

message DeleteRepositoryDraftCommitRequest {
  // The owner of the repository which the repository branch belongs to.
  string repository_owner = 1;
  // The name of the repository which the repository branch belongs to.
  string repository_name = 2;
  // The name of the draft to delete.
  string draft_name = 3;
}

message DeleteRepositoryDraftCommitResponse {}
