Skip to content

Commit 8a37f60

Browse files
authored
fix: adjust get_latest_commit && get_blob API (#1576)
* refactor * restore UI edits Signed-off-by: Ruizhi Huang <[email protected]> * accept code review comments Signed-off-by: Ruizhi Huang <[email protected]> --------- Signed-off-by: Ruizhi Huang <[email protected]>
1 parent ec9ec5c commit 8a37f60

File tree

5 files changed

+40
-10
lines changed

5 files changed

+40
-10
lines changed

ceres/src/api_service/mod.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,14 @@ pub trait ApiHandler: Send + Sync {
8484

8585
async fn get_commits_by_hashes(&self, c_hashes: Vec<String>) -> Result<Vec<Commit>, GitError>;
8686

87-
async fn get_blob_as_string(&self, file_path: PathBuf) -> Result<Option<String>, GitError> {
87+
async fn get_blob_as_string(
88+
&self,
89+
file_path: PathBuf,
90+
refs: Option<&str>,
91+
) -> Result<Option<String>, GitError> {
8892
let filename = file_path.file_name().unwrap().to_str().unwrap();
8993
let parent = file_path.parent().unwrap();
90-
if let Some(tree) = self.search_tree_by_path(parent).await?
94+
if let Some(tree) = self.search_tree_by_path_with_refs(parent, refs).await?
9195
&& let Some(item) = tree.tree_items.into_iter().find(|x| x.name == filename)
9296
{
9397
match self.get_raw_blob_by_hash(&item.id.to_string()).await {
@@ -111,8 +115,14 @@ pub trait ApiHandler: Send + Sync {
111115
let commit = self.get_tree_relate_commit(tree.id, path).await?;
112116
let mut commit_info: LatestCommitInfo = commit.into();
113117

114-
// Build commit binding information
115-
commit_info.binding_info = self.build_commit_binding_info(&commit_info.oid).await?;
118+
if let Some(binding) = self.build_commit_binding_info(&commit_info.oid).await? {
119+
let display = binding.display_name.clone();
120+
let avatar = binding.avatar_url.clone().unwrap_or_default();
121+
122+
// Fill both author for UI consumption
123+
commit_info.author.display_name = display.clone();
124+
commit_info.author.avatar_url = avatar.clone();
125+
}
116126

117127
Ok(commit_info)
118128
}

ceres/src/model/git.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ pub struct TreeQuery {
4646

4747
#[derive(Debug, Deserialize, IntoParams)]
4848
pub struct BlobContentQuery {
49+
#[serde(default)]
50+
pub refs: String,
4951
#[serde(default = "default_path")]
5052
pub path: String,
5153
}
@@ -58,7 +60,6 @@ pub struct LatestCommitInfo {
5860
pub author: UserInfo,
5961
pub committer: UserInfo,
6062
pub status: String,
61-
pub binding_info: Option<CommitBindingInfo>,
6263
}
6364

6465
#[derive(Serialize, Deserialize, ToSchema)]
@@ -89,7 +90,6 @@ impl From<Commit> for LatestCommitInfo {
8990
author,
9091
committer,
9192
status: "success".to_string(),
92-
binding_info: None, // Will be populated at API layer
9393
}
9494
}
9595
}
@@ -210,6 +210,12 @@ pub struct EditFilePayload {
210210
pub content: String,
211211
/// Commit message to use when creating the commit
212212
pub commit_message: String,
213+
/// author email to bind this commit to a user
214+
#[serde(default)]
215+
pub author_email: Option<String>,
216+
/// platform username (used to verify and bind commit to user)
217+
#[serde(default)]
218+
pub author_username: Option<String>,
213219
}
214220

215221
/// Response body after saving an edited file

mono/src/api/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ pub mod util {
158158
let cedar_path = component.join(".mega_cedar.json");
159159
let entity_str = state
160160
.monorepo()
161-
.get_blob_as_string(cedar_path)
161+
.get_blob_as_string(cedar_path, None)
162162
.await
163163
.unwrap();
164164
if let Some(entity_str) = entity_str {

mono/src/api/router/preview_router.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ async fn get_blob_string(
5555
let data = state
5656
.api_handler(query.path.as_ref())
5757
.await?
58-
.get_blob_as_string(query.path.into())
58+
.get_blob_as_string(query.path.into(), Some(query.refs.as_str()))
5959
.await?;
6060
Ok(Json(CommonResult::success(data)))
6161
}
@@ -378,6 +378,20 @@ async fn save_edit(
378378
Json(payload): Json<EditFilePayload>,
379379
) -> Result<Json<CommonResult<EditFileResult>>, ApiError> {
380380
let handler = state.api_handler(payload.path.as_ref()).await?;
381-
let res = handler.save_file_edit(payload).await?;
381+
let res = handler.save_file_edit(payload.clone()).await?;
382+
383+
// If frontend provided author info, bind commit to that user
384+
if let Some(email) = payload.author_email.as_ref() {
385+
let stg = state.storage.commit_binding_storage();
386+
stg.upsert_binding(
387+
&res.commit_id,
388+
email,
389+
payload.author_username.clone(),
390+
false,
391+
)
392+
.await
393+
.map_err(|e| ApiError::from(anyhow::anyhow!("Failed to save commit binding: {}", e)))?;
394+
}
395+
382396
Ok(Json(CommonResult::success(Some(res))))
383397
}

moon/packages/types/generated.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15255,4 +15255,4 @@ Continuously monitors the log file and streams new content as it becomes availab
1525515255
}
1525615256
}
1525715257
}
15258-
}
15258+
}

0 commit comments

Comments
 (0)