Skip to content

Commit 771c32f

Browse files
authored
[orion]ADD: generic API payloads and WS messages model (#1847)
* [orion]ADD: generic API payloads and WS messages model Signed-off-by: AidCheng <cn.aiden.cheng@gmail.com> * [orion]UPDATE: cargo fmt Signed-off-by: AidCheng <cn.aiden.cheng@gmail.com> --------- Signed-off-by: AidCheng <cn.aiden.cheng@gmail.com>
1 parent e151a52 commit 771c32f

5 files changed

Lines changed: 143 additions & 0 deletions

File tree

‎api-model/src/buck2/api.rs‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//! Buck2 build API models.
2+
//!
3+
//! This module defines the request/response types used between
4+
//! Orion-Server and Monorepo during a build.
5+
//!
6+
//! ## Design notes
7+
//! - Types here are **pure data models**
8+
//! - No Buck2 execution logic should live in this module
9+
//! - Used by both server and mono crates only
10+
11+
use serde::{Deserialize, Serialize};
12+
13+
use crate::buck2::types::{ProjectRelativePath, Status};
14+
15+
/// Parameters required to build a task.
16+
#[allow(dead_code)]
17+
#[derive(Debug)]
18+
pub struct TaskBuildRequest {
19+
/// The repository base path
20+
pub repo: String,
21+
/// The change list link (URL)
22+
pub cl: String,
23+
/// The list of file diff changes
24+
pub changes: Vec<Status<ProjectRelativePath>>,
25+
}
26+
27+
/// Result of a task build operation containing status and metadata. Used by Orion-Server
28+
#[allow(dead_code)]
29+
#[derive(Debug, Deserialize, Serialize)]
30+
pub struct TaskBuildResult {
31+
/// Whether the build operation was successful
32+
pub success: bool,
33+
/// Unique identifier for the build task
34+
pub id: String,
35+
/// Process exit code (None if not yet completed)
36+
#[serde(skip_serializing_if = "Option::is_none")]
37+
pub exit_code: Option<i32>,
38+
/// Human-readable status or error message
39+
pub message: String,
40+
}

‎api-model/src/buck2/mod.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod api;
2+
pub mod types;
3+
pub mod ws;

‎api-model/src/buck2/types.rs‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//! Types related to Buck2 build system.
2+
3+
use serde::{Deserialize, Serialize};
4+
use utoipa::ToSchema;
5+
6+
/// Request from server to worker to build a target.
7+
#[allow(dead_code)]
8+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, ToSchema)]
9+
pub enum Status<Path> {
10+
Modified(Path),
11+
Added(Path),
12+
Removed(Path),
13+
}
14+
15+
/// Task phase when in buck2 build
16+
#[allow(dead_code)]
17+
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema, PartialEq, Eq)]
18+
pub enum TaskPhase {
19+
DownloadingSource,
20+
RunningBuild,
21+
}
22+
23+
/// Represents a file path relative to the project root.
24+
#[allow(dead_code)]
25+
#[derive(Clone, Debug, Hash, PartialEq, Eq, Deserialize, Serialize)]
26+
pub struct ProjectRelativePath(String);
27+
28+
impl ProjectRelativePath {
29+
/// Creates a new ProjectRelativePath from a string slice.
30+
pub fn new(path: &str) -> Self {
31+
Self(path.to_owned())
32+
}
33+
34+
/// Attempts to create a ProjectRelativePath from an absolute path and a base path.
35+
pub fn from_abs(abs_path: &str, base: &str) -> Option<Self> {
36+
let opt = abs_path
37+
.strip_prefix(base)
38+
.map(|s| s.trim_start_matches("/"));
39+
opt.map(|s| Self(s.to_owned()))
40+
}
41+
}

‎api-model/src/buck2/ws.rs‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//! Websocket message protocol for communication between orion worker and server.
2+
3+
use serde::{Deserialize, Serialize};
4+
5+
use crate::buck2::types::{ProjectRelativePath, Status, TaskPhase};
6+
7+
/// Message protocol for WebSocket communication between worker and server.
8+
#[allow(dead_code)]
9+
#[derive(Debug, Serialize, Deserialize, Clone)]
10+
#[serde(tag = "type")]
11+
pub enum WSMessage {
12+
// Server -> Worker messages
13+
Task {
14+
id: String,
15+
repo: String,
16+
cl_link: String,
17+
changes: Vec<Status<ProjectRelativePath>>,
18+
},
19+
20+
TaskWithTargets {
21+
id: String,
22+
repo: String,
23+
cl_link: String,
24+
// targets: Vec<Target>,
25+
},
26+
27+
// Worker -> Server messages
28+
Register {
29+
id: String,
30+
hostname: String,
31+
orion_version: String,
32+
},
33+
34+
Heartbeat,
35+
// Sent when a task is in the build process and its execution phase changes.
36+
TaskPhaseUpdate {
37+
id: String,
38+
phase: TaskPhase,
39+
},
40+
41+
TaskAck {
42+
id: String,
43+
success: bool,
44+
message: String,
45+
},
46+
47+
TaskBuildOutput {
48+
id: String,
49+
output: String,
50+
},
51+
52+
TaskBuildComplete {
53+
id: String,
54+
success: bool,
55+
exit_code: Option<i32>,
56+
message: String,
57+
},
58+
}

‎api-model/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
pub mod buck2;
12
pub mod git;

0 commit comments

Comments
 (0)