chore: move pr logic to extra file

This commit is contained in:
Patrick 2024-12-01 21:17:33 +01:00
parent 6cdabd1b61
commit 35b36222d3
Signed by: patrick
GPG key ID: 451F95EFB8BECD0F
2 changed files with 103 additions and 96 deletions

View file

@ -1,14 +1,14 @@
use std::{ use std::{
env, env,
fs::{self, read_dir, File, OpenOptions}, fs::{self, read_dir},
io::BufReader,
}; };
use clap::{Args, Parser, Subcommand}; use clap::{Args, Parser, Subcommand};
use color_eyre::eyre::{bail, ContextCompat, Result}; use color_eyre::eyre::{bail, Result};
use pr::*;
use regex::Regex; use regex::Regex;
use reqwest::{header, Client}; use reqwest::{header, Client};
use serde::Deserialize; mod pr;
#[derive(Parser)] #[derive(Parser)]
#[command(version, about)] #[command(version, about)]
@ -29,10 +29,9 @@ enum CliCommands {
#[derive(Args, Debug, Default)] #[derive(Args, Debug, Default)]
struct UpdatePRs { struct UpdatePRs {
pr: Option<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)]
pr: Option<String>,
} }
#[derive(Args, Debug, Default)] #[derive(Args, Debug, Default)]
@ -47,96 +46,6 @@ struct Track {
pr: String, 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<bool>,
merged_at: Option<String>,
// merge conflicts don't have a merge_commit_sha
merge_commit_sha: Option<String>,
mergeable: Option<bool>,
}
#[derive(Debug, Deserialize)]
struct Compare {
status: String,
}
fn parse_pr(pr: &str) -> Result<u32> {
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::<u32>()?)
} else if let Some(caps) = re2.captures(pr) {
Ok(caps[1].parse::<u32>()?)
} else {
bail!("Could not parse pr number!")
}
}
async fn get_pr(pr: u32, client: &Client) -> Result<PR> {
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<bool> {
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<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();

98
src/pr.rs Normal file
View file

@ -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<bool>,
merged_at: Option<String>,
// merge conflicts don't have a merge_commit_sha
merge_commit_sha: Option<String>,
mergeable: Option<bool>,
}
pub fn parse_pr(pr: &str) -> Result<u32> {
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::<u32>()?)
} else if let Some(caps) = re2.captures(pr) {
Ok(caps[1].parse::<u32>()?)
} else {
bail!("Could not parse pr number!")
}
}
pub async fn get_pr(pr: u32, client: &Client) -> Result<PR> {
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<bool> {
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<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())
}