@@ -238,7 +238,7 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) (Buildkite_api :
238238 | None -> n.branches
239239 | Some build_url ->
240240 let status_switched (_ : branch ) =
241- match Util.Build. get_org_pipeline_build' build_url with
241+ match Util.Build. get_org_pipeline_build n with
242242 | Error e ->
243243 log#error " failed to get org/pipeline/build_nr from build url %s: %s" build_url e;
244244 false
@@ -592,20 +592,21 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) (Buildkite_api :
592592 let process_buildkite_webhook (ctx : Context.t ) headers body =
593593 let open Util.Webhook in
594594 try_process_notification body
595- (let n = Buildkite_webhook_j . webhook_build_payload_of_string body in
595+ (let n = Buildkite_j . webhook_build_payload_of_string body in
596596 log#info " [buildkite_webhook] [%s] event %s: state=%s, branch=%s, commit=%s, pipeline=%s, build_url=%s"
597597 n.pipeline.provider.settings.repository
598- (Buildkite_webhook_j . string_of_webhook_event n.event)
598+ (Buildkite_j . string_of_webhook_event n.event)
599599 (Buildkite_j. string_of_build_state n.build.state)
600600 n.build.branch (String. sub n.build.sha 0 8 ) (pipeline_name n) n.build.web_url;
601601 let secrets = Context. get_secrets_exn ctx in
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
605606 let repo_url = validate_repo_url secrets n in
606607 let % lwt cfg =
607608 match Context. find_repo_config ctx repo_url with
608- | Some config -> Lwt. return config
609+ | Some cfg -> Lwt. return cfg
609610 | None ->
610611 (* Fetch the config from github.
611612 Emulate a repository record with the necessary fields to fetch the config *)
@@ -626,27 +627,48 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) (Buildkite_api :
626627 | Error e -> action_error @@ Printf. sprintf " failed to fetch config for %s: %s" repo_url e)
627628 in
628629 let repo_state = State. find_or_add_repo ctx.state repo_url in
629- let org, pipeline, _build_nr = Result. get_ok @@ Util.Build. get_org_pipeline_build' n.build.web_url in
630+ let org, pipeline, _build_nr = Util.Build. get_org_pipeline_build' n.build.web_url in
630631 let repo_key = repo_key org pipeline in
631632 let is_main_branch = cfg.main_branch_name |> Option. map_default (String. equal n.build.branch) false in
632633 let should_notify = is_main_branch && (notify_fail cfg n || notify_success repo_state repo_key n) in
633634 (match should_notify with
634- | false -> Lwt. return_unit
635+ | false ->
636+ let % lwt (_ : int64 Database.db_use_result ) =
637+ Database.Failed_builds. update_state_after_notification ~repo_state ~has_state_update: false n
638+ " should notify > false"
639+ in
640+ Lwt. return_unit
635641 | true ->
636642 let channel = Common.Status_notification. inject_channel (failed_builds_channel_exn cfg n) in
637643 let % lwt notifications =
638644 match n.build.state with
639645 | Passed ->
646+ (* If the build is successful, we can remove the failed steps from the repo state. *)
640647 Stringtbl. replace repo_state.failed_steps repo_key
641648 { steps = Common.FailedStepSet. empty; last_build = n.build.number };
649+
650+ let % lwt (_ : int64 Database.db_use_result ) =
651+ Database.Failed_builds. update_state_after_notification ~repo_state ~has_state_update: true n
652+ " should notify > true > build state = passed"
653+ in
642654 Lwt. return
643655 [
644656 Slack. generate_failed_build_notification ~is_fix_build_notification: true
645657 ~failed_steps: Common.FailedStepSet. empty n channel;
646658 ]
647659 | Failed ->
648660 (* repo state is updated upon fetching new failed steps *)
649- (match % lwt new_failed_steps ~repo_state ~get_build: (Buildkite_api. get_build ~cache: `Refresh ~ctx ) n with
661+ (match % lwt
662+ new_failed_steps ~repo_state
663+ ~get_build: (Buildkite_api. get_build ~cache: `Refresh ~ctx )
664+ ~db_update: (fun ~repo_state ~has_state_update n msg ->
665+ let % lwt (_ : int64 Database.db_use_result ) =
666+ Database.Failed_builds. update_state_after_notification ~repo_state ~has_state_update n
667+ (Printf. sprintf " should notify > true > build state = failed > %s" msg)
668+ in
669+ Lwt. return_unit)
670+ n
671+ with
650672 | Error e -> action_error e
651673 | Ok failed_steps ->
652674 match FailedStepSet. is_empty failed_steps with
0 commit comments