Skip to content

Commit

Permalink
Add FixedByReceipt
Browse files Browse the repository at this point in the history
  • Loading branch information
ffakenz committed May 6, 2024
1 parent 8acb7b6 commit f420d82
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 12 deletions.
11 changes: 10 additions & 1 deletion backend/backend.did
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ type IssueLink = record {
href: opt text;
};

type FixedByErr = variant {
IssueNotFound : record { issue_nbr : int32 };
};

type FixedByReceipt = variant {
Err: FixedByErr;
Ok: text;
};

type PrDetailsResponse = record {
state: opt text;
closed_at: opt text;
Expand Down Expand Up @@ -53,7 +62,7 @@ type DepositErr = variant {
service : (authority: principal) -> {
// GitHub Service
"get_issue": (GithubToken) -> (Issue);
"get_fixed_by": (GithubToken) -> (text);
"get_fixed_by": (GithubToken) -> (FixedByReceipt);
"get_is_merged": (GithubToken) -> (text);
"get_merged_details": (GithubToken) -> (PrDetailsResponse);
// Bounty Service
Expand Down
3 changes: 2 additions & 1 deletion backend/src/bounty/api/claim.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::bounty::api::state::{Contributor, IssueId, PullRequestId, BOUNTY_STATE};
use crate::provider::github::api::get_fixed_by::GetFixedByError;
use candid::{CandidType, Principal};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -27,7 +28,7 @@ impl IGithubClient for GithubClientMock {
async fn get_issue(&self, issue_nbr: i32) -> IssueResponse {
todo!()
}
async fn get_fixed_by(&self, issue_nbr: i32) -> String {
async fn get_fixed_by(&self, issue_nbr: i32) -> Result<String, GetFixedByError> {
todo!()
}
async fn get_is_merged(&self, pr_nbr: i32) -> String {
Expand Down
5 changes: 2 additions & 3 deletions backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ extern crate derive_builder;

use candid::Principal;

// GITHUB SERVICE
pub mod provider {
pub mod github {
pub mod api {
Expand All @@ -16,11 +15,11 @@ pub mod provider {
pub mod utils;
}
}
use provider::github::api::get_fixed_by::GetFixedByError;
use provider::github::api::get_issue::IssueResponse;
use provider::github::api::get_merged_details::PrDetailsResponse;
use provider::github::client::{GithubClient, IGithubClient};

// BOUNTY SERVICE
pub mod bounty {
pub mod api {
pub mod accept;
Expand Down Expand Up @@ -52,7 +51,7 @@ async fn get_issue(github_token: String) -> IssueResponse {
}

#[ic_cdk::update]
async fn get_fixed_by(github_token: String) -> String {
async fn get_fixed_by(github_token: String) -> Result<String, GetFixedByError> {
let owner = "input-output-hk".to_string();
let repo = "hydra".to_string();
let issue_nbr = 1370;
Expand Down
20 changes: 16 additions & 4 deletions backend/src/provider/github/api/get_fixed_by.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@ use ic_cdk::api::management_canister::http_request::{
http_request, CanisterHttpRequestArgument, HttpMethod,
};

use super::super::utils::github_host;
use crate::provider::github::utils::github_host;
use candid::CandidType;
use serde::{Deserialize, Serialize};

use std::collections::HashSet;

use regex::Regex;

#[derive(Debug, Serialize, Deserialize, CandidType)]
pub enum GetFixedByError {
IssueNotFound { issue_nbr: i32 },
}

// curl https://github.com/input-output-hk/hydra/issues/1370
pub async fn get_fixed_by_impl(owner: String, repo: String, issue_nbr: i32) -> String {
pub async fn get_fixed_by_impl(owner: String, repo: String, issue_nbr: i32) -> Result<String, GetFixedByError> {
// Setup the URL and its query parameters
let url = format!(
"https://{}/{}/{}/issues/{}",
Expand Down Expand Up @@ -55,9 +62,10 @@ pub async fn get_fixed_by_impl(owner: String, repo: String, issue_nbr: i32) -> S
.join(", ");

if let Some(pull_request) = extract_pull_request(&result) {
return pull_request;
return Ok(remove_github_prefix(&pull_request));
}
return "No PR".to_string();
return Err(GetFixedByError::IssueNotFound{issue_nbr});

}
Err((rejection_code, message)) => {
panic!(
Expand All @@ -68,6 +76,10 @@ pub async fn get_fixed_by_impl(owner: String, repo: String, issue_nbr: i32) -> S
}
}

fn remove_github_prefix(url: &str) -> String {
url.replace("https://github.com/", "")
}

fn extract_pull_request(html: &str) -> Option<String> {
// Define a regular expression pattern to match the href attribute
let re = Regex::new(r#"<a\s+[^>]*?href="(.*?)"[^>]*?>"#).unwrap();
Expand Down
6 changes: 3 additions & 3 deletions backend/src/provider/github/client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::api::get_fixed_by::get_fixed_by_impl;
use super::api::get_fixed_by::{get_fixed_by_impl, GetFixedByError};
use super::api::get_is_merged::get_is_merged_impl;
use super::api::get_issue::{get_issue_impl, IssueResponse};
use super::api::get_merged_details::{get_merge_details_impl, PrDetailsResponse};
Expand All @@ -12,7 +12,7 @@ pub struct GithubClient {
#[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_fixed_by(&self, issue_nbr: i32) -> Result<String, GetFixedByError>;
async fn get_is_merged(&self, pr_nbr: i32) -> String;
async fn get_merged_details(&self, pr_nbr: i32) -> PrDetailsResponse;
}
Expand All @@ -28,7 +28,7 @@ impl IGithubClient for GithubClient {
)
.await
}
async fn get_fixed_by(&self, issue_nbr: i32) -> String {
async fn get_fixed_by(&self, issue_nbr: i32) -> Result<String, GetFixedByError> {
get_fixed_by_impl(self.owner.clone(), self.repo.clone(), issue_nbr).await
}
async fn get_is_merged(&self, pr_nbr: i32) -> String {
Expand Down

0 comments on commit f420d82

Please sign in to comment.