Skip to content

Commit f6b10e7

Browse files
authored
fix: Add committer parameter to the create_entry API and bind committer information (#1581)
Signed-off-by: Ruizhi Huang <[email protected]>
1 parent afdc798 commit f6b10e7

File tree

5 files changed

+23
-10
lines changed

5 files changed

+23
-10
lines changed

ceres/src/api_service/import_api_service.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl ApiHandler for ImportApiService {
3535
self.storage.clone()
3636
}
3737

38-
async fn create_monorepo_entry(&self, _: CreateEntryInfo) -> Result<(), GitError> {
38+
async fn create_monorepo_entry(&self, _: CreateEntryInfo) -> Result<String, GitError> {
3939
return Err(GitError::CustomError(
4040
"import dir does not support create entry".to_string(),
4141
));

ceres/src/api_service/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ impl GitObjectCache {
4848
pub trait ApiHandler: Send + Sync {
4949
fn get_context(&self) -> Storage;
5050

51-
async fn create_monorepo_entry(&self, file_info: CreateEntryInfo) -> Result<(), GitError>;
51+
/// Create a file or directory entry under the monorepo path. Returns the new commit id on success.
52+
async fn create_monorepo_entry(&self, file_info: CreateEntryInfo) -> Result<String, GitError>;
5253

5354
async fn get_raw_blob_by_hash(&self, hash: &str) -> Result<Option<raw_blob::Model>, MegaError> {
5455
let context = self.get_context();

ceres/src/api_service/mono_api_service.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,12 @@ impl ApiHandler for MonoApiService {
145145
///
146146
/// # Returns
147147
///
148-
/// Returns `Ok(())` on success, or a `GitError` on failure.
149-
async fn create_monorepo_entry(&self, entry_info: CreateEntryInfo) -> Result<(), GitError> {
148+
/// Returns new commit id on success, or a `GitError` on failure.
149+
async fn create_monorepo_entry(&self, entry_info: CreateEntryInfo) -> Result<String, GitError> {
150150
let storage = self.storage.mono_storage();
151151
let path = PathBuf::from(&entry_info.path);
152152
let mut save_trees = vec![];
153+
let last_commit_id: String;
153154

154155
// Try to get the update chain for the given path.
155156
// If the path exists, return an empty missing_parts and prefix.
@@ -286,6 +287,7 @@ impl ApiHandler for MonoApiService {
286287
})
287288
.collect();
288289
storage.batch_save_model(save_trees).await?;
290+
last_commit_id = new_commit_id;
289291
} else {
290292
// If missing_parts is not empty, we must create intermediate
291293
// directories (trees) for each missing segment. This branch
@@ -395,9 +397,10 @@ impl ApiHandler for MonoApiService {
395397
.batch_save_model(save_trees)
396398
.await
397399
.map_err(|e| GitError::CustomError(e.to_string()))?;
400+
last_commit_id = new_commit_id;
398401
}
399402

400-
Ok(())
403+
Ok(last_commit_id)
401404
}
402405

403406
fn strip_relative(&self, path: &Path) -> Result<PathBuf, MegaError> {

ceres/src/model/git.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ pub struct CreateEntryInfo {
1717
pub path: String,
1818
// pub import_dir: bool,
1919
pub content: Option<String>,
20+
/// web user email for commit binding
21+
pub author_email: Option<String>,
22+
/// web username for commit binding (optional)
23+
pub author_username: Option<String>,
2024
}
2125

2226
impl CreateEntryInfo {

mono/src/api/router/preview_router.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,16 @@ async fn create_entry(
7474
state: State<MonoApiServiceState>,
7575
Json(json): Json<CreateEntryInfo>,
7676
) -> Result<Json<CommonResult<String>>, ApiError> {
77-
state
78-
.api_handler(json.path.as_ref())
79-
.await?
80-
.create_monorepo_entry(json.clone())
81-
.await?;
77+
let handler = state.api_handler(json.path.as_ref()).await?;
78+
let commit_id = handler.create_monorepo_entry(json.clone()).await?;
79+
80+
// If frontend provided author info, bind commit to that user (same as save_edit)
81+
if let Some(email) = json.author_email.as_ref() {
82+
let stg = state.storage.commit_binding_storage();
83+
stg.upsert_binding(&commit_id, email, json.author_username.clone(), false)
84+
.await
85+
.map_err(|e| ApiError::from(anyhow::anyhow!("Failed to save commit binding: {}", e)))?;
86+
}
8287
Ok(Json(CommonResult::success(None)))
8388
}
8489

0 commit comments

Comments
 (0)