From 51f9f0f64cdb7e92837a35572f9959a00d01786d Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Mon, 19 Sep 2022 09:26:25 +0100 Subject: [PATCH] refactor(website): add script to update the code contributors (#3223) --- .cargo/config.toml | 1 + Cargo.lock | 29 ++ Cargo.toml | 1 + website/src/_includes/contributors.md | 672 ++++++++++++++++++++++++++ website/src/credits.md | 303 +----------- xtask/contributors/Cargo.toml | 13 + xtask/contributors/src/main.rs | 99 ++++ xtask/src/lib.rs | 2 +- 8 files changed, 817 insertions(+), 303 deletions(-) create mode 100644 website/src/_includes/contributors.md create mode 100644 xtask/contributors/Cargo.toml create mode 100644 xtask/contributors/src/main.rs diff --git a/.cargo/config.toml b/.cargo/config.toml index a4813f0d89a..84b6f9b2206 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -27,6 +27,7 @@ bench_analyzer = "run -p xtask_bench --release -- --feature analyzer" coverage = "run -p xtask_coverage --profile=release-with-debug --" rome-cli = "run -p rome_cli --release --" rome-cli-dev = "run -p rome_cli --" +contributors = "run -p xtask_contributors --" [profile.release] lto = true diff --git a/Cargo.lock b/Cargo.lock index 5216423acca..2af2bb44136 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -744,6 +744,15 @@ dependencies = [ "libc", ] +[[package]] +name = "html-escape" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8e7479fa1ef38eb49fb6a42c426be515df2d063f06cb8efd3e50af073dbc26c" +dependencies = [ + "utf8-width", +] + [[package]] name = "httparse" version = "1.7.1" @@ -2513,6 +2522,8 @@ dependencies = [ "log", "once_cell", "rustls", + "serde", + "serde_json", "url", "webpki", "webpki-roots", @@ -2531,6 +2542,12 @@ dependencies = [ "serde", ] +[[package]] +name = "utf8-width" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5190c9442dcdaf0ddd50f37420417d219ae5261bbf5db120d0f9bab996c9cba1" + [[package]] name = "valuable" version = "0.1.0" @@ -2837,6 +2854,18 @@ dependencies = [ "xtask", ] +[[package]] +name = "xtask_contributors" +version = "0.0.0" +dependencies = [ + "html-escape", + "pico-args", + "serde", + "serde_json", + "ureq", + "xtask", +] + [[package]] name = "xtask_coverage" version = "0.0.0" diff --git a/Cargo.toml b/Cargo.toml index 1aff5ce69e6..6da4801a0e4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ members = [ "xtask/coverage", "xtask/lintdoc", "xtask/libs_bench", + "xtask/contributors", ] [profile.release-with-debug] diff --git a/website/src/_includes/contributors.md b/website/src/_includes/contributors.md new file mode 100644 index 00000000000..2ad95787f66 --- /dev/null +++ b/website/src/_includes/contributors.md @@ -0,0 +1,672 @@ + + +### Code contributors + + diff --git a/website/src/credits.md b/website/src/credits.md index df0a7c27b34..e5059a78056 100644 --- a/website/src/credits.md +++ b/website/src/credits.md @@ -107,308 +107,7 @@ layout: layouts/page.liquid -## Code Contributors - - - - +{% include ./contributors.md %} ### Acknowledgements diff --git a/xtask/contributors/Cargo.toml b/xtask/contributors/Cargo.toml new file mode 100644 index 00000000000..aa0c4e9b8a9 --- /dev/null +++ b/xtask/contributors/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "xtask_contributors" +version = "0.0.0" +edition = "2021" +publish = false + +[dependencies] +serde = { version = "1.0.133", features = ["derive"] } +serde_json = { version = "1.0.74" } +xtask = { path = '../', version = "0.0" } +ureq = { version = "2.4.0", features = ["json"] } +pico-args = "0.5.0" +html-escape = "0.2.11" \ No newline at end of file diff --git a/xtask/contributors/src/main.rs b/xtask/contributors/src/main.rs new file mode 100644 index 00000000000..998693c740e --- /dev/null +++ b/xtask/contributors/src/main.rs @@ -0,0 +1,99 @@ +use pico_args::Arguments; +use serde::{Deserialize, Serialize}; +use std::fmt::Write; +use xtask::glue::fs2; +use xtask::*; + +/// A token is needed to run this script. To create a token, go to https://github.com/settings/tokens +/// and give it read access to the repository. +/// +/// Only users that have read rights can run this script +fn main() -> Result<()> { + let root = project_root().join("website/src/_includes"); + let mut args = Arguments::from_env(); + let token: String = args.value_from_str("--token").unwrap(); + let contributors = get_contributors(&token); + + let mut content = String::new(); + + let command = "Use the command `cargo contributors`".to_string(); + write!(content, "", prepend_generated_preamble(command))?; + content.push('\n'); + content.push_str("### Code contributors"); + content.push('\n'); + content.push_str(""); + fs2::write(root.join("contributors.md"), content)?; + + Ok(()) +} + +#[derive(Debug, Deserialize, Serialize)] +struct Contributor { + avatar_url: String, + login: String, +} + +fn get_contributors(token: &str) -> Vec { + let mut contributors = Vec::new(); + contributors_request( + "https://api.github.com/repos/rome/tools/contributors", + token, + &mut contributors, + ); + contributors +} + +fn contributors_request(url: &str, token: &str, contributors: &mut Vec) { + let request = ureq::get(url) + .set("User-Agent", "@rome") + .set("Authorization", &format!("token {token}")); + + match request.call() { + Ok(response) => { + if let Some(link) = response.header("link") { + if link.contains("rel=\"next\"") { + let start_index = link + .find("rel=\"prev\", ") + .map(|index| index + "rel=\"prev\", ".len()) + .unwrap_or(0); + // SAFETY: checked before + let end_index = link.find("; rel=\"next\"").unwrap(); + let url = &link[start_index..end_index]; + let url = url.replace('<', "").replace('>', ""); + + contributors_request(&url, token, contributors); + } + } + let result: Result, std::io::Error> = response.into_json(); + if let Ok(new_contributors) = result { + contributors.extend(new_contributors); + } + } + Err(err) => { + eprintln!("{:?}", err); + } + } +} diff --git a/xtask/src/lib.rs b/xtask/src/lib.rs index edcd613f325..ec28b54c3ca 100644 --- a/xtask/src/lib.rs +++ b/xtask/src/lib.rs @@ -43,7 +43,7 @@ pub fn reformat(text: impl Display) -> Result { reformat_without_preamble(text).map(prepend_generated_preamble) } -const PREAMBLE: &str = "Generated file, do not edit by hand, see `xtask/codegen`"; +pub const PREAMBLE: &str = "Generated file, do not edit by hand, see `xtask/codegen`"; pub fn prepend_generated_preamble(content: impl Display) -> String { format!("//! {}\n\n{}", PREAMBLE, content) }