Skip to content

Commit 203ddb7

Browse files
committed
Fix sha calculation
1 parent c56ae44 commit 203ddb7

File tree

4 files changed

+20
-23
lines changed

4 files changed

+20
-23
lines changed

fplus-database/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@ once_cell = "1.8"
2222
serde = { version = "1.0.164", features = ["derive", "std",
2323
"serde_derive", "alloc", "rc"] }
2424
serial_test = "3.0.0"
25+
sha1 = "0.10.6"
26+
serde_json = "1.0.96"
2527

fplus-database/src/database/applications.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use sea_orm::{entity::*, query::*, DbErr};
22
use crate::models::applications::{Column, ActiveModel, Entity as Application, Model as ApplicationModel};
33
use crate::get_database_connection;
4+
use sha1::{Sha1, Digest};
45

56
/**
67
* Get all applications from the database
@@ -123,11 +124,12 @@ pub async fn get_application_by_pr_number(owner: String, repo: String, pr_number
123124
* # Returns
124125
* @return Result<ApplicationModel, sea_orm::DbErr> - The result of the operation
125126
*/
126-
pub async fn merge_application_by_pr_number(owner: String, repo: String, pr_number: u64, sha: String) -> Result<ApplicationModel, sea_orm::DbErr> {
127+
pub async fn merge_application_by_pr_number(owner: String, repo: String, pr_number: u64) -> Result<ApplicationModel, sea_orm::DbErr> {
127128
let conn = get_database_connection().await?;
128129
let pr_application = get_application_by_pr_number(owner.clone(), repo.clone(), pr_number).await?;
129130
let mut exists_merged = true;
130-
let merged_application = match get_application_by_pr_number(owner.clone(), repo.clone(), 0).await {
131+
132+
let mut merged_application = match get_application_by_pr_number(owner.clone(), repo.clone(), 0).await {
131133
Ok(application) => application.into_active_model(),
132134
Err(_) => {
133135
exists_merged = false;
@@ -137,14 +139,24 @@ pub async fn merge_application_by_pr_number(owner: String, repo: String, pr_numb
137139
repo: Set(repo),
138140
pr_number: Set(0),
139141
application: Set(pr_application.application.clone()),
140-
sha: Set(Some(sha)),
141142
path: Set(pr_application.path.clone()),
142143
..Default::default()
143144
}
144145
}
145146
};
146147

148+
let mut hasher = Sha1::new();
149+
let application = match pr_application.application.clone() {
150+
Some(app) => format!("blob {}\x00{}", app.len(), app),
151+
None => "".to_string()
152+
153+
};
154+
hasher.update(application.as_bytes());
155+
let file_sha = format!("{:x}", hasher.finalize());
156+
merged_application.sha = Set(Some(file_sha));
157+
147158
pr_application.delete(&conn).await?;
159+
148160
if exists_merged {
149161
let result = merged_application.update(&conn).await;
150162
match result {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ pub async fn approve(
9898
return HttpResponse::BadRequest().body(e.to_string());
9999
}
100100
};
101-
dbg!(&ldn_application);
102101
match ldn_application
103102
.complete_new_application_approval(signer, request_id, owner, repo)
104103
.await

fplus-lib/src/core/mod.rs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,7 @@ impl LDNApplication {
10381038
}
10391039
log::info!("- Application is in a valid state!");
10401040

1041-
Self::merge_application(pr_number, owner, repo, application.id).await?;
1041+
Self::merge_application(pr_number, owner, repo).await?;
10421042
return Ok(true);
10431043
}
10441044

@@ -1047,7 +1047,7 @@ impl LDNApplication {
10471047

10481048
}
10491049

1050-
pub async fn merge_application(pr_number: u64, owner: String, repo: String, application_id: String) -> Result<bool, LDNError> {
1050+
pub async fn merge_application(pr_number: u64, owner: String, repo: String) -> Result<bool, LDNError> {
10511051
let gh = GithubWrapper::new(owner.clone(), repo.clone());
10521052

10531053
gh.merge_pull_request(pr_number).await.map_err(|e| {
@@ -1057,23 +1057,7 @@ impl LDNApplication {
10571057
))
10581058
})?;
10591059

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| {
1060+
database::applications::merge_application_by_pr_number(owner, repo, pr_number).await.map_err(|e| {
10771061
LDNError::Load(format!(
10781062
"Failed to update application in database. Reason: {}",
10791063
e

0 commit comments

Comments
 (0)