diff --git a/Makefile b/Makefile index 82d38f5..ddb40c7 100644 --- a/Makefile +++ b/Makefile @@ -39,7 +39,7 @@ 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; \ @@ -47,6 +47,17 @@ test-2: install 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: diff --git a/backend/backend.did b/backend/backend.did index 907e83a..e7a0fad 100644 --- a/backend/backend.did +++ b/backend/backend.did @@ -11,4 +11,6 @@ type gh_token = text; service : { "get_gh_issue": (gh_token) -> (issue); "get_gh_fixed_by": (gh_token) -> (text); -} \ No newline at end of file + "get_gh_is_merged": (gh_token) -> (text); +} + diff --git a/backend/src/github/api/get_is_merged.rs b/backend/src/github/api/get_is_merged.rs new file mode 100644 index 0000000..f10c2e5 --- /dev/null +++ b/backend/src/github/api/get_is_merged.rs @@ -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 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 + ); + } + } +} + + diff --git a/backend/src/github/client.rs b/backend/src/github/client.rs index d98c1ae..9778430 100644 --- a/backend/src/github/client.rs +++ b/backend/src/github/client.rs @@ -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; @@ -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 + } + } diff --git a/backend/src/lib.rs b/backend/src/lib.rs index 7a5326c..f8252c8 100644 --- a/backend/src/lib.rs +++ b/backend/src/lib.rs @@ -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; @@ -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; } \ No newline at end of file