Skip to content

Add nightly generator for reports website #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Nightly Build of Reports Website
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just thinking, but if we're adding a Rust project we should also add Rust checks to the CI. So something that builds generator on PRs and checks for clippy warnings.

i.e: https://github.com/Rust-GCC/bottleboard/blob/main/.github/workflows/check.yml or https://github.com/Rust-GCC/testing/blob/main/.github/workflows/build_check.yml


on:
workflow_dispatch:
schedule:
- cron: '0 0 * * *' # Every night at midnight

jobs:
run:
name: Build
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v3

- name: Run generator
env:
REPORTS: https://github.com/Rust-GCC/reporting
WEBSITE: ..
run: |
cd generator
cargo run
- name: Commit changes
uses: EndBug/add-and-commit@v9
with:
default_author: nightly_build_gha
message: Nightly reports generation
add: './_posts/*.md'
1 change: 1 addition & 0 deletions generator/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target/
545 changes: 545 additions & 0 deletions generator/Cargo.lock

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions generator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "generator"
version = "0.1.0"
edition = "2021"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing an author field here :D


[dependencies]
clap = { version = "4.0.26", features = [ "derive", "env"] }
git2 = "0.15.0"
mktemp = "0.5.0"
21 changes: 21 additions & 0 deletions generator/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use std::path::PathBuf;

#[derive(Debug)]
pub enum Error {
CloneFail(git2::Error),
IO(std::io::Error),
InvalidDatePrefix(PathBuf, String),
NoDateInOrgReport(PathBuf),
}

impl From<git2::Error> for Error {
fn from(error: git2::Error) -> Error {
Error::CloneFail(error)
}
}

impl From<std::io::Error> for Error {
fn from(error: std::io::Error) -> Error {
Error::IO(error)
}
}
29 changes: 29 additions & 0 deletions generator/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use std::path::PathBuf;

use clap::Parser;

mod error;
mod reports;

use error::Error;

#[derive(Parser)]
struct Cli {
#[arg(short, long, env = "WEBSITE")]
website: PathBuf,
#[arg(short, long, env = "REPORTS")]
reports: String,
}

fn main() -> Result<(), Error> {
let cli = Cli::parse();
let reports_org_path = reports::clone(cli.reports)?;
let reports_org_list = reports::find(reports_org_path)?;
for report in reports_org_list {
match reports::convert(report, &cli.website) {
Ok(report_md) => {},
Err(e) => eprintln!("{:?}", e),
};
}
Comment on lines +22 to +27
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should just display the monthly and yearly reports on the website. Otherwise, this would create too much noise. We can probably do a .filter() and filter on the name list. And since we have to generate markdown headers which contain yearly or monthly tag, we can do it twice with a different filter and act accordingly.

Ok(())
}
62 changes: 62 additions & 0 deletions generator/src/reports.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use std::path::{Path, PathBuf};
use std::process::Command;

use std::fs;

use git2::Repository;
use mktemp::Temp;

use crate::error::Error;

pub fn clone(reports_repo: String) -> Result<PathBuf, Error> {
let path_to_repo = Temp::new_dir()?.to_path_buf();
Repository::clone(&reports_repo, &path_to_repo)?;
Ok(path_to_repo)
}

pub fn find(reports_org_path: PathBuf) -> Result<Vec<PathBuf>, Error> {
let mut results = Vec::new();
for entry in fs::read_dir(reports_org_path)? {
let entry = entry?;
let path = entry.path();
if let Some(Some("org")) = path.extension().map(|osstr| osstr.to_str()) {
results.push(path);
}
}
Ok(results)
}

fn get_date(report_org_path: &Path) -> Result<String, Error> {
let content = fs::read_to_string(report_org_path)?;
for line in content.lines() {
if line.starts_with("#+date:") {
let date = line
.split(':')
.nth(1)
.ok_or(Error::InvalidDatePrefix(report_org_path.to_path_buf(), line.to_string()))?
.trim()
.to_string();
return Ok(date);
}
}
Err(Error::NoDateInOrgReport(report_org_path.to_path_buf()))
}

pub fn convert(report_org_path: PathBuf, website: &Path) -> Result<PathBuf, Error> {
let mut report_md_filename = get_date(&report_org_path)?;
report_md_filename.push_str("-");
report_md_filename.push_str(report_org_path.file_name().unwrap().to_str().unwrap());
let mut report_md_path = website
.join("_posts")
.join(PathBuf::from(report_md_filename));
report_md_path.set_extension("md");
Command::new("pandoc")
.arg("--from=org")
.arg("--to=gfm")
.arg(report_org_path)
.arg("-o")
.arg(&report_md_path)
.spawn()?;
dbg!(&report_md_path);
Ok(report_md_path)
}