Skip to content

Commit

Permalink
Finish Register Issue
Browse files Browse the repository at this point in the history
  • Loading branch information
manuel-baraschi-peperina committed May 10, 2024
1 parent 1a60657 commit 68bae29
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 28 deletions.
16 changes: 13 additions & 3 deletions backend/src/bounty/api/register_issue.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use std::collections::HashMap;

use super::state::IssueId;
use super::state::{Bounty, Contributor, Issue, PullRequest, BOUNTY_STATE};
use super::state::{Bounty, Contributor, Issue, BOUNTY_STATE};

use candid::CandidType;
use candid::Nat;
use serde::{Deserialize, Serialize};

pub type RegisterIssueError = ();

Expand Down Expand Up @@ -67,6 +65,18 @@ mod test_register_issue {
register_issue_impl(contributor, github_issue_id.clone(), bounty_amount);

assert!(r.is_none());

BOUNTY_STATE.with(|state| {
let bounty_canister = state.borrow();
if let Some(ref bounty_canister) = *bounty_canister {
assert!(bounty_canister
.github_issues
.get(&github_issue_id)
.is_some());
} else {
panic!("Bounty canister state not initialized");
}
});
}
#[test]
fn test_cant_register_issue_twice() {
Expand Down
81 changes: 63 additions & 18 deletions backend/src/bounty/api/unregister_issue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use super::state::{Bounty, Contributor, Issue, PullRequest, BOUNTY_STATE};
use crate::bounty::api::register_issue::RegisterIssueError;
use crate::register_issue_impl;
use candid::CandidType;
use candid::Nat;
use num_bigint::BigUint;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, CandidType)]
pub enum UnRegisterIssueError {
IssueNotFound,
Expand Down Expand Up @@ -41,35 +44,77 @@ mod test_unregister_issue {

#[test]
fn test_unregister_issue() {
let authority =
Principal::from_text("t2y5w-qp34w-qixaj-s67wp-syrei-5yqse-xbed6-z5nsd-fszmf-izgt2-lqe")
.unwrap();
let authority = Principal::anonymous();

init_impl(authority);

let github_issue_id = "input-output-hk/hydra/issues/1370".to_string();
let r1: Option<RegisterIssueError> = register_issue_impl(Issue {
id: github_issue_id.clone(),
maintainer: Contributor {
address: Principal::anonymous(),
crypto_address: "0x1234".to_string(),
},
bounty: Bounty {
amount: 100,
winner: None,
accepted_prs: Default::default(),
},
});

let contributor = Contributor {
address: Principal::anonymous(),
crypto_address: "0x1234".to_string(),
};

let bounty_amount: Nat = Nat(BigUint::from(100u32));

let r: Option<RegisterIssueError> = register_issue_impl(
contributor.clone(),
github_issue_id.clone(),
bounty_amount.clone(),
);

assert!(r.is_none());
let r2: Option<UnRegisterIssueError> = unregister_issue_impl(github_issue_id.clone());
assert!(r2.is_none());

BOUNTY_STATE.with(|state| {
let bounty_canister = state.borrow();
if let Some(ref bounty_canister) = *bounty_canister {
assert!(bounty_canister
.github_issues
.get(&github_issue_id)
.is_none());
} else {
panic!("Bounty canister state not initialized");
}
});
}
#[test]
fn test_cant_unregister_a_non_existent_issue() {
let authority =
Principal::from_text("t2y5w-qp34w-qixaj-s67wp-syrei-5yqse-xbed6-z5nsd-fszmf-izgt2-lqe")
.unwrap();
let authority = Principal::anonymous();
init_impl(authority);
let github_issue_id = "input-output-hk/hydra/issues/1370".to_string();
let r: Option<UnRegisterIssueError> = unregister_issue_impl(github_issue_id.clone());
assert!(r.is_some());
assert!(matches!(r, Some(UnRegisterIssueError::IssueNotFound)));
}

#[test]
fn test_unregister_issue_twice() {
let authority = Principal::anonymous();

init_impl(authority);

let github_issue_id = "input-output-hk/hydra/issues/1370".to_string();

let contributor = Contributor {
address: Principal::anonymous(),
crypto_address: "0x1234".to_string(),
};

let bounty_amount: Nat = Nat(BigUint::from(100u32));

let r: Option<RegisterIssueError> = register_issue_impl(
contributor.clone(),
github_issue_id.clone(),
bounty_amount.clone(),
);

assert!(r.is_none());
let r2: Option<UnRegisterIssueError> = unregister_issue_impl(github_issue_id.clone());
assert!(r2.is_none());

let r3: Option<UnRegisterIssueError> = unregister_issue_impl(github_issue_id.clone());
assert!(r3.is_none());
}
}
18 changes: 11 additions & 7 deletions backend/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[macro_use]
extern crate derive_builder;

use candid::Principal;
use candid::{Nat, Principal};

pub mod provider {
pub mod github {
Expand Down Expand Up @@ -38,7 +38,7 @@ use bounty::api::accept::{accept_impl, AcceptReceipt};
use bounty::api::deposit::{deposit_impl, DepositReceipt};
use bounty::api::init::init_impl;
use bounty::api::register_issue::{register_issue_impl, RegisterIssueReceipt};
use bounty::api::state::{Contributor, Issue};
use bounty::api::state::{Contributor, IssueId, PullRequestId};
use bounty::api::unregister_issue::{unregister_issue_impl, UnRegisterIssueReceipt};

// GITHUB SERVICE
Expand Down Expand Up @@ -103,8 +103,8 @@ fn init(authority: Principal) -> () {
#[ic_cdk::update]
fn accept(
contributor: Contributor,
github_issue_id: String,
github_pr_id: String,
github_issue_id: IssueId,
github_pr_id: PullRequestId,
) -> AcceptReceipt {
return accept_impl(contributor, github_issue_id, github_pr_id);
}
Expand All @@ -120,11 +120,15 @@ async fn healthcheck() -> String {
}

#[ic_cdk::update]
fn register_issue(github_issue: Issue) -> RegisterIssueReceipt {
return register_issue_impl(github_issue);
fn register_issue(
contributor: Contributor,
github_issue_id: IssueId,
amount: Nat,
) -> RegisterIssueReceipt {
return register_issue_impl(contributor, github_issue_id, amount);
}

#[ic_cdk::update]
fn unregister_issue(github_issue_id: String) -> UnRegisterIssueReceipt {
fn unregister_issue(github_issue_id: IssueId) -> UnRegisterIssueReceipt {
return unregister_issue_impl(github_issue_id);
}

0 comments on commit 68bae29

Please sign in to comment.