Skip to content

Commit

Permalink
Merge pull request #8 from husni-zuhdi/get-blog-from-husni-blog-resou…
Browse files Browse the repository at this point in the history
…rces

Get blog from husni blog resources
  • Loading branch information
husni-zuhdi authored Aug 17, 2024
2 parents 8a18e88 + 55361f5 commit e657913
Show file tree
Hide file tree
Showing 18 changed files with 572 additions and 102 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust-unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Run Unit Test
run: cargo test -- --nocapture
run: cargo test -- --nocapture --test-threads=1

2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ tasks:
test:
summary: Run unit test with logs
cmds:
- RUST_LOG=debug cargo test -- --nocapture
- RUST_LOG=debug cargo test -- --nocapture --test-threads=1
run:
summary: Run application with hot-reload
cmds:
Expand Down
4 changes: 2 additions & 2 deletions cmd/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[package]
name = "cmd"
version = "0.1.1"
version = "0.1.2"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
internal = { path = "../internal", version = "0.1.0"}
internal = { path = "../internal", version = "0.1.2"}
actix-web = "4.4.0"
3 changes: 3 additions & 0 deletions env.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ POSTGRES_PASSWORD="admin-password"
POSTGRES_DB="testing"
POSTGRES_HOST="127.0.0.1"
POSTGRES_PORT="5432"
GITHUB_OWNER=husni-zuhdi
GITHUB_REPO=husni-blog-resources
GITHUB_BRANCH=main
5 changes: 4 additions & 1 deletion internal/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "internal"
version = "0.1.1"
version = "0.1.2"
edition = "2021"
build = "build.rs"

Expand All @@ -17,6 +17,9 @@ serde = { version="1.0.192", features = ["derive"]}
serde_json = "1.0.108"
markdown = "1.0.0-alpha.17"
test-log = "0.2.16"
octocrab = "0.39.0"
http-body-util = "0.1.2"
regex = "1.10.6"

[build-dependencies]
anyhow = "1.0.86"
Expand Down
145 changes: 145 additions & 0 deletions internal/src/api/github.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
use crate::model::data::{BlogData, BlogDataType, Trees};
use crate::utils::{capitalize, md_to_html, replace_gh_link};
use http_body_util::BodyExt;
use log::{debug, error, info};
use octocrab;
use serde_json;
use std::num::IntErrorKind;

/// get_gh_blog_data()
/// An async function that
/// take String of repository owner
/// and String of repository repo
/// and String of repository branch
/// Return an Option of Vector of BlogData
///
/// Example:
/// let owner = "husni-zuhdi".to_string();
/// let repo = "husni-blog-resources".to_string();
/// let branch = "main".to_string();
/// let gh_blog_data = get_gh_blog_list(owner, repo, branch).await?;
pub async fn get_gh_blog_data(
owner: String,
repo: String,
branch: String,
) -> Option<Vec<BlogData>> {
let tree_endpoint = format!(
"https://api.github.com/repos/{}/{}/git/trees/{}",
&owner, &repo, &branch
);
let gh_trees = octocrab::instance()._get(tree_endpoint).await;

let trees_result = match gh_trees {
Ok(val) => {
let body_bytes = val.into_body().collect().await.unwrap().to_bytes();
let body_json = String::from_utf8(body_bytes.to_vec()).unwrap();
let result: Trees = serde_json::from_str(&body_json).unwrap();
Some(result)
}
Err(err) => {
error!("Failed to parse Github Trees result: {}", err);
None
}
};

let mut blog_trees: Vec<BlogData> = Vec::new();
match trees_result {
Some(val) => {
for tree in val.tree {
let blog_path = tree.path;

// Check to make sure the path doesn't have a extention
if !blog_path.contains(".") {
// Get blog id with specification of 3 digit integer
let blog_id = blog_path.get(0..3).unwrap();
let blog_name = blog_path.get(4..).unwrap();

match blog_id.parse::<i32>() {
Ok(_) => {
if &blog_id != &"000" {
info!("Blog Name: {}", &blog_name);
let blog_readme_path = format!("{}/README.md", &blog_path);
let blog_content = octocrab::instance()
.repos(&owner, &repo)
.get_content()
.path(&blog_readme_path)
.r#ref(&branch)
.send()
.await;
match blog_content {
Ok(mut res) => {
let content = res.take_items();
let decoded_content =
&content[0].decoded_content().unwrap().clone();

let name_formated = blog_name.replace("-", " ");
let name = capitalize(&name_formated);
info!("Markdown of {} loaded", &blog_name);

let raw_body =
md_to_html(None, Some(decoded_content.to_string()))
.expect("Failed to convert markdown to html");
debug!("HTML Body of {}: {}", &blog_name, &raw_body);

let gh_blog_link = format!(
"https://github.com/{}/{}/tree/{}/{}",
&owner, &repo, &branch, &blog_path
);
let gh_raw_blog_link = format!(
"https://raw.githubusercontent.com/{}/{}/{}/{}",
&owner, &repo, &branch, &blog_path
);
let body = replace_gh_link(
raw_body,
gh_blog_link,
gh_raw_blog_link,
);

blog_trees.push(BlogData {
id: format!("{}-g", blog_id).to_string(),
name,
source: BlogDataType::Github,
filename: format!(
"https://api.github.com/repos/{}/{}/contents/{}",
&owner, &repo, &blog_readme_path
)
.to_string(),
body,
})
}
Err(err) => {
error!(
"Failed to get Blog content with Blog ID {} and Name {}: {}",
&blog_id, &blog_name, err
)
}
}
}
}
Err(err) => {
if err.kind() == &IntErrorKind::InvalidDigit {
continue;
}
println!("Failed to parse Blog ID: {}", err);
}
};
}
}
}
None => {
error!("failed to filter Github Trees result")
}
};
Some(blog_trees)
}

// Test n
// Nge get semua markdown yang ada di repo
// pub async fn get_github_blogs() -> Vec<BlogData> {
// let repo = octocrab::instance()
// .repos("husni-zuhdi", "husni-blog-resources")
// .get()
// .await
// .expect("Failed to fetch blog resources repo");
// repo.contents_url.expect("Failed to get contents url")
// }
1 change: 1 addition & 0 deletions internal/src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod github;
Loading

0 comments on commit e657913

Please sign in to comment.