From 07aeaac5f8fe814edb4128597da1daad603b81aa Mon Sep 17 00:00:00 2001 From: Franco Testagrossa Date: Sat, 4 May 2024 09:39:31 +0200 Subject: [PATCH] Export pub github interface To be imported in the bounty package. --- Cargo.lock | 38 ++++++++++++++++-------- Cargo.toml | 4 +-- bounty/Cargo.toml | 2 ++ bounty/src/api/claim.rs | 54 ++++++++++++++++++++--------------- bounty/src/api/state.rs | 2 +- bounty/src/lib.rs | 4 +-- provider/github/Cargo.toml | 5 ++-- provider/github/src/client.rs | 19 ++++++++---- provider/github/src/lib.rs | 9 +++--- 9 files changed, 85 insertions(+), 52 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5b927b2..3acb594 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -23,11 +23,22 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" +[[package]] +name = "async-trait" +version = "0.1.80" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6fa2087f2753a7da8cc1c0dbfcf89579dd57458e36769de5ac750b4671737ca" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.60", +] + [[package]] name = "autocfg" -version = "1.2.0" +version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1fdabc7756949593fe60f30ec81974b613357de856987752631dea1e3394c80" +checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" [[package]] name = "binread" @@ -65,8 +76,10 @@ dependencies = [ name = "bounty" version = "0.1.0" dependencies = [ + "async-trait", "candid", "futures", + "github", "ic-cdk", "ic-cdk-macros", "ic-ledger-types", @@ -86,9 +99,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "candid" -version = "0.10.7" +version = "0.10.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "818394610ed32d9e4c81025f97c8580698b69542527efde18514cf9ad1f8f5f0" +checksum = "dd5902d37352dffd8bd9177a2daa6444ce3cd0279c91763fb0171c053aa04335" dependencies = [ "anyhow", "binread", @@ -121,9 +134,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.0.95" +version = "1.0.96" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d32a725bc159af97c3e629873bb9f88fb8cf8a4867175f76dc987815ea07c83b" +checksum = "065a29261d53ba54260972629f9ca6bffa69bac13cd1fed61420f7fa68b9f8bd" [[package]] name = "cfg-if" @@ -284,6 +297,7 @@ dependencies = [ name = "github" version = "0.1.0" dependencies = [ + "async-trait", "candid", "ic-cdk", "ic-cdk-macros", @@ -414,9 +428,9 @@ dependencies = [ [[package]] name = "num-traits" -version = "0.2.18" +version = "0.2.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da0df0e5185db44f69b44f26786fe401b6c293d1907744beaa7fa62b2e5a517a" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" dependencies = [ "autocfg", ] @@ -520,9 +534,9 @@ checksum = "e86697c916019a8588c99b5fac3cead74ec0b4b819707a682fd4d23fa0ce1ba1" [[package]] name = "serde" -version = "1.0.199" +version = "1.0.200" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c9f6e76df036c77cd94996771fb40db98187f096dd0b9af39c6c6e452ba966a" +checksum = "ddc6f9cc94d67c0e21aaf7eda3a010fd3af78ebf6e096aa6e2e13c79749cce4f" dependencies = [ "serde_derive", ] @@ -538,9 +552,9 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.199" +version = "1.0.200" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "11bd257a6541e141e42ca6d24ae26f7714887b47e89aa739099104c7e4d3b7fc" +checksum = "856f046b9400cee3c8c94ed572ecdb752444c24528c035cd35882aad6f492bcb" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index 2df1056..72dc3e8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [workspace] members = [ "provider/github", - "bounty" + "bounty", ] -resolver = "2" \ No newline at end of file +resolver = "2" diff --git a/bounty/Cargo.toml b/bounty/Cargo.toml index 4f178d8..90d26f4 100644 --- a/bounty/Cargo.toml +++ b/bounty/Cargo.toml @@ -20,3 +20,5 @@ serde_bytes = "0.11.14" num-bigint = "0.4.4" num-traits = "0.2.18" futures = "0.3.30" +async-trait = "0.1.8" +github = { path = "../provider/github", version = "0.1.0"} diff --git a/bounty/src/api/claim.rs b/bounty/src/api/claim.rs index 52c8c46..34ee906 100644 --- a/bounty/src/api/claim.rs +++ b/bounty/src/api/claim.rs @@ -1,6 +1,9 @@ use crate::api::state::BOUNTY_STATE; use candid::{CandidType, Principal}; use serde::{Deserialize, Serialize}; +use super::state::Contributor; + +use github::{api::{get_issue::IssueResponse, get_merged_details::PrDetailsResponse}, client::IGithubClient}; #[derive(Debug, Serialize, Deserialize, CandidType)] pub enum ClaimError { @@ -10,39 +13,44 @@ pub enum ClaimError { // TODO: remove this after finishing draft impl. #[cfg(test)] -pub struct GithubClient { +pub struct GithubClientMock { principal: Principal, } // TODO: remove this after finishing draft impl. #[cfg(test)] -impl GithubClient { - pub fn new(principal: Principal) -> Self { - GithubClient { principal } - } - - pub async fn get_issue(&self, issue_nbr: i32) -> String { - todo!() - } - pub async fn get_fixed_by(&self, issue_nbr: i32) -> String { - todo!() - } - pub async fn get_is_merged(&self, pr_nbr: i32) -> String { - todo!() - } - pub async fn get_merged_details(&self, pr_nbr: i32) -> String { - todo!() - } +#[async_trait::async_trait] +impl IGithubClient for GithubClientMock { + async fn get_issue(&self, issue_nbr: i32) -> IssueResponse { todo!() } + async fn get_fixed_by(&self, issue_nbr: i32) -> String { todo!() } + async fn get_is_merged(&self, pr_nbr: i32) -> String { todo!() } + async fn get_merged_details(&self, pr_nbr: i32) -> PrDetailsResponse { todo!() } } // FIXME: remove this annotation after finishing draft impl. #[cfg(test)] pub async fn claim_impl( - github_client: GithubClient, + github_client: &dyn IGithubClient, github_issue_id: i32, + github_pr_id: i32, github_token: &str, ) -> Option { - todo!() + let contributor_opt: Option = BOUNTY_STATE.with(|state| { + match state.borrow().as_ref() { + Some(bounty_state) => { + // Access the interested_contributors HashMap from the BountyState + bounty_state.interested_contributors.get(&github_pr_id).cloned() + } + None => panic!("Bounty canister state not initialized") + } + }); + match contributor_opt { + None => Some(ClaimError::PRNotAccepted{github_pr_id}) + , Some(contributor) => { + let issue = github_client.get_issue(github_issue_id).await; + todo!() + } + } } #[cfg(test)] @@ -53,7 +61,7 @@ mod test_claim { use candid::Principal; use futures::executor::block_on; - use super::{claim_impl, ClaimError, GithubClient}; + use super::{claim_impl, ClaimError, GithubClientMock}; fn accept_contributor(principal: &str, crypto_address: &str, github_pr_id: i32) { accept_impl( @@ -76,9 +84,9 @@ mod test_claim { accept_contributor("mxzaz-hqaaa-aaaar-qaada-cai", "contributor_address_1", 1); accept_contributor("n5wcd-faaaa-aaaar-qaaea-cai", "contributor_address_2", 2); - let github_client = GithubClient::new(authority); + let github_client = GithubClientMock{principal: authority}; - let result = block_on(claim_impl(github_client, 2, "GithubToken")); + let result = block_on(claim_impl(&github_client, github_issue_id, 2, "GithubToken")); match result { None => assert!(true), diff --git a/bounty/src/api/state.rs b/bounty/src/api/state.rs index 8af8b94..867d3b1 100644 --- a/bounty/src/api/state.rs +++ b/bounty/src/api/state.rs @@ -15,7 +15,7 @@ pub struct BountyState { pub winner: Option, } -#[derive(Debug, Serialize, Deserialize, CandidType)] +#[derive(Debug, Serialize, Deserialize, CandidType, Clone)] pub struct Contributor { pub address: Principal, pub crypto_address: String, diff --git a/bounty/src/lib.rs b/bounty/src/lib.rs index f5a06d8..0f9549d 100644 --- a/bounty/src/lib.rs +++ b/bounty/src/lib.rs @@ -1,12 +1,12 @@ use candid::Principal; -mod api { +pub mod api { pub mod accept; + pub mod claim; pub mod deposit; pub mod icrc1; pub mod init; pub mod state; - pub mod claim; } use api::accept::accept_impl; diff --git a/provider/github/Cargo.toml b/provider/github/Cargo.toml index 35608d9..23f049e 100644 --- a/provider/github/Cargo.toml +++ b/provider/github/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [lib] -crate-type = ["cdylib"] +crate-type = ["lib", "cdylib", "staticlib"] [dependencies] candid = "0.10.4" @@ -16,4 +16,5 @@ serde = "1.0.199" serde_derive = "1.0.199" serde_json = "1.0.116" serde_bytes = "0.11.14" -regex = "1.5.4" \ No newline at end of file +regex = "1.5.4" +async-trait = "0.1.8" diff --git a/provider/github/src/client.rs b/provider/github/src/client.rs index 47601fd..301971c 100644 --- a/provider/github/src/client.rs +++ b/provider/github/src/client.rs @@ -9,8 +9,17 @@ pub struct GithubClient { pub github_token: String, } -impl GithubClient { - pub async fn get_issue(&self, issue_nbr: i32) -> IssueResponse { +#[async_trait::async_trait] +pub trait IGithubClient { + async fn get_issue(&self, issue_nbr: i32) -> IssueResponse; + async fn get_fixed_by(&self, issue_nbr: i32) -> String; + async fn get_is_merged(&self, pr_nbr: i32) -> String; + async fn get_merged_details(&self, pr_nbr: i32) -> PrDetailsResponse; +} + +#[async_trait::async_trait] +impl IGithubClient for GithubClient { + async fn get_issue(&self, issue_nbr: i32) -> IssueResponse { get_issue_impl( self.owner.clone(), self.repo.clone(), @@ -19,13 +28,13 @@ impl GithubClient { ) .await } - pub async fn get_fixed_by(&self, issue_nbr: i32) -> String { + async fn get_fixed_by(&self, issue_nbr: i32) -> String { get_fixed_by_impl(self.owner.clone(), self.repo.clone(), issue_nbr).await } - pub async fn get_is_merged(&self, pr_nbr: i32) -> String { + async fn get_is_merged(&self, pr_nbr: i32) -> String { get_is_merged_impl(self.owner.clone(), self.repo.clone(), pr_nbr).await } - pub async fn get_merged_details(&self, pr_nbr: i32) -> PrDetailsResponse { + async fn get_merged_details(&self, pr_nbr: i32) -> PrDetailsResponse { get_merge_details_impl( self.owner.clone(), self.repo.clone(), diff --git a/provider/github/src/lib.rs b/provider/github/src/lib.rs index 8edbc54..abb5945 100644 --- a/provider/github/src/lib.rs +++ b/provider/github/src/lib.rs @@ -1,16 +1,15 @@ -mod api { +pub mod api { pub mod get_fixed_by; pub mod get_is_merged; pub mod get_issue; pub mod get_merged_details; } -mod client; -mod utils; +pub mod client; +pub mod utils; use api::get_issue::IssueResponse; use api::get_merged_details::PrDetailsResponse; - -use client::GithubClient; +use client::{GithubClient, IGithubClient}; #[ic_cdk::update] async fn get_issue(github_token: String) -> IssueResponse {