diff --git a/deployment.sh b/deployment.sh index 68cc665b..90448220 100644 --- a/deployment.sh +++ b/deployment.sh @@ -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 diff --git a/src/core/mod.rs b/src/core/mod.rs index a815bd07..bf99d791 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -63,7 +63,7 @@ pub struct ApproveApplicationInfo { #[derive(Debug)] pub struct LDNApplication { - github: GithubWrapper, + github: GithubWrapper<'static>, pub application_id: String, file_sha: String, } @@ -130,7 +130,47 @@ 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, 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(); + dbg!(&branch); + let base = pr.base.ref_field.clone(); + dbg!(&base); + 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) + } + + async fn get_all_active_applications_old() -> Result, LDNApplicationError> { let gh: GithubWrapper = GithubWrapper::new(); let mut apps: Vec = Vec::new(); @@ -155,15 +195,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; } } @@ -612,60 +648,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, ) -> 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, - ) -> 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(), @@ -826,6 +826,7 @@ mod tests { use super::*; + #[ignore] #[tokio::test] async fn end_to_end() { // Test Creating an application diff --git a/src/external_services/github.rs b/src/external_services/github.rs index c77ec25c..8b279499 100644 --- a/src/external_services/github.rs +++ b/src/external_services/github.rs @@ -1,3 +1,4 @@ +#![allow(dead_code)] use http::header::USER_AGENT; use http::{Request, Uri}; use hyper_rustls::HttpsConnectorBuilder; @@ -15,11 +16,26 @@ use octocrab::{AuthState, Error as OctocrabError, Octocrab, OctocrabBuilder, Pag use std::sync::Arc; const GITHUB_API_URL: &str = "https://api.github.com"; -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"; + +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", + } + } +} #[derive(Debug)] pub struct CreateMergeRequestData { @@ -31,12 +47,22 @@ pub struct CreateMergeRequestData { } #[derive(Debug)] -pub struct GithubWrapper { +pub struct GithubWrapper<'a> { pub inner: Arc, + pub owner: &'a str, + pub repo: &'a str, + pub main_branch_hash: &'a str, } -impl GithubWrapper { +impl GithubWrapper<'static> { 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() @@ -57,16 +83,17 @@ impl GithubWrapper { "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 = APP_INSTALLATION_ID - .try_into() - .expect("Invalid installation id"); + let iod: InstallationId = installation_id.try_into().expect("Invalid installation id"); let installation = octocrab.installation(iod); Self { + main_branch_hash, + owner, + repo, inner: Arc::new(installation), } } @@ -74,7 +101,7 @@ impl GithubWrapper { pub async fn list_issues(&self) -> Result, OctocrabError> { let iid = self .inner - .issues(GITHUB_OWNER, GITHUB_REPO) + .issues(self.owner, self.repo) .list() .state(State::Open) .send() @@ -83,11 +110,7 @@ impl GithubWrapper { } pub async fn list_issue(&self, number: u64) -> Result { - let iid = self - .inner - .issues(GITHUB_OWNER, GITHUB_REPO) - .get(number) - .await?; + let iid = self.inner.issues(self.owner, self.repo).get(number).await?; Ok(iid) } @@ -98,7 +121,7 @@ impl GithubWrapper { ) -> Result { let iid = self .inner - .issues(GITHUB_OWNER, GITHUB_REPO) + .issues(self.owner, self.repo) .create_comment(number, body) .await?; Ok(iid) @@ -107,7 +130,7 @@ impl GithubWrapper { pub async fn list_pull_requests(&self) -> Result, OctocrabError> { let iid = self .inner - .pulls(GITHUB_OWNER, GITHUB_REPO) + .pulls(self.owner, self.repo) .list() .state(State::Open) .send() @@ -122,17 +145,29 @@ impl GithubWrapper { ) -> Result { let iid = self .inner - .commits(GITHUB_OWNER, GITHUB_REPO) + .commits(self.owner, self.repo) .create_comment(branch_name, commit_body) .send() .await?; Ok(iid) } + pub async fn get_pull_request_files( + &self, + ) -> Result, OctocrabError> { + let iid: Page = self + .inner + .pulls(self.owner, self.repo) + .media_type(octocrab::params::pulls::MediaType::Full) + .list_files(34) + .await?; + Ok(iid.items.into_iter().map(|i| i.into()).collect()) + } + pub async fn list_branches(&self) -> Result, OctocrabError> { let iid = self .inner - .repos(GITHUB_OWNER, GITHUB_REPO) + .repos(self.owner, self.repo) .list_branches() .send() .await?; @@ -166,11 +201,7 @@ impl GithubWrapper { } pub async fn list_pull_request(&self, number: u64) -> Result { - let iid = self - .inner - .pulls(GITHUB_OWNER, GITHUB_REPO) - .get(number) - .await?; + let iid = self.inner.pulls(self.owner, self.repo).get(number).await?; Ok(iid) } @@ -182,7 +213,7 @@ impl GithubWrapper { ) -> Result { let iid = self .inner - .pulls(GITHUB_OWNER, GITHUB_REPO) + .pulls(self.owner, self.repo) .create(title, head, "main") .body(body) .maintainer_can_modify(true) @@ -198,7 +229,7 @@ impl GithubWrapper { ) -> Result { let iid = self .inner - .pulls(GITHUB_OWNER, GITHUB_REPO) + .pulls(self.owner, self.repo) .update(number) .body(body) .send() @@ -215,7 +246,7 @@ impl GithubWrapper { ) -> Result { let iid = self .inner - .repos(GITHUB_OWNER, GITHUB_REPO) + .repos(self.owner, self.repo) .create_file(path, message, content) .branch(branch) .send() @@ -227,11 +258,7 @@ impl GithubWrapper { &self, number: u64, ) -> Result { - let iid = self - .inner - .pulls(GITHUB_OWNER, GITHUB_REPO) - .get(number) - .await?; + let iid = self.inner.pulls(self.owner, self.repo).get(number).await?; Ok(iid) } @@ -242,7 +269,7 @@ impl GithubWrapper { ) -> Result { let iid = self .inner - .repos(GITHUB_OWNER, GITHUB_REPO) + .repos(self.owner, self.repo) .get_content() .r#ref(branch) .path(path) @@ -261,7 +288,7 @@ impl GithubWrapper { ) -> Result { let iid = self .inner - .repos(GITHUB_OWNER, GITHUB_REPO) + .repos(self.owner, self.repo) .update_file(path, message, content, file_sha) .branch(branch) .send() @@ -269,43 +296,31 @@ impl GithubWrapper { 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/{}", - GITHUB_OWNER, GITHUB_REPO, name + self.owner, self.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 => MAIN_BRANCH_HASH.to_string(), + None => self.main_branch_hash.to_string(), }; let request = Request::builder() .method("POST") .uri(format!( "https://api.github.com/repos/{}/{}/git/refs", - GITHUB_OWNER, GITHUB_REPO + self.owner, self.repo )) .body(format!( r#"{{"ref": "refs/heads/{}","sha": "{}" }}"#, @@ -327,7 +342,7 @@ impl GithubWrapper { pub async fn create_issue(&self, title: &str, body: &str) -> Result { Ok(self .inner - .issues(GITHUB_OWNER, GITHUB_REPO) + .issues(self.owner, self.repo) .create(title) .body(body) .send() @@ -337,7 +352,7 @@ impl GithubWrapper { pub async fn close_issue(&self, issue_number: u64) -> Result { Ok(self .inner - .issues(GITHUB_OWNER, GITHUB_REPO) + .issues(self.owner, self.repo) .update(issue_number) .state(IssueState::Closed) .send() @@ -350,7 +365,7 @@ impl GithubWrapper { ) -> Result, OctocrabError> { let mut pull_requests: Page = self .inner - .pulls(GITHUB_OWNER, GITHUB_REPO) + .pulls(self.owner, self.repo) .list() .state(State::Open) .head(head) @@ -364,7 +379,7 @@ impl GithubWrapper { pub async fn close_pull_request(&self, number: u64) -> Result { Ok(self .inner - .pulls(GITHUB_OWNER, GITHUB_REPO) + .pulls(self.owner, self.repo) .update(number) .state(PullState::Closed) .send() @@ -407,7 +422,7 @@ impl GithubWrapper { pub async fn merge_pull_request(&self, number: u64) -> Result<(), OctocrabError> { let _merge_res = self .inner - .pulls(GITHUB_OWNER, GITHUB_REPO) + .pulls(self.owner, self.repo) .merge(number) .send() .await?;