Externalize contributor fetching

This commit is contained in:
Laurenz 2023-05-17 14:31:55 +02:00
parent d14d1e867f
commit 8971588486
5 changed files with 18 additions and 125 deletions

105
Cargo.lock generated
View File

@ -122,12 +122,6 @@ version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b7e4c2464d97fe331d41de9d5db0def0a96f4d823b8b32a2efd503578988973"
[[package]]
name = "base64"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8"
[[package]]
name = "base64"
version = "0.21.0"
@ -1646,21 +1640,6 @@ dependencies = [
"bytemuck",
]
[[package]]
name = "ring"
version = "0.16.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3053cf52e236a3ed746dfc745aa9cacf1b791d846bdaf412f60a8d7d6e17c8fc"
dependencies = [
"cc",
"libc",
"once_cell",
"spin",
"untrusted",
"web-sys",
"winapi",
]
[[package]]
name = "roff"
version = "0.2.1"
@ -1718,18 +1697,6 @@ dependencies = [
"windows-sys 0.48.0",
]
[[package]]
name = "rustls"
version = "0.20.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fff78fc74d175294f4e83b28343315ffcfb114b156f0185e9741cb5570f50e2f"
dependencies = [
"log",
"ring",
"sct",
"webpki",
]
[[package]]
name = "rustversion"
version = "1.0.12"
@ -1779,16 +1746,6 @@ version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1792db035ce95be60c3f8853017b3999209281c24e2ba5bc8e59bf97a0c590c1"
[[package]]
name = "sct"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d53dcdb7c9f8158937a7981b48accfd39a43af418591a5d008c7b22b5e1b7ca4"
dependencies = [
"ring",
"untrusted",
]
[[package]]
name = "semver"
version = "1.0.17"
@ -1892,12 +1849,6 @@ version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0"
[[package]]
name = "spin"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
[[package]]
name = "stable_deref_trait"
version = "1.2.0"
@ -2365,7 +2316,6 @@ dependencies = [
"typst-library",
"unicode_names2",
"unscanny",
"ureq",
"yaml-front-matter",
]
@ -2539,30 +2489,6 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e9df2af067a7953e9c3831320f35c1cc0600c30d44d9f7a12b01db1cd88d6b47"
[[package]]
name = "untrusted"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a"
[[package]]
name = "ureq"
version = "2.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "338b31dd1314f68f3aabf3ed57ab922df95ffcd902476ca7ba3c4ce7b908c46d"
dependencies = [
"base64 0.13.1",
"flate2",
"log",
"once_cell",
"rustls",
"serde",
"serde_json",
"url",
"webpki",
"webpki-roots",
]
[[package]]
name = "url"
version = "2.3.1"
@ -2580,7 +2506,7 @@ version = "0.32.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b44e14b7678bcc5947b397991432d0c4e02a103958a0ed5e1b9b961ddd08b21"
dependencies = [
"base64 0.21.0",
"base64",
"log",
"pico-args",
"usvg-parser",
@ -2722,35 +2648,6 @@ version = "0.2.84"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0046fef7e28c3804e5e38bfa31ea2a0f73905319b677e57ebe37e49358989b5d"
[[package]]
name = "web-sys"
version = "0.3.61"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e33b99f4b23ba3eec1a53ac264e35a755f00e966e0065077d6027c0f575b0b97"
dependencies = [
"js-sys",
"wasm-bindgen",
]
[[package]]
name = "webpki"
version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f095d78192e208183081cc07bc5515ef55216397af48b873e5edcd72637fa1bd"
dependencies = [
"ring",
"untrusted",
]
[[package]]
name = "webpki-roots"
version = "0.22.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6c71e40d7d2c34a5106301fb632274ca37242cd0c9d3e64dbece371a40a2d87"
dependencies = [
"webpki",
]
[[package]]
name = "weezl"
version = "0.1.7"

View File

@ -22,5 +22,4 @@ serde = { version = "1", features = ["derive"] }
serde_yaml = "0.8"
unicode_names2 = "0.6.0"
unscanny = "0.1"
ureq = { version = "2.6", features = ["json"] }
yaml-front-matter = "0.1"

View File

@ -4,23 +4,15 @@ use std::fmt::Write;
use serde::Deserialize;
use super::Html;
use super::{Html, Resolver};
/// Build HTML detailing the contributors between two tags.
pub fn contributors(from: &str, to: &str) -> Option<Html> {
pub fn contributors(resolver: &dyn Resolver, from: &str, to: &str) -> Option<Html> {
let staff = ["laurmaedje", "reknih"];
let url = format!("https://api.github.com/repos/typst/typst/compare/{from}...{to}");
let response: Response = ureq::get(&url)
.set("X-GitHub-Api-Version", "2022-11-28")
.call()
.ok()?
.into_json()
.ok()?;
// Determine number of contributions per person.
let mut contributors = HashMap::<String, Contributor>::new();
for commit in response.commits {
for commit in resolver.commits(from, to) {
contributors
.entry(commit.author.login.clone())
.or_insert_with(|| Contributor {
@ -80,18 +72,15 @@ struct Contributor {
contributions: usize,
}
/// A commit on the `typst` repository.
#[derive(Debug, Deserialize)]
struct Response {
commits: Vec<Commit>,
}
#[derive(Debug, Deserialize)]
struct Commit {
pub struct Commit {
author: Author,
}
/// A commit author.
#[derive(Debug, Deserialize)]
struct Author {
pub struct Author {
login: String,
avatar_url: String,
}

View File

@ -121,7 +121,7 @@ impl<'a> Handler<'a> {
md::Event::Html(html) if html.starts_with("<contributors") => {
let from = html_attr(html, "from").unwrap();
let to = html_attr(html, "to").unwrap();
let Some(output) = contributors(from, to) else { return false };
let Some(output) = contributors(self.resolver, from, to) else { return false };
*html = output.raw.into();
}

View File

@ -3,7 +3,7 @@
mod contribs;
mod html;
pub use contribs::contributors;
pub use contribs::{contributors, Author, Commit};
pub use html::Html;
use std::fmt::{self, Debug, Formatter};
@ -72,6 +72,9 @@ pub trait Resolver {
/// Produce HTML for an example.
fn example(&self, source: Html, frames: &[Frame]) -> Html;
/// Determine the commits between two tags.
fn commits(&self, from: &str, to: &str) -> Vec<Commit>;
}
/// Details about a documentation page and its children.
@ -81,6 +84,7 @@ pub struct PageModel {
pub title: String,
pub description: String,
pub part: Option<&'static str>,
pub outline: Vec<OutlineItem>,
pub body: BodyModel,
pub children: Vec<Self>,
}
@ -784,5 +788,9 @@ mod tests {
fn image(&self, _: &str, _: &[u8]) -> String {
String::new()
}
fn commits(&self, _: &str, _: &str) -> Vec<Commit> {
vec![]
}
}
}