Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(graphQL): add author_email in pull/issue thread attachment #3530

Merged
merged 15 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions ee/tabby-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ pub use repositories::RepositoryDAO;
pub use server_setting::ServerSettingDAO;
use sqlx::{query, query_scalar, sqlite::SqliteQueryResult, Pool, Sqlite, SqlitePool};
pub use threads::{
ThreadDAO, ThreadMessageAttachmentClientCode, ThreadMessageAttachmentCode,
ThreadMessageAttachmentDoc, ThreadMessageAttachmentIssueDoc, ThreadMessageAttachmentPullDoc,
ThreadMessageAttachmentWebDoc, ThreadMessageDAO,
ThreadDAO, ThreadMessageAttachmentAuthor, ThreadMessageAttachmentClientCode,
ThreadMessageAttachmentCode, ThreadMessageAttachmentDoc, ThreadMessageAttachmentIssueDoc,
ThreadMessageAttachmentPullDoc, ThreadMessageAttachmentWebDoc, ThreadMessageDAO,
};
use tokio::sync::Mutex;
use user_completions::UserCompletionDailyStatsDAO;
Expand Down
9 changes: 9 additions & 0 deletions ee/tabby-db/src/threads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
pub struct ThreadMessageAttachmentIssueDoc {
pub title: String,
pub link: String,
pub author_user_id: Option<String>,
pub body: String,
pub closed: bool,
}
Expand All @@ -58,11 +59,19 @@
pub struct ThreadMessageAttachmentPullDoc {
pub title: String,
pub link: String,
pub author_user_id: Option<String>,
pub body: String,
pub diff: String,
pub merged: bool,
}

#[derive(Serialize, Deserialize)]

Check warning on line 68 in ee/tabby-db/src/threads.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-db/src/threads.rs#L68

Added line #L68 was not covered by tests
pub struct ThreadMessageAttachmentAuthor {
pub id: String,
pub name: String,
pub email: String,
}

