Skip to content
Open
Show file tree
Hide file tree
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
26 changes: 26 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ authors = ["Mark Rousskov <mark.simulacrum@gmail.com>"]
edition = "2024"

[workspace]
resolver = "3"
members = ["utils/compiler-p-high"]

[dependencies]
serde_json = "1"
Expand Down
30 changes: 30 additions & 0 deletions utils/compiler-p-high/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[package]
name = "compiler-p-high"
version = "0.1.0"
edition = "2024"

[dependencies]
anyhow = "1.0.98"
async-trait = "0.1.31"
chrono = { version = "0.4.38", default-features = false, features = ["serde"] }
octocrab = { version = "0.44" }
tera = { version = "1.3.1", default-features = false, features = ["builtins"] }
tokio = { version = "1", default-features = false, features = ["macros", "time", "rt"] }
tracing-subscriber = { version = "0.3", default-features = false, features = ["env-filter", "fmt"] }

# Rust team data and its deps
rust_team_data = { git = "https://github.com/rust-lang/team" }
reqwest = { version = "0.12", features = ["json", "blocking"] }
serde = { version = "1", default-features = false, features = ["derive"] }
tracing = "0.1"

# github.rs and its deps
serde_json = "1"
regex = "1"
bytes = "1.10.1"
itertools = "0.14.0"
secrecy = { version = "0.10", default-features = false, features = ["serde"] }
http = "1.4.0"
http-body-util = "0.1.3"
futures = { version = "0.3", default-features = false, features = ["std"] }
url = "2.1.0"
149 changes: 149 additions & 0 deletions utils/compiler-p-high/src/actions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
use chrono::{DateTime, Utc};
use octocrab::Octocrab;
use std::collections::HashMap;
use std::sync::{Arc, LazyLock};

use async_trait::async_trait;
use tera::{Context, Tera};

// TODO: can this crate be replaced with octocrab?
use crate::github::{self, GithubClient, Repository};
use crate::team_data::TeamClient;

#[async_trait]
pub trait Action {
async fn call(&self, octocrab: &Octocrab) -> anyhow::Result<String>;
}

pub struct Step<'a> {
pub name: &'a str,
pub actions: Vec<Query<'a>>,
}

pub struct Query<'a> {
/// Vec of (owner, name)
pub repos: Vec<(&'a str, &'a str)>,
pub queries: Vec<QueryMap<'a>>,
}

#[derive(Copy, Clone)]
pub enum QueryKind {
List,
Count,
}

pub struct QueryMap<'a> {
pub name: &'a str,
pub kind: QueryKind,
pub query: Arc<dyn github::issue_query::IssuesQuery + Send + Sync>,
}

#[derive(Debug, serde::Serialize)]
pub struct IssueDecorator {
pub number: u64,
pub title: String,
pub html_url: String,
pub repo_name: String,
pub labels: String,
pub author: String,
pub team: String,
pub assignees: String,
// Human (readable) timestamps
pub created_at_hts: String,
pub updated_at_hts: String,
pub is_blocked: bool,
}

// TODO: embed templates in binary
// (so the compiled binary can run from everywhere)
pub static TEMPLATES: LazyLock<Tera> = LazyLock::new(|| match Tera::new("templates/*") {
Ok(t) => t,
Err(e) => {
println!("Parsing error(s): {e}");
::std::process::exit(1);
}
});

pub fn to_human(d: DateTime<Utc>) -> String {
let d1 = chrono::Utc::now() - d;
let days = d1.num_days();
if days > 60 {
format!("{} months ago", days / 30)
} else {
format!("about {days} days ago")
}
}

// TODO: try using the octocrab client
#[async_trait]
impl Action for Step<'_> {
async fn call(&self, _octocrab: &Octocrab) -> anyhow::Result<String> {
let mut gh = GithubClient::new_from_env();
gh.set_retry_rate_limit(true);
let team_api = TeamClient::new_from_env();

let mut context = Context::new();
let mut results = HashMap::new();

let mut handles: Vec<tokio::task::JoinHandle<anyhow::Result<(String, QueryKind, Vec<_>)>>> =
Vec::new();
let semaphore = std::sync::Arc::new(tokio::sync::Semaphore::new(5));

for Query { repos, queries } in &self.actions {
for repo in repos {
let repository = Repository {
full_name: format!("{}/{}", repo.0, repo.1),
// These are unused for query.
default_branch: "main".to_string(),
fork: false,
parent: None,
};

for QueryMap { name, kind, query } in queries {
let semaphore = semaphore.clone();
let name = String::from(*name);
let kind = *kind;
let repository = repository.clone();
let gh = gh.clone();
let team_api = team_api.clone();
let query = query.clone();
handles.push(tokio::task::spawn(async move {
let _permit = semaphore.acquire().await?;
let issues = query.query(&repository, &gh, &team_api).await?;
Ok((name, kind, issues))
}));
}
}
}

for handle in handles {
let (name, kind, issues) = handle.await.unwrap()?;
match kind {
QueryKind::List => {
results.entry(name).or_insert(Vec::new()).extend(issues);
}
QueryKind::Count => {
let count = issues.len();
let result = if let Some(value) = context.get(&name) {
value.as_u64().unwrap() + count as u64
} else {
count as u64
};

context.insert(name, &result);
}
}
}

for (name, issues) in &results {
context.insert(name, issues);
}

let date = chrono::Utc::now();
context.insert("CURRENT_DATE", &date);

Ok(TEMPLATES
.render(&format!("{}.tt", self.name), &context)
.unwrap())
}
}
9 changes: 9 additions & 0 deletions utils/compiler-p-high/src/github.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
mod client;
mod issue;
pub mod issue_query;
mod repos;
mod utils;

pub use client::{GithubClient, default_token_from_env};
pub use issue::*;
pub use repos::*;
Loading