syntax = "proto3";

package mesh.v1;

import "mesh/v1/git_model.proto";

option go_package = "atlassian.com/bitbucket/mesh/rpc/v1";
option java_multiple_files = true;
option java_outer_classname = "HookScriptProtos";
option java_package = "com.atlassian.bitbucket.mesh.rpc.v1";

message RpcCallHookScriptsFragment {
  oneof fragment_oneof {
    //Request to execute one or more hook scripts
    RpcCallHookScriptsRequest request = 1;
    //Hook script contents; sent when Mesh requests it through a RpcHookScriptsMissing fragment
    RpcHookScript hook_script = 2;
  }
}

message RpcCallHookScriptsRequest {
  //The repository to use as the working directory when executing the scripts
  string repository = 1;
  //Options that are applied to each script invocation (so per script)
  RpcHookScriptOptions hook_script_options = 2;
  //The ref updates to provide as input for the hook scripts
  repeated RpcRefUpdate ref_updates = 3;

  bool abort_on_first_veto = 10;
  //Hook script name, e.g. hook-scripts/123/1
  repeated string hook_script = 11;
}

message RpcCallHookScriptsResponse {
  repeated RpcHookScriptResponse responses = 1;
}

message RpcCallHookScriptsResponseFragment {
  oneof fragment_oneof {
    //A chunk of stdout / stderr output generated by a hook script
    RpcHookScriptChunk chunk = 1;
    //Sent when the execution of a hook script is requested, but Mesh does not have the script (yet)
    RpcHookScriptsMissing missing = 2;
    //The result of the hook script invocations
    RpcCallHookScriptsResponse response = 3;
  }
}

/* The ID and contents of a hook script */
message RpcHookScript {
  string id = 1;

  bytes content = 2;
}

/* A chunk of stdout or stderr output generated by a hook script */
message RpcHookScriptChunk {
  string hook_script = 1;
  RpcHookChunk chunk = 2;
}

message RpcHookScriptOptions {
  //The timeouts for each individual script invocation
  RpcGitTimeouts timeouts = 1;
  //The environment variables to apply
  map<string, bytes> environment = 2;
  //The maximum length of script output (stdout and stderr combined) that should be sent back (in bytes)
  uint32 max_output_length = 3;
}

/* The result of the invocation of an individual hook */
message RpcHookScriptResponse {
  string hook_script = 1;
  RpcHookResult result = 3;

  uint64 duration_ms = 10;
  bool timed_out = 11;
}

/* Sent by Mesh when the execution of a hook script is requested, but the hook script is not found */
message RpcHookScriptsMissing {
  repeated string hook_script = 1;
}

