Skip to content

Commit bc049dd

Browse files
refactor: use TaskBuildRequest in bellatrix (#1942)
Signed-off-by: allure <1550220889@qq.com>
1 parent 6a4052f commit bc049dd

File tree

6 files changed

+26
-107
lines changed

6 files changed

+26
-107
lines changed

ceres/src/build_trigger/changes_calculator.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ use std::{
33
sync::Arc,
44
};
55

6+
pub use api_model::buck2::{status::Status, types::ProjectRelativePath};
67
use common::errors::MegaError;
78
use git_internal::hash::ObjectHash;
89
use jupiter::storage::Storage;
910

1011
use crate::{
1112
api_service::{cache::GitObjectCache, mono_api_service::MonoApiService},
12-
build_trigger::{SerializableBuildInfo, SerializableStatus, TriggerContext},
13+
build_trigger::TriggerContext,
1314
model::change_list::{ClDiffFile, ClFilesRes},
1415
};
1516

@@ -29,21 +30,21 @@ impl ChangesCalculator {
2930
pub async fn get_builds_for_commit(
3031
&self,
3132
context: &TriggerContext,
32-
) -> Result<Vec<SerializableBuildInfo>, MegaError> {
33+
) -> Result<Vec<Status<ProjectRelativePath>>, MegaError> {
3334
let old_files = self.get_commit_blobs(&context.from_hash).await?;
3435
let new_files = self.get_commit_blobs(&context.commit_hash).await?;
3536
let diff_files = self.cl_files_list(old_files, new_files).await?;
3637

3738
let changes = self.build_changes(&context.repo_path, diff_files)?;
3839

39-
Ok(vec![SerializableBuildInfo { changes }])
40+
Ok(changes)
4041
}
4142

4243
fn build_changes(
4344
&self,
4445
cl_path: &str,
4546
cl_diff_files: Vec<ClDiffFile>,
46-
) -> Result<Vec<SerializableStatus>, MegaError> {
47+
) -> Result<Vec<Status<ProjectRelativePath>>, MegaError> {
4748
let cl_base = PathBuf::from(cl_path);
4849
let path_str = cl_base.to_str().ok_or_else(|| {
4950
MegaError::Other(format!("CL base path is not valid UTF-8: {:?}", cl_base))
@@ -73,11 +74,11 @@ impl ChangesCalculator {
7374
.to_string();
7475

7576
let status = if s.action == "new" {
76-
SerializableStatus::Added(rel)
77+
Status::Added(ProjectRelativePath::new(&rel))
7778
} else if s.action == "deleted" {
78-
SerializableStatus::Removed(rel)
79+
Status::Removed(ProjectRelativePath::new(&rel))
7980
} else if s.action == "modified" {
80-
SerializableStatus::Modified(rel)
81+
Status::Modified(ProjectRelativePath::new(&rel))
8182
} else {
8283
return Err(MegaError::Other(format!(
8384
"Unsupported change action: {}",

ceres/src/build_trigger/dispatcher.rs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
use std::sync::Arc;
22

3-
use bellatrix::{
4-
Bellatrix,
5-
orion_client::{BuildInfo, OrionBuildRequest},
6-
};
3+
use api_model::buck2::{api::TaskBuildRequest, status::Status, types::ProjectRelativePath};
4+
use bellatrix::Bellatrix;
75
use common::errors::MegaError;
86
use jupiter::storage::Storage;
97

10-
use crate::build_trigger::{BuildTrigger, BuildTriggerPayload, SerializableBuildInfo};
8+
use crate::build_trigger::{BuildTrigger, BuildTriggerPayload};
119

1210
/// Handles dispatching build triggers to the build execution layer (Bellatrix/Orion).
1311
pub struct BuildDispatcher {
@@ -39,24 +37,18 @@ impl BuildDispatcher {
3937
BuildTriggerPayload::BuckFileUpload(p) => (&p.cl_link, &p.repo, &p.builds, p.cl_id),
4038
};
4139

42-
let builds: Vec<SerializableBuildInfo> = serde_json::from_value(builds_json.clone())
43-
.map_err(|e| {
40+
let changes: Vec<Status<ProjectRelativePath>> =
41+
serde_json::from_value(builds_json.clone()).map_err(|e| {
4442
tracing::error!("Failed to deserialize builds from payload: {}", e);
4543
MegaError::Other(format!("Failed to deserialize builds from payload: {}", e))
4644
})?;
4745

48-
let bellatrix_builds: Vec<BuildInfo> = builds
49-
.into_iter()
50-
.map(|info| BuildInfo {
51-
changes: info.changes.into_iter().map(|s| s.into()).collect(),
52-
})
53-
.collect();
54-
55-
let req = OrionBuildRequest {
46+
let req = TaskBuildRequest {
47+
repo: repo.to_string(),
5648
cl_link: cl_link.to_string(),
57-
mount_path: repo.to_string(),
58-
cl: cl_id.unwrap_or(0),
59-
builds: bellatrix_builds,
49+
cl_id: cl_id.unwrap_or(0),
50+
changes,
51+
targets: None,
6052
};
6153

6254
let task_id_str = self.bellatrix.on_post_receive(req).await.map_err(|e| {

ceres/src/build_trigger/model.rs

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::{collections::HashMap, fmt};
22

3+
pub use api_model::buck2::{status::Status, types::ProjectRelativePath};
34
use callisto::mega_cl;
45
use chrono::{DateTime, Utc};
56
use serde::{Deserialize, Serialize};
@@ -577,33 +578,3 @@ impl From<ListTriggersParams> for jupiter::storage::build_trigger_storage::ListT
577578
}
578579
}
579580
}
580-
581-
#[derive(Clone, Serialize, Deserialize, Debug)]
582-
pub struct SerializableBuildInfo {
583-
pub changes: Vec<SerializableStatus>,
584-
}
585-
586-
#[derive(Clone, Serialize, Deserialize, Debug)]
587-
pub enum SerializableStatus {
588-
Modified(String),
589-
Added(String),
590-
Removed(String),
591-
}
592-
593-
impl From<SerializableStatus>
594-
for bellatrix::orion_client::Status<bellatrix::orion_client::ProjectRelativePath>
595-
{
596-
fn from(status: SerializableStatus) -> Self {
597-
match status {
598-
SerializableStatus::Modified(path) => bellatrix::orion_client::Status::Modified(
599-
bellatrix::orion_client::ProjectRelativePath::new(&path),
600-
),
601-
SerializableStatus::Added(path) => bellatrix::orion_client::Status::Added(
602-
bellatrix::orion_client::ProjectRelativePath::new(&path),
603-
),
604-
SerializableStatus::Removed(path) => bellatrix::orion_client::Status::Removed(
605-
bellatrix::orion_client::ProjectRelativePath::new(&path),
606-
),
607-
}
608-
}
609-
}

orion-server/bellatrix/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ name = "bellatrix"
88
path = "src/lib.rs"
99

1010
[dependencies]
11+
api-model = { workspace = true }
1112
common = { workspace = true }
1213
reqwest = { workspace = true, features = ["json"]}
1314
anyhow = { workspace = true }

orion-server/bellatrix/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
pub mod orion_client;
22

3+
use api_model::buck2::api::TaskBuildRequest;
34
use common::config::BuildConfig;
45

5-
use crate::orion_client::{OrionBuildRequest, OrionClient};
6+
use crate::orion_client::OrionClient;
67

78
#[derive(Clone)]
89
pub struct Bellatrix {
@@ -23,7 +24,7 @@ impl Bellatrix {
2324
self.build_config.enable_build
2425
}
2526

26-
pub async fn on_post_receive(&self, req: OrionBuildRequest) -> anyhow::Result<String> {
27+
pub async fn on_post_receive(&self, req: TaskBuildRequest) -> anyhow::Result<String> {
2728
let task_id = self.orion.trigger_build(req).await?;
2829
Ok(task_id)
2930
}

orion-server/bellatrix/src/orion_client/mod.rs

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,5 @@
1-
use serde::{Deserialize, Serialize};
2-
3-
#[derive(Debug, PartialEq, Eq, Hash, Clone, Serialize, Deserialize)]
4-
pub enum Status<Path: Clone> {
5-
Modified(Path),
6-
Added(Path),
7-
Removed(Path),
8-
}
9-
10-
#[derive(Clone, Debug, Hash, PartialEq, Eq, Deserialize, Serialize)]
11-
pub struct ProjectRelativePath(String);
12-
13-
impl ProjectRelativePath {
14-
pub fn new(path: &str) -> Self {
15-
Self(path.to_owned())
16-
}
17-
18-
pub fn from_abs(abs_path: &str, base: &str) -> Option<Self> {
19-
let opt = abs_path
20-
.strip_prefix(base)
21-
.map(|s| s.trim_start_matches("/"));
22-
opt.map(|s| Self(s.to_owned()))
23-
}
24-
}
25-
26-
impl<Path: Clone> Status<Path> {
27-
pub fn path(&self) -> Path {
28-
match self {
29-
Self::Added(p) => p.clone(),
30-
Self::Modified(p) => p.clone(),
31-
Self::Removed(p) => p.clone(),
32-
}
33-
}
34-
}
35-
36-
#[derive(Serialize, Debug)]
37-
pub struct BuildInfo {
38-
pub changes: Vec<Status<ProjectRelativePath>>,
39-
}
40-
41-
#[derive(Serialize, Debug)]
42-
pub struct OrionBuildRequest {
43-
pub cl_link: String,
44-
/// Monorepo mount path (Buck2 project root or subdirectory)
45-
#[serde(rename = "mount_path", alias = "repo", alias = "path")]
46-
pub mount_path: String,
47-
pub cl: i64,
48-
pub builds: Vec<BuildInfo>,
49-
}
1+
pub use api_model::buck2::{api::TaskBuildRequest, status::Status, types::ProjectRelativePath};
2+
use serde::Deserialize;
503

514
/// Response from Orion task handler containing the assigned task ID.
525
#[derive(Deserialize, Debug)]
@@ -69,7 +22,7 @@ impl OrionClient {
6922
}
7023

7124
/// Trigger a build on Orion and return the assigned task ID.
72-
pub async fn trigger_build(&self, req: OrionBuildRequest) -> anyhow::Result<String> {
25+
pub async fn trigger_build(&self, req: TaskBuildRequest) -> anyhow::Result<String> {
7326
let url = format!("{}/task", self.base_url);
7427
tracing::info!("Try to trigger build with params:{:?}", req);
7528
let res = self.client.post(&url).json(&req).send().await?;

0 commit comments

Comments
 (0)