Skip to content

Commit

Permalink
Issue modification creates an application if do not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
Filip Lelek committed Jul 22, 2024
1 parent 608337e commit cb237c5
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 42 deletions.
35 changes: 32 additions & 3 deletions fplus-database/src/database/applications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ pub async fn get_applications() -> Result<Vec<ApplicationModel>, sea_orm::DbErr>
a.id,
a.owner,
a.repo,
a.pr_number,
a.pr_number,
a.issue_number,
a.application,
a.updated_at,
a.sha,
a.path
a.path
FROM
applications a
ORDER BY
Expand All @@ -50,6 +51,10 @@ pub async fn get_applications() -> Result<Vec<ApplicationModel>, sea_orm::DbErr>
owner: app.get("owner").unwrap().as_str().unwrap().to_string(),
repo: app.get("repo").unwrap().as_str().unwrap().to_string(),
pr_number: app.get("pr_number").unwrap().as_i64().unwrap(),
issue_number: app
.get("issue_number")
.expect("Issue number should be in the application data")
.as_i64(),
application: Some(
app.get("application")
.unwrap()
Expand Down Expand Up @@ -323,7 +328,29 @@ pub async fn update_application(
active_application.sha = Set(Some(file_sha));
if let Some(path) = path {
active_application.path = Set(Some(path));
}
};
let updated_application = active_application.update(&conn).await?;
Ok(updated_application)
}
Err(_) => Err(sea_orm::DbErr::Custom(
"Failed to find the application to update.".into(),
)),
}
}

