Skip to content

Commit

Permalink
fix register_issue_impl + backend.did
Browse files Browse the repository at this point in the history
  • Loading branch information
manuel-baraschi-peperina committed May 8, 2024
1 parent 47e719d commit 76f4441
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 54 deletions.
6 changes: 3 additions & 3 deletions backend/backend.did
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ type GithubIssueId = text;

type GithubPullRequestId = text;

type BountyAmmount = nat;
type BountyAmount = nat32;

// Bounty Service

Expand Down Expand Up @@ -110,7 +110,7 @@ type UnRegisterIssueErr = variant {
type UnRegisterIssueReceipt = opt UnRegisterIssueErr;

service : (authority: principal) -> {

// GitHub Service
"get_issue": (GithubToken) -> (IssueReceipt);
"get_fixed_by": (GithubToken) -> (FixedByReceipt);
Expand All @@ -121,6 +121,6 @@ service : (authority: principal) -> {
"healthcheck": () -> (text);
"accept": (Contributor, GithubIssueId, GithubPullRequestId) -> (AcceptReceipt);
"deposit": () -> (DepositReceipt);
"register_issue": (Contributor, GithubIssueId, BountyAmmount) -> (RegisterIssueReceipt);
"register_issue": (Contributor, GithubIssueId, BountyAmount) -> (RegisterIssueReceipt);
"unregister_issue": (GithubIssueId) -> (UnRegisterIssueReceipt);
}
93 changes: 42 additions & 51 deletions backend/src/bounty/api/register_issue.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::state::{Bounty, Contributor, Issue, PullRequest, BOUNTY_STATE};

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

#[derive(Debug, Serialize, Deserialize, CandidType)]
Expand All @@ -10,7 +11,11 @@ pub enum RegisterIssueError {

pub type RegisterIssueReceipt = Option<RegisterIssueError>;

pub fn register_issue_impl(github_issue: Issue) -> RegisterIssueReceipt {
pub fn register_issue_impl(
contributor: Contributor,
github_issue_id: String,
amount: Nat,
) -> RegisterIssueReceipt {
print!("Registering issue: {:?}", github_issue);
return BOUNTY_STATE.with(|state| {
if let Some(ref mut bounty_canister) = *state.borrow_mut() {
Expand Down Expand Up @@ -38,66 +43,52 @@ pub fn register_issue_impl(github_issue: Issue) -> RegisterIssueReceipt {
mod test_register_issue {
use super::*;
use crate::bounty::api::init::init_impl;
use candid::Principal;
use candid::{Nat, Principal};
use num_bigint::BigUint;

#[test]
fn test_register_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<RegisterIssueError> = register_issue_impl(Issue {
id: github_issue_id,
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, github_issue_id.clone(), bounty_amount);

assert!(r.is_none());
}
#[test]
fn test_cant_register_issue_twice() {
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(),
},
});
assert!(r1.is_none());
let r2: 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,
//not sure if next line is correct
accepted_prs: Default::default(),
},
});
assert!(r2.is_some());
assert!(matches!(
r2,
Some(RegisterIssueError::CantRegisterIssueTwice)
));

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, github_issue_id.clone(), bounty_amount);

assert!(r.is_none());

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

assert!(r2.is_none());
}
}

0 comments on commit 76f4441

Please sign in to comment.