1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859openLwt.InfixmoduleRepo_id=Repo_idmoduleApi=ApimoduleAuth=AuthmoduleMetrics=structopenPrometheusletnamespace="ocurrent"letsubsystem="gitlab"letwebhook_events_total=lethelp="Incoming webhook events"inCounter.v_label~label_name:"event"~help~namespace~subsystem"webhook_events_total"end(* Surprisingly this is a string compare, no signing involved.
https://docs.gitlab.com/ee/user/project/integrations/webhooks.html#secret-token
*)letvalidate_webhookwebhook_secretheadersevent=letrequest_header=Option.value~default:"<empty>"@@Cohttp.Header.getheaders"X-Gitlab-Token"inifEqaf.equalrequest_headerwebhook_secretthenOk()elseError(Printf.sprintf{|
Invalid X-Gitlab-Token received for %s.
Please check the Webhook secrets are setup appropriately.
See https://docs.gitlab.com/ee/user/project/integrations/webhooks.html
|}event)letwebhook~webhook_secret=objectinheritCurrent_web.Resource.tmethod!post_raw_sitereqbody=Log.info(funf->f"input_webhook: %a"Cohttp.Request.pp_humreq);letheaders=Cohttp.Request.headersreqinletevent=Cohttp.Header.getheaders"X-Gitlab-Event"inletevent_str=Option.value~default:"NONE"eventinLog.info(funf->f"Got GitLab event %a"Fmt.(option~none:(any"NONE")(quotestring))event);Prometheus.Counter.inc_one(Metrics.webhook_events_totalevent_str);Cohttp_lwt.Body.to_stringbody>>=funbody->matchvalidate_webhookwebhook_secretheadersevent_strwith|Errormsg->Log.warn(funf->f"%s"msg);Cohttp_lwt_unix.Server.respond_string~status:`Unauthorized~body:"Invalid X-Gitlab-Token"()|Ok()->beginmatcheventwith|Some"Merge Request Hook"|Some"Push Hook"->beginmatchGitlab_j.webhook_of_stringbodywith|`MergeRequest_asx->Api.input_webhookx|`Push_asx->Api.input_webhookx|x->Log.warn(funf->f"Unknown GitLab event type %S"(Gitlab_j.string_of_webhookx))end|Somex->Log.warn(funf->f"Unknown GitLab event type %S"x)|None->Log.warn(funf->f"Missing GitLab event type in webhook!")end;Cohttp_lwt_unix.Server.respond_string~status:`OK~body:"OK"()end