From 6ae69508fa98fec185f0cb33348afc11f863e4bc Mon Sep 17 00:00:00 2001 From: Patrick Date: Thu, 14 Nov 2024 20:40:33 +0100 Subject: [PATCH] WIP --- src/main.rs | 48 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index eb1ddb0..2958899 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,11 @@ use std::{ fmt::Write, fs::{self, File}, - io::{self, BufRead}, + io::{self, BufRead, BufReader}, }; use clap::{Args, Parser, Subcommand}; -use color_eyre::eyre::{bail, Result}; +use color_eyre::eyre::{bail, ContextCompat, Result}; use regex::Regex; use reqwest::Client; use serde::Deserialize; @@ -21,7 +21,10 @@ struct Cli { enum CliCommands { Track(Track), GetDiff(GetDiff), + // Update all PR, removing them if they are contained in your local nixpkgs UpdatePrs(UpdatePRs), + // Unconditionally add a new PR + AddPr(AddPR), } #[derive(Args, Debug, Default)] @@ -30,8 +33,15 @@ struct UpdatePRs { pr_file: String, #[arg(long, default_value_t = ("./patches/PR".to_string()))] path: String, - #[arg(long, default_value_t = ("nixos-unstable".to_string()))] - branch: String, +} + +#[derive(Args, Debug, Default)] +struct AddPR { + pr: String, + #[arg(long, default_value_t = ("./pr.txt".to_string()))] + pr_file: String, + #[arg(long, default_value_t = ("./patches/PR".to_string()))] + path: String, } #[derive(Args, Debug, Default)] @@ -57,6 +67,7 @@ const BRANCHES: &[&str] = &[ struct PR { title: String, state: String, + number: u32, merged: Option, merged_at: Option, // merge conflicts don't have a merge_commit_sha @@ -117,6 +128,23 @@ async fn get_diff(pr: u32, path: &str, client: &Client) -> Result<()> { 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(); @@ -149,8 +177,9 @@ async fn main() -> Result<()> { }; let pr = get_pr(l, &client).await?; println!("Fetching diff for PR #{}: {}", l, pr.title); - if contains(&opts.branch, &pr, &client).await? { - println!("PR has reached {}, removing diff", opts.branch); + let branch = get_local_nixp_rev()?; + if contains(&branch, &pr, &client).await? { + println!("PR is contained in your local nixpkgs, removing diff"); fs::remove_file(format!("{}/{}.diff", opts.path, l))?; } else { get_diff(l, &opts.path, &client).await?; @@ -166,6 +195,13 @@ async fn main() -> Result<()> { fs::write(&opts.pr_file, str)?; } + CliCommands::AddPr(opts) => { + let pr = parse_pr(&opts.pr)?; + get_diff(pr, &opts.path, &client).await?; + let pr = get_pr(pr, &client).await?; + let title = pr.title.replace("\n", "//"); + fs::write(&opts.pr_file, format!("{} # {}", pr.number, title))?; + } }; Ok(()) }