hive-forge: pr edit --base retargets a PR
Forgejo's issue edit endpoint (/issues/<n>) has no base field, so `pr edit` could not retarget a stacked PR once its base merged. `--base <BRANCH>` (non-empty) switches the edit onto PATCH /pulls/<n>, which carries `base` alongside title/body/state/milestone. Without `--base`, `pr edit` is unchanged and still goes through the issue endpoint.
This commit is contained in:
parent
18ed3f5040
commit
9e3a00825b
1 changed files with 348 additions and 27 deletions
134
hive-forge/src/verbs/pr_edit.rs
Normal file
134
hive-forge/src/verbs/pr_edit.rs
Normal file
|
|
@ -0,0 +1,134 @@
|
|||
//! `pr edit <number> [--title <t>] [body sources] [--state s]
|
||||
//! [--milestone id] [--base <branch>]` — partial update of a PR. Omitted
|
||||
//! fields keep their current value.
|
||||
//!
|
||||
//! Without `--base` this is `issue edit` unchanged (`/issues/<n>`). The
|
||||
//! base branch only exists on Forgejo's pull-request edit endpoint
|
||||
//! (`PATCH /pulls/<n>`), so `--base` switches the whole edit onto that
|
||||
//! endpoint.
|
||||
|
||||
use anyhow::{Result, bail};
|
||||
use clap::Args as ClapArgs;
|
||||
use clap::builder::NonEmptyStringValueParser;
|
||||
use forgejo_api::structs::EditPullRequestOption;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::client::{Client, index};
|
||||
use crate::verbs::issue_edit::{self, resolve_body};
|
||||
use crate::verbs::print_json;
|
||||
|
||||
#[derive(ClapArgs)]
|
||||
pub struct PrEditArgs {
|
||||
#[command(flatten)]
|
||||
pub(crate) edit: issue_edit::Args,
|
||||
/// Retarget the PR onto this base branch (omit to leave unchanged).
|
||||
#[arg(long, value_name = "BRANCH", value_parser = NonEmptyStringValueParser::new())]
|
||||
base: Option<String>,
|
||||
}
|
||||
|
||||
/// `--milestone 0` means "unset" on the issue endpoint; whether
|
||||
/// `PATCH /pulls/<n>` reads it the same way is unverified, so the
|
||||
/// combination with `--base` is refused rather than sent.
|
||||
fn reject_unverified(args: &PrEditArgs) -> Result<()> {
|
||||
if args.base.is_some() && args.edit.milestone == Some(0) {
|
||||
bail!(
|
||||
"hive-forge: --milestone 0 (unset) cannot be combined with --base: \
|
||||
unsetting a milestone is unverified on the pull-request endpoint. \
|
||||
Run the two edits separately."
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The `PATCH /pulls/<n>` payload, or `None` when `--base` is unset and the
|
||||
/// edit rides the issue endpoint instead.
|
||||
fn pull_payload(args: &PrEditArgs, body: Option<String>) -> Result<Option<EditPullRequestOption>> {
|
||||
let Some(base) = args.base.clone() else {
|
||||
return Ok(None);
|
||||
};
|
||||
Ok(Some(EditPullRequestOption {
|
||||
allow_maintainer_edit: None,
|
||||
assignee: None,
|
||||
assignees: None,
|
||||
base: Some(base),
|
||||
body,
|
||||
due_date: None,
|
||||
labels: None,
|
||||
milestone: args.edit.milestone.map(index).transpose()?,
|
||||
state: args.edit.state.map(|s| <&str>::from(s).to_owned()),
|
||||
title: args.edit.title.clone(),
|
||||
unset_due_date: None,
|
||||
}))
|
||||
}
|
||||
|
||||
/// # Errors
|
||||
///
|
||||
/// Propagates any I/O error from the body input (`--body-file`, stdin),
|
||||
/// any transport error from the Forgejo REST call (network unreachable,
|
||||
/// 4xx/5xx response such as an unknown base branch, token
|
||||
/// missing/invalid), and any I/O error from writing the response to
|
||||
/// stdout.
|
||||
pub fn run(client: &Client, args: PrEditArgs) -> Result<()> {
|
||||
reject_unverified(&args)?;
|
||||
let body = resolve_body(&args.edit)?;
|
||||
let Some(payload) = pull_payload(&args, body.clone())? else {
|
||||
return issue_edit::run_with_body(client, args.edit, body);
|
||||
};
|
||||
let (owner, name) = client.owner_repo()?;
|
||||
let resp = client
|
||||
.api()
|
||||
.repo_edit_pull_request(owner, name, index(args.edit.number)?, payload)
|
||||
.send()?;
|
||||
print_json(&json!({
|
||||
"number": resp.number,
|
||||
"title": resp.title,
|
||||
"state": resp.state,
|
||||
"milestone": resp.milestone.as_ref().and_then(|m| m.title.as_deref()),
|
||||
"base": resp.base.as_ref().and_then(|b| b.r#ref.as_deref()),
|
||||
}))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use clap::Parser;
|
||||
|
||||
#[derive(clap::Parser)]
|
||||
struct Cli {
|
||||
#[command(flatten)]
|
||||
args: PrEditArgs,
|
||||
}
|
||||
|
||||
fn payload(argv: &[&str]) -> Option<serde_json::Value> {
|
||||
let cli = Cli::parse_from(std::iter::once("x").chain(argv.iter().copied()));
|
||||
pull_payload(&cli.args, None)
|
||||
.unwrap()
|
||||
.map(|p| serde_json::to_value(p).unwrap())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn base_flag_is_sent_as_base() {
|
||||
let p = payload(&["5194", "--base", "main"]).expect("pulls payload");
|
||||
assert_eq!(p["base"], "main");
|
||||
assert!(p["title"].is_null());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn base_rides_alongside_other_fields() {
|
||||
let p = payload(&["5194", "--base", "main", "--title", "t", "--state", "open"])
|
||||
.expect("pulls payload");
|
||||
assert_eq!(p["base"], "main");
|
||||
assert_eq!(p["title"], "t");
|
||||
assert_eq!(p["state"], "open");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_base_flag_sends_no_pulls_payload() {
|
||||
assert!(payload(&["5194", "--title", "t"]).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_base_is_rejected() {
|
||||
assert!(Cli::try_parse_from(["x", "5194", "--base", ""]).is_err());
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue