From 706126af56ee5d83910ac4e0a459d68abbefdddb Mon Sep 17 00:00:00 2001 From: Sridhar Ratnakumar Date: Mon, 25 Mar 2024 09:29:20 -0400 Subject: [PATCH] split --- src/cli.rs | 4 +-- src/github/matrix.rs | 37 ++++++++++++++++++++++ src/github/mod.rs | 2 ++ src/{github.rs => github/pull_request.rs} | 38 ++--------------------- src/lib.rs | 2 +- 5 files changed, 44 insertions(+), 39 deletions(-) create mode 100644 src/github/matrix.rs create mode 100644 src/github/mod.rs rename src/{github.rs => github/pull_request.rs} (72%) diff --git a/src/cli.rs b/src/cli.rs index 909bd6f..e62ceb3 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -11,7 +11,7 @@ use nix_rs::{ use crate::{ config, - github::{self, PullRequest, PullRequestRef}, + github::pull_request::{PullRequest, PullRequestRef}, nix::system_list::{SystemFlakeUrl, SystemsList}, }; @@ -27,7 +27,7 @@ pub enum FlakeRef { impl FromStr for FlakeRef { type Err = String; fn from_str(s: &str) -> std::result::Result { - let flake_ref = match github::PullRequestRef::from_web_url(s) { + let flake_ref = match PullRequestRef::from_web_url(s) { Some(pr) => FlakeRef::GithubPR(pr), None => FlakeRef::Flake(FlakeUrl(s.to_string())), }; diff --git a/src/github/matrix.rs b/src/github/matrix.rs new file mode 100644 index 0000000..11c2197 --- /dev/null +++ b/src/github/matrix.rs @@ -0,0 +1,37 @@ +/// Enough types to get branch info from Pull Request URL +use itertools::iproduct; +use nix_rs::flake::system::System; +use serde::{Deserialize, Serialize}; + +use crate::config::Config; + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct GitHubMatrixRow { + pub system: System, + pub subflake: String, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct GitHubMatrix { + pub include: Vec, +} + +impl GitHubMatrix { + pub fn new(systems: Vec, subflakes: Vec) -> Self { + let include = iproduct!(systems, subflakes) + .map(|(system, subflake)| GitHubMatrixRow { system, subflake }) + .collect(); + GitHubMatrix { include } + } +} + +pub(crate) async fn dump_github_actions_matrix( + cfg: &Config, + systems: Vec, +) -> anyhow::Result<()> { + // TODO: Should take into account systems whitelist + // Ref: https://github.com/srid/nixci/blob/efc77c8794e5972617874edd96afa8dd4f1a75b2/src/config.rs#L104-L105 + let matrix = GitHubMatrix::new(systems, cfg.subflakes.0.keys().cloned().collect()); + println!("{}", serde_json::to_string(&matrix)?); + Ok(()) +} diff --git a/src/github/mod.rs b/src/github/mod.rs new file mode 100644 index 0000000..483d4aa --- /dev/null +++ b/src/github/mod.rs @@ -0,0 +1,2 @@ +pub mod matrix; +pub mod pull_request; diff --git a/src/github.rs b/src/github/pull_request.rs similarity index 72% rename from src/github.rs rename to src/github/pull_request.rs index 3f30b20..d81bf85 100644 --- a/src/github.rs +++ b/src/github/pull_request.rs @@ -1,14 +1,11 @@ /// Enough types to get branch info from Pull Request URL use anyhow::{bail, Context}; -use itertools::iproduct; -use nix_rs::flake::{system::System, url::FlakeUrl}; +use nix_rs::flake::url::FlakeUrl; use reqwest::header::USER_AGENT; -use serde::{Deserialize, Serialize}; +use serde::Deserialize; use try_guard::guard; use url::{Host, Url}; -use crate::config::Config; - /// A reference to a Github Pull Request #[derive(Debug, Clone, PartialEq, Eq)] pub struct PullRequestRef { @@ -106,34 +103,3 @@ where bail!("cannot make request: {}", resp.status()) } } - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct GitHubMatrixRow { - pub system: System, - pub subflake: String, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct GitHubMatrix { - pub include: Vec, -} - -impl GitHubMatrix { - pub fn new(systems: Vec, subflakes: Vec) -> Self { - let include = iproduct!(systems, subflakes) - .map(|(system, subflake)| GitHubMatrixRow { system, subflake }) - .collect(); - GitHubMatrix { include } - } -} - -pub(crate) async fn dump_github_actions_matrix( - cfg: &Config, - systems: Vec, -) -> anyhow::Result<()> { - // TODO: Should take into account systems whitelist - // Ref: https://github.com/srid/nixci/blob/efc77c8794e5972617874edd96afa8dd4f1a75b2/src/config.rs#L104-L105 - let matrix = GitHubMatrix::new(systems, cfg.subflakes.0.keys().cloned().collect()); - println!("{}", serde_json::to_string(&matrix)?); - Ok(()) -} diff --git a/src/lib.rs b/src/lib.rs index cd7952e..eb72e70 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,7 +20,7 @@ pub async fn nixci(args: CliArgs) -> anyhow::Result> { match args.command { cli::Command::Build(build_cfg) => nixci_build(args.verbose, &build_cfg, &url, &cfg).await, cli::Command::DumpGithubActionsMatrix { systems, .. } => { - github::dump_github_actions_matrix(&cfg, systems).await?; + github::matrix::dump_github_actions_matrix(&cfg, systems).await?; // TODO: Return something meaningful, or break the function. Ok(vec![]) }