Skip to content

Commit

Permalink
Add error responses to github provider api calls
Browse files Browse the repository at this point in the history
  • Loading branch information
ffakenz committed May 6, 2024
1 parent 864ea75 commit 09c1242
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 8,240 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ node_modules/
dist/

# environment variables
.env
.env

# lock files
Cargo.lock
package-lock.json
4 changes: 2 additions & 2 deletions Cargo.lock

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

14 changes: 7 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ node_modules:

.PHONY: build
.SILENT: build
build: node_modules
build: # node_modules
dfx canister create \
--with-cycles 1_000_000_000_000 \
--specified-id rdmx6-jaaaa-aaaaa-aaadq-cai \
Expand Down Expand Up @@ -55,7 +55,7 @@ clean:
# tests
.PHONY: test-1
.SILENT: test-1
test-1: install
test-1: # install
# Call the backend canister to get the GitHub issue and capture the output
@echo "Calling get_issue on backend canister..."
@TMP_FILE=$$(mktemp); \
Expand All @@ -66,7 +66,7 @@ test-1: install

.PHONY: test-2
.SILENT: test-2
test-2: install
test-2: # install
# Call the backend canister to get the GitHub PR that close some issue and capture the output
@echo "Calling get_fixed_by on backend canister..."
@TMP_FILE=$$(mktemp); \
Expand All @@ -77,7 +77,7 @@ test-2: install

.PHONY: test-3
.SILENT: test-3
test-3: install
test-3: # install
# Call the backend canister to get the GitHub PR merge status and capture the output
@echo "Calling get_is_merged on backend canister..."
@TMP_FILE=$$(mktemp); \
Expand All @@ -88,7 +88,7 @@ test-3: install

.PHONY: test-4
.SILENT: test-4
test-4: install
test-4: # install
# Call the backend canister to get the GitHub closing PR details and capture the output
@echo "Calling get_merged_details on backend canister..."
@TMP_FILE=$$(mktemp); \
Expand All @@ -99,7 +99,7 @@ test-4: install

.PHONY: test-a
.SILENT: test-a
test-a: install
test-a: # install
# Call the backend canister for healthcheck and capture the output
@echo "Calling healthcheck on backend canister..."
@TMP_FILE=$$(mktemp); \
Expand All @@ -110,5 +110,5 @@ test-a: install

