Skip to content

Commit 2fdee1e

Browse files
committed
Merge branch 'main' of github.com:filecoin-project/filplus-backend into feat/security
2 parents a800d21 + 72672f3 commit 2fdee1e

File tree

2 files changed

+103
-56
lines changed

2 files changed

+103
-56
lines changed

fplus-database/src/database/applications.rs

Lines changed: 77 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,61 @@
1-
use sea_orm::{entity::*, query::*, DbErr};
1+
use sea_orm::{entity::*, query::*, DbBackend, DbErr};
22
use crate::models::applications::{Column, ActiveModel, Entity as Application, Model as ApplicationModel};
33
use crate::get_database_connection;
44
use sha1::{Sha1, Digest};
5+
use chrono::{DateTime, Utc, TimeZone};
56

67
/**
78
* Get all applications from the database
89
*
910
* # Returns
1011
* @return Result<Vec<ApplicationModel>, sea_orm::DbErr> - The result of the operation
1112
*/
12-
pub async fn get_applications() ->Result<Vec<ApplicationModel>, sea_orm::DbErr> {
13+
pub async fn get_applications() -> Result<Vec<ApplicationModel>, sea_orm::DbErr> {
1314
let conn = get_database_connection().await?;
14-
Application::find().all(&conn).await
15+
16+
//Get all applications from the database.
17+
//Distinct on is not supported in sea_orm yet, so we have to use raw SQL
18+
let app_data = JsonValue::find_by_statement(Statement::from_sql_and_values(
19+
DbBackend::Postgres,
20+
r#"
21+
SELECT DISTINCT ON (owner, repo, id)
22+
a.id,
23+
a.owner,
24+
a.repo,
25+
a.pr_number,
26+
a.application,
27+
a.updated_at,
28+
a.sha,
29+
a.path
30+
FROM
31+
applications a
32+
ORDER BY
33+
a.owner,
34+
a.repo,
35+
a.id,
36+
a.pr_number DESC
37+
"#,
38+
[],
39+
))
40+
.all(&conn)
41+
.await?;
42+
43+
//Iterate over the results and convert them to ApplicationModel
44+
let mut applications: Vec<ApplicationModel> = Vec::new();
45+
for app in app_data {
46+
applications.push(ApplicationModel {
47+
id: app.get("id").unwrap().as_str().unwrap().to_string(),
48+
owner: app.get("owner").unwrap().as_str().unwrap().to_string(),
49+
repo: app.get("repo").unwrap().as_str().unwrap().to_string(),
50+
pr_number: app.get("pr_number").unwrap().as_i64().unwrap() as i64,
51+
application: Some(app.get("application").unwrap().as_str().unwrap().to_string()),
52+
updated_at: Utc.from_utc_datetime(&app.get("updated_at").unwrap().as_str().unwrap().parse::<DateTime<Utc>>().unwrap().naive_utc()),
53+
sha: Some(app.get("sha").unwrap().as_str().unwrap().to_string()),
54+
path: Some(app.get("path").unwrap().as_str().unwrap().to_string()),
55+
});
56+
}
57+
Ok(applications)
58+
1559
}
1660

1761
/**
@@ -24,12 +68,22 @@ pub async fn get_applications() ->Result<Vec<ApplicationModel>, sea_orm::DbErr>
2468
* # Returns
2569
* @return Result<Vec<ApplicationModel>, sea_orm::DbErr> - The result of the operation
2670
*/
27-
pub async fn get_merged_applications(owner: String, repo: String) -> Result<Vec<ApplicationModel>, sea_orm::DbErr> {
71+
pub async fn get_merged_applications(owner: Option<String>, repo: Option<String>) -> Result<Vec<ApplicationModel>, sea_orm::DbErr> {
2872
let conn = get_database_connection().await?;
29-
Application::find()
30-
.filter(Column::Owner.contains(owner))
31-
.filter(Column::Repo.contains(repo))
32-
.filter(Column::PrNumber.eq(0))
73+
let mut query = Application::find()
74+
.filter(Column::PrNumber.eq(0));
75+
if let Some(owner) = owner.clone() {
76+
query = query.filter(Column::Owner.contains(owner));
77+
}
78+
if let Some(repo) = repo {
79+
if owner.is_none() {
80+
return Err(DbErr::Custom(format!("Owner is required to get merged applications").into()));
81+
}
82+
query = query.filter(Column::Repo.contains(repo));
83+
}
84+
query
85+
.order_by(Column::Owner, Order::Asc)
86+
.order_by(Column::Repo, Order::Asc)
3387
.all(&conn)
3488
.await
3589
}
@@ -44,12 +98,22 @@ pub async fn get_merged_applications(owner: String, repo: String) -> Result<Vec<
4498
* # Returns
4599
* @return Result<Vec<ApplicationModel>, sea_orm::DbErr> - The result of the operation
46100
*/
47-
pub async fn get_active_applications(owner: String, repo: String) -> Result<Vec<ApplicationModel>, sea_orm::DbErr> {
101+
pub async fn get_active_applications(owner: Option<String>, repo: Option<String>) -> Result<Vec<ApplicationModel>, sea_orm::DbErr> {
48102
let conn = get_database_connection().await?;
49-
Application::find()
50-
.filter(Column::Owner.contains(owner))
51-
.filter(Column::Repo.contains(repo))
52-
.filter(Column::PrNumber.ne(0))
103+
let mut query = Application::find()
104+
.filter(Column::PrNumber.ne(0));
105+
if let Some(owner) = owner.clone() {
106+
query = query.filter(Column::Owner.contains(owner));
107+
}
108+
if let Some(repo) = repo {
109+
if owner.is_none() {
110+
return Err(DbErr::Custom(format!("Owner is required to get merged applications").into()));
111+
}
112+
query = query.filter(Column::Repo.contains(repo));
113+
}
114+
query
115+
.order_by(Column::Owner, Order::Asc)
116+
.order_by(Column::Repo, Order::Asc)
53117
.all(&conn)
54118
.await
55119
}

fplus-lib/src/core/mod.rs

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -286,50 +286,32 @@ impl LDNApplication {
286286
}
287287

288288
pub async fn all_applications() -> Result<Vec<(ApplicationFile, String, String)>, Vec<LDNError>> {
289-
//TODO: Avoid filtering by allocators. Simply get all active & merged applications from the database.
290-
let allocators = match database::allocators::get_allocators().await {
291-
Ok(allocs) => allocs,
292-
Err(e) => return Err(vec![LDNError::Load(format!("Failed to retrieve allocators: {}", e))]),
293-
}; let mut all_apps: Vec<(ApplicationFile, String, String)> = Vec::new();
294-
let mut errors: Vec<LDNError> = Vec::new();
295-
296-
for allocator in allocators {
297-
match Self::active(allocator.owner.clone(), allocator.repo.clone(), None).await {
298-
Ok(apps) => {
299-
for app in apps {
300-
all_apps.push((app, allocator.owner.clone(), allocator.repo.clone()));
301-
}
302-
},
303-
Err(e) => {
304-
errors.push(e);
305-
eprintln!("Failed to process active applications for allocator {}:{}", allocator.repo, allocator.owner);
306-
},
307-
}
308-
309-
match Self::merged(allocator.owner.clone(), allocator.repo.clone()).await {
310-
Ok(merged_apps) => {
311-
for (_, app) in merged_apps {
312-
all_apps.push((app, allocator.owner.clone(), allocator.repo.clone()));
313-
}
314-
},
315-
Err(e) => {
316-
errors.push(e);
317-
eprintln!("Failed to process merged applications for allocator {}:{}", allocator.repo, allocator.owner);
318-
},
319-
}
320-
}
321-
322-
if errors.is_empty() {
323-
Ok(all_apps)
324-
} else {
325-
Err(errors)
289+
let db_apps = database::applications::get_applications().await;
290+
let mut all_apps: Vec<(ApplicationFile, String, String)> = Vec::new();
291+
match db_apps {
292+
Ok(apps) => {
293+
for app in apps {
294+
let app_file = match ApplicationFile::from_str(&app.application.unwrap()) {
295+
Ok(app) => app,
296+
Err(e) => {
297+
return Err(vec![LDNError::Load(format!("Failed to parse application file from DB /// {}", e))]);
298+
}
299+
};
300+
all_apps.push((app_file, app.owner, app.repo));
301+
}
302+
return Ok(all_apps);
303+
},
304+
Err(e) => {
305+
return Err(vec![LDNError::Load(format!("Failed to retrieve applications from the database /// {}", e))]);
306+
},
326307
}
308+
327309
}
328310

329311

330312
pub async fn active(owner: String, repo: String, filter: Option<String>) -> Result<Vec<ApplicationFile>, LDNError> {
331313
// Get all active applications from the database.
332-
let active_apps_result = database::applications::get_active_applications(owner, repo).await;
314+
let active_apps_result = database::applications::get_active_applications(Some(owner), Some(repo)).await;
333315

334316
// Handle errors in getting active applications.
335317
let active_apps = match active_apps_result {
@@ -351,7 +333,8 @@ impl LDNApplication {
351333
if let Some(app_json) = app_model.application {
352334
match from_str::<ApplicationFile>(&app_json) {
353335
Ok(app) => apps.push(app),
354-
Err(e) => return Err(LDNError::Load(format!("Failed to parse application file: {}", e))),
336+
//if error, don't push into apps
337+
Err(_) => {}
355338
}
356339
}
357340
}
@@ -930,7 +913,7 @@ impl LDNApplication {
930913

931914
pub async fn merged(owner: String, repo: String) -> Result<Vec<(ApplicationGithubInfo, ApplicationFile)>, LDNError> {
932915
// Retrieve all applications in the main branch from the database.
933-
let merged_apps_result = database::applications::get_merged_applications(owner.clone(), repo.clone()).await;
916+
let merged_apps_result = database::applications::get_merged_applications(Some(owner.clone()), Some(repo.clone())).await;
934917

935918
// Handle errors in getting applications from the main branch.
936919
let merged_app_models = match merged_apps_result {
@@ -946,7 +929,7 @@ impl LDNApplication {
946929
if let Some(app_json) = app_model.application {
947930
match from_str::<ApplicationFile>(&app_json) {
948931
Ok(app) => merged_apps.push((ApplicationGithubInfo {sha: app_model.sha.unwrap(), path: app_model.path.unwrap()}, app)),
949-
Err(e) => return Err(LDNError::Load(format!("Failed to parse application file: {}", e))),
932+
Err(_) => {},
950933
}
951934
}
952935
}
@@ -1803,7 +1786,7 @@ Your Datacap Allocation Request has been {} by the Notary
18031786

18041787
pub async fn cache_renewal_active(owner: String, repo: String) -> Result<(), LDNError> {
18051788
let active_from_gh: Vec<ApplicationFileWithDate> = LDNApplication::active_apps_with_last_update(owner.clone(), repo.clone(), None).await?;
1806-
let active_from_db: Vec<ApplicationModel> = database::applications::get_active_applications(owner.clone(), repo.clone()).await.unwrap();
1789+
let active_from_db: Vec<ApplicationModel> = database::applications::get_active_applications(Some(owner.clone()), Some(repo.clone())).await.unwrap();
18071790

18081791
let mut db_apps_set: HashSet<String> = HashSet::new();
18091792
let mut processed_gh_apps: HashSet<String> = HashSet::new();
@@ -1860,7 +1843,7 @@ Your Datacap Allocation Request has been {} by the Notary
18601843

18611844
pub async fn cache_renewal_merged(owner: String, repo: String) -> Result<(), LDNError> {
18621845
let merged_from_gh: Vec<ApplicationFileWithDate> = LDNApplication::merged_apps_with_last_update(owner.clone(), repo.clone(), None).await?;
1863-
let merged_from_db: Vec<ApplicationModel> = database::applications::get_merged_applications(owner.clone(), repo.clone()).await.unwrap();
1846+
let merged_from_db: Vec<ApplicationModel> = database::applications::get_merged_applications(Some(owner.clone()), Some(repo.clone())).await.unwrap();
18641847

18651848
let mut db_apps_set: HashSet<String> = HashSet::new();
18661849
let mut processed_gh_apps: HashSet<String> = HashSet::new();

0 commit comments

Comments
 (0)