56 lines
1.9 KiB
Rust
56 lines
1.9 KiB
Rust
|
//
|
||
|
use crate as github;
|
||
|
use git_next_config as config;
|
||
|
use git_next_git as git;
|
||
|
|
||
|
use kxio::network;
|
||
|
|
||
|
// https://docs.github.com/en/rest/repos/webhooks?apiVersion=2022-11-28#create-a-repository-webhook
|
||
|
pub async fn register(
|
||
|
github: &github::Github,
|
||
|
webhook_url: &config::server::WebhookUrl,
|
||
|
) -> git::forge::webhook::Result<config::RegisteredWebhook> {
|
||
|
let net = &github.net;
|
||
|
let repo_details = &github.repo_details;
|
||
|
let authorisation = config::WebhookAuth::generate();
|
||
|
let request = network::NetRequest::new(
|
||
|
network::RequestMethod::Post,
|
||
|
network::NetUrl::new(format!(
|
||
|
"https://api.github.com/repos/{}/hooks",
|
||
|
repo_details.repo_path
|
||
|
)),
|
||
|
github::webhook::headers(repo_details.forge.token()),
|
||
|
network::RequestBody::Json(network::json!({
|
||
|
"name": "web",
|
||
|
"active": true,
|
||
|
"events": ["push"],
|
||
|
"config": {
|
||
|
"url": webhook_url.as_ref(),
|
||
|
"content_type": "json",
|
||
|
"secret": authorisation.to_string(),
|
||
|
"insecure_ssl": "0",
|
||
|
}
|
||
|
})),
|
||
|
network::ResponseType::Json,
|
||
|
None,
|
||
|
network::NetRequestLogging::None,
|
||
|
);
|
||
|
let result = net.post_json::<github::GithubHook>(request).await;
|
||
|
match result {
|
||
|
Ok(response) => {
|
||
|
let Some(hook) = response.response_body() else {
|
||
|
return Err(git::forge::webhook::Error::NetworkResponseEmpty);
|
||
|
};
|
||
|
tracing::info!(webhook_id = %hook.id, "Webhook registered");
|
||
|
Ok(config::RegisteredWebhook::new(
|
||
|
config::WebhookId::new(format!("{}", hook.id)),
|
||
|
authorisation,
|
||
|
))
|
||
|
}
|
||
|
Err(e) => {
|
||
|
tracing::warn!("Failed to register webhook");
|
||
|
Err(git::forge::webhook::Error::FailedToRegister(e.to_string()))
|
||
|
}
|
||
|
}
|
||
|
}
|