.PHONY: test-deposit
.SILENT: test-deposit
test-deposit: install
test-deposit: # install
./make/test/deposit.sh
34 changes: 31 additions & 3 deletions backend/backend.did
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,41 @@ type IssueLink = record {

type FixedByErr = variant {
IssueNotFound : record { github_issue_id : text };
Rejected : record { error_message : text };
};

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

type IsMergedErr = variant {
Rejected : record { error_message : text };
};

type IsMergedReceipt = variant {
Err: IsMergedErr;
Ok: text;
};

type IssueErr = variant {
Rejected : record { error_message : text };
};

type IssueReceipt = variant {
Err: IssueErr;
Ok: Issue;
};

type MergeDetailsErr = variant {
Rejected : record { error_message : text };
};

type MergeDetailsReceipt = variant {
Err: MergeDetailsErr;
Ok: PrDetailsResponse;
};

type PrDetailsResponse = record {
state: opt text;
closed_at: opt text;
Expand Down Expand Up @@ -61,10 +89,10 @@ type DepositErr = variant {

service : (authority: principal) -> {
// GitHub Service
"get_issue": (GithubToken) -> (Issue);
"get_issue": (GithubToken) -> (IssueReceipt);
"get_fixed_by": (GithubToken) -> (FixedByReceipt);
"get_is_merged": (GithubToken) -> (text);
"get_merged_details": (GithubToken) -> (PrDetailsResponse);
"get_is_merged": (GithubToken) -> (IsMergedReceipt);
"get_merged_details": (GithubToken) -> (MergeDetailsReceipt);
// Bounty Service
"healthcheck": () -> (text);
"accept": (Contributor, github_issue_id: text, github_pr_id: text) -> ();
Expand Down
25 changes: 15 additions & 10 deletions backend/src/bounty/api/claim.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::bounty::api::state::{Contributor, IssueId, PullRequestId, BOUNTY_STATE};
use crate::provider::github::api::get_fixed_by::GetFixedByError;
use crate::provider::github::api::get_fixed_by::FixedByErr;
use crate::provider::github::api::get_is_merged::IsMergedErr;
use crate::provider::github::api::get_issue::IssueErr;
use crate::provider::github::api::get_merged_details::MergeDetailsErr;
use candid::{CandidType, Principal};
use regex::Regex;
use serde::{Deserialize, Serialize};
Expand All @@ -26,16 +29,16 @@ pub struct GithubClientMock {
#[cfg(test)]
#[async_trait::async_trait]
impl IGithubClient for GithubClientMock {
async fn get_issue(&self, issue_nbr: i32) -> IssueResponse {
async fn get_issue(&self, issue_nbr: i32) -> Result<IssueResponse, IssueErr> {
todo!()
}
async fn get_fixed_by(&self, issue_nbr: i32) -> Result<String, GetFixedByError> {
async fn get_fixed_by(&self, issue_nbr: i32) -> Result<String, FixedByErr> {
todo!()
}
async fn get_is_merged(&self, pr_nbr: i32) -> String {
async fn get_is_merged(&self, pr_nbr: i32) -> Result<String, IsMergedErr> {
todo!()
}
async fn get_merged_details(&self, pr_nbr: i32) -> PrDetailsResponse {
async fn get_merged_details(&self, pr_nbr: i32) -> Result<PrDetailsResponse, MergeDetailsErr> {
todo!()
}
}
Expand All @@ -47,7 +50,7 @@ pub async fn claim_impl(
github_issue_id: IssueId,
github_pr_id: PullRequestId,
) -> Option<ClaimError> {
use crate::bounty::api::state::Issue;
use crate::{bounty::api::state::Issue, provider::github::api::get_merged_details::MergeDetailsErr};

let issue_opt: Option<Issue> = BOUNTY_STATE.with(|state| {
match state.borrow().as_ref() {
Expand All @@ -70,10 +73,12 @@ pub async fn claim_impl(
github_pr_id: github_pr_id.clone(),
}),
Some(pull_request) => {
let pr_response: PrDetailsResponse =
github_client.get_merged_details(extract_pull_number(&github_pr_id).unwrap()).await;
let issue_response: IssueResponse =
github_client.get_issue(extract_issue_number(&github_issue_id).unwrap()).await;
let pr_response: Result<PrDetailsResponse, MergeDetailsErr> = github_client
.get_merged_details(extract_pull_number(&github_pr_id).unwrap())
.await;
let issue_response: Result<IssueResponse, IssueErr> = github_client
.get_issue(extract_issue_number(&github_issue_id).unwrap())
.await;

todo!()
}
Expand Down
15 changes: 8 additions & 7 deletions backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ 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::api::get_fixed_by::FixedByErr;
use provider::github::api::get_is_merged::IsMergedErr;
use provider::github::api::get_issue::{IssueErr, IssueResponse};
use provider::github::api::get_merged_details::{MergeDetailsErr, PrDetailsResponse};
use provider::github::client::{GithubClient, IGithubClient};

pub mod bounty {
Expand All @@ -38,7 +39,7 @@ use bounty::api::state::Contributor;

// GITHUB SERVICE
#[ic_cdk::update]
async fn get_issue(github_token: String) -> IssueResponse {
async fn get_issue(github_token: String) -> Result<IssueResponse, IssueErr> {
let owner = "input-output-hk".to_string();
let repo = "hydra".to_string();
let issue_nbr = 1218;
Expand All @@ -51,7 +52,7 @@ async fn get_issue(github_token: String) -> IssueResponse {
}

#[ic_cdk::update]
async fn get_fixed_by(github_token: String) -> Result<String, GetFixedByError> {
async fn get_fixed_by(github_token: String) -> Result<String, FixedByErr> {
let owner = "input-output-hk".to_string();
let repo = "hydra".to_string();
let issue_nbr = 1370;
Expand All @@ -64,7 +65,7 @@ async fn get_fixed_by(github_token: String) -> Result<String, GetFixedByError> {
}

#[ic_cdk::update]
async fn get_is_merged(github_token: String) -> String {
async fn get_is_merged(github_token: String) -> Result<String, IsMergedErr> {
let owner = "input-output-hk".to_string();
let repo = "hydra".to_string();
let pr_nbr = 1266;
Expand All @@ -77,7 +78,7 @@ async fn get_is_merged(github_token: String) -> String {
}

#[ic_cdk::update]
async fn get_merged_details(github_token: String) -> PrDetailsResponse {
async fn get_merged_details(github_token: String) -> Result<PrDetailsResponse, MergeDetailsErr> {
let owner = "input-output-hk".to_string();
let repo = "hydra".to_string();
let pr_nbr = 1266;
Expand Down
10 changes: 6 additions & 4 deletions backend/src/provider/github/api/get_fixed_by.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ use std::collections::HashSet;
use regex::Regex;

#[derive(Debug, Serialize, Deserialize, CandidType)]
pub enum GetFixedByError {
pub enum FixedByErr {
IssueNotFound { github_issue_id: String },
Rejected { error_message: String },
}

// curl https://github.com/input-output-hk/hydra/issues/1370
pub async fn get_fixed_by_impl(owner: String, repo: String, issue_nbr: i32) -> Result<String, GetFixedByError> {
pub async fn get_fixed_by_impl(owner: String, repo: String, issue_nbr: i32) -> Result<String, FixedByErr> {
// Setup the URL and its query parameters
let url = format!(
"https://{}/{}/{}/issues/{}",
Expand Down Expand Up @@ -72,14 +73,15 @@ pub async fn get_fixed_by_impl(owner: String, repo: String, issue_nbr: i32) -> R
repo,
issue_nbr
);
return Err(GetFixedByError::IssueNotFound{github_issue_id: issue_not_found});
return Err(FixedByErr::IssueNotFound{github_issue_id: issue_not_found});

}
Err((rejection_code, message)) => {
panic!(
let error_message = format!(
"The http_request resulted in an error. RejectionCode: {:?}, Error: {}",
rejection_code, message
);
return Err(FixedByErr::Rejected{error_message});
}
}
}
Expand Down
14 changes: 11 additions & 3 deletions backend/src/provider/github/api/get_is_merged.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@ use ic_cdk::api::management_canister::http_request::{
};

use super::super::utils::github_api_host;
use candid::CandidType;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, CandidType)]
pub enum IsMergedErr {
Rejected { error_message: String },
}

//https://api.github.com/repos/input-output-hk/hydra/pulls/1266/merge
pub async fn get_is_merged_impl(owner: String, repo: String, pr_nbr: i32) -> String {
pub async fn get_is_merged_impl(owner: String, repo: String, pr_nbr: i32) -> Result<String, IsMergedErr> {
// Setup the URL and its query parameters
let url = format!(
"https://{}/repos/{}/{}/pulls/{}/merge",
Expand All @@ -31,13 +38,14 @@ pub async fn get_is_merged_impl(owner: String, repo: String, pr_nbr: i32) -> Str
// Make the HTTP request and wait for the response
match http_request(request, cycles).await {
Ok((response,)) => {
return response.status.to_string();
Ok(response.status.to_string())
}
Err((rejection_code, message)) => {
panic!(
let error_message = format!(
"The http_request resulted in an error. RejectionCode: {:?}, Error: {}",
rejection_code, message
);
Err(IsMergedErr::Rejected{error_message})
}
}
}
12 changes: 9 additions & 3 deletions backend/src/provider/github/api/get_issue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ use serde::{Deserialize, Serialize};

use super::super::utils::{github_api_host, mk_request_headers};

#[derive(Debug, Serialize, Deserialize, CandidType)]
pub enum IssueErr {
Rejected { error_message: String },
}

// Define the IssueResponse struct to represent the transformed response
#[derive(Debug, Serialize, Deserialize, CandidType)]
pub struct IssueResponse {
Expand All @@ -24,7 +29,7 @@ pub async fn get_issue_impl(
repo: String,
issue_nbr: i32,
github_token: String,
) -> IssueResponse {
) -> Result<IssueResponse, IssueErr> {
// Setup the URL and its query parameters
let url = format!(
"https://{}/repos/{}/{}/issues/{}",
Expand Down Expand Up @@ -60,13 +65,14 @@ pub async fn get_issue_impl(
println!("Transformed response: {:?}", transformed_response);

// Return the transformed response
transformed_response
Ok(transformed_response)
}
Err((rejection_code, message)) => {
panic!(
let error_message = format!(
"The http_request resulted in an error. RejectionCode: {:?}, Error: {}",
rejection_code, message
);
Err(IssueErr::Rejected { error_message })
}
}
}
Expand Down
12 changes: 9 additions & 3 deletions backend/src/provider/github/api/get_merged_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,17 @@ pub struct User {
pub id: u64,
}

#[derive(Debug, Serialize, Deserialize, CandidType)]
pub enum MergeDetailsErr {
Rejected { error_message: String },
}

pub async fn get_merge_details_impl(
owner: String,
repo: String,
pr_nbr: i32,
github_token: String,
) -> PrDetailsResponse {
) -> Result<PrDetailsResponse, MergeDetailsErr> {
// Setup the URL and its query parameters
let url = format!(
"https://{}/repos/{}/{}/pulls/{}",
Expand Down Expand Up @@ -77,13 +82,14 @@ pub async fn get_merge_details_impl(
println!("Transformed response: {:?}", transformed_response);

// Return the transformed response
transformed_response
Ok(transformed_response)
}
Err((rejection_code, message)) => {
panic!(
let error_message = format!(
"The http_request resulted in an error. RejectionCode: {:?}, Error: {}",
rejection_code, message
);
Err(MergeDetailsErr::Rejected { error_message })
}
}
}
Expand Down
Loading

0 comments on commit 09c1242

Please sign in to comment.