@@ -118,47 +118,62 @@ module Failed_builds = struct
118
118
type n = Buildkite_t .webhook_build_payload
119
119
120
120
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)
162
177
163
178
let update_state_after_notification ~(repo_state : State_t.repo_state ) ~has_state_update (n : n ) last_handled_in =
164
179
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
167
182
| Some state -> State_j. string_of_failed_steps state
168
183
| None -> " no state found to write"
169
184
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 )
174
203
end
175
204
176
205
module Debug_db = struct
0 commit comments