Skip to content

Commit

Permalink
Attempt to solve #3
Browse files Browse the repository at this point in the history
  • Loading branch information
Daguis committed Apr 27, 2024
1 parent 5ad5db0 commit b0063aa
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 2 deletions.
13 changes: 12 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,25 @@ test-1: install
.PHONY: test
.SILENT: test
test-2: install
# Call the backend canister to get the GitHub issue and capture the output
# Call the backend canister to get the GitHub PR that close some issue and capture the output
@echo "Calling get_gh_fixed_by on backend canister..."
@TMP_FILE=$$(mktemp); \
dfx canister call backend get_gh_fixed_by '("${GITHUB_TOKEN}")' > $$TMP_FILE; \
echo "get_gh_fixed_by response:"; \
cat $$TMP_FILE; \
rm -f $$TMP_FILE

.PHONY: test
.SILENT: test
test-3: install
# Call the backend canister to get the GitHub PR merge status and capture the output
@echo "Calling get_gh_is_merged on backend canister..."
@TMP_FILE=$$(mktemp); \
dfx canister call backend get_gh_is_merged '("${GITHUB_TOKEN}")' > $$TMP_FILE; \
echo "get_gh_is_merged response:"; \
cat $$TMP_FILE; \
rm -f $$TMP_FILE

.PHONY: clean
.SILENT: clean
clean:
Expand Down
4 changes: 3 additions & 1 deletion backend/backend.did
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ type gh_token = text;
service : {
"get_gh_issue": (gh_token) -> (issue);
"get_gh_fixed_by": (gh_token) -> (text);
}
"get_gh_is_merged": (gh_token) -> (text);
}

49 changes: 49 additions & 0 deletions backend/src/github/api/get_is_merged.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use ic_cdk::api::management_canister::http_request::{
http_request, CanisterHttpRequestArgument, HttpMethod,
};

use super::super::utils::github_api_host;

//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 {
// Setup the URL and its query parameters
let url = format!(
"https://{}/repos/{}/{}/pulls/{}/merge",
github_api_host(),
owner,
repo,
pr_nbr
);

// Create the request argument
let request = CanisterHttpRequestArgument {
url: url.to_string(),
method: HttpMethod::GET,
body: None,
max_response_bytes: None,
transform: None,
headers: vec![],
};

// Make the HTTP request and wait for the response
match http_request(request).await {
Ok((response,)) => {
//We need to decode that Vec<u8> that is the body into readable text.
//To do this, we:
// 1. Call `String::from_utf8()` on response.body
// 3. We use a switch to explicitly call out both cases of decoding the Blob into ?Text
let str_body = String::from_utf8(response.body)
.expect("Transformed response is not UTF-8 encoded.");

return str_body;
}
Err((rejection_code, message)) => {
panic!(
"The http_request resulted in an error. RejectionCode: {:?}, Error: {}",
rejection_code, message
);
}
}
}


10 changes: 10 additions & 0 deletions backend/src/github/client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::api::get_fixed_by::get_fixed_by_impl;
use super::api::get_issue::get_issue_impl;
use super::api::get_is_merged::get_is_merged_impl;

use super::utils::IssueResponse;

Expand Down Expand Up @@ -27,4 +28,13 @@ impl GithubClient {
)
.await
}
pub async fn get_is_merged(&self, pr_nbr: i32) -> String {
get_is_merged_impl(
self.owner.clone(),
self.repo.clone(),
pr_nbr,
)
.await
}

}
10 changes: 10 additions & 0 deletions backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod github {
pub mod api {
pub mod get_issue;
pub mod get_fixed_by;
pub mod get_is_merged;
}
pub mod utils;
pub mod client;
Expand All @@ -26,4 +27,13 @@ async fn get_gh_fixed_by(github_token: String) -> String {
let issue_nbr = 1370;
let client = GithubClient{owner, repo, github_token};
return client.get_fixed_by(issue_nbr).await;
}

#[ic_cdk::update]
async fn get_gh_is_merged(github_token: String) -> String {
let owner = "input-output-hk".to_string();
let repo = "hydra".to_string();
let pr_nbr = 1266;
let client = GithubClient{owner, repo, github_token};
return client.get_is_merged(pr_nbr).await;
}

0 comments on commit b0063aa

Please sign in to comment.