Skip to content

Support Github rebuild via webooks.#283

Merged
tmcgilchrist merged 2 commits intoocurrent:masterfrom
tmcgilchrist:rebuild
Aug 25, 2021
Merged

Support Github rebuild via webooks.#283
tmcgilchrist merged 2 commits intoocurrent:masterfrom
tmcgilchrist:rebuild

Conversation

@tmcgilchrist
Copy link
Copy Markdown
Member

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 CheckRunStatus now includes the job_id

  type t = {
      state: state;
      description: string option;
      url: Uri.t option;
      job_id: string option;
    }

Copy link
Copy Markdown
Contributor

@talex5 talex5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@tmcgilchrist
Copy link
Copy Markdown
Member Author

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 sender on the Webhook) that requested the rebuild so we can filter with has_role. We don't seem to have an option to not show the Re-run button for different users.
But we can provide our own set of actions as buttons on the CheckRun.

Copy link
Copy Markdown
Contributor

@talex5 talex5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the secret is needed for all web-hooks, not just for apps.

let webhook = object
let validate_webhook_payload webhook_secret body headers =
match webhook_secret with
| None -> true (* No webhook secret supplied skip the validation. *)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should just require the secret in all cases.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed this.

Comment on lines 36 to 43
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;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking for true in each case seems unnecessary. Also, the error message is rather unclear ("or invalid X-Hub-Signature-256 false").

Suggested change
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)
)

~docv:"REPO"
[]

let webhook_secret =
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this should be moved into the plugin? Everyone using OAuth is going to need this.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That link is broken now. But we want to make sure people read the secret from a file for OAuth pipelines too.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@tmcgilchrist tmcgilchrist force-pushed the rebuild branch 2 times, most recently from 7a7f62c to fe804df Compare August 17, 2021 03:09
Passing job_id through to set_status, which then gets sent back via
webhooks and can be looked up as a job to rebuild.
@tmcgilchrist tmcgilchrist force-pushed the rebuild branch 4 times, most recently from 6f5a2be to de8aa3f Compare August 25, 2021 01:16
Copy link
Copy Markdown
Contributor

@talex5 talex5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

(** 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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(** [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

@tmcgilchrist tmcgilchrist merged commit 3c1979d into ocurrent:master Aug 25, 2021
@tmcgilchrist tmcgilchrist deleted the rebuild branch August 25, 2021 07:59
tmcgilchrist added a commit to tmcgilchrist/opam-repository that referenced this pull request Apr 7, 2022
…, 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants