Skip to content

Commit 0e1055d

Browse files
database: add Failed_builds calls result handlers
1 parent f76a5d0 commit 0e1055d

File tree

2 files changed

+80
-51
lines changed

2 files changed

+80
-51
lines changed

lib/action.ml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) (Buildkite_api :
602602
match validate_signature ?signing_key:secrets.buildkite_signing_secret ~headers body with
603603
| Error e -> action_error e
604604
| Ok () ->
605-
let%lwt (_ : int64 Database.db_use_result) = Database.Failed_builds.create ~ctx n in
605+
let%lwt () = Database.Failed_builds.create ~ctx n in
606606
let repo_url = validate_repo_url secrets n in
607607
let%lwt cfg =
608608
match Context.find_repo_config ctx repo_url with
@@ -634,7 +634,7 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) (Buildkite_api :
634634
(* we don't want to consider builds notifications for builds that finish out of order *)
635635
match Stringtbl.find_opt repo_state.failed_steps repo_key with
636636
| Some state when n.build.number < state.last_build ->
637-
let%lwt (_ : int64 Database.db_use_result) =
637+
let%lwt () =
638638
Database.Failed_builds.update_state_after_notification ~repo_state ~has_state_update:false n
639639
(Printf.sprintf "is_timely > false > build number < last build number")
640640
in
@@ -646,7 +646,7 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) (Buildkite_api :
646646
in
647647
(match should_notify with
648648
| false ->
649-
let%lwt (_ : int64 Database.db_use_result) =
649+
let%lwt () =
650650
Database.Failed_builds.update_state_after_notification ~repo_state ~has_state_update:false n
651651
"should notify > false"
652652
in
@@ -681,7 +681,7 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) (Buildkite_api :
681681
Stringtbl.replace repo_state.failed_steps repo_key
682682
{ steps = FailedStepSet.empty; last_build = n.build.number };
683683

684-
let%lwt (_ : int64 Database.db_use_result) =
684+
let%lwt () =
685685
Database.Failed_builds.update_state_after_notification ~repo_state ~has_state_update:true n
686686
"should notify > true > build state = passed"
687687
in
@@ -692,7 +692,7 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) (Buildkite_api :
692692
]
693693
| false ->
694694
(* the build was successful, but we haven't fixed all the steps. Update state, but don't notify *)
695-
let%lwt (_ : int64 Database.db_use_result) =
695+
let%lwt () =
696696
Database.Failed_builds.update_state_after_notification ~repo_state
697697
~has_state_update:(FailedStepSet.equal state_failed_steps state.steps)
698698
n "should notify > false > build state = passed w/ failed steps"
@@ -706,7 +706,7 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) (Buildkite_api :
706706
new_failed_steps ~cfg ~repo_state
707707
~get_build:(Buildkite_api.get_build ~cache:`Refresh ~ctx)
708708
~db_update:(fun ~repo_state ~has_state_update n msg ->
709-
let%lwt (_ : int64 Database.db_use_result) =
709+
let%lwt () =
710710
Database.Failed_builds.update_state_after_notification ~repo_state ~has_state_update n
711711
(Printf.sprintf "should notify > true > build state = failed > %s" msg)
712712
in

lib/database.ml

Lines changed: 74 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -118,47 +118,62 @@ module Failed_builds = struct
118118
type n = Buildkite_t.webhook_build_payload
119119

120120
let create ~(ctx : Context.t)
121-
({ build = { id; sha; meta_data; state; web_url; number; branch; created_at; _ }; pipeline; _ } : n) =
122-
with_db "Failed_builds.create" (fun dbd ->
123-
let org, pipeline_name, _build_nr = Util.Build.get_org_pipeline_build' web_url in
124-
let repo_url = Util.Build.git_ssh_to_https pipeline.repository in
125-
let repo_state = State.find_or_add_repo ctx.state repo_url in
126-
let%lwt (build : Buildkite_t.get_build_res) =
127-
(* TODO: review. Can we `Use cache here? *)
128-
let%lwt b = Api_remote.Buildkite.get_build ~cache:`Refresh ~ctx web_url in
129-
Lwt.return @@ Result.get_ok b
130-
in
131-
let jobs =
132-
List.filter
133-
(function
134-
| Buildkite_t.Script _ | Buildkite_t.Trigger _ -> true
135-
| _ -> false)
136-
build.jobs
137-
in
138-
let build_payload = Buildkite_j.string_of_get_build_res { build with jobs } in
139-
let commit_author = Util.Webhook.extract_metadata_email meta_data |> Option.default "" in
140-
let commit_url = Printf.sprintf "%s/commit/%s" repo_url sha in
141-
let notification_created_at = Common.Timestamp.wrap_with_fallback created_at |> Ptime.to_float_s in
142-
let state_before_notification =
143-
match Common.Stringtbl.find_opt repo_state.failed_steps (Util.Webhook.repo_key org pipeline_name) with
144-
| Some state -> State_j.string_of_failed_steps state
145-
| None -> ""
146-
in
147-
148-
(* These values should/will be updated using the update_state_after_notification function. *)
149-
let state_after_notification = "" in
150-
let has_state_update = false in
151-
152-
T.create ~id ~sha ~build_payload
153-
~pipeline_payload:(Buildkite_j.string_of_pipeline pipeline)
154-
~jobs:(Buildkite_j.string_of_jobs jobs) ~commit_author ~commit_url
155-
~build_state:(Buildkite_j.string_of_build_state state)
156-
~build_url:web_url ~build_number:(Int64.of_int number) ~is_canceled:(state = Canceled)
157-
~pipeline:(Util.Webhook.repo_key org pipeline_name)
158-
~repository:repo_url ~branch ~state_before_notification ~state_after_notification ~has_state_update
159-
~notification_created_at
160-
~created_at:(Ptime_clock.now () |> Ptime.to_float_s)
161-
~last_handled_in:"create" dbd)
121+
({ build = { id; sha; meta_data; state; web_url; number; branch; created_at; _ }; pipeline; _ } as n : n) =
122+
let op_name = "Failed_builds.create" in
123+
let handle_create r =
124+
let repo_slug = n.pipeline.provider.settings.repository in
125+
match%lwt r with
126+
| Ok _ ->
127+
log#debug "[%s] [%s] created failed build" op_name repo_slug;
128+
Lwt.return_unit
129+
| Error e ->
130+
log#error "[%s] [%s] failed to create failed build: %s" op_name repo_slug e;
131+
Lwt.return_unit
132+
| Db_unavailable ->
133+
log#debug "[%s] [%s] database unavailable" op_name repo_slug;
134+
Lwt.return_unit
135+
in
136+
handle_create
137+
@@ with_db op_name (fun dbd ->
138+
let org, pipeline_name, _build_nr = Util.Build.get_org_pipeline_build' web_url in
139+
let repo_url = Util.Build.git_ssh_to_https pipeline.repository in
140+
let repo_state = State.find_or_add_repo ctx.state repo_url in
141+
let%lwt (build : Buildkite_t.get_build_res) =
142+
(* TODO: review. Can we `Use cache here? *)
143+
let%lwt b = Api_remote.Buildkite.get_build ~cache:`Refresh ~ctx web_url in
144+
Lwt.return @@ Result.get_ok b
145+
in
146+
let jobs =
147+
List.filter
148+
(function
149+
| Buildkite_t.Script _ | Buildkite_t.Trigger _ -> true
150+
| _ -> false)
151+
build.jobs
152+
in
153+
let build_payload = Buildkite_j.string_of_get_build_res { build with jobs } in
154+
let commit_author = Util.Webhook.extract_metadata_email meta_data |> Option.default "" in
155+
let commit_url = Printf.sprintf "%s/commit/%s" repo_url sha in
156+
let notification_created_at = Common.Timestamp.wrap_with_fallback created_at |> Ptime.to_float_s in
157+
let state_before_notification =
158+
match Common.Stringtbl.find_opt repo_state.failed_steps (Util.Webhook.repo_key org pipeline_name) with
159+
| Some state -> State_j.string_of_failed_steps state
160+
| None -> ""
161+
in
162+
163+
(* These values should/will be updated using the update_state_after_notification function. *)
164+
let state_after_notification = "" in
165+
let has_state_update = false in
166+
167+
T.create ~id ~sha ~build_payload
168+
~pipeline_payload:(Buildkite_j.string_of_pipeline pipeline)
169+
~jobs:(Buildkite_j.string_of_jobs jobs) ~commit_author ~commit_url
170+
~build_state:(Buildkite_j.string_of_build_state state)
171+
~build_url:web_url ~build_number:(Int64.of_int number) ~is_canceled:(state = Canceled)
172+
~pipeline:(Util.Webhook.repo_key org pipeline_name)
173+
~repository:repo_url ~branch ~state_before_notification ~state_after_notification ~has_state_update
174+
~notification_created_at
175+
~created_at:(Ptime_clock.now () |> Ptime.to_float_s)
176+
~last_handled_in:"create" dbd)
162177

163178
let update_state_after_notification ~(repo_state : State_t.repo_state) ~has_state_update (n : n) last_handled_in =
164179
let org, pipeline_name, _build_nr = Util.Build.get_org_pipeline_build' n.build.web_url in
@@ -167,10 +182,24 @@ module Failed_builds = struct
167182
| Some state -> State_j.string_of_failed_steps state
168183
| None -> "no state found to write"
169184
in
170-
171-
with_db
172-
(Printf.sprintf "Failed_builds.update_state_after_notification %s" last_handled_in)
173-
(T.update_state_after_notification ~id:n.build.id ~has_state_update ~state_after_notification ~last_handled_in)
185+
let op_name = "Failed_builds.update_state_after_notification" in
186+
let handle_update r =
187+
let repo_slug = n.pipeline.provider.settings.repository in
188+
match%lwt r with
189+
| Ok _ ->
190+
log#debug "[%s] [%s] updated state after notification" op_name repo_slug;
191+
Lwt.return_unit
192+
| Error e ->
193+
log#error "[%s] [%s] failed to update state after notification: %s" op_name repo_slug e;
194+
Lwt.return_unit
195+
| Db_unavailable ->
196+
log#debug "[%s] [%s] database unavailable" op_name repo_slug;
197+
Lwt.return_unit
198+
in
199+
handle_update
200+
@@ with_db
201+
(Printf.sprintf "Failed_builds.update_state_after_notification %s" last_handled_in)
202+
(T.update_state_after_notification ~id:n.build.id ~has_state_update ~state_after_notification ~last_handled_in)
174203
end
175204

176205
module Debug_db = struct

0 commit comments

Comments
 (0)