Skip to content

Commit 67fcec6

Browse files
Filip-Lfilip-neti
andauthored
Create separate function to update installation id (#236)
Co-authored-by: Filip Lelek <[email protected]>
1 parent 6d2a8f9 commit 67fcec6

File tree

3 files changed

+55
-29
lines changed

3 files changed

+55
-29
lines changed

fplus-database/src/database/allocators.rs

+25
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,31 @@ pub async fn create_or_update_allocator(
207207
}
208208
}
209209

210+
/**
211+
* Update installation ID for allocator in the database
212+
*
213+
* # Arguments
214+
* @param owner: String - The owner of the repository
215+
* @param repo: String - The repository name
216+
* @param installation_id: Option<i64> - The installation ID
217+
*/
218+
pub async fn update_allocator_installation_ids(
219+
owner: String,
220+
repo: String,
221+
installation_id: Option<i64>,
222+
) -> Result<(), sea_orm::DbErr> {
223+
let existing_allocator = get_allocator(&owner, &repo).await?;
224+
if let Some(allocator_model) = existing_allocator {
225+
let conn = get_database_connection().await?;
226+
let mut allocator_active_model = allocator_model.into_active_model();
227+
if installation_id.is_some() {
228+
allocator_active_model.installation_id = Set(installation_id);
229+
}
230+
allocator_active_model.update(&conn).await?;
231+
}
232+
Ok(())
233+
}
234+
210235
/**
211236
* Update the multisig threshold of an allocator in the database
212237
*

fplus-http-server/src/main.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ async fn main() -> std::io::Result<()> {
5757

5858
tokio::spawn(async {
5959
run_cron("0 0 0,4,8,12,16,20 * * * *", || {
60-
tokio::spawn(update_installation_ids_logic())
60+
tokio::spawn(async {
61+
if let Err(e) = update_installation_ids_logic().await {
62+
eprintln!("Error: {:?}", e);
63+
}
64+
})
6165
})
6266
.await;
6367
});

fplus-lib/src/core/allocator/mod.rs

+25-28
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ use crate::helpers::process_amount;
22
use fplus_database::database::allocation_amounts::{
33
create_allocation_amount, delete_allocation_amounts_by_allocator_id,
44
};
5-
use fplus_database::database::allocators::{create_or_update_allocator, get_allocators};
5+
use fplus_database::database::allocators::{
6+
create_or_update_allocator, get_allocators, update_allocator_installation_ids,
7+
};
68
use fplus_database::models::allocators::Model;
79
use octocrab::auth::create_jwt;
810
use octocrab::models::repos::{Content, ContentItems};
@@ -365,40 +367,34 @@ pub async fn fetch_repositories_for_installation_id(
365367
Ok(repositories)
366368
}
367369

368-
pub async fn update_installation_ids_in_db(installation: InstallationRepositories) {
370+
pub async fn update_installation_ids_in_db(
371+
installation: InstallationRepositories,
372+
) -> Result<(), LDNError> {
369373
let installation_id = installation.installation_id;
370374
for repo in installation.repositories.iter() {
371-
let owner = repo.owner.clone();
372-
let repo = repo.slug.clone();
373-
let _ = create_or_update_allocator(
374-
owner,
375-
repo,
375+
update_allocator_installation_ids(
376+
repo.owner.clone(),
377+
repo.slug.clone(),
376378
Some(installation_id.try_into().unwrap()),
377-
None,
378-
None,
379-
None,
380-
None,
381-
None,
382-
None,
383-
None,
384-
None,
385-
None,
386-
None,
387-
None,
388379
)
389-
.await;
380+
.await
381+
.map_err(|e| {
382+
LDNError::Load(format!(
383+
"Failed to update installation id in database for repo: {} {} /// {}",
384+
repo.owner.clone(),
385+
repo.slug.clone(),
386+
e
387+
))
388+
})?;
390389
}
390+
Ok(())
391391
}
392392

393-
pub async fn update_installation_ids_logic() {
393+
pub async fn update_installation_ids_logic() -> Result<(), LDNError> {
394394
let client = Client::new();
395-
let jwt = match generate_github_app_jwt().await {
396-
Ok(jwt) => jwt,
397-
Err(e) => {
398-
log::error!("Failed to generate GitHub App JWT: {}", e);
399-
return;
400-
}
401-
};
395+
let jwt = generate_github_app_jwt()
396+
.await
397+
.map_err(|e| LDNError::Load(format!("Failed to generate GitHub App JWT: {}", e)))?;
402398

403399
let installation_ids_result = fetch_installation_ids(&client, &jwt).await;
404400
let mut results: Vec<InstallationRepositories> = Vec::new();
@@ -415,8 +411,9 @@ pub async fn update_installation_ids_logic() {
415411
}
416412

417413
for installation in results.iter() {
418-
update_installation_ids_in_db(installation.clone()).await;
414+
update_installation_ids_in_db(installation.clone()).await?;
419415
}
416+
Ok(())
420417
}
421418

422419
pub async fn force_update_allocators(

0 commit comments

Comments
 (0)