This commit is contained in:
Patrick 2024-11-14 20:40:33 +01:00
parent de9e2f573b
commit 6ae69508fa
Signed by: patrick
GPG key ID: 451F95EFB8BECD0F

View file

@ -1,11 +1,11 @@
use std::{ use std::{
fmt::Write, fmt::Write,
fs::{self, File}, fs::{self, File},
io::{self, BufRead}, io::{self, BufRead, BufReader},
}; };
use clap::{Args, Parser, Subcommand}; use clap::{Args, Parser, Subcommand};
use color_eyre::eyre::{bail, Result}; use color_eyre::eyre::{bail, ContextCompat, Result};
use regex::Regex; use regex::Regex;
use reqwest::Client; use reqwest::Client;
use serde::Deserialize; use serde::Deserialize;
@ -21,7 +21,10 @@ struct Cli {
enum CliCommands { enum CliCommands {
Track(Track), Track(Track),
GetDiff(GetDiff), GetDiff(GetDiff),
// Update all PR, removing them if they are contained in your local nixpkgs
UpdatePrs(UpdatePRs), UpdatePrs(UpdatePRs),
// Unconditionally add a new PR
AddPr(AddPR),
} }
#[derive(Args, Debug, Default)] #[derive(Args, Debug, Default)]
@ -30,8 +33,15 @@ struct UpdatePRs {
pr_file: String, pr_file: String,
#[arg(long, default_value_t = ("./patches/PR".to_string()))] #[arg(long, default_value_t = ("./patches/PR".to_string()))]
path: 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)] #[derive(Args, Debug, Default)]
@ -57,6 +67,7 @@ const BRANCHES: &[&str] = &[
struct PR { struct PR {
title: String, title: String,
state: String, state: String,
number: u32,
merged: Option<bool>, merged: Option<bool>,
merged_at: Option<String>, merged_at: Option<String>,
// merge conflicts don't have a merge_commit_sha // 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(()) Ok(())
} }
fn get_local_nixp_rev() -> Result<String> {
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] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
let cli = Cli::parse(); let cli = Cli::parse();
@ -149,8 +177,9 @@ async fn main() -> Result<()> {
}; };
let pr = get_pr(l, &client).await?; let pr = get_pr(l, &client).await?;
println!("Fetching diff for PR #{}: {}", l, pr.title); println!("Fetching diff for PR #{}: {}", l, pr.title);
if contains(&opts.branch, &pr, &client).await? { let branch = get_local_nixp_rev()?;
println!("PR has reached {}, removing diff", opts.branch); if contains(&branch, &pr, &client).await? {
println!("PR is contained in your local nixpkgs, removing diff");
fs::remove_file(format!("{}/{}.diff", opts.path, l))?; fs::remove_file(format!("{}/{}.diff", opts.path, l))?;
} else { } else {
get_diff(l, &opts.path, &client).await?; get_diff(l, &opts.path, &client).await?;
@ -166,6 +195,13 @@ async fn main() -> Result<()> {
fs::write(&opts.pr_file, str)?; 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(()) Ok(())
} }