Skip to content

Commit

Permalink
Merge pull request #15 from filecoin-project/feat/general-app-loader
Browse files Browse the repository at this point in the history
Feat/general app loader
  • Loading branch information
jbesraa authored Sep 1, 2023
2 parents 285dcf7 + 6b789cf commit d32bae5
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 111 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ chrono = "0.4.26"
base64 = "0.13"
structopt = { version = "0.3", default-features = false }
actix-cors = "0.6.4"
reqwest = "0.11.18"
reqwest = { version = "0.11.18", features = ["json"] }
futures = "0.3.28"
2 changes: 1 addition & 1 deletion deployment.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
sudo docker build . -t fp-core
sudo docker build --platform=linux/amd64 . -t fp-core
sudo docker tag fp-core:latest public.ecr.aws/b3c4u5n1/filecoin-core-api:latest
sudo aws ecr-public get-login-password --region us-east-1 | sudo docker login --username AWS --password-stdin public.ecr.aws
sudo docker push public.ecr.aws/b3c4u5n1/filecoin-core-api:latest
Expand Down
114 changes: 62 additions & 52 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(dead_code)]
pub mod application;
use std::{
fmt::Display,
Expand All @@ -10,7 +11,9 @@ use actix_web::{
web::Bytes,
};
use chrono::Utc;
use octocrab::models::{pulls::PullRequest, repos::ContentItems};
use octocrab::models::{pulls::{PullRequest, FileDiff}, repos::ContentItems};
use futures::future;
use reqwest::Response;

use self::application::{
allocations::{AllocationRequest, ApplicationAllocationTypes, ApplicationAllocationsSigner},
Expand Down Expand Up @@ -63,7 +66,7 @@ pub struct ApproveApplicationInfo {

#[derive(Debug)]
pub struct LDNApplication {
github: GithubWrapper,
github: GithubWrapper<'static>,
pub application_id: String,
file_sha: String,
}
Expand Down Expand Up @@ -130,7 +133,53 @@ impl MessageBody for LDNApplicationError {
impl LDNApplication {
/// Get Active Applications
/// Returns a list of all active applications
/// New Implementation for get_all_active_applications
/// we want to get all the pull requests, validate and then get the files from them
/// we need to know how to build the path paramer the is used to get the file along with the
/// branch name.
/// WIP
pub async fn get_all_active_applications() -> Result<Vec<ApplicationFile>, LDNApplicationError>
{
let gh: GithubWrapper = GithubWrapper::new();
let mut apps: Vec<ApplicationFile> = Vec::new();
let pull_requests = gh.list_pull_requests().await.unwrap();
let pull_requests = future::try_join_all(pull_requests
.into_iter()
.map(|pr: PullRequest| {
let number = pr.number;
gh.get_pull_request_files(number)
})
.collect::<Vec<_>>()).await.unwrap().into_iter().flatten();
let pull_requests: Vec<Response> = match future::try_join_all(pull_requests
.into_iter()
.map(|fd: FileDiff| {
reqwest::Client::new()
.get(&fd.raw_url.to_string())
.send()
})
.collect::<Vec<_>>()).await {
Ok(res) => res,
Err(_) => return Err(LDNApplicationError::LoadApplicationError("Failed to get pull request files".to_string()))
};
let pull_requests = match future::try_join_all(pull_requests
.into_iter()
.map(|r: Response| {
r.text()
})
.collect::<Vec<_>>()).await {
Ok(res) => res,
Err(_) => return Err(LDNApplicationError::LoadApplicationError("Failed to get pull request files".to_string()))
};
for r in pull_requests {
match serde_json::from_str::<ApplicationFile>(&r) {
Ok(app) => apps.push(app),
Err(_) => continue
}
}
Ok(apps)
}

async fn get_all_active_applications_old() -> Result<Vec<ApplicationFile>, LDNApplicationError>
{
let gh: GithubWrapper = GithubWrapper::new();
let mut apps: Vec<ApplicationFile> = Vec::new();
Expand All @@ -155,15 +204,11 @@ impl LDNApplication {
match app.await {
Ok(app) => match app {
Ok(app) => apps.push(app),
Err(e) => {
dbg!("here".to_string());
dbg!(e.to_string());
Err(_) => {
continue;
}
},
Err(e) => {
dbg!("here".to_string());
dbg!(e.to_string());
Err(_) => {
continue;
}
}
Expand Down Expand Up @@ -612,60 +657,24 @@ pub struct LDNPullRequest {
}

impl LDNPullRequest {
async fn create_empty_pr_for_branch(
async fn create_empty_pr(
application_id: String,
owner_name: String,
app_branch_name: String,
base_hash: Option<String>,
) -> Result<(u64, String), LDNApplicationError> {
let initial_commit = Self::application_initial_commit(&owner_name, &application_id);
let create_ref_request =
match GithubWrapper::build_create_ref_request(app_branch_name.clone(), None) {
Ok(req) => req,
Err(e) => {
return Err(LDNApplicationError::NewApplicationError(format!(
"Application issue {} cannot create branch request object /// {}",
application_id, e
)))
}
};

let merge_request_data: CreateMergeRequestData = CreateMergeRequestData {
application_id: application_id.clone(),
owner_name,
ref_request: create_ref_request,
file_content: "{}".to_string(),
commit: initial_commit,
};

let gh: GithubWrapper = GithubWrapper::new();
let (pr, file_sha) = match gh.create_merge_request(merge_request_data).await {
Ok((pr, file_sha)) => (pr, file_sha),
let create_ref_request = match GithubWrapper::new()
.build_create_ref_request(app_branch_name.clone(), base_hash)
{
Ok(req) => req,
Err(e) => {
return Err(LDNApplicationError::NewApplicationError(format!(
"Application issue cannot create branch /// {}",
"Application issue cannot create branch request object /// {}",
e
)));
)))
}
};
Ok((pr.number, file_sha))
}
async fn create_empty_pr(
application_id: String,
owner_name: String,
app_branch_name: String,
base_hash: Option<String>,
) -> Result<(u64, String), LDNApplicationError> {
let initial_commit = Self::application_initial_commit(&owner_name, &application_id);
let create_ref_request =
match GithubWrapper::build_create_ref_request(app_branch_name.clone(), base_hash) {
Ok(req) => req,
Err(e) => {
return Err(LDNApplicationError::NewApplicationError(format!(
"Application issue cannot create branch request object /// {}",
e
)))
}
};

let merge_request_data: CreateMergeRequestData = CreateMergeRequestData {
application_id: application_id.clone(),
Expand Down Expand Up @@ -826,6 +835,7 @@ mod tests {

use super::*;

#[ignore]
#[tokio::test]
async fn end_to_end() {
// Test Creating an application
Expand Down
Loading

0 comments on commit d32bae5

Please sign in to comment.