Skip to content

Commit

Permalink
Aplying reviews on Register Issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Daguis committed May 10, 2024
1 parent 76f4441 commit 1a60657
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
50 changes: 29 additions & 21 deletions backend/src/bounty/api/register_issue.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,40 @@
use std::collections::HashMap;

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

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

#[derive(Debug, Serialize, Deserialize, CandidType)]
pub enum RegisterIssueError {
CantRegisterIssueTwice,
}
pub type RegisterIssueError = ();

pub type RegisterIssueReceipt = Option<RegisterIssueError>;

pub fn register_issue_impl(
contributor: Contributor,
github_issue_id: String,
github_issue_id: IssueId,
amount: Nat,
) -> RegisterIssueReceipt {
print!("Registering issue: {:?}", github_issue);
return BOUNTY_STATE.with(|state| {
if let Some(ref mut bounty_canister) = *state.borrow_mut() {
let mut issue_exists = false;

if let Some(ref mut issue) = bounty_canister.github_issues.get_mut(&github_issue.id) {
issue_exists = true;
}

if issue_exists {
Some(RegisterIssueError::CantRegisterIssueTwice)
} else {
let issue_exists = bounty_canister.github_issues.contains_key(&github_issue_id);
if !issue_exists {
let github_issue = Issue {
id: github_issue_id.clone(),
maintainer: contributor,
bounty: Bounty {
amount: amount,
winner: None,
accepted_prs: HashMap::new(),
},
};
// TODO: Check contributor it's registered and github_issue_id exists on github
bounty_canister
.github_issues
.insert(github_issue.id.clone(), github_issue);
None
.insert(github_issue_id.clone(), github_issue);
}
None
} else {
panic!("Bounty canister state not initialized")
}
Expand Down Expand Up @@ -81,13 +83,19 @@ mod test_register_issue {

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

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

assert!(r.is_none());

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

assert!(r2.is_none());
}
Expand Down
4 changes: 2 additions & 2 deletions backend/src/bounty/api/state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;

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

pub type IssueId = String;
Expand All @@ -20,7 +20,7 @@ pub struct PullRequest {

#[derive(Debug, Serialize, Deserialize, CandidType, Clone, Builder)]
pub struct Bounty {
pub amount: i32,
pub amount: Nat,
pub winner: Option<PullRequestId>,
pub accepted_prs: HashMap<PullRequestId, PullRequest>,
}
Expand Down

0 comments on commit 1a60657

Please sign in to comment.