syntax = "proto3";

package builder;
option go_package = "github.com/gitpod-io/gitpod/image-builder/api";

import "content-service-api/initializer.proto";

service ImageBuilder {
    // ResolveBaseImage returns the "digest" form of a Docker image tag thereby making it absolute.
    rpc ResolveBaseImage(ResolveBaseImageRequest) returns (ResolveBaseImageResponse) {};

    // ResolveWorkspaceImage returns information about a build configuration without actually attempting to build anything.
    rpc ResolveWorkspaceImage(ResolveWorkspaceImageRequest) returns (ResolveWorkspaceImageResponse) {};

    // Build initiates the build of a Docker image using a build configuration. If a build of this
    // configuration is already ongoing no new build will be started.
    rpc Build(BuildRequest) returns (stream BuildResponse) {};

    // Logs listens to the build output of an ongoing Docker build identified build the build ID
    rpc Logs(LogsRequest) returns (stream LogsResponse) {};

    // ListBuilds returns a list of currently running builds
    rpc ListBuilds(ListBuildsRequest) returns (ListBuildsResponse) {};
}

message BuildSource {
    oneof from {
        BuildSourceReference ref = 1;
        BuildSourceDockerfile file = 2;
    };
}

message BuildSourceReference {
    string ref = 1;
}

message BuildSourceDockerfile {
    contentservice.WorkspaceInitializer source = 1;
    string dockerfile_version = 2;
    string dockerfile_path = 3;
    string context_path = 4;
}

message ResolveBaseImageRequest {
    string ref = 1;
    BuildRegistryAuth auth = 2;
}

message ResolveBaseImageResponse {
    string ref = 1;
}

message ResolveWorkspaceImageRequest {
    BuildSource source = 1;
    BuildRegistryAuth auth = 2;
}

message ResolveWorkspaceImageResponse {
    string ref = 1;
    string base_ref = 3;
    BuildStatus status = 2;
}

message BuildRequest {
    BuildSource source = 1;
    BuildRegistryAuth auth = 2;
    bool forceRebuild = 3;
}

message BuildRegistryAuth {
    oneof mode {
        BuildRegistryAuthTotal total = 1;
        BuildRegistryAuthSelective selective = 2;
    }
}

message BuildRegistryAuthTotal {
    bool allow_all = 1;
}

message BuildRegistryAuthSelective {
    bool allow_baserep = 1;
    bool allow_workspacerep = 2;
    repeated string any_of = 3;
}

message BuildResponse {
    string ref = 1;
    string base_ref = 4;
    BuildStatus status = 2;
    string message = 3;
}

enum BuildStatus {
    unknown = 0;
    running = 1;
    done_success = 2;
    done_failure = 3;
}

message LogsRequest {
    string build_ref = 1;
    bool censored = 2;
}

message LogsResponse {
    bytes content = 1;
}

message ListBuildsRequest {}

message ListBuildsResponse {
    repeated BuildInfo builds = 1;
}

message BuildInfo {
    string ref = 1;
    BuildStatus status = 2;
    int64 started_at = 3;
}