feat: add update capabilities

This commit is contained in:
Patrick 2024-11-12 19:13:47 +01:00
parent 41d5af895e
commit b177dc30a6
Signed by: patrick
GPG key ID: 451F95EFB8BECD0F

View file

@ -1,4 +1,7 @@
use std::fs; use std::{
fs::{self, File},
io::{self, BufRead},
};
use clap::{Args, Parser, Subcommand}; use clap::{Args, Parser, Subcommand};
use color_eyre::eyre::Result; use color_eyre::eyre::Result;
@ -16,6 +19,17 @@ struct Cli {
enum CliCommands { enum CliCommands {
Track(Track), Track(Track),
GetDiff(GetDiff), GetDiff(GetDiff),
UpdatePrs(UpdatePRs),
}
#[derive(Args, Debug, Default)]
struct UpdatePRs {
#[arg(long, default_value_t = ("./pr.txt".to_string()))]
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)] #[derive(Args, Debug, Default)]
@ -41,9 +55,10 @@ const BRANCHES: &[&str] = &[
struct PR { struct PR {
title: String, title: String,
state: String, state: String,
merged: bool, merged: Option<bool>,
merged_at: Option<String>, merged_at: Option<String>,
merge_commit_sha: String, // merge conflicts don't have a merge_commit_sha
merge_commit_sha: Option<String>,
mergeable: bool, mergeable: bool,
} }
@ -64,16 +79,18 @@ async fn get_pr(pr: &str, client: &Client) -> Result<PR> {
Ok(v) Ok(v)
} }
async fn contains(branch: &str, pr: &PR, client: &Client) -> Result<bool> { async fn contains(branch: &str, pr: &PR, client: &Client) -> Result<bool> {
let req = client if let Some(sha) = &pr.merge_commit_sha {
.get(format!( let req = client
"https://api.github.com/repos/nixos/nixpkgs/compare/{}...{}", .get(format!(
branch, pr.merge_commit_sha "https://api.github.com/repos/nixos/nixpkgs/compare/{}...{}",
)) branch, sha
.send(); ))
let text = &req.await?.text().await?; .send();
let v: Compare = serde_json::from_str(text)?; let text = &req.await?.text().await?;
if v.status == "identical" || v.status == "behind" { let v: Compare = serde_json::from_str(text)?;
return Ok(true); if v.status == "identical" || v.status == "behind" {
return Ok(true);
}
} }
Ok(false) Ok(false)
} }
@ -101,6 +118,25 @@ async fn main() -> Result<()> {
CliCommands::GetDiff(opts) => { CliCommands::GetDiff(opts) => {
get_diff(&opts.pr, &opts.path, &client).await?; get_diff(&opts.pr, &opts.path, &client).await?;
} }
CliCommands::UpdatePrs(opts) => {
let mut trackable: Vec<String> = Vec::new();
{
let file = File::open(opts.pr_file.clone())?;
for l in io::BufReader::new(file).lines() {
let l = l?;
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);
fs::remove_file(format!("{}/{}.diff", opts.path, l))?;
} else {
get_diff(&l, &opts.path, &client).await?;
trackable.push(l.clone());
}
}
}
fs::write(&opts.pr_file, trackable.join("\n"))?;
}
}; };
Ok(()) Ok(())
} }