From c0af6327dfcabefce32565d46440925e2b0be5a3 Mon Sep 17 00:00:00 2001 From: Wei Zhang Date: Tue, 17 Dec 2024 20:30:43 +0800 Subject: [PATCH] chore: commit fields as optional currently Signed-off-by: Wei Zhang --- crates/tabby-common/src/api/code.rs | 5 ++++- crates/tabby-index/src/code/repository.rs | 2 +- crates/tabby/src/services/code.rs | 11 +++++------ ee/tabby-schema/src/dao.rs | 4 ++-- ee/tabby-schema/src/schema/thread/types.rs | 2 +- ee/tabby-webserver/src/service/answer.rs | 14 ++++++++------ 6 files changed, 21 insertions(+), 17 deletions(-) diff --git a/crates/tabby-common/src/api/code.rs b/crates/tabby-common/src/api/code.rs index a170b6b023e7..4e4e920aa744 100644 --- a/crates/tabby-common/src/api/code.rs +++ b/crates/tabby-common/src/api/code.rs @@ -30,7 +30,10 @@ pub struct CodeSearchDocument { pub body: String, pub filepath: String, pub git_url: String, - pub commit: String, + + // FIXME(kweizh): This should be a required field after 0.25.0. + pub commit: Option, + pub language: String, pub start_line: usize, } diff --git a/crates/tabby-index/src/code/repository.rs b/crates/tabby-index/src/code/repository.rs index 625182f1d6c6..7991cff46a72 100644 --- a/crates/tabby-index/src/code/repository.rs +++ b/crates/tabby-index/src/code/repository.rs @@ -49,7 +49,7 @@ impl RepositoryExt for CodeRepository { } } - get_commit_sha(&self) + get_commit_sha(self) } } diff --git a/crates/tabby/src/services/code.rs b/crates/tabby/src/services/code.rs index 03080ddb34b4..16a6c723470a 100644 --- a/crates/tabby/src/services/code.rs +++ b/crates/tabby/src/services/code.rs @@ -187,12 +187,12 @@ fn create_hit(scores: CodeSearchScores, doc: TantivyDocument) -> CodeSearchHit { .to_owned(), // commit is introduced in v0.23, but it is also a required field // so we need to handle the case where it's not present - commit: get_json_text_field_or_default( + commit: get_json_text_field_optional( &doc, schema.field_chunk_attributes, code::fields::CHUNK_COMMIT, ) - .to_owned(), + .map(|s| s.to_owned()), language: get_json_text_field( &doc, schema.field_chunk_attributes, @@ -236,16 +236,15 @@ fn get_json_text_field<'a>(doc: &'a TantivyDocument, field: schema::Field, name: .unwrap() } -fn get_json_text_field_or_default<'a>( +fn get_json_text_field_optional<'a>( doc: &'a TantivyDocument, field: schema::Field, name: &str, -) -> &'a str { +) -> Option<&'a str> { doc.get_first(field) .and_then(|value| value.as_object()) .and_then(|mut obj| obj.find(|(k, _)| *k == name)) .and_then(|(_, v)| v.as_str()) - .unwrap_or("") } struct CodeSearchService { @@ -298,7 +297,7 @@ mod tests { body: body.to_string(), filepath: "".to_owned(), git_url: "".to_owned(), - commit: "".to_owned(), + commit: Some("".to_owned()), language: "".to_owned(), start_line: 0, }, diff --git a/ee/tabby-schema/src/dao.rs b/ee/tabby-schema/src/dao.rs index fad9b1928475..60b7c64c8073 100644 --- a/ee/tabby-schema/src/dao.rs +++ b/ee/tabby-schema/src/dao.rs @@ -202,7 +202,7 @@ impl From for thread::MessageAttachmentCode { fn from(value: ThreadMessageAttachmentCode) -> Self { Self { git_url: value.git_url, - commit: value.commit.unwrap_or_default(), + commit: value.commit, filepath: value.filepath, language: value.language, content: value.content, @@ -215,7 +215,7 @@ impl From<&thread::MessageAttachmentCode> for ThreadMessageAttachmentCode { fn from(val: &thread::MessageAttachmentCode) -> Self { ThreadMessageAttachmentCode { git_url: val.git_url.clone(), - commit: Some(val.commit.clone()), + commit: val.commit.clone(), filepath: val.filepath.clone(), language: val.language.clone(), content: val.content.clone(), diff --git a/ee/tabby-schema/src/schema/thread/types.rs b/ee/tabby-schema/src/schema/thread/types.rs index 7985a9194833..8745dc7502c8 100644 --- a/ee/tabby-schema/src/schema/thread/types.rs +++ b/ee/tabby-schema/src/schema/thread/types.rs @@ -72,7 +72,7 @@ pub struct MessageAttachmentClientCode { #[derive(GraphQLObject, Clone)] pub struct MessageAttachmentCode { pub git_url: String, - pub commit: String, + pub commit: Option, pub filepath: String, pub language: String, pub content: String, diff --git a/ee/tabby-webserver/src/service/answer.rs b/ee/tabby-webserver/src/service/answer.rs index d7a6d256968a..d5a5bce7b525 100644 --- a/ee/tabby-webserver/src/service/answer.rs +++ b/ee/tabby-webserver/src/service/answer.rs @@ -767,7 +767,7 @@ mod tests { })], code: vec![tabby_schema::thread::MessageAttachmentCode { git_url: "https://github.com/".to_owned(), - commit: "commit".to_owned(), + commit: Some("commit".to_owned()), filepath: "server.py".to_owned(), language: "python".to_owned(), content: "from flask import Flask\n\napp = Flask(__name__)\n\n@app.route('/')\ndef hello():\n return 'Hello, World!'".to_owned(), @@ -801,7 +801,7 @@ mod tests { )], code: vec![tabby_schema::thread::MessageAttachmentCode { git_url: "https://github.com".to_owned(), - commit: "commit".to_owned(), + commit: Some("commit".to_owned()), filepath: "server.py".to_owned(), language: "python".to_owned(), content: "print('Hello, server!')".to_owned(), @@ -978,7 +978,7 @@ mod tests { )], code: vec![tabby_schema::thread::MessageAttachmentCode { git_url: "https://github.com".to_owned(), - commit: "commit".to_owned(), + commit: Some("commit".to_owned()), filepath: "server.py".to_owned(), language: "python".to_owned(), content: "print('Hello, server!')".to_owned(), @@ -1040,7 +1040,7 @@ mod tests { )], code: vec![tabby_schema::thread::MessageAttachmentCode { git_url: "https://github.com".to_owned(), - commit: "commit".to_owned(), + commit: Some("commit".to_owned()), filepath: "server.py".to_owned(), language: "python".to_owned(), content: "print('Hello, server!')".to_owned(), @@ -1338,7 +1338,7 @@ mod tests { body: "fn test1() {}\nfn test2() {}".to_string(), filepath: "test.rs".to_string(), git_url: "https://github.com/test/repo.git".to_string(), - commit: "commit".to_string(), + commit: Some("commit".to_string()), language: "rust".to_string(), start_line: 1, }, @@ -1355,7 +1355,7 @@ mod tests { body: "fn test3() {}\nfn test4() {}".to_string(), filepath: "test.rs".to_string(), git_url: "https://github.com/test/repo.git".to_string(), - commit: "commit".to_string(), + commit: Some("commit".to_string()), language: "rust".to_string(), start_line: 3, }, @@ -1370,5 +1370,7 @@ mod tests { let result = merge_code_snippets(&repo.unwrap(), hits).await; assert_eq!(result.len(), 2); + assert_eq!(result[0].doc.commit, Some("commit".to_string())); + assert_eq!(result[1].doc.commit, Some("commit".to_string())); } }