63 lines
2.2 KiB
Rust
63 lines
2.2 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 repo_details = &github.repo_details;
|
|
if repo_details.repo_config.is_none() {
|
|
return Err(git::forge::webhook::Error::NoRepoConfig);
|
|
};
|
|
|
|
let net = &github.net;
|
|
|
|
let hostname = repo_details.forge.hostname();
|
|
let authorisation = config::WebhookAuth::generate();
|
|
let request = network::NetRequest::new(
|
|
network::RequestMethod::Post,
|
|
network::NetUrl::new(format!(
|
|
"https://api.{hostname}/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 {
|
|
#[cfg(not(tarpaulin_include))]
|
|
// request response is Json so response_body never returns None
|
|
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()))
|
|
}
|
|
}
|
|
}
|