feat: db: get site details from repository URL
This commit is contained in:
parent
f26075b881
commit
51e3924d71
2 changed files with 156 additions and 4 deletions
|
@ -189,6 +189,56 @@
|
||||||
},
|
},
|
||||||
"query": "SELECT repo_url, branch, hostname, owned_by, site_secret\n FROM librepages_sites\n WHERE pub_id = $1\n AND\n owned_by = (SELECT ID from librepages_users WHERE name = $2)\n AND\n deleted = false;\n "
|
"query": "SELECT repo_url, branch, hostname, owned_by, site_secret\n FROM librepages_sites\n WHERE pub_id = $1\n AND\n owned_by = (SELECT ID from librepages_users WHERE name = $2)\n AND\n deleted = false;\n "
|
||||||
},
|
},
|
||||||
|
"4445ff3226af3b5a24b255c5bb012c99b222cc7bd6dda80f232809ed273fc712": {
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"name": "repo_url",
|
||||||
|
"ordinal": 0,
|
||||||
|
"type_info": "Varchar"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "site_secret",
|
||||||
|
"ordinal": 1,
|
||||||
|
"type_info": "Varchar"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "branch",
|
||||||
|
"ordinal": 2,
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "hostname",
|
||||||
|
"ordinal": 3,
|
||||||
|
"type_info": "Varchar"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "owned_by",
|
||||||
|
"ordinal": 4,
|
||||||
|
"type_info": "Int4"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "pub_id",
|
||||||
|
"ordinal": 5,
|
||||||
|
"type_info": "Uuid"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"nullable": [
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Text"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"query": "SELECT repo_url, site_secret, branch, hostname, owned_by, pub_id\n FROM librepages_sites\n WHERE repo_url = $1\n AND deleted = false;\n "
|
||||||
|
},
|
||||||
"53f3c21c06c8d1c218537dfa9183fd0604aaf28200d8aa12e97db4ac317df39e": {
|
"53f3c21c06c8d1c218537dfa9183fd0604aaf28200d8aa12e97db4ac317df39e": {
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [
|
"columns": [
|
||||||
|
@ -422,6 +472,26 @@
|
||||||
},
|
},
|
||||||
"query": "SELECT EXISTS (SELECT 1 from librepages_users WHERE email = $1)"
|
"query": "SELECT EXISTS (SELECT 1 from librepages_users WHERE email = $1)"
|
||||||
},
|
},
|
||||||
|
"90907d6cb4ca3b485f7b583584fb5821a950362679d061e490545c76634c211e": {
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"name": "exists",
|
||||||
|
"ordinal": 0,
|
||||||
|
"type_info": "Bool"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"nullable": [
|
||||||
|
null
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Text"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"query": "SELECT EXISTS (SELECT 1 from librepages_sites WHERE repo_url = $1)"
|
||||||
|
},
|
||||||
"924e756de5544cece865a10a7e136ecc6126e3a603947264cc7899387c18c819": {
|
"924e756de5544cece865a10a7e136ecc6126e3a603947264cc7899387c18c819": {
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [],
|
"columns": [],
|
||||||
|
|
90
src/db.rs
90
src/db.rs
|
@ -276,6 +276,53 @@ impl Database {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get_site_from_repo_url(&self, repo_url: &str) -> ServiceResult<Site> {
|
||||||
|
struct S {
|
||||||
|
repo_url: String,
|
||||||
|
branch: String,
|
||||||
|
hostname: String,
|
||||||
|
owned_by: i32,
|
||||||
|
site_secret: String,
|
||||||
|
pub_id: Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
let site = sqlx::query_as!(
|
||||||
|
S,
|
||||||
|
"SELECT repo_url, site_secret, branch, hostname, owned_by, pub_id
|
||||||
|
FROM librepages_sites
|
||||||
|
WHERE repo_url = $1
|
||||||
|
AND deleted = false;
|
||||||
|
",
|
||||||
|
repo_url,
|
||||||
|
)
|
||||||
|
.fetch_one(&self.pool)
|
||||||
|
.await
|
||||||
|
.map_err(|e| map_row_not_found_err(e, ServiceError::WebsiteNotFound))?;
|
||||||
|
|
||||||
|
struct Owner {
|
||||||
|
name: String,
|
||||||
|
}
|
||||||
|
let owner = sqlx::query_as!(
|
||||||
|
Owner,
|
||||||
|
"SELECT name FROM librepages_users WHERE ID = $1",
|
||||||
|
site.owned_by
|
||||||
|
)
|
||||||
|
.fetch_one(&self.pool)
|
||||||
|
.await
|
||||||
|
.map_err(|e| map_row_not_found_err(e, ServiceError::WebsiteNotFound))?;
|
||||||
|
|
||||||
|
let site = Site {
|
||||||
|
site_secret: site.site_secret,
|
||||||
|
branch: site.branch,
|
||||||
|
hostname: site.hostname,
|
||||||
|
owner: owner.name,
|
||||||
|
repo_url: site.repo_url,
|
||||||
|
pub_id: site.pub_id,
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(site)
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn get_site_from_pub_id(&self, pub_id: Uuid, owner: String) -> ServiceResult<Site> {
|
pub async fn get_site_from_pub_id(&self, pub_id: Uuid, owner: String) -> ServiceResult<Site> {
|
||||||
struct S {
|
struct S {
|
||||||
repo_url: String,
|
repo_url: String,
|
||||||
|
@ -433,6 +480,24 @@ impl Database {
|
||||||
Ok(resp)
|
Ok(resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// check if site with repository exists
|
||||||
|
pub async fn site_with_repository_exists(&self, url: &str) -> ServiceResult<bool> {
|
||||||
|
let res = sqlx::query!(
|
||||||
|
"SELECT EXISTS (SELECT 1 from librepages_sites WHERE repo_url = $1)",
|
||||||
|
url,
|
||||||
|
)
|
||||||
|
.fetch_one(&self.pool)
|
||||||
|
.await
|
||||||
|
.map_err(map_register_err)?;
|
||||||
|
|
||||||
|
let mut resp = false;
|
||||||
|
if let Some(x) = res.exists {
|
||||||
|
resp = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(resp)
|
||||||
|
}
|
||||||
|
|
||||||
/// check if event type exists
|
/// check if event type exists
|
||||||
async fn event_type_exists(&self, event: &Event) -> ServiceResult<bool> {
|
async fn event_type_exists(&self, event: &Event) -> ServiceResult<bool> {
|
||||||
let res = sqlx::query!(
|
let res = sqlx::query!(
|
||||||
|
@ -672,7 +737,6 @@ impl Database {
|
||||||
Ok(h)
|
Ok(h)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// register a webhook against a site
|
/// register a webhook against a site
|
||||||
pub async fn webhoo_link_site(&self, auth_token: &str, repo_url: &Url) -> ServiceResult<()> {
|
pub async fn webhoo_link_site(&self, auth_token: &str, repo_url: &Url) -> ServiceResult<()> {
|
||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
|
@ -1036,7 +1100,7 @@ mod tests {
|
||||||
|
|
||||||
let site = Site {
|
let site = Site {
|
||||||
site_secret: "foobar".into(),
|
site_secret: "foobar".into(),
|
||||||
repo_url: "https://git.batsense.net/LibrePages/librepages.git".into(),
|
repo_url: "https://git.test_db_sites.example.org/LibrePages/librepages.git".into(),
|
||||||
branch: "librepages".into(),
|
branch: "librepages".into(),
|
||||||
hostname: "db_works.tests.librepages.librepages.org".into(),
|
hostname: "db_works.tests.librepages.librepages.org".into(),
|
||||||
pub_id: Uuid::new_v4(),
|
pub_id: Uuid::new_v4(),
|
||||||
|
@ -1045,12 +1109,20 @@ mod tests {
|
||||||
|
|
||||||
// test if hostname exists. Should be false
|
// test if hostname exists. Should be false
|
||||||
assert!(!db.hostname_exists(&site.hostname).await.unwrap());
|
assert!(!db.hostname_exists(&site.hostname).await.unwrap());
|
||||||
|
assert!(!db
|
||||||
|
.site_with_repository_exists(&site.repo_url)
|
||||||
|
.await
|
||||||
|
.unwrap());
|
||||||
|
|
||||||
// testing adding site
|
// testing adding site
|
||||||
db.add_site(&site).await.unwrap();
|
db.add_site(&site).await.unwrap();
|
||||||
|
|
||||||
// test if hostname exists. Should be true
|
// test if hostname exists. Should be true
|
||||||
assert!(db.hostname_exists(&site.hostname).await.unwrap());
|
assert!(db.hostname_exists(&site.hostname).await.unwrap());
|
||||||
|
assert!(db
|
||||||
|
.site_with_repository_exists(&site.repo_url)
|
||||||
|
.await
|
||||||
|
.unwrap());
|
||||||
|
|
||||||
// get site
|
// get site
|
||||||
let db_site = db.get_site(p.username, &site.hostname).await.unwrap();
|
let db_site = db.get_site(p.username, &site.hostname).await.unwrap();
|
||||||
|
@ -1062,6 +1134,12 @@ mod tests {
|
||||||
db.get_site_from_secret(&site.site_secret).await.unwrap()
|
db.get_site_from_secret(&site.site_secret).await.unwrap()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// get site by repo_url
|
||||||
|
assert_eq!(
|
||||||
|
db_site,
|
||||||
|
db.get_site_from_repo_url(&site.repo_url).await.unwrap()
|
||||||
|
);
|
||||||
|
|
||||||
// list all sites owned by user
|
// list all sites owned by user
|
||||||
let db_sites = db.list_all_sites(p.username).await.unwrap();
|
let db_sites = db.list_all_sites(p.username).await.unwrap();
|
||||||
assert_eq!(db_sites.len(), 1);
|
assert_eq!(db_sites.len(), 1);
|
||||||
|
@ -1117,8 +1195,12 @@ mod tests {
|
||||||
Some(ServiceError::WebhookNotFound)
|
Some(ServiceError::WebhookNotFound)
|
||||||
);
|
);
|
||||||
|
|
||||||
db.webhoo_link_site(&hook.auth_token, &Url::parse(&site.repo_url).unwrap()).await.unwrap();
|
db.webhoo_link_site(&hook.auth_token, &Url::parse(&site.repo_url).unwrap())
|
||||||
db.webhoo_link_site(&hook.auth_token, &Url::parse(&site.repo_url).unwrap()).await.unwrap();
|
.await
|
||||||
|
.unwrap();
|
||||||
|
db.webhoo_link_site(&hook.auth_token, &Url::parse(&site.repo_url).unwrap())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
// delete site
|
// delete site
|
||||||
db.delete_site(p.username, &site.hostname).await.unwrap();
|
db.delete_site(p.username, &site.hostname).await.unwrap();
|
||||||
|
|
Loading…
Reference in a new issue