Support Github rebuild via webooks.#283
Conversation
talex5
left a comment
There was a problem hiding this comment.
This looks good. However, if we're performing actions in response to webhooks (rather than just triggering another query to GitHub) then we really should be validating incoming webhooks:
https://docs.github.com/en/developers/webhooks-and-events/webhooks/securing-your-webhooks
We should also think a bit about access control. Who does GitHub allow to click on the rebuild button? Possibly it should be tied in with the existing has_role access control system. At least, it needs to be documented.
Happily we get the GH User (as |
e910470 to
6006baa
Compare
talex5
left a comment
There was a problem hiding this comment.
I think the secret is needed for all web-hooks, not just for apps.
plugins/github/current_github.ml
Outdated
| let webhook = object | ||
| let validate_webhook_payload webhook_secret body headers = | ||
| match webhook_secret with | ||
| | None -> true (* No webhook secret supplied skip the validation. *) |
There was a problem hiding this comment.
I think we should just require the secret in all cases.
plugins/github/current_github.ml
Outdated
| begin match event, validate_webhook_payload webhook_secret body headers with | ||
| | Some "installation_repositories", true -> Installation.input_installation_repositories_webhook () | ||
| | Some "installation", true -> App.input_installation_webhook () | ||
| | Some ("pull_request" | "push" | "create"), true -> Api.input_webhook json_body | ||
| | Some "check_run", true -> Api.rebuild_webhook ~engine ~has_role json_body | ||
| | Some x, b -> Log.warn (fun f -> f "Unknown GitHub event type %S or invalid X-Hub-Signature-256 %b" x b) | ||
| | None, _ -> Log.warn (fun f -> f "Missing GitHub event type in webhook!") | ||
| end; |
There was a problem hiding this comment.
Checking for true in each case seems unnecessary. Also, the error message is rather unclear ("or invalid X-Hub-Signature-256 false").
| begin match event, validate_webhook_payload webhook_secret body headers with | |
| | Some "installation_repositories", true -> Installation.input_installation_repositories_webhook () | |
| | Some "installation", true -> App.input_installation_webhook () | |
| | Some ("pull_request" | "push" | "create"), true -> Api.input_webhook json_body | |
| | Some "check_run", true -> Api.rebuild_webhook ~engine ~has_role json_body | |
| | Some x, b -> Log.warn (fun f -> f "Unknown GitHub event type %S or invalid X-Hub-Signature-256 %b" x b) | |
| | None, _ -> Log.warn (fun f -> f "Missing GitHub event type in webhook!") | |
| end; | |
| if validate_webhook_payload webhook_secret body headers then ( | |
| match event with | |
| | Some "installation_repositories" -> Installation.input_installation_repositories_webhook () | |
| | Some "installation" -> App.input_installation_webhook () | |
| | Some ("pull_request" | "push" | "create") -> Api.input_webhook json_body | |
| | Some "check_run" -> Api.rebuild_webhook ~engine ~has_role json_body | |
| | Some x -> Log.warn (fun f -> f "Unknown GitHub event type %S" x) | |
| | None, _ -> Log.warn (fun f -> f "Missing GitHub event type in webhook!") | |
| ) else ( | |
| Log.warn (fun f -> f "Invalid X-Hub-Signature-256" x) | |
| ) |
doc/examples/github.ml
Outdated
| ~docv:"REPO" | ||
| [] | ||
|
|
||
| let webhook_secret = |
There was a problem hiding this comment.
Perhaps this should be moved into the plugin? Everyone using OAuth is going to need this.
There was a problem hiding this comment.
This was only for the demo, the webhook secret from a file is here.
https://github.com/ocurrent/ocurrent/pull/283/files/4e0305335ff9d4feafae708c8926a1880766bdd6#diff-cdd21afd7356cc62801e8a3d1e29ed9791707d3f6639c817603b72cd21817ae3R210-R216
There was a problem hiding this comment.
That link is broken now. But we want to make sure people read the secret from a file for OAuth pipelines too.
There was a problem hiding this comment.
Changed the examples to read this from a file and restructured the types in Current_github.App and Api to include the web hook_secret. The examples now work the same as the other pipelines like ocaml-ci
7a7f62c to
fe804df
Compare
Passing job_id through to set_status, which then gets sent back via webhooks and can be looked up as a job to rebuild.
6f5a2be to
de8aa3f
Compare
plugins/github/current_github.mli
Outdated
| (** Construct a CheckRunStatus.t | ||
| A maximum of three actions are accepted by GitHub. | ||
| val v : ?text:string -> ?summary:string -> ?url:Uri.t -> ?actions:action list -> ?identifier:string -> state -> t | ||
| (** [v ?text ?summary ?url ?actions ?identifier] creates a CheckRunStatus with [?text] description, a link to |
There was a problem hiding this comment.
| (** [v ?text ?summary ?url ?actions ?identifier] creates a CheckRunStatus with [?text] description, a link to | |
| (** [v ?text ?summary ?url ?actions ?identifier state] creates a CheckRunStatus with [?text] description, a link to |
…, current_github, current_git, current_examples, current_docker and current (0.6) CHANGES: Core: - Implement labelling of clusters on the Graphviz diagram (@ewanmellor ocurrent/ocurrent#255) - Abort propagation on constant/equal changes (@art-w ocurrent/ocurrent#318) API: - GitHub: Record build status using CheckRun (@tmcgilchrist ocurrent/ocurrent#279) - GitHub: Add details_url to check_run. (@tmcgilchrist ocurrent/ocurrent#282) - GitHub: Add Current_github.Api.cmdliner_opt to allow writing pipelines which can optionally be run as GitHub apps. (@talex5 ocurrent/ocurrent#281) - GitHub: Provide markdown details for CheckRun. (@tmcgilchrist ocurrent/ocurrent#288) - GitHub: Fix wrong name used for repository (@tmcgilchrist ocurrent/ocurrent#289 ocurrent/ocurrent#290) - GitHub: Support Github rebuild via webooks. (@tmcgilchrist ocurrent/ocurrent#283) - GitHub: monitor GraphQL queries (@art-w ocurrent/ocurrent#298) - GitHub: Limit CheckRunStatus summary and text fields to 65535. (@tmcgilchrist ocurrent/ocurrent#300) - GitHub: Log extra context for Webhook validation failure. (@tmcgilchrist ocurrent/ocurrent#302) - GitLab: Initial GitLab plugin work. (@tmcgilchrist ocurrent/ocurrent#299) - Git: Make git reset less verbose (@kit-ty-kate ocurrent/ocurrent#293) Web UI: - Use Lwt.pause instead of Lwt_unix.yield (@MisterDA ocurrent/ocurrent#297) - Use `ansi` instead of `current_ansi` (@samoht ocurrent/ocurrent#321) - Show line numbers and allow jumping to specific lines in job logs (@punchagan ocurrent/ocurrent#309) Docker: - Explicitly set confirmation levels to allow for manually triggered jobs. (@tmcgilchrist ocurrent/ocurrent#304) - Stop using `Dockerfile.t` completely and use strings instead. (@MisterDA ocurrent/ocurrent#301 ocurrent/ocurrent#316) Other: - Update to cohttp 4.0.0 (ocurrent/ocurrent#274, @talex5) - Move `Current_incr` to its own repository (ocurrent/ocurrent#284, @talex5)
Passing job_id through to set_status, which then gets sent back via webhooks and can be looked up as a job to rebuild. The core piece is
CheckRunStatusnow includes the job_id