feat(#1081): add --approve/--request-changes/--comment to pr-reviews
This commit is contained in:
parent
5d4b62ccca
commit
817d94023d
1 changed files with 70 additions and 19 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
//! `pr-reviews <number> [repo]` — list PR reviews with id/state/user/body.
|
//! `pr-reviews <number>` — list reviews, or submit one via `--approve` /
|
||||||
|
//! `--request-changes` / `--comment`.
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::{bail, Result};
|
||||||
use clap::Args as ClapArgs;
|
use clap::Args as ClapArgs;
|
||||||
use serde_json::{Value, json};
|
use serde_json::{Value, json};
|
||||||
|
|
||||||
|
|
@ -11,26 +12,76 @@ use crate::verbs::print_json;
|
||||||
pub struct Args {
|
pub struct Args {
|
||||||
/// PR number.
|
/// PR number.
|
||||||
number: u64,
|
number: u64,
|
||||||
|
|
||||||
|
/// Approve the PR (submit an APPROVED review).
|
||||||
|
#[arg(long, conflicts_with_all = ["request_changes", "comment"])]
|
||||||
|
approve: bool,
|
||||||
|
|
||||||
|
/// Request changes on the PR (submit a REQUEST_CHANGES review).
|
||||||
|
#[arg(long, conflicts_with_all = ["approve", "comment"])]
|
||||||
|
request_changes: bool,
|
||||||
|
|
||||||
|
/// Leave a comment review (submit a COMMENT review).
|
||||||
|
#[arg(long, conflicts_with_all = ["approve", "request_changes"])]
|
||||||
|
comment: bool,
|
||||||
|
|
||||||
|
/// Optional body / message for the review (used with --approve,
|
||||||
|
/// --request-changes, or --comment).
|
||||||
|
#[arg(long, short = 'm')]
|
||||||
|
body: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(client: &Client, args: Args) -> Result<()> {
|
pub fn run(client: &Client, args: Args) -> Result<()> {
|
||||||
let repo = client.repo();
|
let repo = client.repo();
|
||||||
let v = client.get_json(&format!("/repos/{repo}/pulls/{}/reviews", args.number))?;
|
|
||||||
let trimmed: Vec<Value> = v
|
let event = if args.approve {
|
||||||
.as_array()
|
Some("APPROVED")
|
||||||
.map(|a| {
|
} else if args.request_changes {
|
||||||
a.iter()
|
Some("REQUEST_CHANGES")
|
||||||
.map(|r| {
|
} else if args.comment {
|
||||||
json!({
|
Some("COMMENT")
|
||||||
"id": r.get("id"),
|
} else {
|
||||||
"state": r.get("state"),
|
None
|
||||||
"user": r.get("user").and_then(|u| u.get("login")),
|
};
|
||||||
"body": r.get("body"),
|
|
||||||
"comments_count": r.get("comments_count"),
|
if let Some(ev) = event {
|
||||||
|
// Submit a review.
|
||||||
|
let payload = json!({
|
||||||
|
"event": ev,
|
||||||
|
"body": args.body.unwrap_or_default(),
|
||||||
|
});
|
||||||
|
let v = client
|
||||||
|
.post_json(&format!("/repos/{repo}/pulls/{}/reviews", args.number), &payload)?;
|
||||||
|
// Print a compact summary rather than the full review blob.
|
||||||
|
let summary = json!({
|
||||||
|
"id": v.get("id"),
|
||||||
|
"state": v.get("state"),
|
||||||
|
"user": v.get("user").and_then(|u| u.get("login")),
|
||||||
|
});
|
||||||
|
print_json(&summary)
|
||||||
|
} else {
|
||||||
|
if args.body.is_some() {
|
||||||
|
bail!("--body requires one of --approve / --request-changes / --comment");
|
||||||
|
}
|
||||||
|
// List mode (original behaviour).
|
||||||
|
let v =
|
||||||
|
client.get_json(&format!("/repos/{repo}/pulls/{}/reviews", args.number))?;
|
||||||
|
let trimmed: Vec<Value> = v
|
||||||
|
.as_array()
|
||||||
|
.map(|a| {
|
||||||
|
a.iter()
|
||||||
|
.map(|r| {
|
||||||
|
json!({
|
||||||
|
"id": r.get("id"),
|
||||||
|
"state": r.get("state"),
|
||||||
|
"user": r.get("user").and_then(|u| u.get("login")),
|
||||||
|
"body": r.get("body"),
|
||||||
|
"comments_count": r.get("comments_count"),
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
.collect()
|
||||||
.collect()
|
})
|
||||||
})
|
.unwrap_or_default();
|
||||||
.unwrap_or_default();
|
print_json(&Value::Array(trimmed))
|
||||||
print_json(&Value::Array(trimmed))
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue