diff --git a/deployment.sh b/deployment.sh index 90448220..68cc665b 100644 --- a/deployment.sh +++ b/deployment.sh @@ -1,4 +1,4 @@ -sudo docker build --platform=linux/amd64 . -t fp-core +sudo docker build . -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 diff --git a/src/core/mod.rs b/src/core/mod.rs index b6837825..967bc9ae 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -62,7 +62,7 @@ pub struct ApproveApplicationInfo { #[derive(Debug)] pub struct LDNApplication { - github: GithubWrapper<'static>, + github: GithubWrapper, pub application_id: String, file_sha: String, } @@ -81,15 +81,6 @@ pub struct CompleteGovernanceReviewInfo { actor: String, } -fn http_server() -> reqwest::Client { - let client = reqwest::Client::builder() - .user_agent("FP-CORE/0.1.0") - .connection_verbose(true) - .build() - .expect("Failed to build client"); - client -} - impl Display for LDNApplicationError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { @@ -136,86 +127,7 @@ impl MessageBody for LDNApplicationError { } impl LDNApplication { - /// 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, LDNApplicationError> - { - let gh: GithubWrapper = GithubWrapper::new(); - let mut apps: Vec = Vec::new(); - let pull_requests = gh.list_pull_requests().await.unwrap(); - let pull_requests = pull_requests - .into_iter() - .map(|pr: PullRequest| { - let branch = pr.head.ref_field.clone(); - let base = pr.base.ref_field.clone(); - return (branch, base); - }) - .collect::>(); - let app_futures: Vec<_> = pull_requests - .into_iter() - .map(|i| tokio::spawn(LDNApplication::app_file_without_load(i.0))) - .collect(); - for app in app_futures { - match app.await { - Ok(app) => match app { - Ok(app) => apps.push(app), - Err(_) => { - continue; - } - }, - Err(_) => { - continue; - } - } - } - Ok(apps) - } - - pub async fn active_prs() -> Result, LDNApplicationError> { - let gh: GithubWrapper = GithubWrapper::new(); - let mut pull_requests: Vec = gh.get_all_pull_requests().await.unwrap(); - let mut ret: Vec = vec![]; - while let Some(pr_number) = pull_requests.pop() { - let files = gh.get_pull_request_files(pr_number).await.unwrap(); - let blolb_url = match files.get(0) { - Some(file) => file.raw_url.clone(), - None => continue, - }; - let scheme = blolb_url.scheme(); - let host = match blolb_url.host_str() { - Some(host) => host, - None => continue, - }; - let path = blolb_url.path(); - let url = format!("{}://{}{}", scheme, host, path); - let client = http_server(); - let res = match client.get(&url).send().await { - Ok(res) => res, - Err(_) => { - continue; - } - }; - let res = match res.text().await { - Ok(res) => res, - Err(_) => { - continue; - } - }; - let res: ApplicationFile = match serde_json::from_str(&res) { - Ok(res) => res, - Err(_) => { - continue; - } - }; - ret.push(res); - } - Ok(ret) - } - - async fn get_all_active_applications_old() -> Result, LDNApplicationError> { let gh: GithubWrapper = GithubWrapper::new(); let mut apps: Vec = Vec::new(); @@ -232,6 +144,7 @@ impl LDNApplication { return (id, owner_name); }) .collect::>(); + dbg!(&pull_requests); let app_futures: Vec<_> = pull_requests .into_iter() .map(|i| tokio::spawn(LDNApplication::app_file_without_load(i.0))) @@ -240,11 +153,15 @@ impl LDNApplication { match app.await { Ok(app) => match app { Ok(app) => apps.push(app), - Err(_) => { + Err(e) => { + dbg!("here".to_string()); + dbg!(e.to_string()); continue; } }, - Err(_) => { + Err(e) => { + dbg!("here".to_string()); + dbg!(e.to_string()); continue; } } @@ -690,24 +607,60 @@ pub struct LDNPullRequest { } impl LDNPullRequest { - async fn create_empty_pr( + async fn create_empty_pr_for_branch( application_id: String, owner_name: String, app_branch_name: String, - base_hash: Option, ) -> Result<(u64, String), LDNApplicationError> { let initial_commit = Self::application_initial_commit(&owner_name, &application_id); - let create_ref_request = match GithubWrapper::new() - .build_create_ref_request(app_branch_name.clone(), base_hash) - { - Ok(req) => req, + 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), Err(e) => { return Err(LDNApplicationError::NewApplicationError(format!( - "Application issue cannot create branch request object /// {}", + "Application issue cannot create branch /// {}", e - ))) + ))); } }; + Ok((pr.number, file_sha)) + } + async fn create_empty_pr( + application_id: String, + owner_name: String, + app_branch_name: String, + base_hash: Option, + ) -> 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(), @@ -869,127 +822,124 @@ mod tests { use super::*; - #[ignore] #[tokio::test] async fn end_to_end() { - let all_applications = LDNApplication::get_all_active_applications().await.unwrap(); - dbg!(&all_applications); - // // Test Creating an application - // let gh = GithubWrapper::new(); - - // // let branches = gh.list_branches().await.unwrap(); - // let issue = gh.list_issue(14).await.unwrap(); - // let test_issue: Issue = gh - // .create_issue("from test", &issue.body.unwrap()) - // .await - // .unwrap(); - // assert!(LDNApplication::new(CreateApplicationInfo { - // application_id: test_issue.number.to_string(), - // }) - // .await - // .is_ok()); - - // let application_id = test_issue.number.to_string(); - - // // validate file was created - // assert!(gh - // .get_file( - // &LDNPullRequest::application_path(application_id.as_str()), - // &LDNPullRequest::application_branch_name(application_id.as_str()) - // ) - // .await - // .is_ok()); - - // // validate pull request was created - // assert!(gh - // .get_pull_request_by_head(&LDNPullRequest::application_branch_name( - // application_id.as_str() - // )) - // .await - // .is_ok()); - - // // Test Triggering an application - // let ldn_application_before_trigger = - // LDNApplication::load(application_id.clone()).await.unwrap(); - // ldn_application_before_trigger - // .complete_governance_review(CompleteGovernanceReviewInfo { - // actor: "actor_address".to_string(), - // }) - // .await - // .unwrap(); - // let ldn_application_after_trigger = - // LDNApplication::load(application_id.clone()).await.unwrap(); - // assert_eq!( - // ldn_application_after_trigger.app_state().await.unwrap(), - // ApplicationFileState::Proposal - // ); - // dbg!("waiting for 2 second"); - // sleep(Duration::from_millis(1000)).await; - - // // // Test Proposing an application - // let ldn_application_after_trigger_success = - // LDNApplication::load(application_id.clone()).await.unwrap(); - // ldn_application_after_trigger_success - // .complete_new_application_proposal(CompleteNewApplicationProposalInfo { - // request_id: "request_id".to_string(), - // signer: ApplicationAllocationsSigner { - // signing_address: "signing_address".to_string(), - // time_of_signature: "time_of_signature".to_string(), - // message_cid: "message_cid".to_string(), - // }, - // }) - // .await - // .unwrap(); - // let ldn_application_after_proposal = - // LDNApplication::load(application_id.clone()).await.unwrap(); - // assert_eq!( - // ldn_application_after_proposal.app_state().await.unwrap(), - // ApplicationFileState::Approval - // ); - // dbg!("waiting for 2 second"); - // sleep(Duration::from_millis(1000)).await; - - // // Test Approving an application - // let ldn_application_after_proposal_success = - // LDNApplication::load(application_id.clone()).await.unwrap(); - // ldn_application_after_proposal_success - // .complete_new_application_approval(CompleteNewApplicationProposalInfo { - // request_id: "request_id".to_string(), - // signer: ApplicationAllocationsSigner { - // signing_address: "signing_address".to_string(), - // time_of_signature: "time_of_signature".to_string(), - // message_cid: "message_cid".to_string(), - // }, - // }) - // .await - // .unwrap(); - // let ldn_application_after_approval = - // LDNApplication::load(application_id.clone()).await.unwrap(); - // assert_eq!( - // ldn_application_after_approval.app_state().await.unwrap(), - // ApplicationFileState::Confirmed - // ); - // dbg!("waiting for 2 second"); - // sleep(Duration::from_millis(1000)).await; - - // // // Cleanup - // assert!(gh.close_issue(test_issue.number).await.is_ok()); - // assert!(gh - // .close_pull_request( - // gh.get_pull_request_by_head(&LDNPullRequest::application_branch_name( - // &application_id.clone() - // )) - // .await - // .unwrap()[0] - // .number, - // ) - // .await - // .is_ok()); - // let remove_branch_request = gh - // .build_remove_ref_request(LDNPullRequest::application_branch_name( - // &application_id.clone(), - // )) - // .unwrap(); - // assert!(gh.remove_branch(remove_branch_request).await.is_ok()); + // Test Creating an application + let gh = GithubWrapper::new(); + + // let branches = gh.list_branches().await.unwrap(); + let issue = gh.list_issue(14).await.unwrap(); + let test_issue: Issue = gh + .create_issue("from test", &issue.body.unwrap()) + .await + .unwrap(); + assert!(LDNApplication::new(CreateApplicationInfo { + application_id: test_issue.number.to_string(), + }) + .await + .is_ok()); + + let application_id = test_issue.number.to_string(); + + // validate file was created + assert!(gh + .get_file( + &LDNPullRequest::application_path(application_id.as_str()), + &LDNPullRequest::application_branch_name(application_id.as_str()) + ) + .await + .is_ok()); + + // validate pull request was created + assert!(gh + .get_pull_request_by_head(&LDNPullRequest::application_branch_name( + application_id.as_str() + )) + .await + .is_ok()); + + // Test Triggering an application + let ldn_application_before_trigger = + LDNApplication::load(application_id.clone()).await.unwrap(); + ldn_application_before_trigger + .complete_governance_review(CompleteGovernanceReviewInfo { + actor: "actor_address".to_string(), + }) + .await + .unwrap(); + let ldn_application_after_trigger = + LDNApplication::load(application_id.clone()).await.unwrap(); + assert_eq!( + ldn_application_after_trigger.app_state().await.unwrap(), + ApplicationFileState::Proposal + ); + dbg!("waiting for 2 second"); + sleep(Duration::from_millis(1000)).await; + + // // Test Proposing an application + let ldn_application_after_trigger_success = + LDNApplication::load(application_id.clone()).await.unwrap(); + ldn_application_after_trigger_success + .complete_new_application_proposal(CompleteNewApplicationProposalInfo { + request_id: "request_id".to_string(), + signer: ApplicationAllocationsSigner { + signing_address: "signing_address".to_string(), + time_of_signature: "time_of_signature".to_string(), + message_cid: "message_cid".to_string(), + }, + }) + .await + .unwrap(); + let ldn_application_after_proposal = + LDNApplication::load(application_id.clone()).await.unwrap(); + assert_eq!( + ldn_application_after_proposal.app_state().await.unwrap(), + ApplicationFileState::Approval + ); + dbg!("waiting for 2 second"); + sleep(Duration::from_millis(1000)).await; + + // Test Approving an application + let ldn_application_after_proposal_success = + LDNApplication::load(application_id.clone()).await.unwrap(); + ldn_application_after_proposal_success + .complete_new_application_approval(CompleteNewApplicationProposalInfo { + request_id: "request_id".to_string(), + signer: ApplicationAllocationsSigner { + signing_address: "signing_address".to_string(), + time_of_signature: "time_of_signature".to_string(), + message_cid: "message_cid".to_string(), + }, + }) + .await + .unwrap(); + let ldn_application_after_approval = + LDNApplication::load(application_id.clone()).await.unwrap(); + assert_eq!( + ldn_application_after_approval.app_state().await.unwrap(), + ApplicationFileState::Confirmed + ); + dbg!("waiting for 2 second"); + sleep(Duration::from_millis(1000)).await; + + // // Cleanup + assert!(gh.close_issue(test_issue.number).await.is_ok()); + assert!(gh + .close_pull_request( + gh.get_pull_request_by_head(&LDNPullRequest::application_branch_name( + &application_id.clone() + )) + .await + .unwrap()[0] + .number, + ) + .await + .is_ok()); + let remove_branch_request = gh + .build_remove_ref_request(LDNPullRequest::application_branch_name( + &application_id.clone(), + )) + .unwrap(); + assert!(gh.remove_branch(remove_branch_request).await.is_ok()); } } diff --git a/src/external_services/github.rs b/src/external_services/github.rs index ab075574..c77ec25c 100644 --- a/src/external_services/github.rs +++ b/src/external_services/github.rs @@ -1,10 +1,8 @@ -#![allow(dead_code)] use http::header::USER_AGENT; use http::{Request, Uri}; use hyper_rustls::HttpsConnectorBuilder; -use markdown::{mdast::Node, to_mdast, ParseOptions}; -use crate::core::{LDNPullRequest, ParsedApplicationDataFields}; +use crate::core::LDNPullRequest; use octocrab::auth::AppAuth; use octocrab::models::issues::{Comment, Issue}; use octocrab::models::pulls::PullRequest; @@ -17,26 +15,11 @@ use octocrab::{AuthState, Error as OctocrabError, Octocrab, OctocrabBuilder, Pag use std::sync::Arc; const GITHUB_API_URL: &str = "https://api.github.com"; - -struct GithubParams<'a> { - pub owner: &'a str, - pub repo: &'a str, - pub app_id: u64, - pub installation_id: u64, - pub main_branch_hash: &'a str, -} - -impl GithubParams<'static> { - fn test_env() -> Self { - Self { - owner: "filecoin-project", - repo: "filplus-tooling-backend-test", - app_id: 373258, - installation_id: 40514592, - main_branch_hash: "650a0aec11dc1cc436a45b316db5bb747e518514", - } - } -} +const GITHUB_OWNER: &str = "filecoin-project"; +const GITHUB_REPO: &str = "filplus-tooling-backend-test"; +const APP_ID: u64 = 373258; +const APP_INSTALLATION_ID: u64 = 40514592; +const MAIN_BRANCH_HASH: &str = "650a0aec11dc1cc436a45b316db5bb747e518514"; #[derive(Debug)] pub struct CreateMergeRequestData { @@ -48,22 +31,12 @@ pub struct CreateMergeRequestData { } #[derive(Debug)] -pub struct GithubWrapper<'a> { +pub struct GithubWrapper { pub inner: Arc, - pub owner: &'a str, - pub repo: &'a str, - pub main_branch_hash: &'a str, } -impl GithubWrapper<'static> { +impl GithubWrapper { pub fn new() -> Self { - let GithubParams { - owner, - repo, - app_id, - installation_id, - main_branch_hash, - } = GithubParams::test_env(); let connector = HttpsConnectorBuilder::new() .with_native_roots() // enabled the `rustls-native-certs` feature in hyper-rustls .https_only() @@ -84,17 +57,16 @@ impl GithubWrapper<'static> { "octocrab".parse().unwrap(), )]))) .with_auth(AuthState::App(AppAuth { - app_id: app_id.into(), + app_id: APP_ID.into(), key, })) .build() .expect("Could not create Octocrab instance"); - let iod: InstallationId = installation_id.try_into().expect("Invalid installation id"); + let iod: InstallationId = APP_INSTALLATION_ID + .try_into() + .expect("Invalid installation id"); let installation = octocrab.installation(iod); Self { - main_branch_hash, - owner, - repo, inner: Arc::new(installation), } } @@ -102,7 +74,7 @@ impl GithubWrapper<'static> { pub async fn list_issues(&self) -> Result, OctocrabError> { let iid = self .inner - .issues(self.owner, self.repo) + .issues(GITHUB_OWNER, GITHUB_REPO) .list() .state(State::Open) .send() @@ -111,7 +83,11 @@ impl GithubWrapper<'static> { } pub async fn list_issue(&self, number: u64) -> Result { - let iid = self.inner.issues(self.owner, self.repo).get(number).await?; + let iid = self + .inner + .issues(GITHUB_OWNER, GITHUB_REPO) + .get(number) + .await?; Ok(iid) } @@ -122,7 +98,7 @@ impl GithubWrapper<'static> { ) -> Result { let iid = self .inner - .issues(self.owner, self.repo) + .issues(GITHUB_OWNER, GITHUB_REPO) .create_comment(number, body) .await?; Ok(iid) @@ -131,7 +107,7 @@ impl GithubWrapper<'static> { pub async fn list_pull_requests(&self) -> Result, OctocrabError> { let iid = self .inner - .pulls(self.owner, self.repo) + .pulls(GITHUB_OWNER, GITHUB_REPO) .list() .state(State::Open) .send() @@ -146,30 +122,17 @@ impl GithubWrapper<'static> { ) -> Result { let iid = self .inner - .commits(self.owner, self.repo) + .commits(GITHUB_OWNER, GITHUB_REPO) .create_comment(branch_name, commit_body) .send() .await?; Ok(iid) } - pub async fn get_pull_request_files( - &self, - number: u64, - ) -> Result, OctocrabError> { - let iid: Page = self - .inner - .pulls(self.owner, self.repo) - .media_type(octocrab::params::pulls::MediaType::Full) - .list_files(number) - .await?; - Ok(iid.items.into_iter().map(|i| i.into()).collect()) - } - pub async fn list_branches(&self) -> Result, OctocrabError> { let iid = self .inner - .repos(self.owner, self.repo) + .repos(GITHUB_OWNER, GITHUB_REPO) .list_branches() .send() .await?; @@ -203,7 +166,11 @@ impl GithubWrapper<'static> { } pub async fn list_pull_request(&self, number: u64) -> Result { - let iid = self.inner.pulls(self.owner, self.repo).get(number).await?; + let iid = self + .inner + .pulls(GITHUB_OWNER, GITHUB_REPO) + .get(number) + .await?; Ok(iid) } @@ -215,7 +182,7 @@ impl GithubWrapper<'static> { ) -> Result { let iid = self .inner - .pulls(self.owner, self.repo) + .pulls(GITHUB_OWNER, GITHUB_REPO) .create(title, head, "main") .body(body) .maintainer_can_modify(true) @@ -231,7 +198,7 @@ impl GithubWrapper<'static> { ) -> Result { let iid = self .inner - .pulls(self.owner, self.repo) + .pulls(GITHUB_OWNER, GITHUB_REPO) .update(number) .body(body) .send() @@ -248,7 +215,7 @@ impl GithubWrapper<'static> { ) -> Result { let iid = self .inner - .repos(self.owner, self.repo) + .repos(GITHUB_OWNER, GITHUB_REPO) .create_file(path, message, content) .branch(branch) .send() @@ -260,7 +227,11 @@ impl GithubWrapper<'static> { &self, number: u64, ) -> Result { - let iid = self.inner.pulls(self.owner, self.repo).get(number).await?; + let iid = self + .inner + .pulls(GITHUB_OWNER, GITHUB_REPO) + .get(number) + .await?; Ok(iid) } @@ -271,7 +242,7 @@ impl GithubWrapper<'static> { ) -> Result { let iid = self .inner - .repos(self.owner, self.repo) + .repos(GITHUB_OWNER, GITHUB_REPO) .get_content() .r#ref(branch) .path(path) @@ -290,7 +261,7 @@ impl GithubWrapper<'static> { ) -> Result { let iid = self .inner - .repos(self.owner, self.repo) + .repos(GITHUB_OWNER, GITHUB_REPO) .update_file(path, message, content, file_sha) .branch(branch) .send() @@ -298,31 +269,43 @@ impl GithubWrapper<'static> { Ok(iid) } + pub fn build_governance_review_branch(name: String) -> Result, http::Error> { + Ok(Request::builder() + .method("POST") + .uri(format!( + "https://api.github.com/repos/{}/{}/git/refs", + GITHUB_OWNER, GITHUB_REPO + )) + .body(format!( + r#"{{"ref": "refs/heads/{}/allocation","sha": "a9f99d9fc56cae689a0bf0ee177c266287eb48cd"}}"#, + name + ))?) + } + pub fn build_remove_ref_request(&self, name: String) -> Result, http::Error> { let request = Request::builder() .method("DELETE") .uri(format!( "https://api.github.com/repos/{}/{}/git/refs/heads/{}", - self.owner, self.repo, name + GITHUB_OWNER, GITHUB_REPO, name )) .body("".to_string())?; Ok(request) } pub fn build_create_ref_request( - &self, name: String, head_hash: Option, ) -> Result, http::Error> { let hash = match head_hash { Some(hash) => hash, - None => self.main_branch_hash.to_string(), + None => MAIN_BRANCH_HASH.to_string(), }; let request = Request::builder() .method("POST") .uri(format!( "https://api.github.com/repos/{}/{}/git/refs", - self.owner, self.repo + GITHUB_OWNER, GITHUB_REPO )) .body(format!( r#"{{"ref": "refs/heads/{}","sha": "{}" }}"#, @@ -344,7 +327,7 @@ impl GithubWrapper<'static> { pub async fn create_issue(&self, title: &str, body: &str) -> Result { Ok(self .inner - .issues(self.owner, self.repo) + .issues(GITHUB_OWNER, GITHUB_REPO) .create(title) .body(body) .send() @@ -354,37 +337,20 @@ impl GithubWrapper<'static> { pub async fn close_issue(&self, issue_number: u64) -> Result { Ok(self .inner - .issues(self.owner, self.repo) + .issues(GITHUB_OWNER, GITHUB_REPO) .update(issue_number) .state(IssueState::Closed) .send() .await?) } - pub async fn get_all_pull_requests(&self) -> Result, OctocrabError> { - let mut pull_requests: Page = self - .inner - .pulls(self.owner, self.repo) - .list() - .state(State::Open) - .base("main") - .send() - .await?; - let pull_requests_vec: Vec = pull_requests - .take_items() - .into_iter() - .map(|pr| pr.number) - .collect(); - Ok(pull_requests_vec) - } - pub async fn get_pull_request_by_head( &self, head: &str, ) -> Result, OctocrabError> { let mut pull_requests: Page = self .inner - .pulls(self.owner, self.repo) + .pulls(GITHUB_OWNER, GITHUB_REPO) .list() .state(State::Open) .head(head) @@ -398,7 +364,7 @@ impl GithubWrapper<'static> { pub async fn close_pull_request(&self, number: u64) -> Result { Ok(self .inner - .pulls(self.owner, self.repo) + .pulls(GITHUB_OWNER, GITHUB_REPO) .update(number) .state(PullState::Closed) .send() @@ -441,7 +407,7 @@ impl GithubWrapper<'static> { pub async fn merge_pull_request(&self, number: u64) -> Result<(), OctocrabError> { let _merge_res = self .inner - .pulls(self.owner, self.repo) + .pulls(GITHUB_OWNER, GITHUB_REPO) .merge(number) .send() .await?; @@ -449,146 +415,15 @@ impl GithubWrapper<'static> { } } -#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)] -pub struct TestPatch { - pub hello: String, - pub second: String, - pub third: String, -} - -#[derive(serde::Serialize, serde::Deserialize, Debug)] -pub enum ParsedTestPatch { - Hello, - Second, - Third, -} - -impl From for ParsedTestPatch { - fn from(s: String) -> Self { - dbg!(&s); - match s.as_str() { - "Hello" => ParsedTestPatch::Hello, - "Second" => ParsedTestPatch::Second, - "Third" => ParsedTestPatch::Third, - _ => panic!("Unknown property"), - } - } -} - -pub fn parser(body: &str) -> TestPatch { - let tree: Node = to_mdast(body, &ParseOptions::default()).unwrap(); - let mut hello: Option = None; - let mut second: Option = None; - let mut third: Option = None; - // let mut custom_notary: Option = None; - for (index, i) in tree.children().unwrap().into_iter().enumerate().step_by(2) { - let prop: ParsedTestPatch = i.to_string().into(); - let tree = tree.children().unwrap().into_iter(); - let value = match tree.skip(index + 1).next() { - Some(v) => v.to_string(), - None => continue, - }; - match prop { - ParsedTestPatch::Hello => { - hello = Some(value); - } - ParsedTestPatch::Second => { - second = Some(value); - } - ParsedTestPatch::Third => { - third = Some(value); - } - } - } - let parsed_ldn = TestPatch { - hello: hello.unwrap_or_else(|| "No Name".to_string()), - second: second.unwrap_or_else(|| "No Region".to_string()), - third: third.unwrap_or_else(|| "No Region".to_string()), - }; - parsed_ldn -} - -fn remove_invalid_chars(mut s: String) -> String { - s.retain(|x| !['+', '\n', '\'', '-'].contains(&x)); - s -} - -fn http_server() -> reqwest::Client { - let client = reqwest::Client::builder() - .user_agent("FP-CORE/0.1.0") - .connection_verbose(true) - .build() - .expect("Failed to build client"); - client -} - #[cfg(test)] mod tests { - use crate::{ - core::application::ApplicationFile, - external_services::github::{http_server, remove_invalid_chars, GithubWrapper, TestPatch}, - }; + use crate::external_services::github::GithubWrapper; - #[ignore] #[tokio::test] async fn test_basic_integration() { let gh = GithubWrapper::new(); - let mut pull_requests: Vec = gh.get_all_pull_requests().await.unwrap(); - dbg!(&pull_requests); - let mut ret: Vec = vec![]; - while let Some(pr_number) = pull_requests.pop() { - let files = gh.get_pull_request_files(pr_number).await.unwrap(); - let blolb_url = match files.get(0) { - Some(file) => file.raw_url.clone(), - None => continue, - }; - let scheme = blolb_url.scheme(); - let host = match blolb_url.host_str() { - Some(host) => host, - None => continue, - }; - let path = blolb_url.path(); - let url = format!("{}://{}{}", scheme, host, path); - let client = http_server(); - let res = match client.get(&url).send().await { - Ok(res) => res, - Err(_) => { - continue; - } - }; - let res = match res.text().await { - Ok(res) => res, - Err(_) => { - continue; - } - }; - let res: ApplicationFile = match serde_json::from_str(&res) { - Ok(res) => res, - Err(_) => { - continue; - } - }; - ret.push(res); - } - dbg!(&ret); - // // dbg!(&files.get(0).unwrap().blob_url); - // dbg!(&files.get(0).unwrap().patch); - // reqwest. - // let url = format!(blolb_url.schema); - // let mut patch = files.get(0).unwrap().patch.clone().unwrap(); - // let offset = patch.find("{").unwrap(); - // let deleted = patch.drain(..offset).collect::(); - // // dbg!(&deleted); - // // dbg!(&patch); - // // dbg!(&patch); - // let parsed = remove_invalid_chars(patch); - // dbg!(&parsed); - // let test_patch: TestPatch = serde_json::from_str(&parsed).unwrap(); - // dbg!(&test_patch); - // dbg!(&test_patch); - // assert!(false); - // assert!(gh.list_issues().await.is_ok()); - // assert!(gh.list_pull_requests().await.is_ok()); - // assert!(gh.list_branches().await.is_ok()); + assert!(gh.list_issues().await.is_ok()); + assert!(gh.list_pull_requests().await.is_ok()); + assert!(gh.list_branches().await.is_ok()); } } diff --git a/src/router/application.rs b/src/router/application.rs index f804c6c2..5496bd1a 100644 --- a/src/router/application.rs +++ b/src/router/application.rs @@ -116,7 +116,7 @@ pub async fn get_application(id: web::Path) -> actix_web::Result actix_web::Result { - let apps = match LDNApplication::active_prs().await { + let apps = match LDNApplication::get_all_active_applications().await { Ok(app) => app, Err(e) => { return Ok(HttpResponse::BadRequest().body(e.to_string()));