Skip to content

Commit c56ae44

Browse files
committed
Get SHA from github in merge
1 parent f7d62c7 commit c56ae44

File tree

5 files changed

+130
-28
lines changed

5 files changed

+130
-28
lines changed

fplus-database/src/database/applications.rs

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -123,30 +123,41 @@ pub async fn get_application_by_pr_number(owner: String, repo: String, pr_number
123123
* # Returns
124124
* @return Result<ApplicationModel, sea_orm::DbErr> - The result of the operation
125125
*/
126-
pub async fn merge_application_by_pr_number(owner: String, repo: String, pr_number: u64) -> Result<ApplicationModel, sea_orm::DbErr> {
126+
pub async fn merge_application_by_pr_number(owner: String, repo: String, pr_number: u64, sha: String) -> Result<ApplicationModel, sea_orm::DbErr> {
127127
let conn = get_database_connection().await?;
128-
let existing_application = get_application_by_pr_number(owner.clone(), repo.clone(), pr_number).await?;
129-
let mut active_application = existing_application.into_active_model();
130-
131-
let application_id = if let Some(id) = active_application.id.take() {
132-
id
133-
} else {
134-
return Err(DbErr::Custom("Application ID is not set".into()));
135-
};
136-
137-
// Delete the application with pr_number 0
138-
Application::delete_many()
139-
.filter(Column::Id.eq(application_id))
140-
.filter(Column::Owner.contains(owner))
141-
.filter(Column::Repo.contains(repo))
142-
.filter(Column::PrNumber.eq(0))
143-
.exec(&conn)
144-
.await?;
145-
146-
// Update the application and set pr_number to 0
147-
active_application.pr_number = Set(0);
148-
let updated_application = active_application.update(&conn).await?;
149-
Ok(updated_application)
128+
let pr_application = get_application_by_pr_number(owner.clone(), repo.clone(), pr_number).await?;
129+
let mut exists_merged = true;
130+
let merged_application = match get_application_by_pr_number(owner.clone(), repo.clone(), 0).await {
131+
Ok(application) => application.into_active_model(),
132+
Err(_) => {
133+
exists_merged = false;
134+
ActiveModel {
135+
id: Set(pr_application.id.clone()),
136+
owner: Set(owner),
137+
repo: Set(repo),
138+
pr_number: Set(0),
139+
application: Set(pr_application.application.clone()),
140+
sha: Set(Some(sha)),
141+
path: Set(pr_application.path.clone()),
142+
..Default::default()
143+
}
144+
}
145+
};
146+
147+
pr_application.delete(&conn).await?;
148+
if exists_merged {
149+
let result = merged_application.update(&conn).await;
150+
match result {
151+
Ok(application) => Ok(application),
152+
Err(e) => Err(sea_orm::DbErr::Custom(format!("Failed to merge application: {}", e))),
153+
}
154+
} else {
155+
let result = merged_application.insert(&conn).await;
156+
match result {
157+
Ok(application) => Ok(application),
158+
Err(e) => Err(sea_orm::DbErr::Custom(format!("Failed to merge application: {}", e))),
159+
}
160+
}
150161
}
151162

152163
/**

fplus-database/src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,4 +232,27 @@ mod tests {
232232

233233
}
234234

235+
/**
236+
* Test the create_application function
237+
*
238+
* # Returns
239+
* @return () - The result of the test
240+
*/
241+
#[tokio::test]
242+
#[serial]
243+
async fn test_create_application() {
244+
setup_test_environment().await;
245+
246+
let id = "test_id".to_string();
247+
let owner = "keyko-io".to_string();
248+
let repo = "great-test-library".to_string();
249+
let pr_number = 14;
250+
let app_file = "test_app_file".to_string();
251+
let sha = "test_sha".to_string();
252+
let path = "test_path".to_string();
253+
254+
let result = database::applications::create_application(id, owner, repo, pr_number, app_file, sha, path).await;
255+
assert!(result.is_ok());
256+
}
257+
235258
}

fplus-http-server/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ async fn main() -> std::io::Result<()> {
4040
.service(router::application::validate_application_trigger)
4141
.service(router::application::validate_application_proposal)
4242
.service(router::application::validate_application_approval)
43+
.service(router::application::validate_application_merge)
4344
.service(router::application::cache_renewal)
4445
.service(router::blockchain::address_allowance)
4546
.service(router::blockchain::verified_clients)

fplus-http-server/src/router/application.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,15 +231,15 @@ pub async fn validate_application_approval(
231231
}
232232
}
233233

234-
#[post("application/merge")]
235-
pub async fn merge_application(
234+
#[post("application/merge/validate")]
235+
pub async fn validate_application_merge(
236236
info: web::Json<ValidationPullRequestData>,
237237
) -> impl Responder {
238238
let ValidationPullRequestData { pr_number, user_handle: _, owner, repo } = info.into_inner();
239239
let pr_number = pr_number.trim_matches('"').parse::<u64>();
240240

241241
match pr_number {
242-
Ok(pr_number) => match LDNApplication::merge_application(pr_number, owner, repo).await {
242+
Ok(pr_number) => match LDNApplication::validate_merge_application(pr_number, owner, repo).await {
243243
Ok(result) => HttpResponse::Ok().json(result),
244244
Err(e) => HttpResponse::InternalServerError().json(e.to_string()),
245245
},

fplus-lib/src/core/mod.rs

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ impl LDNApplication {
290290
}
291291

292292
pub async fn all_applications() -> Result<Vec<(ApplicationFile, String, String)>, Vec<LDNError>> {
293+
//TODO: Avoid filtering by allocators. Simply get all active & merged applications from the database.
293294
let allocators = match database::allocators::get_allocators().await {
294295
Ok(allocs) => allocs,
295296
Err(e) => return Err(vec![LDNError::Load(format!("Failed to retrieve allocators: {}", e))]),
@@ -996,7 +997,57 @@ impl LDNApplication {
996997
Err(LDNError::Load("Failed to get application file".to_string()))
997998
}
998999

999-
pub async fn merge_application(pr_number: u64, owner: String, repo: String) -> Result<bool, LDNError> {
1000+
pub async fn validate_merge_application(pr_number: u64, owner: String, repo: String) -> Result<bool, LDNError> {
1001+
log::info!("Starting validate_merge_application:");
1002+
log::info!(
1003+
"- Validating merge for PR number {}",
1004+
pr_number,
1005+
);
1006+
1007+
let application = match LDNApplication::single_active(pr_number, owner.clone(), repo.clone()).await {
1008+
Ok(app) => {
1009+
log::info!("- Got application");
1010+
app
1011+
}
1012+
Err(err) => {
1013+
log::error!("- Failed to get application. Reason: {}", err);
1014+
return Err(LDNError::Load(format!(
1015+
"Failed to get application. Reason: {}",
1016+
err
1017+
)));
1018+
}
1019+
};
1020+
1021+
// conditions for automerge:
1022+
// 1. Application is in Granted state
1023+
// 2. Application has Validated by and Validated at fields set
1024+
// 3. Application doesn't have an active request
1025+
if application.lifecycle.get_state() == AppState::Granted {
1026+
if application.lifecycle.validated_by.is_empty() {
1027+
log::warn!("- Application has not been validated");
1028+
return Ok(false);
1029+
}
1030+
if application.lifecycle.validated_at.is_empty() {
1031+
log::warn!("- Application has not been validated at");
1032+
return Ok(false);
1033+
}
1034+
let active_request = application.allocation.active();
1035+
if active_request.is_some() {
1036+
log::warn!("- Application has an active request");
1037+
return Ok(false);
1038+
}
1039+
log::info!("- Application is in a valid state!");
1040+
1041+
Self::merge_application(pr_number, owner, repo, application.id).await?;
1042+
return Ok(true);
1043+
}
1044+
1045+
log::warn!("- Application is not in a valid state");
1046+
return Ok(false);
1047+
1048+
}
1049+
1050+
pub async fn merge_application(pr_number: u64, owner: String, repo: String, application_id: String) -> Result<bool, LDNError> {
10001051
let gh = GithubWrapper::new(owner.clone(), repo.clone());
10011052

10021053
gh.merge_pull_request(pr_number).await.map_err(|e| {
@@ -1006,7 +1057,23 @@ impl LDNApplication {
10061057
))
10071058
})?;
10081059

1009-
database::applications::merge_application_by_pr_number(owner, repo, pr_number).await.map_err(|e| {
1060+
//Get file SHA with get_file function
1061+
1062+
let file_name = LDNPullRequest::application_path(&application_id);
1063+
let branch_name = "main";
1064+
let file_sha = match GithubWrapper::new(owner.clone(), repo.clone())
1065+
.get_file(&file_name, &branch_name).await {
1066+
Ok(file) => file.items.get(0).unwrap().sha.clone(),
1067+
Err(e) => {
1068+
log::error!("- Failed to get file content. Reason: {}", e);
1069+
return Err(LDNError::Load(format!(
1070+
"Failed to get file content. Reason: {}",
1071+
e
1072+
)));
1073+
}
1074+
};
1075+
1076+
database::applications::merge_application_by_pr_number(owner, repo, pr_number, file_sha).await.map_err(|e| {
10101077
LDNError::Load(format!(
10111078
"Failed to update application in database. Reason: {}",
10121079
e

0 commit comments

Comments
 (0)