mirror of
https://seed.radicle.dev/rad:zwTxygwuz5LDGBq255RA2CbNGrz8.git
synced 2026-11-29 14:50:35 +08:00
212 lines
6.6 KiB
Rust
212 lines
6.6 KiB
Rust
//! Messages for communicating with the adapter.
|
|
//!
|
|
//! The broker spawns an adapter child process, and sends it a request
|
|
//! via the child's stdin. The child sends responses via its stdout,
|
|
//! which the broker reads and processes.
|
|
|
|
use std::{
|
|
fmt,
|
|
io::{BufRead, BufReader, Read, Write},
|
|
};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
pub use radicle::git::Oid;
|
|
pub use radicle::prelude::{NodeId, RepoId};
|
|
|
|
/// The type of a run identifier. For maximum generality, this is a
|
|
/// string rather than an integer.
|
|
///
|
|
/// # Example
|
|
/// ```rust
|
|
/// use radicle_ci_broker::msg::RunId;
|
|
/// let id = RunId::from("abracadabra");
|
|
/// println!("{}", id.to_string());
|
|
/// ```
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct RunId {
|
|
id: String,
|
|
}
|
|
|
|
impl From<&str> for RunId {
|
|
fn from(id: &str) -> Self {
|
|
Self { id: id.into() }
|
|
}
|
|
}
|
|
|
|
impl fmt::Display for RunId {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|
write!(f, "{}", self.id)
|
|
}
|
|
}
|
|
|
|
/// The result of a CI run.
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
#[serde(rename_all = "snake_case")]
|
|
#[non_exhaustive]
|
|
pub enum RunResult {
|
|
/// CI run was successful.
|
|
Success,
|
|
|
|
/// CI run failed.
|
|
Failure,
|
|
|
|
/// CI run has failed in a way external to the software under test.
|
|
Error(String),
|
|
}
|
|
|
|
impl fmt::Display for RunResult {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|
match self {
|
|
Self::Failure => write!(f, "failure"),
|
|
Self::Success => write!(f, "success"),
|
|
Self::Error(expl) => write!(f, "error: {}", expl),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A request message sent by the broker to its adapter child process.
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
#[serde(tag = "request")]
|
|
#[serde(rename_all = "snake_case")]
|
|
#[non_exhaustive]
|
|
pub enum Request {
|
|
/// Trigger a run.
|
|
Trigger { repo: RepoId, commit: Oid },
|
|
}
|
|
|
|
impl Request {
|
|
/// Create an event to trigger a run.
|
|
pub fn trigger(repo: RepoId, commit: Oid) -> Self {
|
|
Self::Trigger { repo, commit }
|
|
}
|
|
|
|
/// Serialize the request as a single-line JSON, including the
|
|
/// newline. This is meant for the broker to use.
|
|
pub fn to_writer<W: Write>(&self, mut writer: W) -> Result<(), MessageError> {
|
|
let mut line = serde_json::to_string(&self).map_err(MessageError::SerializeRequest)?;
|
|
line.push('\n');
|
|
writer
|
|
.write(line.as_bytes())
|
|
.map_err(MessageError::WriteRequest)?;
|
|
Ok(())
|
|
}
|
|
|
|
/// Read a request from a reader. This is meant for the adapter to
|
|
/// use.
|
|
pub fn from_reader<R: Read>(reader: R) -> Result<Self, MessageError> {
|
|
let mut line = String::new();
|
|
let mut r = BufReader::new(reader);
|
|
r.read_line(&mut line).map_err(MessageError::ReadLine)?;
|
|
let req: Self =
|
|
serde_json::from_slice(line.as_bytes()).map_err(MessageError::DeserializeRequest)?;
|
|
Ok(req)
|
|
}
|
|
}
|
|
|
|
/// A response message from the adapter child process to the broker.
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
#[serde(rename_all = "snake_case")]
|
|
#[serde(tag = "response")]
|
|
pub enum Response {
|
|
/// A CI run has been triggered.
|
|
Triggered { run_id: RunId },
|
|
|
|
/// A CI run has finished.
|
|
Finished { result: RunResult },
|
|
}
|
|
|
|
impl Response {
|
|
/// Create a `Response::Triggered` message.
|
|
pub fn triggered(run_id: RunId) -> Self {
|
|
Self::Triggered { run_id }
|
|
}
|
|
|
|
/// Create a `Response::Finished` message.
|
|
pub fn finished(result: RunResult) -> Self {
|
|
Self::Finished { result }
|
|
}
|
|
|
|
/// Create a `Response::Error` message.
|
|
pub fn error(explanation: &str) -> Self {
|
|
Self::finished(RunResult::Error(explanation.into()))
|
|
}
|
|
|
|
/// Does the message indicate a result for the CI run?
|
|
pub fn result(&self) -> Option<&RunResult> {
|
|
if let Self::Finished { result } = self {
|
|
Some(result)
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
/// Serialize a response as a single-line JSON, including the
|
|
/// newline. This is meant for the adapter to use.
|
|
pub fn to_writer<W: Write>(&self, mut writer: W) -> Result<(), MessageError> {
|
|
let mut line = serde_json::to_string(&self).map_err(MessageError::SerializeResponse)?;
|
|
line.push('\n');
|
|
writer
|
|
.write(line.as_bytes())
|
|
.map_err(MessageError::WriteResponse)?;
|
|
Ok(())
|
|
}
|
|
|
|
/// Read a response from a reader. This is meant for the broker to
|
|
/// use.
|
|
pub fn from_reader<R: Read + BufRead>(reader: &mut R) -> Result<Option<Self>, MessageError> {
|
|
let mut line = String::new();
|
|
let mut r = BufReader::new(reader);
|
|
let n = r.read_line(&mut line).map_err(MessageError::ReadLine)?;
|
|
if n == 0 {
|
|
// Child's stdout was closed.
|
|
Ok(None)
|
|
} else {
|
|
let req: Self = serde_json::from_slice(line.as_bytes())
|
|
.map_err(MessageError::DeserializeResponse)?;
|
|
Ok(Some(req))
|
|
}
|
|
}
|
|
}
|
|
|
|
/// All possible errors from the CI broker messages.
|
|
#[derive(Debug, thiserror::Error)]
|
|
pub enum MessageError {
|
|
/// Failed to serialize a request message as JSON. This should
|
|
/// never happen and likely indicate a programming failure.
|
|
#[error("failed to serialize a request into JSON to a file handle")]
|
|
SerializeRequest(#[source] serde_json::Error),
|
|
|
|
/// Failed to serialize a response message as JSON. This should never
|
|
/// happen and likely indicate a programming failure.
|
|
#[error("failed to serialize a request into JSON to a file handle")]
|
|
SerializeResponse(#[source] serde_json::Error),
|
|
|
|
/// Failed to write the serialized request message to an open file.
|
|
#[error("failed to write JSON to file handle")]
|
|
WriteRequest(#[source] std::io::Error),
|
|
|
|
/// Failed to write the serialized response message to an open
|
|
/// file.
|
|
#[error("failed to write JSON to file handle")]
|
|
WriteResponse(#[source] std::io::Error),
|
|
|
|
/// Failed to read a line of JSON from an open file.
|
|
#[error("failed to read line from file handle")]
|
|
ReadLine(#[source] std::io::Error),
|
|
|
|
/// Failed to parse JSON as a request or a response.
|
|
#[error("failed to read a JSON request from a file handle")]
|
|
DeserializeRequest(#[source] serde_json::Error),
|
|
|
|
/// Failed to parse JSON as a response or a response.
|
|
#[error("failed to read a JSON request from a file handle")]
|
|
DeserializeResponse(#[source] serde_json::Error),
|
|
|
|
/// Error from Radicle.
|
|
#[error(transparent)]
|
|
RadicleProfile(#[from] radicle::profile::Error),
|
|
}
|