use std::{future::Future, path::Path, time::Duration};
use rho_protocol::{
methods::{
ForgeFetchParams, ForgeFetchResponse, ForgePullRequestActParams,
ForgePullRequestActResponse, ForgeStatusParams, GhStatus, FORGE_DEADLINE_SECS,
},
Caller, ClientEnvironment, ForgeService, RpcError,
};
use tokio::process::Command;
use super::machine::{
absolute, blocking, failed, host_path, log_call, spawn_stream, Machine, Origin,
LOGIN_SHELL_DEADLINE,
};
use crate::{
app::pull_requests::quiet_gh,
process_tree::{supervised_output, supervised_output_with_input},
};
mod act;
mod fetch;
mod status;
pub(super) use fetch::Web;
const FORGE_DEADLINE: Duration = Duration::from_secs(FORGE_DEADLINE_SECS);
impl ForgeService for Machine {
async fn status(
&self,
caller: Caller,
params: ForgeStatusParams,
) -> Result<GhStatus, RpcError> {
status::status(self, caller, params).await
}
async fn act(
&self,
caller: Caller,
params: ForgePullRequestActParams,
) -> Result<ForgePullRequestActResponse, RpcError> {
act::act(self, caller, params).await
}
async fn fetch(
&self,
caller: Caller,
params: ForgeFetchParams,
) -> Result<ForgeFetchResponse, RpcError> {
fetch::fetch(self, caller, params, self.forge_web()).await
}
}
fn var<'a>(environment: &'a ClientEnvironment, name: &str) -> Option<&'a str> {
environment
.iter()
.find_map(|(set, value)| (set == name).then_some(value))
}
fn gh_command(program: &Path, environment: &ClientEnvironment) -> Command {
let mut command = Command::new(program);
command.env_clear().envs(environment.iter());
quiet_gh(&mut command);
command
}
async fn within_deadline<T>(
call: impl Future<Output = Result<T, RpcError>>,
) -> Result<T, RpcError> {
match tokio::time::timeout(FORGE_DEADLINE, call).await {
Ok(answer) => answer,
Err(_) => {
tracing::warn!(
deadline_secs = FORGE_DEADLINE_SECS,
"gh didn't answer in time, so it was killed"
);
Err(failed(format!(
"gh didn't answer within {FORGE_DEADLINE_SECS} s (FORGE_DEADLINE_SECS)."
)))
}
}
}
#[cfg(all(test, unix))]
#[path = "machine_forge_tests.rs"]
mod tests;