store build logs in db
endpoint to get logs checks if pkg already in db force build flag
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
use crate::aur::aur::get_info_by_name;
|
||||
use crate::builder::types::Action;
|
||||
use crate::db::prelude::{Packages, Versions};
|
||||
use crate::db::{packages, versions};
|
||||
use rocket::response::status::NotFound;
|
||||
use rocket::serde::json::Json;
|
||||
use rocket::serde::Deserialize;
|
||||
use rocket::{post, State};
|
||||
use rocket_okapi::okapi::schemars;
|
||||
use rocket_okapi::{openapi, JsonSchema};
|
||||
use sea_orm::ActiveModelTrait;
|
||||
use sea_orm::{ActiveModelTrait, ColumnTrait, EntityTrait, QueryFilter};
|
||||
use sea_orm::{DatabaseConnection, Set};
|
||||
use tokio::sync::broadcast::Sender;
|
||||
|
||||
@@ -14,6 +16,7 @@ use tokio::sync::broadcast::Sender;
|
||||
#[serde(crate = "rocket::serde")]
|
||||
pub struct AddBody {
|
||||
name: String,
|
||||
force_build: bool,
|
||||
}
|
||||
|
||||
#[openapi(tag = "test")]
|
||||
@@ -22,29 +25,56 @@ pub async fn package_add(
|
||||
db: &State<DatabaseConnection>,
|
||||
input: Json<AddBody>,
|
||||
tx: &State<Sender<Action>>,
|
||||
) -> Result<(), String> {
|
||||
) -> Result<(), NotFound<String>> {
|
||||
let db = db as &DatabaseConnection;
|
||||
let pkg_name = &input.name;
|
||||
|
||||
let pkg = get_info_by_name(pkg_name)
|
||||
let pkt_model = match Packages::find()
|
||||
.filter(packages::Column::Name.eq(input.name.clone()))
|
||||
.one(db)
|
||||
.await
|
||||
.map_err(|_| "couldn't download package metadata".to_string())?;
|
||||
.map_err(|e| NotFound(e.to_string()))?
|
||||
{
|
||||
None => {
|
||||
let new_package = packages::ActiveModel {
|
||||
name: Set(input.name.clone()),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let new_package = packages::ActiveModel {
|
||||
name: Set(pkg_name.clone()),
|
||||
..Default::default()
|
||||
new_package.save(db).await.expect("TODO: panic message")
|
||||
}
|
||||
Some(p) => p.into(),
|
||||
};
|
||||
|
||||
let pkt_model = new_package.save(db).await.expect("TODO: panic message");
|
||||
let pkg = get_info_by_name(input.name.clone().as_str())
|
||||
.await
|
||||
.map_err(|_| NotFound("couldn't download package metadata".to_string()))?;
|
||||
|
||||
let new_version = versions::ActiveModel {
|
||||
version: Set(pkg.version.clone()),
|
||||
package_id: Set(pkt_model.id.clone().unwrap()),
|
||||
..Default::default()
|
||||
let version_model = match Versions::find()
|
||||
.filter(versions::Column::Version.eq(pkg.version.clone()))
|
||||
.one(db)
|
||||
.await
|
||||
.map_err(|e| NotFound(e.to_string()))?
|
||||
{
|
||||
None => {
|
||||
let new_version = versions::ActiveModel {
|
||||
version: Set(pkg.version.clone()),
|
||||
package_id: Set(pkt_model.id.clone().unwrap()),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
new_version.save(db).await.expect("TODO: panic message")
|
||||
}
|
||||
Some(p) => {
|
||||
// todo add check if this version was successfully built
|
||||
// if not allow build
|
||||
if input.force_build {
|
||||
p.into()
|
||||
} else {
|
||||
return Err(NotFound("Version already existing".to_string()));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let version_model = new_version.save(db).await.expect("TODO: panic message");
|
||||
|
||||
let _ = tx.send(Action::Build(
|
||||
pkg.name,
|
||||
pkg.version,
|
||||
|
@@ -1,7 +1,9 @@
|
||||
use crate::api::add::okapi_add_operation_for_package_add_;
|
||||
use crate::api::add::package_add;
|
||||
use crate::api::list::okapi_add_operation_for_package_list_;
|
||||
use crate::api::list::okapi_add_operation_for_search_;
|
||||
use crate::api::list::okapi_add_operation_for_build_output_;
|
||||
use crate::api::list::okapi_add_operation_for_list_builds_;
|
||||
use crate::api::list::{build_output, okapi_add_operation_for_package_list_};
|
||||
use crate::api::list::{list_builds, okapi_add_operation_for_search_};
|
||||
use crate::api::list::{package_list, search};
|
||||
use crate::api::remove::okapi_add_operation_for_package_del_;
|
||||
use crate::api::remove::okapi_add_operation_for_version_del_;
|
||||
@@ -10,5 +12,13 @@ use rocket::Route;
|
||||
use rocket_okapi::openapi_get_routes;
|
||||
|
||||
pub fn build_api() -> Vec<Route> {
|
||||
openapi_get_routes![search, package_list, package_add, package_del, version_del]
|
||||
openapi_get_routes![
|
||||
search,
|
||||
package_list,
|
||||
package_add,
|
||||
package_del,
|
||||
version_del,
|
||||
build_output,
|
||||
list_builds
|
||||
]
|
||||
}
|
||||
|
@@ -1,13 +1,14 @@
|
||||
use crate::aur::aur::query_aur;
|
||||
use crate::db::migration::JoinType;
|
||||
use crate::db::prelude::Packages;
|
||||
use crate::db::{packages, versions};
|
||||
use crate::db::prelude::{Builds, Packages};
|
||||
use crate::db::{builds, packages, versions};
|
||||
use rocket::response::status::NotFound;
|
||||
use rocket::serde::json::Json;
|
||||
use rocket::serde::{Deserialize, Serialize};
|
||||
use rocket::{get, State};
|
||||
use rocket_okapi::okapi::schemars;
|
||||
use rocket_okapi::{openapi, JsonSchema};
|
||||
use sea_orm::ColumnTrait;
|
||||
use sea_orm::{ColumnTrait, QueryFilter};
|
||||
use sea_orm::{DatabaseConnection, EntityTrait, FromQueryResult, QuerySelect, RelationTrait};
|
||||
|
||||
#[derive(Serialize, JsonSchema)]
|
||||
@@ -20,7 +21,7 @@ pub struct ApiPackage {
|
||||
#[openapi(tag = "test")]
|
||||
#[get("/search?<query>")]
|
||||
pub async fn search(query: &str) -> Result<Json<Vec<ApiPackage>>, String> {
|
||||
match query_aur(query).await {
|
||||
return match query_aur(query).await {
|
||||
Ok(v) => {
|
||||
let mapped = v
|
||||
.iter()
|
||||
@@ -29,19 +30,19 @@ pub async fn search(query: &str) -> Result<Json<Vec<ApiPackage>>, String> {
|
||||
version: x.version.clone(),
|
||||
})
|
||||
.collect();
|
||||
return Ok(Json(mapped));
|
||||
Ok(Json(mapped))
|
||||
}
|
||||
Err(e) => {
|
||||
return Err(format!("{}", e));
|
||||
}
|
||||
}
|
||||
Err(e) => Err(format!("{}", e)),
|
||||
};
|
||||
}
|
||||
|
||||
#[derive(FromQueryResult, Deserialize, JsonSchema, Serialize)]
|
||||
#[serde(crate = "rocket::serde")]
|
||||
pub struct ListPackageModel {
|
||||
id: i32,
|
||||
name: String,
|
||||
count: i32,
|
||||
status: i32,
|
||||
}
|
||||
|
||||
#[openapi(tag = "test")]
|
||||
@@ -56,6 +57,8 @@ pub async fn package_list(
|
||||
.select_only()
|
||||
.column_as(versions::Column::Id.count(), "count")
|
||||
.column(packages::Column::Name)
|
||||
.column(packages::Column::Id)
|
||||
.column(packages::Column::Status)
|
||||
.group_by(packages::Column::Name)
|
||||
.into_model::<ListPackageModel>()
|
||||
.all(db)
|
||||
@@ -64,3 +67,56 @@ pub async fn package_list(
|
||||
|
||||
Ok(Json(all))
|
||||
}
|
||||
|
||||
#[openapi(tag = "test")]
|
||||
#[get("/builds/output?<buildid>")]
|
||||
pub async fn build_output(
|
||||
db: &State<DatabaseConnection>,
|
||||
buildid: i32,
|
||||
) -> Result<String, NotFound<String>> {
|
||||
let db = db as &DatabaseConnection;
|
||||
|
||||
let build = Builds::find_by_id(buildid)
|
||||
.one(db)
|
||||
.await
|
||||
.map_err(|e| NotFound(e.to_string()))?
|
||||
.ok_or(NotFound("couldn't find id".to_string()))?;
|
||||
|
||||
build.ouput.ok_or(NotFound("No Output".to_string()))
|
||||
}
|
||||
|
||||
#[derive(FromQueryResult, Deserialize, JsonSchema, Serialize)]
|
||||
#[serde(crate = "rocket::serde")]
|
||||
pub struct ListBuildsModel {
|
||||
id: i32,
|
||||
pkg_id: i32,
|
||||
version_id: i32,
|
||||
status: Option<i32>,
|
||||
}
|
||||
|
||||
#[openapi(tag = "test")]
|
||||
#[get("/builds?<pkgid>")]
|
||||
pub async fn list_builds(
|
||||
db: &State<DatabaseConnection>,
|
||||
pkgid: i32,
|
||||
) -> Result<Json<Vec<ListBuildsModel>>, NotFound<String>> {
|
||||
let db = db as &DatabaseConnection;
|
||||
|
||||
let build = Builds::find()
|
||||
.filter(builds::Column::PkgId.eq(pkgid))
|
||||
.all(db)
|
||||
.await
|
||||
.map_err(|e| NotFound(e.to_string()))?;
|
||||
|
||||
Ok(Json(
|
||||
build
|
||||
.iter()
|
||||
.map(|x| ListBuildsModel {
|
||||
id: x.id,
|
||||
status: x.status,
|
||||
pkg_id: x.pkg_id,
|
||||
version_id: x.version_id,
|
||||
})
|
||||
.collect::<Vec<_>>(),
|
||||
))
|
||||
}
|
||||
|
Reference in New Issue
Block a user