2024-05-25 11:25:13 +01:00
|
|
|
//
|
2024-07-31 07:20:42 +01:00
|
|
|
use git_next_core::{git, server::RepoListenUrl, WebhookId};
|
2024-07-25 09:02:43 +01:00
|
|
|
|
2024-05-25 11:25:13 +01:00
|
|
|
use kxio::network;
|
|
|
|
|
|
|
|
use crate::webhook::Hook;
|
|
|
|
|
|
|
|
pub async fn list(
|
|
|
|
repo_details: &git::RepoDetails,
|
2024-07-31 07:20:42 +01:00
|
|
|
repo_listen_url: &RepoListenUrl,
|
2024-05-25 11:25:13 +01:00
|
|
|
net: &network::Network,
|
2024-07-25 09:02:43 +01:00
|
|
|
) -> git::forge::webhook::Result<Vec<WebhookId>> {
|
|
|
|
let mut ids: Vec<WebhookId> = vec![];
|
2024-05-25 11:25:13 +01:00
|
|
|
let hostname = &repo_details.forge.hostname();
|
|
|
|
let repo_path = &repo_details.repo_path;
|
|
|
|
let mut page = 1;
|
|
|
|
loop {
|
|
|
|
use secrecy::ExposeSecret;
|
|
|
|
let token = &repo_details.forge.token().expose_secret();
|
|
|
|
let url =
|
|
|
|
format!("https://{hostname}/api/v1/repos/{repo_path}/hooks?page={page}&token={token}");
|
|
|
|
let net_url = network::NetUrl::new(url);
|
|
|
|
let request = network::NetRequest::new(
|
|
|
|
network::RequestMethod::Get,
|
|
|
|
net_url,
|
|
|
|
network::NetRequestHeaders::new(),
|
|
|
|
network::RequestBody::None,
|
|
|
|
network::ResponseType::Json,
|
|
|
|
None,
|
|
|
|
network::NetRequestLogging::None,
|
|
|
|
);
|
|
|
|
let result = net.get::<Vec<Hook>>(request).await;
|
|
|
|
match result {
|
|
|
|
Ok(response) => {
|
|
|
|
if let Some(list) = response.response_body() {
|
|
|
|
if list.is_empty() {
|
|
|
|
return Ok(ids);
|
|
|
|
}
|
|
|
|
for hook in list {
|
|
|
|
if let Some(existing_url) = hook.config.get("url") {
|
2024-07-31 07:20:42 +01:00
|
|
|
if existing_url.starts_with(&repo_listen_url.to_string()) {
|
2024-05-25 11:25:13 +01:00
|
|
|
ids.push(hook.id());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
page += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
return Err(git::forge::webhook::Error::Network(e));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|