Skip to content

Commit

Permalink
claim impl: first draft
Browse files Browse the repository at this point in the history
  • Loading branch information
ffakenz committed May 3, 2024
1 parent 559e1c4 commit 9e05cce
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 3 deletions.
111 changes: 111 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions bounty/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ serde_json = "1.0.116"
serde_bytes = "0.11.14"
num-bigint = "0.4.4"
num-traits = "0.2.18"
futures = "0.3.30"
15 changes: 12 additions & 3 deletions bounty/src/api/accept.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
use super::state::{Contributor, BOUNTY_STATE};

pub fn accept_impl(contributor: Contributor, github_pr_id: i32) -> () {
// FIXME check contributor is the owner of the PR
// FIXME: check contributor is the owner of the PR.
BOUNTY_STATE.with(|state| {
if let Some(ref mut bounty_canister) = *state.borrow_mut() {
// Add the contributor to the interested contributors list
bounty_canister.interested_contributors.insert(github_pr_id, contributor);
if bounty_canister.interested_contributors.contains_key(&github_pr_id) {
// do not update if the key is present.
// > The contributor is suppose to be the PR owner and the principal who called accept,
// > thus its fixed.
// FIXME: change response type to include a propper domain error.
// The response should be a Result type (Either).
panic!("Can't accept twice");
} else {
// Add the contributor to the interested contributors list.
bounty_canister.interested_contributors.insert(github_pr_id, contributor);
}
}
});
}
Expand Down
100 changes: 100 additions & 0 deletions bounty/src/api/claim.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
use crate::api::state::BOUNTY_STATE;
use candid::{CandidType, Principal};
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, CandidType)]
pub enum ClaimError {
PRNotAccepted { github_pr_id: i32 },
PRNotMerged { github_pr_id: i32 },
}

// TODO: remove this after finishing draft impl.
#[cfg(test)]
pub struct GithubClient {
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!()
}
}

// FIXME: remove this annotation after finishing draft impl.
#[cfg(test)]
pub async fn claim_impl(
github_client: GithubClient,
github_issue_id: i32,
github_token: &str,
) -> Option<ClaimError> {
todo!()
}

#[cfg(test)]
mod test_claim {
use crate::api::accept::accept_impl;
use crate::api::init::init_impl;
use crate::api::state::{Contributor, BOUNTY_STATE};
use candid::Principal;
use futures::executor::block_on;

use super::{claim_impl, ClaimError, GithubClient};

fn accept_contributor(principal: &str, crypto_address: &str, github_pr_id: i32) {
accept_impl(
Contributor {
address: Principal::from_text(principal).unwrap(),
crypto_address: crypto_address.to_string(),
},
github_pr_id,
);
}

#[test]
fn test_accept() {
let github_issue_id = 123;

let authority = Principal::from_text("rdmx6-jaaaa-aaaaa-aaadq-cai").unwrap();

init_impl(authority, github_issue_id);

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 result = block_on(claim_impl(github_client, 2, "GithubToken"));

match result {
None => assert!(true),
Some(claim_error) => match claim_error {
ClaimError::PRNotAccepted { github_pr_id: _ } => assert!(false),
ClaimError::PRNotMerged { github_pr_id: _ } => assert!(false),
},
}

BOUNTY_STATE.with(|state| {
let bounty_canister = state.borrow();
if let Some(ref bounty_canister) = *bounty_canister {
assert_eq!(bounty_canister.winner.unwrap(), 2);
} else {
panic!("Bounty canister state not initialized");
}
});
}
}
1 change: 1 addition & 0 deletions bounty/src/api/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub fn init_impl(authority: Principal, github_issue_id: i32) -> () {
github_issue_id,
interested_contributors: HashMap::new(),
claimed: false,
winner: None
});
});
}
Expand Down
1 change: 1 addition & 0 deletions bounty/src/api/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct BountyState {
pub github_issue_id: IssueId,
pub interested_contributors: HashMap<PullRequestId, Contributor>,
pub claimed: bool,
pub winner: Option<PullRequestId>,
}

#[derive(Debug, Serialize, Deserialize, CandidType)]
Expand Down
1 change: 1 addition & 0 deletions bounty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod api {
pub mod icrc1;
pub mod init;
pub mod state;
pub mod claim;
}

use api::accept::accept_impl;
Expand Down

0 comments on commit 9e05cce

Please sign in to comment.