From 35b36222d3abf6da2db1b4eea1100ffa29329db0 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sun, 1 Dec 2024 21:17:33 +0100 Subject: [PATCH] chore: move pr logic to extra file --- src/main.rs | 101 +++------------------------------------------------- src/pr.rs | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 96 deletions(-) create mode 100644 src/pr.rs diff --git a/src/main.rs b/src/main.rs index 5724e65..3300818 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,14 @@ use std::{ env, - fs::{self, read_dir, File, OpenOptions}, - io::BufReader, + fs::{self, read_dir}, }; use clap::{Args, Parser, Subcommand}; -use color_eyre::eyre::{bail, ContextCompat, Result}; +use color_eyre::eyre::{bail, Result}; +use pr::*; use regex::Regex; use reqwest::{header, Client}; -use serde::Deserialize; +mod pr; #[derive(Parser)] #[command(version, about)] @@ -29,10 +29,9 @@ enum CliCommands { #[derive(Args, Debug, Default)] struct UpdatePRs { + pr: Option, #[arg(long, default_value_t = ("./patches/PR".to_string()))] path: String, - #[arg(long)] - pr: Option, } #[derive(Args, Debug, Default)] @@ -47,96 +46,6 @@ struct Track { pr: String, } -const BRANCHES: &[&str] = &[ - "master", - "staging-next", - "nixos-unstable-small", - "nixos-unstable", -]; - -#[derive(Debug, Deserialize)] -struct PR { - title: String, - state: String, - number: u32, - merged: Option, - merged_at: Option, - // merge conflicts don't have a merge_commit_sha - merge_commit_sha: Option, - mergeable: Option, -} - -#[derive(Debug, Deserialize)] -struct Compare { - status: String, -} - -fn parse_pr(pr: &str) -> Result { - let re = Regex::new(r"^[0-9]*$")?; - let re2 = Regex::new(r"^https://github.com/NixOS/nixpkgs/pull/([0-9]*)$")?; - if let Some(caps) = re.captures(pr) { - Ok(caps[0].parse::()?) - } else if let Some(caps) = re2.captures(pr) { - Ok(caps[1].parse::()?) - } else { - bail!("Could not parse pr number!") - } -} - -async fn get_pr(pr: u32, client: &Client) -> Result { - let request = client - .get(format!( - "https://api.github.com/repos/nixos/nixpkgs/pulls/{}", - pr - )) - .send(); - let text = request.await?.text().await?; - let v: PR = serde_json::from_str(&text)?; - Ok(v) -} - -async fn contains(branch: &str, pr: &PR, client: &Client) -> Result { - if let Some(sha) = &pr.merge_commit_sha { - let req = client - .get(format!( - "https://api.github.com/repos/nixos/nixpkgs/compare/{}...{}", - branch, sha - )) - .send(); - let text = &req.await?.text().await?; - let v: Compare = serde_json::from_str(text)?; - if v.status == "identical" || v.status == "behind" { - return Ok(true); - } - } - Ok(false) -} -async fn get_diff(pr: u32, path: &str, client: &Client) -> Result<()> { - let req = client - .get(format!("https://github.com/nixos/nixpkgs/pull/{}.diff", pr)) - .send(); - let text = &req.await?.text().await?; - fs::write(format!("{}/{}.diff", path, pr), text)?; - Ok(()) -} - -fn get_local_nixp_rev() -> Result { - let file = File::open("./flake.lock")?; - let reader = BufReader::new(file); - let val: serde_json::Value = serde_json::from_reader(reader)?; - let s = val - .pointer("/nodes/root/inputs/nixpkgs") - .wrap_err("Could not find inputs.nixpkgs for local flake")? - .as_str() - .wrap_err("nixpkgs link link not a string")?; - let rev = val - .pointer(&format!("/nodes/{}/locked/rev", s)) - .wrap_err("Could not get rev of nixpkgs")?; - rev.as_str() - .wrap_err("rev not a string") - .map(|x| x.to_string()) -} - #[tokio::main] async fn main() -> Result<()> { let cli = Cli::parse(); diff --git a/src/pr.rs b/src/pr.rs new file mode 100644 index 0000000..8d19eb3 --- /dev/null +++ b/src/pr.rs @@ -0,0 +1,98 @@ +use color_eyre::eyre::{bail, ContextCompat, Result}; +use regex::Regex; +use reqwest::Client; +use serde::Deserialize; +use std::{ + fs::{self, File}, + io::BufReader, +}; + +#[derive(Debug, Deserialize)] +struct Compare { + status: String, +} + +pub const BRANCHES: &[&str] = &[ + "master", + "staging-next", + "nixos-unstable-small", + "nixos-unstable", +]; + +#[derive(Debug, Deserialize)] +pub struct PR { + pub title: String, + state: String, + number: u32, + merged: Option, + merged_at: Option, + // merge conflicts don't have a merge_commit_sha + merge_commit_sha: Option, + mergeable: Option, +} + +pub fn parse_pr(pr: &str) -> Result { + let re = Regex::new(r"^[0-9]*$")?; + let re2 = Regex::new(r"^https://github.com/NixOS/nixpkgs/pull/([0-9]*)$")?; + if let Some(caps) = re.captures(pr) { + Ok(caps[0].parse::()?) + } else if let Some(caps) = re2.captures(pr) { + Ok(caps[1].parse::()?) + } else { + bail!("Could not parse pr number!") + } +} + +pub async fn get_pr(pr: u32, client: &Client) -> Result { + let request = client + .get(format!( + "https://api.github.com/repos/nixos/nixpkgs/pulls/{}", + pr + )) + .send(); + let text = request.await?.text().await?; + let v: PR = serde_json::from_str(&text)?; + Ok(v) +} + +pub async fn contains(branch: &str, pr: &PR, client: &Client) -> Result { + if let Some(sha) = &pr.merge_commit_sha { + let req = client + .get(format!( + "https://api.github.com/repos/nixos/nixpkgs/compare/{}...{}", + branch, sha + )) + .send(); + let text = &req.await?.text().await?; + let v: Compare = serde_json::from_str(text)?; + if v.status == "identical" || v.status == "behind" { + return Ok(true); + } + } + Ok(false) +} +pub async fn get_diff(pr: u32, path: &str, client: &Client) -> Result<()> { + let req = client + .get(format!("https://github.com/nixos/nixpkgs/pull/{}.diff", pr)) + .send(); + let text = &req.await?.text().await?; + fs::write(format!("{}/{}.diff", path, pr), text)?; + Ok(()) +} + +pub fn get_local_nixp_rev() -> Result { + let file = File::open("./flake.lock")?; + let reader = BufReader::new(file); + let val: serde_json::Value = serde_json::from_reader(reader)?; + let s = val + .pointer("/nodes/root/inputs/nixpkgs") + .wrap_err("Could not find inputs.nixpkgs for local flake")? + .as_str() + .wrap_err("nixpkgs link link not a string")?; + let rev = val + .pointer(&format!("/nodes/{}/locked/rev", s)) + .wrap_err("Could not get rev of nixpkgs")?; + rev.as_str() + .wrap_err("rev not a string") + .map(|x| x.to_string()) +}