|
| 1 | +use crate::schema::Team; |
| 2 | +use anyhow::Context; |
| 3 | +use std::path::{Path, PathBuf}; |
| 4 | + |
| 5 | +/// Generates the contents of `.github/CODEOWNERS`, based on |
| 6 | +/// the infra admins in `infra-admins.toml`. |
| 7 | +pub fn generate_codeowners_file() -> anyhow::Result<()> { |
| 8 | + let admins = load_infra_admins()?; |
| 9 | + let codeowners_content = generate_codeowners_content(admins); |
| 10 | + std::fs::write(codeowners_path(), codeowners_content).context("cannot write CODEOWNERS")?; |
| 11 | + Ok(()) |
| 12 | +} |
| 13 | + |
| 14 | +fn generate_codeowners_content(admins: Vec<String>) -> String { |
| 15 | + use std::fmt::Write; |
| 16 | + |
| 17 | + let mut output = String::new(); |
| 18 | + writeln!( |
| 19 | + output, |
| 20 | + r#"# This is an automatically generated file |
| 21 | +# Run `cargo run ci generate-codeowners` to regenerate it. |
| 22 | +"# |
| 23 | + ) |
| 24 | + .unwrap(); |
| 25 | + |
| 26 | + let admin_list = admins |
| 27 | + .iter() |
| 28 | + .map(|admin| format!("@{admin}")) |
| 29 | + .collect::<Vec<_>>() |
| 30 | + .join(" "); |
| 31 | + |
| 32 | + // Set of paths that should only be modifiable by infra-admins |
| 33 | + let mut secure_paths = vec![ |
| 34 | + "/.github/".to_string(), |
| 35 | + "/src/".to_string(), |
| 36 | + "/rust_team_data/".to_string(), |
| 37 | + "/repos/rust-lang/team.toml".to_string(), |
| 38 | + "/repos/rust-lang/sync-team.toml".to_string(), |
| 39 | + "/teams/infra-admins.toml".to_string(), |
| 40 | + "/teams/team-repo-admins.toml".to_string(), |
| 41 | + ".cargo".to_string(), |
| 42 | + "target".to_string(), |
| 43 | + "Cargo.lock".to_string(), |
| 44 | + "Cargo.toml".to_string(), |
| 45 | + "config.toml".to_string(), |
| 46 | + ]; |
| 47 | + for admin in admins { |
| 48 | + secure_paths.push(format!("/people/{admin}.toml")); |
| 49 | + } |
| 50 | + |
| 51 | + for path in secure_paths { |
| 52 | + writeln!(output, "{path} {admin_list}").unwrap(); |
| 53 | + } |
| 54 | + output |
| 55 | +} |
| 56 | + |
| 57 | +fn codeowners_path() -> PathBuf { |
| 58 | + Path::new(&env!("CARGO_MANIFEST_DIR")) |
| 59 | + .join(".github") |
| 60 | + .join("CODEOWNERS") |
| 61 | +} |
| 62 | + |
| 63 | +fn load_infra_admins() -> anyhow::Result<Vec<String>> { |
| 64 | + let admins = std::fs::read_to_string( |
| 65 | + Path::new(&env!("CARGO_MANIFEST_DIR")) |
| 66 | + .join("teams") |
| 67 | + .join("infra-admins.toml"), |
| 68 | + ) |
| 69 | + .context("cannot load infra-admins.toml")?; |
| 70 | + let team: Team = toml::from_str(&admins).context("cannot deserialize infra-admins")?; |
| 71 | + Ok(team |
| 72 | + .raw_people() |
| 73 | + .members |
| 74 | + .iter() |
| 75 | + .map(|member| member.github.clone()) |
| 76 | + .collect()) |
| 77 | +} |
0 commit comments