Skip to content

Commit 6a130c0

Browse files
authored
Merge pull request #141 from filecoin-project/feat/auto-clone-repo
Testing clone, modified environment variables and CI/CD
2 parents 3120f6c + 24e2281 commit 6a130c0

File tree

6 files changed

+142
-16
lines changed

6 files changed

+142
-16
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: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use octocrab::models::repos::ContentItems;
22

33
use crate::config::get_env_var_or_default;
4-
use crate::external_services::github::github_async_new;
4+
use crate::external_services::github::{github_async_new, GithubWrapper};
55
use crate::{base64::decode_allocator_model, error::LDNError};
66

77
use self::file::AllocatorModel;
@@ -12,17 +12,17 @@ pub async fn process_allocator_file(file_name: &str) -> Result<AllocatorModel, L
1212

1313
let owner = get_env_var_or_default("ALLOCATOR_GOVERNANCE_OWNER");
1414
let repo = get_env_var_or_default("ALLOCATOR_GOVERNANCE_REPO");
15+
let installation_id = get_env_var_or_default("GITHUB_INSTALLATION_ID");
1516
let branch = "main";
1617
let path = file_name.to_string();
1718

18-
let gh = github_async_new(owner.to_string(), repo.to_string()).await;
19+
let gh = GithubWrapper::new(owner.clone(), repo.clone(), installation_id);
1920
let content_items = gh.get_file(&path, branch).await.map_err(|e| LDNError::Load(e.to_string()))?;
2021
let model = content_items_to_allocator_model(content_items).map_err(|e| LDNError::Load(e.to_string()))?;
2122

2223
Ok(model)
2324
}
2425

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

fplus-lib/src/external_services/github.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,4 +711,23 @@ impl GithubWrapper {
711711
.await?;
712712
Ok(pull_request.head.ref_field.clone())
713713
}
714+
715+
pub async fn get_files_from_public_repo(
716+
&self,
717+
owner: &str,
718+
repo: &str,
719+
path: Option<&str>
720+
) -> Result<ContentItems, OctocrabError> {
721+
let branch = "main";
722+
let octocrab = Octocrab::builder().build()?;
723+
let gh = octocrab.repos(owner, repo);
724+
725+
//if path is not provided, take all files from root
726+
let contents_items = match path {
727+
Some(p) => gh.get_content().r#ref(branch).path(p).send().await?,
728+
None => gh.get_content().r#ref(branch).send().await?,
729+
};
730+
731+
Ok(contents_items)
732+
}
714733
}

0 commit comments

Comments
 (0)