Skip to content

Commit db4fc67

Browse files
committed
Merge branch 'main' into feat/multisig-support
2 parents 818f833 + 6a130c0 commit db4fc67

File tree

9 files changed

+380
-292
lines changed

9 files changed

+380
-292
lines changed

.github/workflows/deploy_to_prod.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ on:
88
jobs:
99
deploy_to_prod:
1010
runs-on: ubuntu-latest
11+
environment: production
1112

1213
steps:
1314
- name: Checkout code
@@ -49,13 +50,12 @@ jobs:
4950
},
5051
"environment": {
5152
"GH_PRIVATE_KEY": "${{secrets.GH_PRIVATE_KEY}}",
52-
"GITHUB_OWNER": "${{secrets.GH_OWNER}}",
53-
"GITHUB_REPO": "${{secrets.GH_REPO}}",
54-
"GITHUB_APP_ID": "${{secrets.GH_APP_ID}}",
55-
"GITHUB_INSTALLATION_ID": "${{secrets.GH_INSTALLATION_ID}}",
53+
"GITHUB_APP_ID": "${{vars.GH_APP_ID}}",
5654
"FILPLUS_ENV": "prod",
5755
"RUST_LOG": "debug",
58-
"BOT_USER": "${{secrets.BOT_USER}}"
56+
"BOT_USER": "${{vars.BOT_USER}}",
57+
"BACKEND_URL": "${{vars.BACKEND_URL}}",
58+
"DB_URL": "${{secrets.DB_URL}}"
5959
}
6060
}
6161
}' > containers.json

.github/workflows/deploy_to_staging.yml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ jobs:
2424
name: Deploy to staging
2525
needs: end_to_end_tests
2626
runs-on: ubuntu-latest
27+
environment: staging
2728

2829
steps:
2930
- name: Checkout code
@@ -64,8 +65,14 @@ jobs:
6465
"8080": "HTTP"
6566
},
6667
"environment": {
67-
"GH_PRIVATE_KEY": "${{secrets.GH_STAGING_PRIVATE_KEY}}",
68-
"DB_URL": "${{secrets.STAGING_DB_URL}}"
68+
"GH_PRIVATE_KEY": "${{secrets.GH_PRIVATE_KEY}}",
69+
"DB_URL": "${{secrets.DB_URL}}"
70+
"GITHUB_APP_ID": "${{vars.GH_APP_ID}}",
71+
"FILPLUS_ENV": "prod",
72+
"RUST_LOG": "debug",
73+
"BOT_USER": "${{vars.BOT_USER}}",
74+
"BACKEND_URL": "${{vars.BACKEND_URL}}",
75+
"DB_URL": "${{secrets.DB_URL}}"
6976
}
7077
}
7178
}' > containers.json

fplus-database/src/lib.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,22 @@ pub async fn get_database_connection() -> Result<DatabaseConnection, DbErr> {
5050
Err(DbErr::Custom("Database connection is not established".into()))
5151
}
5252
}
53+
54+
/**
55+
* Sets up the initial test environment (database connection and env variables)
56+
*/
57+
pub async fn setup_test_environment() {
58+
init();
59+
setup().await.expect("Failed to setup database connection.");
60+
}
61+
5362
#[cfg(test)]
5463
mod tests {
5564

5665
use super::*;
5766
use tokio;
5867
use serial_test::serial;
5968

60-
/**
61-
* Sets up the initial test environment (database connection and env variables)
62-
*/
63-
async fn setup_test_environment() {
64-
init();
65-
setup().await.expect("Failed to setup database connection.");
66-
}
67-
6869
/**
6970
* Test the establish_connection function
7071
*

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use actix_web::{get, post, put, delete, web, HttpResponse, Responder};
22
use fplus_database::database::allocators as allocators_db;
3-
use fplus_lib::core::{allocator::process_allocator_file, AllocatorUpdateInfo, ChangedAllocator};
3+
use fplus_lib::core::{allocator::{process_allocator_file, is_allocator_repo_created, create_allocator_repo}, AllocatorUpdateInfo, ChangedAllocator};
44

55
/**
66
* Get all allocators
@@ -44,15 +44,30 @@ pub async fn create_from_json(file: web::Json<ChangedAllocator>) -> actix_web::R
4444
Some(model.application.verifiers_gh_handles.join(", ")) // Join verifiers in a string if exists
4545
};
4646

47-
match allocators_db::create_or_update_allocator(
48-
model.owner,
49-
model.repo,
47+
let allocator_model = match allocators_db::create_or_update_allocator(
48+
model.owner.clone(),
49+
model.repo.clone(),
5050
Some(model.installation_id as i64),
5151
Some(model.multisig_address),
5252
verifiers_gh_handles,
5353
model.multisig_threshold
5454
).await {
55-
Ok(allocator_model) => Ok(HttpResponse::Ok().json(allocator_model)),
55+
Ok(allocator_model) => allocator_model,
56+
Err(e) => return Ok(HttpResponse::BadRequest().body(e.to_string())),
57+
};
58+
59+
match is_allocator_repo_created(&model.owner, &model.repo).await {
60+
Ok(true) => Ok(HttpResponse::Ok().json(allocator_model)),
61+
Ok(false) => {
62+
//Create allocator repo. If it fails, return http error
63+
match create_allocator_repo(&model.owner, &model.repo).await {
64+
Ok(files_list) => {
65+
log::info!("Allocator repo created successfully: {:?}", files_list);
66+
Ok(HttpResponse::Ok().json(allocator_model))
67+
}
68+
Err(e) => Ok(HttpResponse::BadRequest().body(e.to_string())),
69+
}
70+
},
5671
Err(e) => Ok(HttpResponse::BadRequest().body(e.to_string())),
5772
}
5873
},

fplus-lib/src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub fn default_env_vars() -> &'static HashMap<&'static str, &'static str> {
99
m.insert("GITHUB_OWNER", "filecoin-project");
1010
m.insert("GITHUB_REPO", "filecoin-plus-falcon");
1111
m.insert("GITHUB_APP_ID", "826129");
12-
m.insert("GITHUB_INSTALLATION_ID", "47207972");
12+
m.insert("GITHUB_INSTALLATION_ID", "48206379");
1313
m.insert("RUST_LOG", "info");
1414
m.insert("RUST_BACKTRACE", "1");
1515
m.insert("DB_URL", "");

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

Lines changed: 91 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ use octocrab::models::repos::ContentItems;
22

33
use crate::config::get_env_var_or_default;
44
use crate::external_services::filecoin::get_multisig_threshold_for_actor;
5+
use crate::external_services::github::{github_async_new, GithubWrapper};
56
use crate::{
6-
base64::decode_allocator_model, error::LDNError, external_services::github::GithubWrapper,
7+
base64::decode_allocator_model, error::LDNError,
78
};
89

910
use self::file::AllocatorModel;
@@ -13,16 +14,13 @@ pub mod file;
1314
pub async fn process_allocator_file(file_name: &str) -> Result<AllocatorModel, LDNError> {
1415
let owner = get_env_var_or_default("ALLOCATOR_GOVERNANCE_OWNER");
1516
let repo = get_env_var_or_default("ALLOCATOR_GOVERNANCE_REPO");
17+
let installation_id = get_env_var_or_default("GITHUB_INSTALLATION_ID");
1618
let branch = "main";
1719
let path = file_name.to_string();
1820

19-
let gh = GithubWrapper::new(owner.to_string(), repo.to_string());
20-
let content_items = gh
21-
.get_file(&path, branch)
22-
.await
23-
.map_err(|e| LDNError::Load(e.to_string()))?;
24-
let mut model = content_items_to_allocator_model(content_items)
25-
.map_err(|e| LDNError::Load(e.to_string()))?;
21+
let gh = GithubWrapper::new(owner.clone(), repo.clone(), installation_id);
22+
let content_items = gh.get_file(&path, branch).await.map_err(|e| LDNError::Load(e.to_string()))?;
23+
let mut model = content_items_to_allocator_model(content_items).map_err(|e| LDNError::Load(e.to_string()))?;
2624

2725
// Get multisig threshold from the blockchain if multisig address is available
2826
if let Ok(blockchain_threshold) = get_multisig_threshold_for_actor(&model.multisig_address).await {
@@ -63,3 +61,88 @@ fn content_items_to_allocator_model(file: ContentItems) -> Result<AllocatorModel
6361
}
6462
}
6563
}
64+
65+
pub async fn is_allocator_repo_created(owner: &str, repo: &str) -> Result<bool, LDNError> {
66+
let repo_flag_file = "invalisd.md";
67+
let applications_directory = "applications";
68+
let gh = github_async_new(owner.to_string(), repo.to_string()).await;
69+
let all_files_result = gh.get_files(applications_directory).await.map_err(|e| {
70+
LDNError::Load(format!("Failed to retrieve all files from GitHub. Reason: {}", e))
71+
});
72+
73+
match all_files_result {
74+
Ok(content_items) => {
75+
let mut is_repo_created = false;
76+
for file in content_items.items.iter() {
77+
if file.name == repo_flag_file {
78+
is_repo_created = true;
79+
break;
80+
}
81+
}
82+
Ok(is_repo_created)
83+
},
84+
Err(e) => {
85+
if e.to_string().contains("GitHub: This repository is empty") || e.to_string().contains("GitHub: Not Found"){
86+
Ok(false)
87+
} else {
88+
Err(e)
89+
}
90+
},
91+
}
92+
}
93+
94+
pub async fn create_allocator_repo(owner: &str, repo: &str) -> Result<(), LDNError> {
95+
let gh = github_async_new(owner.to_string(), repo.to_string()).await;
96+
let mut dirs = Vec::new();
97+
dirs.push("".to_string());
98+
99+
while dirs.len() > 0 {
100+
let dir = dirs.pop().unwrap();
101+
let files_list = gh.get_files_from_public_repo("clriesco", "filplus-allocator-template", Some(&dir)).await.map_err(|e| {
102+
LDNError::Load(format!("Failed to retrieve all files from GitHub. Reason: {}", e))
103+
})?;
104+
105+
for file in files_list.items.iter() {
106+
let file_path = file.path.clone();
107+
if file.r#type == "dir" {
108+
dirs.push(file_path);
109+
continue;
110+
}
111+
let file = reqwest::Client::new()
112+
.get(&file.download_url.clone().unwrap())
113+
.send()
114+
.await
115+
.map_err(|e| LDNError::Load(format!("here {}", e)))?;
116+
let file = file
117+
.text()
118+
.await
119+
.map_err(|e| LDNError::Load(format!("here1 {}", e)))?;
120+
121+
//Get file from target repo. If file does not exist or fails to retrieve, create it
122+
let target_file = gh.get_file(&file_path, "main").await.map_err(|e| {
123+
LDNError::Load(format!("Failed to retrieve file from GitHub. Reason: {} in file {}", e, file_path))
124+
});
125+
126+
match target_file {
127+
Ok(target_file) => {
128+
if target_file.items.is_empty() {
129+
log::info!("Creating file in target repo: {}", file_path);
130+
gh.add_file(&file_path, &file, "first commit", "main").await.map_err(|e| {
131+
LDNError::Load(format!("Failed to create file in GitHub. Reason: {} in file {}", e, file_path))
132+
})?;
133+
} else {
134+
log::info!("File already exists in target repo: {}", file_path);
135+
}
136+
},
137+
Err(_) => {
138+
log::info!("Creating file in target repo: {}", file_path);
139+
gh.add_file(&file_path, &file, "first commit", "main").await.map_err(|e| {
140+
LDNError::Load(format!("Failed to create file in GitHub. Reason: {} in file {}", e, file_path))
141+
})?;
142+
},
143+
}
144+
}
145+
}
146+
147+
Ok(())
148+
}

0 commit comments

Comments
 (0)