pub async fn update_application_issue_number(
owner: String,
repo: String,
pr_number: u64,
issue_number: Option<i64>,
) -> Result<ApplicationModel, sea_orm::DbErr> {
let conn = get_database_connection().await?;

match get_application_by_pr_number(owner.clone(), repo.clone(), pr_number).await {
Ok(existing_application) => {
let mut active_application: ActiveModel = existing_application.into_active_model();
active_application.issue_number = Set(issue_number);

let updated_application = active_application.update(&conn).await?;
Ok(updated_application)
}
Expand Down Expand Up @@ -353,6 +380,7 @@ pub async fn create_application(
owner: String,
repo: String,
pr_number: u64,
issue_number: i64,
app_file: String,
path: String,
) -> Result<ApplicationModel, sea_orm::DbErr> {
Expand All @@ -368,6 +396,7 @@ pub async fn create_application(
owner: Set(owner),
repo: Set(repo),
pr_number: Set(pr_number as i64),
issue_number: Set(Some(issue_number)),
application: Set(Some(app_file)),
sha: Set(Some(file_sha)),
path: Set(Some(path)),
Expand Down
2 changes: 2 additions & 0 deletions fplus-database/src/models/applications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ pub struct Model {
pub repo: String,
#[sea_orm(primary_key, auto_increment = false)]
pub pr_number: i64,
#[sea_orm(nullable)]
pub issue_number: Option<i64>,
#[sea_orm(column_type = "Text", nullable)]
pub application: Option<String>,
#[sea_orm(nullable)]
Expand Down
1 change: 1 addition & 0 deletions fplus-http-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ async fn main() -> std::io::Result<()> {
.service(router::application::cache_renewal)
.service(router::application::update_from_issue)
.service(router::application::trigger_ssa)
.service(router::application::update_application_issue_number)
.service(router::application::submit_kyc)
.service(router::blockchain::address_allowance)
.service(router::blockchain::verified_clients)
Expand Down
10 changes: 9 additions & 1 deletion fplus-http-server/src/router/application.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use actix_web::{get, post, web, HttpResponse, Responder};
use actix_web::{get, post, put, web, HttpResponse, Responder};
use fplus_lib::core::{
application::file::VerifierInput, ApplicationQueryParams, BranchDeleteInfo,
CompleteGovernanceReviewInfo, CompleteNewApplicationApprovalInfo,
Expand Down Expand Up @@ -498,3 +498,11 @@ pub async fn remove_pending_allocation(
Err(e) => HttpResponse::BadRequest().body(e.to_string()),
}
}

#[put("/application/update_application_issue_number")]
pub async fn update_application_issue_number() -> actix_web::Result<impl Responder> {
match LDNApplication::update_application_issue_number().await {
Ok(result) => Ok(HttpResponse::Ok().json(result)),
Err(e) => Ok(HttpResponse::BadRequest().body(e.to_string())),
}
}
101 changes: 63 additions & 38 deletions fplus-lib/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -759,11 +759,18 @@ impl LDNApplication {
Ok(prs) => {
if let Some(pr) = prs.first() {
let number = pr.number;
let issue_number = issue_number.parse::<i64>().map_err(|e| {
LDNError::New(format!(
"Parse issue number: {} to i64 failed. {}",
issue_number, e
))
})?;
database::applications::create_application(
application_id.clone(),
info.owner.clone(),
info.repo.clone(),
number,
issue_number,
file_content,
LDNPullRequest::application_path(&app_id),
)
Expand Down Expand Up @@ -2359,11 +2366,18 @@ impl LDNApplication {
})?;
}
Err(_) => {
let _ = database::applications::create_application(
let issue_number = application_file.issue_number.parse::<i64>().map_err(|e| {
LDNError::New(format!(
"Parse issue number: {} to i64 failed. {}",
application_file.issue_number, e
))
})?;
database::applications::create_application(
application_id,
owner.clone(),
repo.clone(),
pr_number,
issue_number,
file_content.clone(),
filename.clone(),
)
Expand Down Expand Up @@ -2574,26 +2588,10 @@ impl LDNApplication {
{
Ok(app) => app,
Err(e) => {
log::warn!("Failed to get application model: {}", e);
log::warn!("Failed to get application model: {}. ", e);

//Application Id has not been found. That means the user has modified the wallet address
LDNApplication::issue_error(
issue_number.clone(),
info.owner.clone(),
info.repo.clone(),
"Application not found. If you have modified the wallet address, please create a new application."
).await?;
LDNApplication::add_error_label(
issue_number.clone(),
"".to_string(),
info.owner.clone(),
info.repo.clone(),
)
.await?;
return Err(LDNError::New(format!(
"Failed to get application model: {}",
e
)));
return Self::new_from_issue(info).await;
}
};

Expand Down Expand Up @@ -3272,25 +3270,6 @@ Your Datacap Allocation Request has been {} by the Notary
Ok(true)
}

async fn issue_error(
issue_number: String,
owner: String,
repo: String,
message: &str,
) -> Result<bool, LDNError> {
let gh = github_async_new(owner, repo).await;
gh.add_comment_to_issue(issue_number.parse().unwrap(), message)
.await
.map_err(|e| {
LDNError::New(format!(
"Error adding comment to issue {} /// {}",
issue_number, e
))
})
.unwrap();
Ok(true)
}

async fn issue_additional_info_required(
issue_number: String,
owner: String,
Expand Down Expand Up @@ -3499,12 +3478,23 @@ _The initial issue can be edited in order to solve the request of the verifier.
if !db_apps_set.contains(&gh_app.application_file.id)
&& !processed_gh_apps.contains(&gh_app.application_file.id)
{
let issue_number = gh_app
.application_file
.issue_number
.parse::<i64>()
.map_err(|e| {
LDNError::New(format!(
"Parse issue number: {} to i64 failed. {}",
gh_app.application_file.issue_number, e
))
})?;
// Call the create_application function if the GH app is not in DB
database::applications::create_application(
gh_app.application_file.id.clone(),
owner.clone(),
repo.clone(),
gh_app.pr_number,
issue_number,
serde_json::to_string_pretty(&gh_app.application_file).unwrap(),
gh_app.path,
)
Expand Down Expand Up @@ -3569,12 +3559,23 @@ _The initial issue can be edited in order to solve the request of the verifier.
if !db_apps_set.contains(&gh_app.application_file.id)
&& !processed_gh_apps.contains(&gh_app.application_file.id)
{
let issue_number = gh_app
.application_file
.issue_number
.parse::<i64>()
.map_err(|e| {
LDNError::New(format!(
"Parse issue number: {} to i64 failed. {}",
gh_app.application_file.issue_number, e
))
})?;
// Call the create_application function if the GH app is not in DB
database::applications::create_application(
gh_app.application_file.id.clone(),
owner.clone(),
repo.clone(),
0,
issue_number,
serde_json::to_string_pretty(&gh_app.application_file).unwrap(),
gh_app.path,
)
Expand Down Expand Up @@ -4134,6 +4135,26 @@ _The initial issue can be edited in order to solve the request of the verifier.
})?;
Ok(())
}
pub async fn update_application_issue_number() -> Result<(), LDNError> {
let applications = database::applications::get_applications()
.await
.map_err(|e| LDNError::New(format!("Getting all aplication failed: {}", e)))?;
for app in applications {
if let Some(app_str) = app.application {
let application_file = serde_json::from_str::<ApplicationFile>(&app_str).unwrap();
let issue_number = application_file.issue_number.parse::<i64>().ok();
database::applications::update_application_issue_number(
app.owner,
app.repo,
app.pr_number as u64,
issue_number,
)
.await
.map_err(|e| LDNError::New(format!("Update issue number failed: {}", e)))?;
}
}
Ok(())
}
}

#[derive(Serialize, Deserialize, Debug)]
Expand Down Expand Up @@ -4237,11 +4258,15 @@ impl LDNPullRequest {
{
Ok(pr) => {
if should_create_in_db {
let issue_number = issue_number.parse::<i64>().map_err(|e| {
LDNError::New(format!("Parse issue number to i64 failed: {}", e))
})?;
database::applications::create_application(
application_id.clone(),
owner,
repo,
pr.0.number,
issue_number,
file_content,
file_name,
)
Expand Down

0 comments on commit cb237c5

Please sign in to comment.