feat: add update capabilities
This commit is contained in:
parent
41d5af895e
commit
b177dc30a6
44
src/main.rs
44
src/main.rs
|
@ -1,4 +1,7 @@
|
|||
use std::fs;
|
||||
use std::{
|
||||
fs::{self, File},
|
||||
io::{self, BufRead},
|
||||
};
|
||||
|
||||
use clap::{Args, Parser, Subcommand};
|
||||
use color_eyre::eyre::Result;
|
||||
|
@ -16,6 +19,17 @@ struct Cli {
|
|||
enum CliCommands {
|
||||
Track(Track),
|
||||
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)]
|
||||
|
@ -41,9 +55,10 @@ const BRANCHES: &[&str] = &[
|
|||
struct PR {
|
||||
title: String,
|
||||
state: String,
|
||||
merged: bool,
|
||||
merged: Option<bool>,
|
||||
merged_at: Option<String>,
|
||||
merge_commit_sha: String,
|
||||
// merge conflicts don't have a merge_commit_sha
|
||||
merge_commit_sha: Option<String>,
|
||||
mergeable: bool,
|
||||
}
|
||||
|
||||
|
@ -64,10 +79,11 @@ async fn get_pr(pr: &str, client: &Client) -> Result<PR> {
|
|||
Ok(v)
|
||||
}
|
||||
async fn contains(branch: &str, pr: &PR, client: &Client) -> Result<bool> {
|
||||
if let Some(sha) = &pr.merge_commit_sha {
|
||||
let req = client
|
||||
.get(format!(
|
||||
"https://api.github.com/repos/nixos/nixpkgs/compare/{}...{}",
|
||||
branch, pr.merge_commit_sha
|
||||
branch, sha
|
||||
))
|
||||
.send();
|
||||
let text = &req.await?.text().await?;
|
||||
|
@ -75,6 +91,7 @@ async fn contains(branch: &str, pr: &PR, client: &Client) -> Result<bool> {
|
|||
if v.status == "identical" || v.status == "behind" {
|
||||
return Ok(true);
|
||||
}
|
||||
}
|
||||
Ok(false)
|
||||
}
|
||||
async fn get_diff(pr: &str, path: &str, client: &Client) -> Result<()> {
|
||||
|
@ -101,6 +118,25 @@ async fn main() -> Result<()> {
|
|||
CliCommands::GetDiff(opts) => {
|
||||
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(())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue