Skip to content

Commit

Permalink
Resolve #2
Browse files Browse the repository at this point in the history
  • Loading branch information
ffakenz committed Apr 27, 2024
1 parent 06fa7c2 commit 5ad5db0
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

13 changes: 12 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ upgrade: build

.PHONY: test
.SILENT: test
test: install
test-1: install
# Call the backend canister to get the GitHub issue and capture the output
@echo "Calling get_gh_issue on backend canister..."
@TMP_FILE=$$(mktemp); \
Expand All @@ -36,6 +36,17 @@ test: install
cat $$TMP_FILE; \
rm -f $$TMP_FILE

.PHONY: test
.SILENT: test
test-2: install
# Call the backend canister to get the GitHub 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: clean
.SILENT: clean
clean:
Expand Down
1 change: 1 addition & 0 deletions backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ ic-cdk-macros = "0.6.0"
serde = "1.0.152"
serde_json = "1.0.93"
serde_bytes = "0.11.9"
regex = "1.5.4"
1 change: 1 addition & 0 deletions backend/backend.did
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ type gh_token = text;

service : {
"get_gh_issue": (gh_token) -> (issue);
"get_gh_fixed_by": (gh_token) -> (text);
}
76 changes: 76 additions & 0 deletions backend/src/github/api/get_fixed_by.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use ic_cdk::api::management_canister::http_request::{
http_request, CanisterHttpRequestArgument, HttpMethod,
};

use super::super::utils::github_host;

use std::collections::HashSet;

use regex::Regex;

// 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 {
// Setup the URL and its query parameters
let url = format!(
"https://{}/{}/{}/issues/{}",
github_host(),
owner,
repo,
issue_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.");

let fixed_by_lines = str_body.lines().fold(HashSet::new(), |mut set, line| {
if line.contains("Fixed by") {
set.insert(line.to_string());
}
set
});

let result = fixed_by_lines.into_iter().collect::<Vec<String>>().join(", ");

if let Some(pull_request) = extract_pull_request(&result) {
return pull_request;
}
return "No PR".to_string();
}
Err((rejection_code, message)) => {
panic!(
"The http_request resulted in an error. RejectionCode: {:?}, Error: {}",
rejection_code, message
);
}
}
}

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();

// Extract the href attribute from the HTML string
if let Some(captures) = re.captures(html) {
if let Some(href) = captures.get(1) {
return Some(href.as_str().to_string());
}
}
None
}
2 changes: 1 addition & 1 deletion backend/src/github/api/get_issue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub async fn get_issue_impl(
method: HttpMethod::GET,
body: None,
max_response_bytes: None,
transform: None, // We'll handle transformation separately
transform: None,
headers: request_headers,
};

Expand Down
9 changes: 9 additions & 0 deletions backend/src/github/client.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::api::get_fixed_by::get_fixed_by_impl;
use super::api::get_issue::get_issue_impl;

use super::utils::IssueResponse;
Expand All @@ -18,4 +19,12 @@ impl GithubClient {
)
.await
}
pub async fn get_fixed_by(&self, issue_nbr: i32) -> String {
get_fixed_by_impl(
self.owner.clone(),
self.repo.clone(),
issue_nbr,
)
.await
}
}
4 changes: 4 additions & 0 deletions backend/src/github/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ pub fn github_api_host() -> String {
return "api.github.com".to_string();
}

pub fn github_host() -> String {
return "github.com".to_string();
}

pub fn mk_request_headers(github_token: String) -> Vec<HttpHeader> {
return vec![
HttpHeader {
Expand Down
10 changes: 10 additions & 0 deletions backend/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod github {
pub mod api {
pub mod get_issue;
pub mod get_fixed_by;
}
pub mod utils;
pub mod client;
Expand All @@ -16,4 +17,13 @@ async fn get_gh_issue(github_token: String) -> IssueResponse {
let issue_nbr = 1404;
let client = GithubClient{owner, repo, github_token};
return client.get_issue(issue_nbr).await;
}

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

0 comments on commit 5ad5db0

Please sign in to comment.