Skip to content

Commit

Permalink
Export pub github interface
Browse files Browse the repository at this point in the history
To be imported in the bounty package.
  • Loading branch information
ffakenz committed May 4, 2024
1 parent 9e05cce commit 07aeaac
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 52 deletions.
38 changes: 26 additions & 12 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[workspace]
members = [
"provider/github",
"bounty"
"bounty",
]
resolver = "2"
resolver = "2"
2 changes: 2 additions & 0 deletions bounty/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
54 changes: 31 additions & 23 deletions bounty/src/api/claim.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<ClaimError> {
todo!()
let contributor_opt: Option<Contributor> = 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)]
Expand All @@ -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(
Expand All @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion bounty/src/api/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct BountyState {
pub winner: Option<PullRequestId>,
}

#[derive(Debug, Serialize, Deserialize, CandidType)]
#[derive(Debug, Serialize, Deserialize, CandidType, Clone)]
pub struct Contributor {
pub address: Principal,
pub crypto_address: String,
Expand Down
4 changes: 2 additions & 2 deletions bounty/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
5 changes: 3 additions & 2 deletions provider/github/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
regex = "1.5.4"
async-trait = "0.1.8"
19 changes: 14 additions & 5 deletions provider/github/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(),
Expand Down
9 changes: 4 additions & 5 deletions provider/github/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down

0 comments on commit 07aeaac

Please sign in to comment.