Skip to content

Commit

Permalink
chore: commit fields as optional currently
Browse files Browse the repository at this point in the history
Signed-off-by: Wei Zhang <[email protected]>
  • Loading branch information
zwpaper committed Dec 17, 2024
1 parent 302ea6e commit c0af632
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 17 deletions.
5 changes: 4 additions & 1 deletion crates/tabby-common/src/api/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,

pub language: String,
pub start_line: usize,
}
Expand Down
2 changes: 1 addition & 1 deletion crates/tabby-index/src/code/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl RepositoryExt for CodeRepository {
}
}

get_commit_sha(&self)
get_commit_sha(self)
}
}

Expand Down
11 changes: 5 additions & 6 deletions crates/tabby/src/services/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
},
Expand Down
4 changes: 2 additions & 2 deletions ee/tabby-schema/src/dao.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ impl From<ThreadMessageAttachmentCode> 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,
Expand All @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion ee/tabby-schema/src/schema/thread/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub struct MessageAttachmentClientCode {
#[derive(GraphQLObject, Clone)]
pub struct MessageAttachmentCode {
pub git_url: String,
pub commit: String,
pub commit: Option<String>,
pub filepath: String,
pub language: String,
pub content: String,
Expand Down
14 changes: 8 additions & 6 deletions ee/tabby-webserver/src/service/answer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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,
},
Expand All @@ -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,
},
Expand All @@ -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()));
}
}

0 comments on commit c0af632

Please sign in to comment.