Skip to content

Commit

Permalink
Merge pull request #92 from terrateamio/pro-371-add-store-action-output
Browse files Browse the repository at this point in the history
Pro 371 add store action output
  • Loading branch information
orbitz authored Nov 22, 2024
2 parents 356e231 + 0639c73 commit 516ec1e
Show file tree
Hide file tree
Showing 18 changed files with 1,418 additions and 32 deletions.
98 changes: 98 additions & 0 deletions api_schemas/terrat/api.json
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,9 @@
},
{
"$ref": "#/components/schemas/work-manifest-tf-operation-result"
},
{
"$ref": "#/components/schemas/work-manifest-tf-operation-result2"
}
]
},
Expand Down Expand Up @@ -1064,6 +1067,21 @@
],
"type": "object"
},
"work-manifest-tf-operation-result2": {
"additionalProperties": false,
"properties": {
"steps": {
"items": {
"$ref": "#/components/schemas/workflow-step-output"
},
"type": "array"
}
},
"required": [
"steps"
],
"type": "object"
},
"work-manifest-unsafe-apply": {
"additionalProperties": false,
"properties": {
Expand Down Expand Up @@ -1424,6 +1442,86 @@
]
},
"type": "array"
},
"workflow-step-output": {
"additionalProperties": false,
"properties": {
"ignore_errors": {
"type": "boolean"
},
"payload": {
"additionalProperties": {
"type": null
},
"properties": {},
"type": "object"
},
"scope": {
"oneOf": [
{
"$ref": "#/components/schemas/workflow-step-output-scope-dirspace"
},
{
"$ref": "#/components/schemas/workflow-step-output-scope-run"
}
]
},
"step": {
"type": "string"
},
"success": {
"type": "boolean"
}
},
"required": [
"ignore_errors",
"location",
"payload",
"scope",
"step",
"success"
],
"type": "object"
},
"workflow-step-output-scope-dirspace": {
"additionalProperties": false,
"properties": {
"dir": {
"type": "string"
},
"type": {
"const": "dirspace",
"type": "string"
},
"workspace": {
"type": "string"
}
},
"required": [
"dir",
"workspace"
],
"type": "object"
},
"workflow-step-output-scope-run": {
"additionalProperties": false,
"properties": {
"flow": {
"type": "string"
},
"subflow": {
"type": "string"
},
"type": {
"const": "run",
"type": "string"
}
},
"required": [
"flow",
"subflow"
],
"type": "object"
}
}
},
Expand Down
35 changes: 26 additions & 9 deletions code/src/snabela/snabela.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ module Kv = struct
let float f = V (F f)
let string s = V (S s)
let bool b = V (B b)

let to_string = function
| I v -> CCInt.to_string v
| F v -> CCFloat.to_string v
| S v -> v
| B v -> Bool.to_string v
end

module Transformer = struct
Expand Down Expand Up @@ -214,15 +220,26 @@ let rec eval_template buf t kv template section =
| _ -> assert false