#[derive(Serialize, Deserialize)]
pub struct ThreadMessageAttachmentCode {
pub git_url: String,
Expand Down
2 changes: 2 additions & 0 deletions ee/tabby-schema/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -512,13 +512,15 @@ type MessageAttachmentCodeScores {
type MessageAttachmentIssueDoc {
title: String!
link: String!
author: User
body: String!
closed: Boolean!
}

type MessageAttachmentPullDoc {
title: String!
link: String!
author: User
body: String!
patch: String!
merged: Boolean!
Expand Down
99 changes: 39 additions & 60 deletions ee/tabby-schema/src/dao.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
EmailSettingDAO, IntegrationDAO, InvitationDAO, JobRunDAO, OAuthCredentialDAO,
ServerSettingDAO, ThreadDAO, ThreadMessageAttachmentClientCode, ThreadMessageAttachmentCode,
ThreadMessageAttachmentDoc, ThreadMessageAttachmentIssueDoc, ThreadMessageAttachmentPullDoc,
ThreadMessageAttachmentWebDoc, ThreadMessageDAO, UserEventDAO,
ThreadMessageAttachmentWebDoc, UserEventDAO,
};

use crate::{
integration::{Integration, IntegrationKind, IntegrationStatus},
interface::UserValue,
repository::RepositoryKind,
schema::{
auth::{self, OAuthCredential, OAuthProvider},
Expand All @@ -22,7 +23,7 @@
user_event::{EventKind, UserEvent},
CoreError,
},
thread::{self, MessageAttachment},
thread::{self},
};

impl From<InvitationDAO> for auth::Invitation {
Expand Down Expand Up @@ -228,33 +229,36 @@
}
}

impl From<ThreadMessageAttachmentDoc> for thread::MessageAttachmentDoc {
fn from(value: ThreadMessageAttachmentDoc) -> Self {
match value {
ThreadMessageAttachmentDoc::Web(val) => {
thread::MessageAttachmentDoc::Web(thread::MessageAttachmentWebDoc {
title: val.title,
link: val.link,
content: val.content,
})
}
ThreadMessageAttachmentDoc::Issue(val) => {
thread::MessageAttachmentDoc::Issue(thread::MessageAttachmentIssueDoc {
title: val.title,
link: val.link,
body: val.body,
closed: val.closed,
})
}
ThreadMessageAttachmentDoc::Pull(val) => {
thread::MessageAttachmentDoc::Pull(thread::MessageAttachmentPullDoc {
title: val.title,
link: val.link,
body: val.body,
patch: val.diff,
merged: val.merged,
})
}
pub fn from_thread_message_attachment_document(
doc: ThreadMessageAttachmentDoc,
author: Option<UserValue>,
) -> thread::MessageAttachmentDoc {
match doc {
ThreadMessageAttachmentDoc::Web(web) => {
thread::MessageAttachmentDoc::Web(thread::MessageAttachmentWebDoc {
title: web.title,
link: web.link,
content: web.content,
})

Check warning on line 242 in ee/tabby-schema/src/dao.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-schema/src/dao.rs#L232-L242

Added lines #L232 - L242 were not covered by tests
}
ThreadMessageAttachmentDoc::Issue(issue) => {
thread::MessageAttachmentDoc::Issue(thread::MessageAttachmentIssueDoc {
title: issue.title,
link: issue.link,
author,
body: issue.body,
closed: issue.closed,
})

Check warning on line 251 in ee/tabby-schema/src/dao.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-schema/src/dao.rs#L244-L251

Added lines #L244 - L251 were not covered by tests
}
ThreadMessageAttachmentDoc::Pull(pull) => {
thread::MessageAttachmentDoc::Pull(thread::MessageAttachmentPullDoc {
title: pull.title,
link: pull.link,
author,
body: pull.body,
patch: pull.diff,
merged: pull.merged,
})

Check warning on line 261 in ee/tabby-schema/src/dao.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-schema/src/dao.rs#L253-L261

Added lines #L253 - L261 were not covered by tests
}
}
}
Expand All @@ -273,6 +277,9 @@
ThreadMessageAttachmentDoc::Issue(ThreadMessageAttachmentIssueDoc {
title: val.title.clone(),
link: val.link.clone(),
author_user_id: val.author.as_ref().map(|x| match x {
UserValue::UserSecured(user) => user.id.to_string(),
}),

Check warning on line 282 in ee/tabby-schema/src/dao.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-schema/src/dao.rs#L280-L282

Added lines #L280 - L282 were not covered by tests
body: val.body.clone(),
closed: val.closed,
})
Expand All @@ -281,6 +288,9 @@
ThreadMessageAttachmentDoc::Pull(ThreadMessageAttachmentPullDoc {
title: val.title.clone(),
link: val.link.clone(),
author_user_id: val.author.as_ref().map(|x| match x {
UserValue::UserSecured(user) => user.id.to_string(),
}),

Check warning on line 293 in ee/tabby-schema/src/dao.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-schema/src/dao.rs#L291-L293

Added lines #L291 - L293 were not covered by tests
body: val.body.clone(),
diff: val.patch.clone(),
merged: val.merged,
Expand All @@ -301,37 +311,6 @@
}
}

impl TryFrom<ThreadMessageDAO> for thread::Message {
type Error = anyhow::Error;
fn try_from(value: ThreadMessageDAO) -> Result<Self, Self::Error> {
let code = value.code_attachments;
let client_code = value.client_code_attachments;
let doc = value.doc_attachments;

let attachment = MessageAttachment {
code: code
.map(|x| x.0.into_iter().map(|i| i.into()).collect())
.unwrap_or_default(),
client_code: client_code
.map(|x| x.0.into_iter().map(|i| i.into()).collect())
.unwrap_or_default(),
doc: doc
.map(|x| x.0.into_iter().map(|i| i.into()).collect())
.unwrap_or_default(),
};

Ok(Self {
id: value.id.as_id(),
thread_id: value.thread_id.as_id(),
role: thread::Role::from_enum_str(&value.role)?,
content: value.content,
attachment,
created_at: value.created_at,
updated_at: value.updated_at,
})
}
}

lazy_static! {
static ref HASHER: HashIds = HashIds::builder()
.with_salt("tabby-id-serializer")
Expand Down
2 changes: 1 addition & 1 deletion ee/tabby-schema/src/schema/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@
}
}

#[derive(Debug, GraphQLObject)]
#[derive(Debug, GraphQLObject, Clone)]

Check warning on line 177 in ee/tabby-schema/src/schema/auth.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-schema/src/schema/auth.rs#L177

Added line #L177 was not covered by tests
#[graphql(context = Context, impl = [UserValue])]
pub struct UserSecured {
// === implements User ===
Expand Down
28 changes: 15 additions & 13 deletions ee/tabby-schema/src/schema/thread/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
use serde::Serialize;
use tabby_common::api::{
code::{CodeSearchDocument, CodeSearchHit, CodeSearchScores},
structured_doc::{DocSearchDocument, DocSearchHit},
structured_doc::DocSearchDocument,
};
use validator::Validate;

use crate::{juniper::relay::NodeType, Context};
use crate::{interface::UserValue, juniper::relay::NodeType, Context};

#[derive(GraphQLEnum, Serialize, Clone, PartialEq, Eq)]
pub enum Role {
Expand Down Expand Up @@ -55,6 +55,7 @@
}

#[derive(GraphQLObject, Clone, Default)]
#[graphql(context = Context)]
pub struct MessageAttachment {
pub code: Vec<MessageAttachmentCode>,
pub client_code: Vec<MessageAttachmentClientCode>,
Expand Down Expand Up @@ -122,6 +123,7 @@
}

#[derive(GraphQLUnion, Clone)]
#[graphql(context = Context)]
pub enum MessageAttachmentDoc {
Web(MessageAttachmentWebDoc),
Issue(MessageAttachmentIssueDoc),
Expand All @@ -136,24 +138,28 @@
}

#[derive(GraphQLObject, Clone)]
#[graphql(context = Context)]
pub struct MessageAttachmentIssueDoc {
pub title: String,
pub link: String,
pub author: Option<UserValue>,
pub body: String,
pub closed: bool,
}

#[derive(GraphQLObject, Clone)]
#[graphql(context = Context)]
pub struct MessageAttachmentPullDoc {
pub title: String,
pub link: String,
pub author: Option<UserValue>,
pub body: String,
pub patch: String,
pub merged: bool,
}

impl From<DocSearchDocument> for MessageAttachmentDoc {
fn from(doc: DocSearchDocument) -> Self {
impl MessageAttachmentDoc {
pub fn from_doc_search_document(doc: DocSearchDocument, author: Option<UserValue>) -> Self {
match doc {
DocSearchDocument::Web(web) => MessageAttachmentDoc::Web(MessageAttachmentWebDoc {
title: web.title,
Expand All @@ -164,13 +170,15 @@
MessageAttachmentDoc::Issue(MessageAttachmentIssueDoc {
title: issue.title,
link: issue.link,
author,

Check warning on line 173 in ee/tabby-schema/src/schema/thread/types.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-schema/src/schema/thread/types.rs#L173

Added line #L173 was not covered by tests
body: issue.body,
closed: issue.closed,
})
}
DocSearchDocument::Pull(pull) => MessageAttachmentDoc::Pull(MessageAttachmentPullDoc {
title: pull.title,
link: pull.link,
author,

Check warning on line 181 in ee/tabby-schema/src/schema/thread/types.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-schema/src/schema/thread/types.rs#L181

Added line #L181 was not covered by tests
body: pull.body,
patch: pull.diff,
merged: pull.merged,
Expand All @@ -180,20 +188,12 @@
}

#[derive(GraphQLObject)]
#[graphql(context = Context)]
pub struct MessageDocSearchHit {
pub doc: MessageAttachmentDoc,
pub score: f64,
}

impl From<DocSearchHit> for MessageDocSearchHit {
fn from(hit: DocSearchHit) -> Self {
Self {
doc: hit.doc.into(),
score: hit.score as f64,
}
}
}

#[derive(GraphQLObject)]
#[graphql(context = Context)]
pub struct Thread {
Expand Down Expand Up @@ -245,6 +245,7 @@
}

#[derive(GraphQLObject)]
#[graphql(context = Context)]
pub struct ThreadAssistantMessageAttachmentsDoc {
pub hits: Vec<MessageDocSearchHit>,
}
Expand All @@ -263,6 +264,7 @@
///
/// Apart from `thread_message_content_delta`, all other items will only appear once in the stream.
#[derive(GraphQLUnion)]
#[graphql(context = Context)]
pub enum ThreadRunItem {
ThreadCreated(ThreadCreated),
ThreadRelevantQuestions(ThreadRelevantQuestions),
Expand Down
15 changes: 15 additions & 0 deletions ee/tabby-webserver/src/service/access_policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,18 @@ impl AccessPolicyService for AccessPolicyServiceImpl {
pub fn create(db: DbConn, context: Arc<dyn ContextService>) -> impl AccessPolicyService {
AccessPolicyServiceImpl { db, context }
}

#[cfg(test)]
pub mod testutils {
use tabby_schema::policy::AccessPolicy;

use super::*;

pub async fn make_policy() -> AccessPolicy {
AccessPolicy::new(
DbConn::new_in_memory().await.unwrap(),
&ID::from("nihao".to_string()),
false,
)
}
}
Loading
Loading