use rho_protocol::{
methods::{
ForgePullRequestActParams, ForgePullRequestActResponse, ForgePullRequestAction,
PullRequestMergeMethod, FORGE_PULL_REQUEST_ACT,
},
Caller, ErrorCode, RpcError,
};
use super::{
absolute, failed, gh_command, log_call, supervised_output, supervised_output_with_input,
within_deadline, Machine,
};
use crate::app::pull_requests::gh_failure;
pub(super) async fn act(
machine: &Machine,
caller: Caller,
params: ForgePullRequestActParams,
) -> Result<ForgePullRequestActResponse, RpcError> {
let ForgePullRequestActParams {
gh,
pull_request,
action,
..
} = params;
let repo = format!(
"{}/{}/{}",
pull_request.host, pull_request.owner, pull_request.name
);
let shown = format!("{}/{pull_request}", pull_request.host);
tracing::info_span!("forge_act", pull_request = %shown, action = kind(&action))
.in_scope(|| log_call(&caller, FORGE_PULL_REQUEST_ACT, [&gh]));
let Pr {
verb,
options,
input,
} = pr(action)?;
let gh = absolute(&gh)?;
within_deadline(async {
let environment = machine.login_environment().await;
let mut command = gh_command(gh, &environment);
command
.args(["pr", verb])
.arg(pull_request.number.to_string())
.arg(format!("--repo={repo}"))
.args(options);
let output = match input {
Some(input) => supervised_output_with_input(&mut command, input).await,
None => supervised_output(&mut command).await,
};
let output =
output.map_err(|error| failed(format!("Couldn't run {}: {error}", gh.display())))?;
if output.status.success() {
Ok(ForgePullRequestActResponse::default())
} else {
Err(failed(gh_failure(&output.stderr, output.status)))
}
})
.await
}
fn kind(action: &ForgePullRequestAction) -> &'static str {
match action {
ForgePullRequestAction::Comment { .. } => "comment",
ForgePullRequestAction::Ready => "ready",
ForgePullRequestAction::Draft => "draft",
ForgePullRequestAction::Merge { .. } => "merge",
_ => "unknown",
}
}
struct Pr {
verb: &'static str,
options: Vec<String>,
input: Option<Vec<u8>>,
}
fn pr(action: ForgePullRequestAction) -> Result<Pr, RpcError> {
let (verb, options, input) = match action {
ForgePullRequestAction::Comment { body } => (
"comment",
vec!["--body-file=-".to_owned()],
Some(body.into_bytes()),
),
ForgePullRequestAction::Ready => ("ready", Vec::new(), None),
ForgePullRequestAction::Draft => ("ready", vec!["--undo".to_owned()], None),
ForgePullRequestAction::Merge { method, head_oid } => {
let method = match method {
PullRequestMergeMethod::Merge => "--merge",
PullRequestMergeMethod::Squash => "--squash",
PullRequestMergeMethod::Rebase => "--rebase",
_ => return Err(unknown("merge method")),
};
whole_commit(&head_oid)?;
let options = vec![method.to_owned(), format!("--match-head-commit={head_oid}")];
("merge", options, None)
}
_ => return Err(unknown("action")),
};
Ok(Pr {
verb,
options,
input,
})
}
fn whole_commit(head_oid: &str) -> Result<(), RpcError> {
let hex = head_oid.bytes().all(|byte| byte.is_ascii_hexdigit());
if hex && matches!(head_oid.len(), 40 | 64) {
return Ok(());
}
let digits = if hex {
"hex digits"
} else {
"characters, not all hex"
};
Err(RpcError::new(
ErrorCode::InvalidParams,
format!(
"head_oid isn't a whole commit id, 40 hex digits (SHA-1) or 64 (SHA-256): it has {} {digits}.",
head_oid.chars().count()
),
))
}
fn unknown(what: &str) -> RpcError {
RpcError::new(
ErrorCode::InvalidParams,
format!("This daemon doesn't know that {what}."),
)
}