and eval_bool_section ln buf t kv ts section key b =
match Kv.Map.get key kv with
| Some (Kv.V (Kv.B v)) when v = b ->
let ts = eval_template buf t kv ts key in
eval_template buf t kv ts section
| Some (Kv.V (Kv.B _)) ->
let ts = skip_section key ts in
eval_template buf t kv ts section
| Some _ -> raise (Apply_error (`Expected_boolean (key, ln)))
| None -> raise (Apply_error (`Missing_key (key, ln)))
match CCString.Split.left ~by:"=" key with
| None -> (
match Kv.Map.get key kv with
| Some (Kv.V (Kv.B v)) when v = b ->
let ts = eval_template buf t kv ts key in
eval_template buf t kv ts section
| Some (Kv.V (Kv.B _)) ->
let ts = skip_section key ts in
eval_template buf t kv ts section
| Some _ -> raise (Apply_error (`Expected_boolean (key, ln)))
| None -> raise (Apply_error (`Missing_key (key, ln))))
| Some (key', value) -> (
match Kv.Map.get key' kv with
| Some (Kv.V v) when CCString.equal (Kv.to_string v) value = b ->
let ts = eval_template buf t kv ts key in
eval_template buf t kv ts section
| Some _ ->
let ts = skip_section key ts in
eval_template buf t kv ts section
| None -> raise (Apply_error (`Missing_key (key, ln))))

and eval_list_section ln buf t kv ts section key =
match Kv.Map.get key kv with
Expand Down
3 changes: 2 additions & 1 deletion code/src/snabela/snabela_lexer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ exception Tokenize_error of err
open Token

let key =
[%sedlex.regexp? ('a' .. 'z' | 'A' .. 'Z'), Star ('a' .. 'z' | 'A' .. 'Z' | '0' .. '9' | '_')]
[%sedlex.regexp?
('a' .. 'z' | 'A' .. 'Z'), Star ('a' .. 'z' | 'A' .. 'Z' | '0' .. '9' | '_' | '=')]

let rec token ln bldr buf =
match%sedlex buf with
Expand Down
74 changes: 74 additions & 0 deletions code/src/terrat/terrat_evaluator3.ml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ module Msg = struct
result : Terrat_api_components_work_manifest_tf_operation_result.t;
work_manifest : ('account, 'target) Terrat_work_manifest3.Existing.t;
}
| Tf_op_result2 of {
is_layered_run : bool;
remaining_layers : Terrat_change_match3.Dirspace_config.t list list;
result : Terrat_api_components_work_manifest_tf_operation_result2.t;
work_manifest : ('account, 'target) Terrat_work_manifest3.Existing.t;
}
| Unexpected_temporary_err
| Unlock_success
end
Expand Down Expand Up @@ -507,13 +513,23 @@ module type S = sig
val work_manifest_result :
Terrat_api_components_work_manifest_tf_operation_result.t -> Work_manifest_result.t

val work_manifest_result2 :
Terrat_api_components_work_manifest_tf_operation_result2.t -> Work_manifest_result.t

val store_tf_operation_result :
request_id:string ->
Db.t ->
Uuidm.t ->
Terrat_api_components_work_manifest_tf_operation_result.t ->
(unit, [> `Error ]) result Abb.Future.t

val store_tf_operation_result2 :
request_id:string ->
Db.t ->
Uuidm.t ->
Terrat_api_components_work_manifest_tf_operation_result2.t ->
(unit, [> `Error ]) result Abb.Future.t

val query_conflicting_work_manifests_in_repo :
request_id:string ->
Db.t ->
Expand Down Expand Up @@ -1037,6 +1053,18 @@ module Make (S : S) = struct
time))
(fun () -> S.store_tf_operation_result ~request_id db work_manifest_id result)

let store_tf_operation_result2 request_id db work_manifest_id result =
Abbs_time_it.run
(fun time ->
Logs.info (fun m ->
m
"EVALUATOR : %s : STORE_TF_OPERATION_RESULT2 : id=%a : time=%f"
request_id
Uuidm.pp
work_manifest_id
time))
(fun () -> S.store_tf_operation_result2 ~request_id db work_manifest_id result)

let query_conflicting_work_manifests_in_repo request_id db pull_request dirspaces op =
Abbs_time_it.run
(fun time ->
Expand Down Expand Up @@ -3270,6 +3298,8 @@ module Make (S : S) = struct
>>= fun () -> Abb.Future.return (Ok ())
| Terrat_api_components_work_manifest_result.Work_manifest_tf_operation_result _ ->
assert false
| Terrat_api_components_work_manifest_result.Work_manifest_tf_operation_result2 _ ->
assert false
| Terrat_api_components_work_manifest_result.Work_manifest_build_config_result _ ->
assert false

Expand Down Expand Up @@ -4039,6 +4069,48 @@ module Make (S : S) = struct
| Terrat_api_components_work_manifest_result.Work_manifest_index_result _ -> assert false
| Terrat_api_components_work_manifest_result.Work_manifest_build_config_result _ ->
assert false
| Terrat_api_components_work_manifest_result.Work_manifest_tf_operation_result2 result ->
Dv.client ctx state
>>= fun client ->
Dv.matches ctx state op
>>= fun matches ->
let work_manifest_result = S.work_manifest_result2 result in
store_tf_operation_result2
state.State.request_id
ctx.Ctx.storage
work_manifest.Wm.id
result
>>= fun () ->
run_interactive ctx state (fun () ->
Dv.pull_request ctx state
>>= fun pull_request ->
create_op_commit_checks_of_result
state.State.request_id
ctx.Ctx.config
client
work_manifest.Wm.account
(S.Pull_request.repo pull_request)
(S.Pull_request.branch_ref pull_request)
work_manifest
work_manifest_result
>>= fun () ->
publish_msg
state.State.request_id
client
(Event.user state.State.event)
pull_request
(Msg.Tf_op_result2
{
is_layered_run = CCList.length matches.Dv.Matches.all_matches > 1;
remaining_layers = matches.Dv.Matches.all_unapplied_matches;
result;
work_manifest;
}))
>>= fun () ->
if not work_manifest_result.Work_manifest_result.overall_success then
(* If the run failed, then we're done. *)
Abb.Future.return (Error (`Noop state))
else Abb.Future.return (Ok ())
| Terrat_api_components_work_manifest_result.Work_manifest_tf_operation_result result ->
Dv.client ctx state
>>= fun client ->
Expand Down Expand Up @@ -5712,6 +5784,8 @@ module Make (S : S) = struct
>>= fun () -> Abb.Future.return (Error (`Noop state))
| Terrat_api_components_work_manifest_result.Work_manifest_tf_operation_result _ ->
assert false
| Terrat_api_components_work_manifest_result.Work_manifest_tf_operation_result2 _ ->
assert false
| Terrat_api_components_work_manifest_result.Work_manifest_index_result _ -> assert false)
~fallthrough:H.log_state_err_iter
end
Expand Down
16 changes: 16 additions & 0 deletions code/src/terrat/terrat_evaluator3.mli
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ module Msg : sig
result : Terrat_api_components_work_manifest_tf_operation_result.t;
work_manifest : ('account, 'target) Terrat_work_manifest3.Existing.t;
}
| Tf_op_result2 of {
is_layered_run : bool;
remaining_layers : Terrat_change_match3.Dirspace_config.t list list;
result : Terrat_api_components_work_manifest_tf_operation_result2.t;
work_manifest : ('account, 'target) Terrat_work_manifest3.Existing.t;
}
| Unexpected_temporary_err
| Unlock_success
end
Expand Down Expand Up @@ -448,13 +454,23 @@ module type S = sig
val work_manifest_result :
Terrat_api_components_work_manifest_tf_operation_result.t -> Work_manifest_result.t

val work_manifest_result2 :
Terrat_api_components_work_manifest_tf_operation_result2.t -> Work_manifest_result.t

val store_tf_operation_result :
request_id:string ->
Db.t ->
Uuidm.t ->
Terrat_api_components_work_manifest_tf_operation_result.t ->
(unit, [> `Error ]) result Abb.Future.t

val store_tf_operation_result2 :
request_id:string ->
Db.t ->
Uuidm.t ->
Terrat_api_components_work_manifest_tf_operation_result2.t ->
(unit, [> `Error ]) result Abb.Future.t

val query_conflicting_work_manifests_in_repo :
request_id:string ->
Db.t ->
Expand Down
Loading

0 comments on commit 516ec1e

Please sign in to comment.