Skip to content

Commit a70727f

Browse files
committed
Testing clone, modified environment variables and CI/CD
1 parent 0f5468b commit a70727f

File tree

6 files changed

+139
-14
lines changed

6 files changed

+139
-14
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-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,14 +44,29 @@ 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
).await {
54-
Ok(allocator_model) => Ok(HttpResponse::Ok().json(allocator_model)),
54+
Ok(allocator_model) => allocator_model,
55+
Err(e) => return Ok(HttpResponse::BadRequest().body(e.to_string())),
56+
};
57+
58+
match is_allocator_repo_created(&model.owner, &model.repo).await {
59+
Ok(true) => Ok(HttpResponse::Ok().json(allocator_model)),
60+
Ok(false) => {
61+
//Create allocator repo. If it fails, return http error
62+
match create_allocator_repo(&model.owner, &model.repo).await {
63+
Ok(files_list) => {
64+
log::info!("Allocator repo created successfully: {:?}", files_list);
65+
Ok(HttpResponse::Ok().json(allocator_model))
66+
}
67+
Err(e) => Ok(HttpResponse::BadRequest().body(e.to_string())),
68+
}
69+
},
5570
Err(e) => Ok(HttpResponse::BadRequest().body(e.to_string())),
5671
}
5772
},

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: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ pub async fn process_allocator_file(file_name: &str) -> Result<AllocatorModel, L
2121
Ok(model)
2222
}
2323

24-
2524
fn content_items_to_allocator_model(file: ContentItems) -> Result<AllocatorModel, LDNError> {
2625
let encoded_content = match file.items.get(0).and_then(|f| f.content.clone()) {
2726
Some(content) => {
@@ -47,4 +46,89 @@ fn content_items_to_allocator_model(file: ContentItems) -> Result<AllocatorModel
4746
Err(LDNError::Load("Failed to parse allocator model".to_string()))
4847
}
4948
}
49+
}
50+
51+
pub async fn is_allocator_repo_created(owner: &str, repo: &str) -> Result<bool, LDNError> {
52+
let repo_flag_file = "invalisd.md";
53+
let applications_directory = "applications";
54+
let gh = GithubWrapper::new(owner.to_string(), repo.to_string());
55+
let all_files_result = gh.get_files(applications_directory).await.map_err(|e| {
56+
LDNError::Load(format!("Failed to retrieve all files from GitHub. Reason: {}", e))
57+
});
58+
59+
match all_files_result {
60+
Ok(content_items) => {
61+
let mut is_repo_created = false;
62+
for file in content_items.items.iter() {
63+
if file.name == repo_flag_file {
64+
is_repo_created = true;
65+
break;
66+
}
67+
}
68+
Ok(is_repo_created)
69+
},
70+
Err(e) => {
71+
if e.to_string().contains("GitHub: This repository is empty") || e.to_string().contains("GitHub: Not Found"){
72+
Ok(false)
73+
} else {
74+
Err(e)
75+
}
76+
},
77+
}
78+
}
79+
80+
pub async fn create_allocator_repo(owner: &str, repo: &str) -> Result<(), LDNError> {
81+
let gh = GithubWrapper::new(owner.to_string(), repo.to_string());
82+
let mut dirs = Vec::new();
83+
dirs.push("".to_string());
84+
85+
while dirs.len() > 0 {
86+
let dir = dirs.pop().unwrap();
87+
let files_list = gh.get_files_from_public_repo("clriesco", "filplus-allocator-template", Some(&dir)).await.map_err(|e| {
88+
LDNError::Load(format!("Failed to retrieve all files from GitHub. Reason: {}", e))
89+
})?;
90+
91+
for file in files_list.items.iter() {
92+
let file_path = file.path.clone();
93+
if file.r#type == "dir" {
94+
dirs.push(file_path);
95+
continue;
96+
}
97+
let file = reqwest::Client::new()
98+
.get(&file.download_url.clone().unwrap())
99+
.send()
100+
.await
101+
.map_err(|e| LDNError::Load(format!("here {}", e)))?;
102+
let file = file
103+
.text()
104+
.await
105+
.map_err(|e| LDNError::Load(format!("here1 {}", e)))?;
106+
107+
//Get file from target repo. If file does not exist or fails to retrieve, create it
108+
let target_file = gh.get_file(&file_path, "main").await.map_err(|e| {
109+
LDNError::Load(format!("Failed to retrieve file from GitHub. Reason: {} in file {}", e, file_path))
110+
});
111+
112+
match target_file {
113+
Ok(target_file) => {
114+
if target_file.items.is_empty() {
115+
log::info!("Creating file in target repo: {}", file_path);
116+
gh.add_file(&file_path, &file, "first commit", "main").await.map_err(|e| {
117+
LDNError::Load(format!("Failed to create file in GitHub. Reason: {} in file {}", e, file_path))
118+
})?;
119+
} else {
120+
log::info!("File already exists in target repo: {}", file_path);
121+
}
122+
},
123+
Err(_) => {
124+
log::info!("Creating file in target repo: {}", file_path);
125+
gh.add_file(&file_path, &file, "first commit", "main").await.map_err(|e| {
126+
LDNError::Load(format!("Failed to create file in GitHub. Reason: {} in file {}", e, file_path))
127+
})?;
128+
},
129+
}
130+
}
131+
}
132+
133+
Ok(())
50134
}

fplus-lib/src/external_services/github.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,4 +694,23 @@ impl GithubWrapper {
694694
.await?;
695695
Ok(pull_request.head.ref_field.clone())
696696
}
697+
698+
pub async fn get_files_from_public_repo(
699+
&self,
700+
owner: &str,
701+
repo: &str,
702+
path: Option<&str>
703+
) -> Result<ContentItems, OctocrabError> {
704+
let branch = "main";
705+
let octocrab = Octocrab::builder().build()?;
706+
let gh = octocrab.repos(owner, repo);
707+
708+
//if path is not provided, take all files from root
709+
let contents_items = match path {
710+
Some(p) => gh.get_content().r#ref(branch).path(p).send().await?,
711+
None => gh.get_content().r#ref(branch).send().await?,
712+
};
713+
714+
Ok(contents_items)
715+
}
697716
}

0 commit comments

Comments
 (0)