Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat: ability to remove github link (#2288)

Will Hutchinson87a58dddcd22
diff --git a/js/app/packages/service-clients/service-auth/generated/client.ts b/js/app/packages/service-clients/service-auth/generated/client.ts
index e3ce379e2a..e89e19326d 100644
--- a/js/app/packages/service-clients/service-auth/generated/client.ts
+++ b/js/app/packages/service-clients/service-auth/generated/client.ts
@@ -569,6 +569,66 @@ export const initGithubLink = async (
} as initGithubLinkResponse;
};
+/**
+ * @summary Deletes a github link for a user
+ */
+export type deleteGithubLinkResponse200 = {
+ data: EmptyResponse;
+ status: 200;
+};
+
+export type deleteGithubLinkResponse400 = {
+ data: ErrorResponse;
+ status: 400;
+};
+
+export type deleteGithubLinkResponse401 = {
+ data: ErrorResponse;
+ status: 401;
+};
+
+export type deleteGithubLinkResponse500 = {
+ data: ErrorResponse;
+ status: 500;
+};
+
+export type deleteGithubLinkResponseSuccess = deleteGithubLinkResponse200 & {
+ headers: Headers;
+};
+export type deleteGithubLinkResponseError = (
+ | deleteGithubLinkResponse400
+ | deleteGithubLinkResponse401
+ | deleteGithubLinkResponse500
+) & {
+ headers: Headers;
+};
+
+export type deleteGithubLinkResponse =
+ | deleteGithubLinkResponseSuccess
+ | deleteGithubLinkResponseError;
+
+export const getDeleteGithubLinkUrl = () => {
+ return `/link/github`;
+};
+
+export const deleteGithubLink = async (
+ options?: RequestInit
+): Promise<deleteGithubLinkResponse> => {
+ const res = await fetch(getDeleteGithubLinkUrl(), {
+ ...options,
+ method: 'DELETE',
+ });
+
+ const body = [204, 205, 304].includes(res.status) ? null : await res.text();
+
+ const data: deleteGithubLinkResponse['data'] = body ? JSON.parse(body) : {};
+ return {
+ data,
+ status: res.status,
+ headers: res.headers,
+ } as deleteGithubLinkResponse;
+};
+
/**
* @summary Completes the apple login flow
*/
diff --git a/js/app/packages/service-clients/service-auth/openapi.json b/js/app/packages/service-clients/service-auth/openapi.json
index 3f3feb137e..618d50d826 100644
--- a/js/app/packages/service-clients/service-auth/openapi.json
+++ b/js/app/packages/service-clients/service-auth/openapi.json
@@ -422,6 +422,53 @@
}
}
}
+ },
+ "delete": {
+ "tags": ["link::github"],
+ "summary": "Deletes a github link for a user",
+ "operationId": "delete_github_link",
+ "responses": {
+ "200": {
+ "description": "",
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/EmptyResponse"
+ }
+ }
+ }
+ },
+ "400": {
+ "description": "",
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/ErrorResponse"
+ }
+ }
+ }
+ },
+ "401": {
+ "description": "",
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/ErrorResponse"
+ }
+ }
+ }
+ },
+ "500": {
+ "description": "",
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/ErrorResponse"
+ }
+ }
+ }
+ }
+ }
}
},
"/login/apple": {
diff --git a/rust/cloud-storage/authentication_service/src/api/link/github.rs b/rust/cloud-storage/authentication_service/src/api/link/github.rs
index 86d8f380b3..9db0a15618 100644
--- a/rust/cloud-storage/authentication_service/src/api/link/github.rs
+++ b/rust/cloud-storage/authentication_service/src/api/link/github.rs
@@ -1,15 +1,16 @@
use axum::{
- Extension, Json,
+ Json,
extract::State,
http::StatusCode,
response::{IntoResponse, Response},
};
use github::domain::{models::GithubError, ports::GithubLinkService};
use macro_middleware::tracking::ClientIp;
+use model_user::axum_extractor::MacroUserExtractor;
use crate::api::{context::ApiContext, oauth2::OAuthState};
-use model::{response::ErrorResponse, user::UserContext};
+use model::response::{EmptyResponse, ErrorResponse};
#[derive(serde::Deserialize, serde::Serialize, Debug, utoipa::ToSchema)]
pub struct InitGithubLinkResponse {
@@ -73,18 +74,18 @@ impl IntoResponse for InitGithubLinkError {
(status = 500, body=ErrorResponse),
)
)]
-#[tracing::instrument(skip(ctx, ip_context, user_context), fields(client_ip=%ip_context, user_id=%user_context.user_id, fusion_user_id=%user_context.fusion_user_id), err)]
+#[tracing::instrument(skip(ctx, ip_context, user_context), fields(client_ip=%ip_context, user_id=%user_context.user_context.user_id, fusion_user_id=%user_context.user_context.fusion_user_id), err)]
pub async fn init_github_link_handler(
State(ctx): State<ApiContext>,
ip_context: ClientIp,
- user_context: Extension<UserContext>,
+ user_context: MacroUserExtractor,
) -> Result<Json<InitGithubLinkResponse>, InitGithubLinkError> {
// TODO: this should probably be a middleware or extractor
// Check count of in-progress links
let count =
macro_db_client::in_progress_user_link::count_existing_in_progress_user_links_for_user(
&ctx.db,
- &user_context.fusion_user_id,
+ &user_context.user_context.fusion_user_id,
)
.await?;
@@ -95,7 +96,7 @@ pub async fn init_github_link_handler(
// Create in-progress link
let link_id = macro_db_client::in_progress_user_link::create_in_progress_user_link(
&ctx.db,
- &user_context.fusion_user_id,
+ &user_context.user_context.fusion_user_id,
)
.await?;
@@ -129,3 +130,57 @@ pub async fn init_github_link_handler(
link_id,
}))
}
+
+/// Error type for delete Github link operations
+#[derive(thiserror::Error, Debug)]
+pub enum DeleteGithubLinkError {
+ /// Internal error
+ #[error("internal error occurred")]
+ InternalError(#[from] anyhow::Error),
+ /// Internal github error
+ #[error("internal error occurred")]
+ GithubServiceError(#[from] GithubError),
+}
+
+impl IntoResponse for DeleteGithubLinkError {
+ fn into_response(self) -> Response {
+ let message = self.to_string();
+ let status_code: StatusCode = match &self {
+ DeleteGithubLinkError::InternalError(_)
+ | DeleteGithubLinkError::GithubServiceError(_) => StatusCode::INTERNAL_SERVER_ERROR,
+ };
+
+ (
+ status_code,
+ Json(ErrorResponse {
+ message: message.into(),
+ }),
+ )
+ .into_response()
+ }
+}
+
+/// Deletes a github link for a user
+#[utoipa::path(
+ delete,
+ operation_id = "delete_github_link",
+ path = "/link/github",
+ responses(
+ (status = 200, body=EmptyResponse),
+ (status = 400, body=ErrorResponse),
+ (status = 401, body=ErrorResponse),
+ (status = 500, body=ErrorResponse),
+ )
+ )]
+#[tracing::instrument(skip(ctx, ip_context, user_context), fields(client_ip=%ip_context, user_id=%user_context.macro_user_id), err)]
+pub async fn delete_github_link_handler(
+ State(ctx): State<ApiContext>,
+ ip_context: ClientIp,
+ user_context: MacroUserExtractor,
+) -> Result<Json<EmptyResponse>, DeleteGithubLinkError> {
+ ctx.github_link_service
+ .delete_user_link(&user_context.macro_user_id)
+ .await?;
+
+ Ok(Json(EmptyResponse::default()))
+}
diff --git a/rust/cloud-storage/authentication_service/src/api/link/mod.rs b/rust/cloud-storage/authentication_service/src/api/link/mod.rs
index 29a9bb3f61..a267d38b55 100644
--- a/rust/cloud-storage/authentication_service/src/api/link/mod.rs
+++ b/rust/cloud-storage/authentication_service/src/api/link/mod.rs
@@ -1,5 +1,8 @@
use crate::api::context::ApiContext;
-use axum::{Router, routing::post};
+use axum::{
+ Router,
+ routing::{delete, post},
+};
pub(in crate::api) mod create_in_progress_link;
pub(in crate::api) mod github;
@@ -9,4 +12,5 @@ pub fn router(_state: ApiContext) -> Router<ApiContext> {
Router::new()
.route("/", post(create_in_progress_link::handler))
.route("/github", post(github::init_github_link_handler))
+ .route("/github", delete(github::delete_github_link_handler))
}
diff --git a/rust/cloud-storage/authentication_service/src/api/swagger.rs b/rust/cloud-storage/authentication_service/src/api/swagger.rs
index c8ff3c70d6..fd131e2ddf 100644
--- a/rust/cloud-storage/authentication_service/src/api/swagger.rs
+++ b/rust/cloud-storage/authentication_service/src/api/swagger.rs
@@ -68,6 +68,7 @@ use model::user::{
/// /link
link::create_in_progress_link::handler,
link::github::init_github_link_handler,
+ link::github::delete_github_link_handler,
/// /oauth
oauth::oauth_redirect::handler,
diff --git a/rust/cloud-storage/github/.sqlx/query-03a323aefdba22727c6789b28d90d7200e795a4591812dab4c4e7718c5512055.json b/rust/cloud-storage/github/.sqlx/query-03a323aefdba22727c6789b28d90d7200e795a4591812dab4c4e7718c5512055.json
deleted file mode 100644
index 83caec4173..0000000000
--- a/rust/cloud-storage/github/.sqlx/query-03a323aefdba22727c6789b28d90d7200e795a4591812dab4c4e7718c5512055.json
+++ /dev/null
@@ -1,23 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n WITH RECURSIVE project_hierarchy AS (\n SELECT p.id as project_id\n FROM email_threads t\n JOIN \"Project\" p ON t.project_id = p.id AND p.\"deletedAt\" IS NULL\n WHERE t.id = $1\n UNION ALL\n SELECT parent.id as project_id\n FROM project_hierarchy ph\n JOIN \"Project\" parent ON parent.id = (\n SELECT \"parentId\" FROM \"Project\" WHERE id = ph.project_id AND \"parentId\" IS NOT NULL AND \"deletedAt\" IS NULL\n )\n )\n SELECT DISTINCT user_id FROM \"UserItemAccess\"\n WHERE item_id IN (\n SELECT $2\n UNION\n SELECT project_id FROM project_hierarchy\n )\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "user_id",
- "type_info": "Text"
- }
- ],
- "parameters": {
- "Left": [
- "Uuid",
- "Text"
- ]
- },
- "nullable": [
- false
- ]
- },
- "hash": "03a323aefdba22727c6789b28d90d7200e795a4591812dab4c4e7718c5512055"
-}
diff --git a/rust/cloud-storage/github/.sqlx/query-37b5168f739c490a3373c65eab35f247a34af0a8e659cbad61cb99e3094e5a60.json b/rust/cloud-storage/github/.sqlx/query-37b5168f739c490a3373c65eab35f247a34af0a8e659cbad61cb99e3094e5a60.json
deleted file mode 100644
index 44d3a67e72..0000000000
--- a/rust/cloud-storage/github/.sqlx/query-37b5168f739c490a3373c65eab35f247a34af0a8e659cbad61cb99e3094e5a60.json
+++ /dev/null
@@ -1,23 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n SELECT EXISTS (\n SELECT 1\n FROM public.email_threads t\n JOIN public.email_links l ON l.id = t.link_id\n WHERE t.id = $1::uuid\n AND l.macro_id = $2\n ) AS \"exists!\"\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "exists!",
- "type_info": "Bool"
- }
- ],
- "parameters": {
- "Left": [
- "Uuid",
- "Text"
- ]
- },
- "nullable": [
- null
- ]
- },
- "hash": "37b5168f739c490a3373c65eab35f247a34af0a8e659cbad61cb99e3094e5a60"
-}
diff --git a/rust/cloud-storage/github/.sqlx/query-3a7be99469ed150e0506efa786b672198a6d3419c6bd5548418520cd3e6a8e95.json b/rust/cloud-storage/github/.sqlx/query-3a7be99469ed150e0506efa786b672198a6d3419c6bd5548418520cd3e6a8e95.json
deleted file mode 100644
index 60bfee0647..0000000000
--- a/rust/cloud-storage/github/.sqlx/query-3a7be99469ed150e0506efa786b672198a6d3419c6bd5548418520cd3e6a8e95.json
+++ /dev/null
@@ -1,23 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n WITH RECURSIVE project_hierarchy AS (\n SELECT p.id as project_id\n FROM \"Document\" d\n JOIN \"Project\" p ON d.\"projectId\" = p.id AND p.\"deletedAt\" IS NULL\n WHERE d.id = $1 AND d.\"deletedAt\" IS NULL\n UNION ALL\n SELECT parent.id as project_id\n FROM project_hierarchy ph\n JOIN \"Project\" parent ON parent.id = (\n SELECT \"parentId\" FROM \"Project\" WHERE id = ph.project_id AND \"parentId\" IS NOT NULL AND \"deletedAt\" IS NULL\n )\n )\n SELECT access_level FROM (\n -- Source 1: Cast the AccessLevel enum to text.\n SELECT access_level::text FROM \"UserItemAccess\"\n WHERE user_id = $2 AND item_id IN (\n SELECT $1\n UNION\n SELECT project_id FROM project_hierarchy\n )\n UNION ALL\n -- Source 2: Select the publicAccessLevel (which is already text).\n SELECT \"publicAccessLevel\" as access_level\n FROM \"SharePermission\"\n WHERE \"isPublic\" = true AND \"publicAccessLevel\" IS NOT NULL AND id IN (\n SELECT \"sharePermissionId\" FROM \"DocumentPermission\" WHERE \"documentId\" = $1\n UNION\n SELECT \"sharePermissionId\" FROM \"ProjectPermission\" WHERE \"projectId\" IN (SELECT project_id FROM project_hierarchy)\n )\n ) as all_levels\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "access_level",
- "type_info": "Text"
- }
- ],
- "parameters": {
- "Left": [
- "Text",
- "Text"
- ]
- },
- "nullable": [
- null
- ]
- },
- "hash": "3a7be99469ed150e0506efa786b672198a6d3419c6bd5548418520cd3e6a8e95"
-}
diff --git a/rust/cloud-storage/github/.sqlx/query-4afa79ca95bdbcebe9d3de7ebb204636419e6cf89e650a8716c40ab2e588aa5b.json b/rust/cloud-storage/github/.sqlx/query-4afa79ca95bdbcebe9d3de7ebb204636419e6cf89e650a8716c40ab2e588aa5b.json
deleted file mode 100644
index 58618b2697..0000000000
--- a/rust/cloud-storage/github/.sqlx/query-4afa79ca95bdbcebe9d3de7ebb204636419e6cf89e650a8716c40ab2e588aa5b.json
+++ /dev/null
@@ -1,23 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n -- CTE to recursively find all parent projects, starting from the given project.\n WITH RECURSIVE project_hierarchy AS (\n -- Base case: Start with the project ID provided.\n SELECT id as project_id\n FROM \"Project\"\n WHERE id = $1 AND \"deletedAt\" IS NULL\n UNION ALL\n -- Recursive case: Find the parent of the project from the previous step.\n SELECT parent.id as project_id\n FROM project_hierarchy ph\n JOIN \"Project\" parent ON parent.id = (\n SELECT \"parentId\" FROM \"Project\" WHERE id = ph.project_id AND \"parentId\" IS NOT NULL AND \"deletedAt\" IS NULL\n )\n )\n -- The subquery now gathers all levels as plain text.\n SELECT access_level FROM (\n -- Source 1: Cast the AccessLevel enum to text.\n SELECT access_level::text FROM \"UserItemAccess\"\n WHERE user_id = $2 AND item_id IN (\n -- The hierarchy CTE includes the starting project, so this is all we need.\n SELECT project_id FROM project_hierarchy\n )\n UNION ALL\n -- Source 2: Select the publicAccessLevel (which is already text).\n SELECT \"publicAccessLevel\" as access_level\n FROM \"SharePermission\"\n WHERE \"isPublic\" = true AND \"publicAccessLevel\" IS NOT NULL AND id IN (\n -- We only need to check ProjectPermission for the items in the hierarchy.\n SELECT \"sharePermissionId\" FROM \"ProjectPermission\"\n WHERE \"projectId\" IN (SELECT project_id FROM project_hierarchy)\n )\n ) as all_levels\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "access_level",
- "type_info": "Text"
- }
- ],
- "parameters": {
- "Left": [
- "Text",
- "Text"
- ]
- },
- "nullable": [
- null
- ]
- },
- "hash": "4afa79ca95bdbcebe9d3de7ebb204636419e6cf89e650a8716c40ab2e588aa5b"
-}
diff --git a/rust/cloud-storage/github/.sqlx/query-4c695b011d49ba7d5fce4b309875ed952dc386ac760444d7883c5a76718f15be.json b/rust/cloud-storage/github/.sqlx/query-4c695b011d49ba7d5fce4b309875ed952dc386ac760444d7883c5a76718f15be.json
deleted file mode 100644
index 524cc25ec7..0000000000
--- a/rust/cloud-storage/github/.sqlx/query-4c695b011d49ba7d5fce4b309875ed952dc386ac760444d7883c5a76718f15be.json
+++ /dev/null
@@ -1,23 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n WITH RECURSIVE project_hierarchy AS (\n SELECT p.id as project_id\n FROM \"Chat\" c\n JOIN \"Project\" p ON c.\"projectId\" = p.id AND p.\"deletedAt\" IS NULL\n WHERE c.id = $1 AND c.\"deletedAt\" IS NULL\n UNION ALL\n SELECT parent.id as project_id\n FROM project_hierarchy ph\n JOIN \"Project\" parent ON parent.id = (\n SELECT \"parentId\" FROM \"Project\" WHERE id = ph.project_id AND \"parentId\" IS NOT NULL AND \"deletedAt\" IS NULL\n )\n )\n SELECT access_level FROM (\n -- Source 1: Cast the AccessLevel enum to text.\n SELECT access_level::text FROM \"UserItemAccess\"\n WHERE user_id = $2 AND item_id IN (\n SELECT $1 -- The chat itself\n UNION\n SELECT project_id FROM project_hierarchy -- All parent projects\n )\n UNION ALL\n -- Source 2: Select the publicAccessLevel (which is already text).\n SELECT \"publicAccessLevel\" as access_level\n FROM \"SharePermission\"\n WHERE \"isPublic\" = true AND \"publicAccessLevel\" IS NOT NULL AND id IN (\n SELECT \"sharePermissionId\" FROM \"ChatPermission\" WHERE \"chatId\" = $1\n UNION\n SELECT \"sharePermissionId\" FROM \"ProjectPermission\" WHERE \"projectId\" IN (SELECT project_id FROM project_hierarchy)\n )\n ) as all_levels\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "access_level",
- "type_info": "Text"
- }
- ],
- "parameters": {
- "Left": [
- "Text",
- "Text"
- ]
- },
- "nullable": [
- null
- ]
- },
- "hash": "4c695b011d49ba7d5fce4b309875ed952dc386ac760444d7883c5a76718f15be"
-}
diff --git a/rust/cloud-storage/github/.sqlx/query-54e3755d63a676a7fa70da77321e618032f0130c73eb6b0ae985f07c5c6c448b.json b/rust/cloud-storage/github/.sqlx/query-54e3755d63a676a7fa70da77321e618032f0130c73eb6b0ae985f07c5c6c448b.json
deleted file mode 100644
index 03a8dbf6bb..0000000000
--- a/rust/cloud-storage/github/.sqlx/query-54e3755d63a676a7fa70da77321e618032f0130c73eb6b0ae985f07c5c6c448b.json
+++ /dev/null
@@ -1,22 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n WITH RECURSIVE project_hierarchy AS (\n SELECT p.id as project_id\n FROM \"Chat\" c\n JOIN \"Project\" p ON c.\"projectId\" = p.id AND p.\"deletedAt\" IS NULL\n WHERE c.id = $1 AND c.\"deletedAt\" IS NULL\n UNION ALL\n SELECT parent.id as project_id\n FROM project_hierarchy ph\n JOIN \"Project\" parent ON parent.id = (\n SELECT \"parentId\" FROM \"Project\" WHERE id = ph.project_id AND \"parentId\" IS NOT NULL AND \"deletedAt\" IS NULL\n )\n )\n SELECT DISTINCT user_id FROM \"UserItemAccess\"\n WHERE item_id IN (\n SELECT $1\n UNION\n SELECT project_id FROM project_hierarchy\n )\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "user_id",
- "type_info": "Text"
- }
- ],
- "parameters": {
- "Left": [
- "Text"
- ]
- },
- "nullable": [
- false
- ]
- },
- "hash": "54e3755d63a676a7fa70da77321e618032f0130c73eb6b0ae985f07c5c6c448b"
-}
diff --git a/rust/cloud-storage/github/.sqlx/query-7617f808df94ffe77c8195a911ee6b230a21fe9935e099d1310d244248ca253a.json b/rust/cloud-storage/github/.sqlx/query-7617f808df94ffe77c8195a911ee6b230a21fe9935e099d1310d244248ca253a.json
deleted file mode 100644
index 655495fb29..0000000000
--- a/rust/cloud-storage/github/.sqlx/query-7617f808df94ffe77c8195a911ee6b230a21fe9935e099d1310d244248ca253a.json
+++ /dev/null
@@ -1,23 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n SELECT c.id\n FROM comms_channels c\n INNER JOIN comms_channel_participants cp ON cp.channel_id = c.id\n WHERE cp.user_id = $1 AND cp.left_at IS NULL\n AND c.id = ANY($2::uuid[])\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "id",
- "type_info": "Uuid"
- }
- ],
- "parameters": {
- "Left": [
- "Text",
- "UuidArray"
- ]
- },
- "nullable": [
- false
- ]
- },
- "hash": "7617f808df94ffe77c8195a911ee6b230a21fe9935e099d1310d244248ca253a"
-}
diff --git a/rust/cloud-storage/github/.sqlx/query-79d3bc72e6f424765706f7deea426379ae49cbd70dd122daedd95343618b9ac6.json b/rust/cloud-storage/github/.sqlx/query-79d3bc72e6f424765706f7deea426379ae49cbd70dd122daedd95343618b9ac6.json
deleted file mode 100644
index 2bde15938f..0000000000
--- a/rust/cloud-storage/github/.sqlx/query-79d3bc72e6f424765706f7deea426379ae49cbd70dd122daedd95343618b9ac6.json
+++ /dev/null
@@ -1,22 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n WITH RECURSIVE project_hierarchy AS (\n SELECT id as project_id\n FROM \"Project\"\n WHERE id = $1 AND \"deletedAt\" IS NULL\n UNION ALL\n SELECT parent.id as project_id\n FROM project_hierarchy ph\n JOIN \"Project\" parent ON parent.id = (\n SELECT \"parentId\" FROM \"Project\" WHERE id = ph.project_id AND \"parentId\" IS NOT NULL AND \"deletedAt\" IS NULL\n )\n )\n SELECT DISTINCT user_id FROM \"UserItemAccess\"\n WHERE item_id IN (\n SELECT project_id FROM project_hierarchy\n )\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "user_id",
- "type_info": "Text"
- }
- ],
- "parameters": {
- "Left": [
- "Text"
- ]
- },
- "nullable": [
- false
- ]
- },
- "hash": "79d3bc72e6f424765706f7deea426379ae49cbd70dd122daedd95343618b9ac6"
-}
diff --git a/rust/cloud-storage/github/.sqlx/query-96d853b887f2e6d7aac6b15ddd2b71a8d8900d2ef30047da7ca1a5940503b6d0.json b/rust/cloud-storage/github/.sqlx/query-96d853b887f2e6d7aac6b15ddd2b71a8d8900d2ef30047da7ca1a5940503b6d0.json
deleted file mode 100644
index e42c9f2009..0000000000
--- a/rust/cloud-storage/github/.sqlx/query-96d853b887f2e6d7aac6b15ddd2b71a8d8900d2ef30047da7ca1a5940503b6d0.json
+++ /dev/null
@@ -1,22 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n WITH RECURSIVE project_hierarchy AS (\n SELECT p.id as project_id\n FROM \"Document\" d\n JOIN \"Project\" p ON d.\"projectId\" = p.id AND p.\"deletedAt\" IS NULL\n WHERE d.id = $1 AND d.\"deletedAt\" IS NULL\n UNION ALL\n SELECT parent.id as project_id\n FROM project_hierarchy ph\n JOIN \"Project\" parent ON parent.id = (\n SELECT \"parentId\" FROM \"Project\" WHERE id = ph.project_id AND \"parentId\" IS NOT NULL AND \"deletedAt\" IS NULL\n )\n )\n SELECT DISTINCT user_id FROM \"UserItemAccess\"\n WHERE item_id IN (\n SELECT $1\n UNION\n SELECT project_id FROM project_hierarchy\n )\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "user_id",
- "type_info": "Text"
- }
- ],
- "parameters": {
- "Left": [
- "Text"
- ]
- },
- "nullable": [
- false
- ]
- },
- "hash": "96d853b887f2e6d7aac6b15ddd2b71a8d8900d2ef30047da7ca1a5940503b6d0"
-}
diff --git a/rust/cloud-storage/github/.sqlx/query-d536db3ccfb32aee9a48227e2b15d2404023ea24b62c159466c95899d76e29ed.json b/rust/cloud-storage/github/.sqlx/query-d536db3ccfb32aee9a48227e2b15d2404023ea24b62c159466c95899d76e29ed.json
deleted file mode 100644
index 6ef8856744..0000000000
--- a/rust/cloud-storage/github/.sqlx/query-d536db3ccfb32aee9a48227e2b15d2404023ea24b62c159466c95899d76e29ed.json
+++ /dev/null
@@ -1,24 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n WITH RECURSIVE project_hierarchy AS (\n SELECT p.id as project_id\n FROM email_threads t\n JOIN \"Project\" p ON t.project_id = p.id AND p.\"deletedAt\" IS NULL\n WHERE t.id = $1\n UNION ALL\n SELECT parent.id as project_id\n FROM project_hierarchy ph\n JOIN \"Project\" parent ON parent.id = (\n SELECT \"parentId\" FROM \"Project\" WHERE id = ph.project_id AND \"parentId\" IS NOT NULL AND \"deletedAt\" IS NULL\n )\n )\n SELECT access_level FROM (\n -- Source 1: Cast the AccessLevel enum to text.\n SELECT access_level::text FROM \"UserItemAccess\"\n WHERE user_id = $3 AND item_id IN (\n SELECT $2 -- The thread ID as text\n UNION\n SELECT project_id FROM project_hierarchy -- All parent projects\n )\n UNION ALL\n -- Source 2: Select the publicAccessLevel (which is already text).\n SELECT \"publicAccessLevel\" as access_level\n FROM \"SharePermission\"\n WHERE \"isPublic\" = true AND \"publicAccessLevel\" IS NOT NULL AND id IN (\n SELECT \"sharePermissionId\" FROM \"EmailThreadPermission\" WHERE \"threadId\" = $2\n UNION\n SELECT \"sharePermissionId\" FROM \"ProjectPermission\" WHERE \"projectId\" IN (SELECT project_id FROM project_hierarchy)\n )\n ) as all_levels\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "access_level",
- "type_info": "Text"
- }
- ],
- "parameters": {
- "Left": [
- "Uuid",
- "Text",
- "Text"
- ]
- },
- "nullable": [
- null
- ]
- },
- "hash": "d536db3ccfb32aee9a48227e2b15d2404023ea24b62c159466c95899d76e29ed"
-}
diff --git a/rust/cloud-storage/github/.sqlx/query-f4e290fa3d4abc3cc6c28f3e88b35cb2d29a67a4c437234f0ac7d1e6baa34669.json b/rust/cloud-storage/github/.sqlx/query-f4e290fa3d4abc3cc6c28f3e88b35cb2d29a67a4c437234f0ac7d1e6baa34669.json
new file mode 100644
index 0000000000..28c3067d28
--- /dev/null
+++ b/rust/cloud-storage/github/.sqlx/query-f4e290fa3d4abc3cc6c28f3e88b35cb2d29a67a4c437234f0ac7d1e6baa34669.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n DELETE FROM github_links\n WHERE id = $1\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "f4e290fa3d4abc3cc6c28f3e88b35cb2d29a67a4c437234f0ac7d1e6baa34669"
+}
diff --git a/rust/cloud-storage/github/Cargo.toml b/rust/cloud-storage/github/Cargo.toml
index 62f398519d..83d886b83a 100644
--- a/rust/cloud-storage/github/Cargo.toml
+++ b/rust/cloud-storage/github/Cargo.toml
@@ -33,6 +33,7 @@ entity_access = { path = "../entity_access", default-features = false, features
hex = { workspace = true }
hmac = "0.12.1"
jsonwebtoken = { workspace = true }
+macro_env = { path = "../macro_env", features = ["frontend_url"] }
regex = { workspace = true }
reqwest = { workspace = true, optional = true }
serde = { workspace = true }
@@ -44,7 +45,6 @@ thiserror = { workspace = true }
tracing = { workspace = true }
urlencoding = { workspace = true, optional = true }
uuid = { workspace = true }
-macro_env = { path = "../macro_env" }
fusionauth = { path = "../fusionauth", optional = true }
macro_user_id = { path = "../macro_user_id" }
diff --git a/rust/cloud-storage/github/src/domain/ports/link.rs b/rust/cloud-storage/github/src/domain/ports/link.rs
index ed7a0c419b..64add1a24e 100644
--- a/rust/cloud-storage/github/src/domain/ports/link.rs
+++ b/rust/cloud-storage/github/src/domain/ports/link.rs
@@ -45,6 +45,12 @@ pub trait GithubRepo: Send + Sync + 'static {
&self,
in_progress_link_id: &uuid::Uuid,
) -> impl Future<Output = Result<(), Self::Err>> + Send;
+
+ /// Deletes the github link from the repo
+ fn delete_github_link(
+ &self,
+ link_id: &uuid::Uuid,
+ ) -> impl Future<Output = Result<(), Self::Err>> + Send;
}
/// Repository for handling github oauth related actions.
@@ -93,6 +99,13 @@ pub trait Auth: Send + Sync + 'static {
access_token: &str,
) -> impl Future<Output = Result<(), Self::Err>> + Send;
+ /// Deletes the github link for a auth user
+ fn delete_user_link(
+ &self,
+ github_link: &GithubLink,
+ github_idp_id: &str,
+ ) -> impl Future<Output = Result<(), Self::Err>> + Send;
+
/// Retreives the users github access token
fn retreive_access_token(
&self,
@@ -121,4 +134,16 @@ pub trait GithubLinkService: Send + Sync + 'static {
redirect_uri: &str,
code: &str,
) -> impl Future<Output = Result<GithubLink, GithubError>> + Send;
+
+ /// Gets a users github link
+ fn get_user_link(
+ &self,
+ user_id: &MacroUserId<Lowercase<'static>>,
+ ) -> impl Future<Output = Result<GithubLink, GithubError>> + Send;
+
+ /// Deletes the link for the user
+ fn delete_user_link(
+ &self,
+ user_id: &MacroUserId<Lowercase<'static>>,
+ ) -> impl Future<Output = Result<(), GithubError>> + Send;
}
diff --git a/rust/cloud-storage/github/src/domain/service/link.rs b/rust/cloud-storage/github/src/domain/service/link.rs
index 7259dfd5ac..bfc04534a7 100644
--- a/rust/cloud-storage/github/src/domain/service/link.rs
+++ b/rust/cloud-storage/github/src/domain/service/link.rs
@@ -54,6 +54,53 @@ impl<R: GithubRepo, U: GithubOauth, F: Auth> GithubLinkService for GithubLinkSer
.map_err(|e| GithubError::Internal(e.into()))
}
+ #[tracing::instrument(skip(self), err)]
+ async fn get_user_link(
+ &self,
+ macro_user_id: &MacroUserId<Lowercase<'static>>,
+ ) -> Result<GithubLink, GithubError> {
+ self.repo
+ .get_github_link_by_user_id(macro_user_id)
+ .await
+ .map_err(|e| GithubError::Internal(e.into()))
+ }
+
+ #[tracing::instrument(skip(self), err)]
+ async fn delete_user_link(
+ &self,
+ macro_user_id: &MacroUserId<Lowercase<'static>>,
+ ) -> Result<(), GithubError> {
+ // Get link
+ let link = match self.repo.get_github_link_by_user_id(macro_user_id).await {
+ Ok(link) => link,
+ Err(e) => {
+ let e: anyhow::Error = e.into();
+ if let Some(db_err) = e.downcast_ref::<sqlx::Error>()
+ && matches!(db_err, sqlx::Error::RowNotFound)
+ {
+ tracing::trace!("no github link found for user");
+ return Ok(());
+ } else {
+ return Err(GithubError::Internal(e));
+ }
+ }
+ };
+
+ // Delete link from Auth
+ self.auth
+ .delete_user_link(&link, &self.config.idp_id)
+ .await
+ .map_err(|e| GithubError::Internal(e.into()))?;
+
+ // Delete from repo
+ self.repo
+ .delete_github_link(&link.id)
+ .await
+ .map_err(|e| GithubError::Internal(e.into()))?;
+
+ Ok(())
+ }
+
#[tracing::instrument(skip(self), err)]
async fn link_user(
&self,
diff --git a/rust/cloud-storage/github/src/outbound/github_auth_client.rs b/rust/cloud-storage/github/src/outbound/github_auth_client.rs
index e391a914a9..7f4eb8ec23 100644
--- a/rust/cloud-storage/github/src/outbound/github_auth_client.rs
+++ b/rust/cloud-storage/github/src/outbound/github_auth_client.rs
@@ -3,12 +3,16 @@
use anyhow::Context;
use fusionauth::{
FusionAuthClient,
+ error::FusionAuthClientError,
identity_provider::{IdentityProviderLink, LinkUserRequest},
};
use redis::AsyncCommands;
use redis::aio::MultiplexedConnection;
-use crate::domain::{models::GithubAccessToken, ports::Auth};
+use crate::domain::{
+ models::{GithubAccessToken, GithubLink},
+ ports::Auth,
+};
/// TTL for github tokens: 1 day
const TTL_SECONDS: u64 = 60 * 60 * 24;
@@ -100,4 +104,24 @@ impl Auth for GithubAuthImpl {
Ok(GithubAccessToken::new(link.token.clone()))
}
+
+ #[tracing::instrument(skip(self), err)]
+ async fn delete_user_link(
+ &self,
+ github_link: &GithubLink,
+ github_idp_id: &str,
+ ) -> Result<(), Self::Err> {
+ match self
+ .fusionauth_client
+ .unlink_user(
+ &github_link.fusionauth_user_id.to_string(),
+ github_idp_id,
+ &github_link.github_user_id,
+ )
+ .await
+ {
+ Ok(()) | Err(FusionAuthClientError::NoIdentityProviderFound) => Ok(()),
+ Err(e) => Err(e.into()),
+ }
+ }
}
diff --git a/rust/cloud-storage/github/src/outbound/pg_github_repo.rs b/rust/cloud-storage/github/src/outbound/pg_github_repo.rs
index 1cae05f817..067d11f77b 100644
--- a/rust/cloud-storage/github/src/outbound/pg_github_repo.rs
+++ b/rust/cloud-storage/github/src/outbound/pg_github_repo.rs
@@ -165,4 +165,19 @@ impl GithubRepo for PgGithubRepo {
Ok(())
}
+
+ #[tracing::instrument(skip(self), err)]
+ async fn delete_github_link(&self, link_id: &uuid::Uuid) -> Result<(), Self::Err> {
+ sqlx::query!(
+ r#"
+ DELETE FROM github_links
+ WHERE id = $1
+ "#,
+ link_id
+ )
+ .execute(&self.pool)
+ .await?;
+
+ Ok(())
+ }
}
diff --git a/rust/cloud-storage/github/src/outbound/pg_github_repo/test.rs b/rust/cloud-storage/github/src/outbound/pg_github_repo/test.rs
index 17eaa004a1..a926453ccc 100644
--- a/rust/cloud-storage/github/src/outbound/pg_github_repo/test.rs
+++ b/rust/cloud-storage/github/src/outbound/pg_github_repo/test.rs
@@ -171,6 +171,25 @@ async fn test_delete_in_progress_user_link(pool: Pool<Postgres>) {
assert!(row.is_none());
}
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../../fixtures", scripts("github_test_data"))
+)]
+async fn test_delete_github_link(pool: Pool<Postgres>) {
+ let repo = PgGithubRepo::new(pool.clone());
+
+ let id = uuid::Uuid::parse_str("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa").unwrap();
+
+ repo.delete_github_link(&id).await.unwrap();
+
+ // Verify it was deleted
+ let row = sqlx::query!("SELECT id FROM github_links WHERE id = $1", id)
+ .fetch_optional(&pool)
+ .await
+ .unwrap();
+ assert!(row.is_none());
+}
+
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn test_delete_in_progress_user_link_nonexistent(pool: Pool<Postgres>) {
let repo = PgGithubRepo::new(pool);