From ce4cc54cc9af434dd925c2b1ce1669df0dc237d1 Mon Sep 17 00:00:00 2001 From: Harry Date: Wed, 25 Jun 2025 14:51:55 +0800 Subject: [PATCH] feat(oauth): merge tool oauth and remove sequence number branches --- .../console/workspace/tool_providers.py | 10 ++------ api/core/tools/tool_manager.py | 2 +- .../python/examples/github/provider/github.py | 2 +- ...c_merge_tool_oauth_and_remove_sequence_.py | 25 +++++++++++++++++++ .../tools/builtin_tools_manage_service.py | 5 ++-- 5 files changed, 32 insertions(+), 12 deletions(-) create mode 100644 api/migrations/versions/2025_06_25_1101-46d46b3f389c_merge_tool_oauth_and_remove_sequence_.py diff --git a/api/controllers/console/workspace/tool_providers.py b/api/controllers/console/workspace/tool_providers.py index a4839fe8a1..c581a39200 100644 --- a/api/controllers/console/workspace/tool_providers.py +++ b/api/controllers/console/workspace/tool_providers.py @@ -676,14 +676,9 @@ class ToolPluginOAuthApi(Resource): if not user.is_admin_or_owner: raise Forbidden() - # check if user client is configured and enabled then using user client - # if user client is not configured then using system client tenant_id = user.current_tenant_id - user_id = user.id - - plugin_oauth_config = BuiltinToolManageService.get_builtin_tool_provider( + plugin_oauth_config = BuiltinToolManageService.get_builtin_tool_oauth_client( tenant_id=tenant_id, - user_id=user_id, provider=provider, plugin_id=plugin_id, ) @@ -727,9 +722,8 @@ class ToolOAuthCallback(Resource): context.get("provider"), ) oauth_handler = OAuthHandler() - plugin_oauth_config = BuiltinToolManageService.get_builtin_tool_provider( + plugin_oauth_config = BuiltinToolManageService.get_builtin_tool_oauth_client( tenant_id=tenant_id, - user_id=user_id, provider=provider, plugin_id=plugin_id, ) diff --git a/api/core/tools/tool_manager.py b/api/core/tools/tool_manager.py index f25267dbf6..86ffa01667 100644 --- a/api/core/tools/tool_manager.py +++ b/api/core/tools/tool_manager.py @@ -579,7 +579,7 @@ class ToolManager: if "builtin" in filters: def get_builtin_providers(tenant_id): - # according to multi credentials, select the one with is_default=True first, then created_at oldest + # according to multi credentials, select the one with is_default=True first, then created_at oldest # for compatibility with old version sql = """ SELECT DISTINCT ON (tenant_id, provider) id diff --git a/api/dify-plugin-sdks/python/examples/github/provider/github.py b/api/dify-plugin-sdks/python/examples/github/provider/github.py index 36f2f85910..7fb7bd33df 100644 --- a/api/dify-plugin-sdks/python/examples/github/provider/github.py +++ b/api/dify-plugin-sdks/python/examples/github/provider/github.py @@ -64,4 +64,4 @@ class GithubProvider(ToolProvider): if response.status_code != 200: raise ToolProviderCredentialValidationError(response.json().get("message")) except Exception as e: - raise ToolProviderCredentialValidationError(str(e)) \ No newline at end of file + raise ToolProviderCredentialValidationError(str(e)) diff --git a/api/migrations/versions/2025_06_25_1101-46d46b3f389c_merge_tool_oauth_and_remove_sequence_.py b/api/migrations/versions/2025_06_25_1101-46d46b3f389c_merge_tool_oauth_and_remove_sequence_.py new file mode 100644 index 0000000000..a3c51e7e75 --- /dev/null +++ b/api/migrations/versions/2025_06_25_1101-46d46b3f389c_merge_tool_oauth_and_remove_sequence_.py @@ -0,0 +1,25 @@ +"""merge tool oauth and remove sequence number branches + +Revision ID: 46d46b3f389c +Revises: 0ab65e1cc7fa, 71f5020c6470 +Create Date: 2025-06-25 11:01:55.215896 + +""" +from alembic import op +import models as models +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '46d46b3f389c' +down_revision = ('0ab65e1cc7fa', '71f5020c6470') +branch_labels = None +depends_on = None + + +def upgrade(): + pass + + +def downgrade(): + pass diff --git a/api/services/tools/builtin_tools_manage_service.py b/api/services/tools/builtin_tools_manage_service.py index 6728a19391..b4f043c647 100644 --- a/api/services/tools/builtin_tools_manage_service.py +++ b/api/services/tools/builtin_tools_manage_service.py @@ -299,7 +299,7 @@ class BuiltinToolManageService: db.session.delete(provider_obj) db.session.commit() - + # delete cache provider_controller = ToolManager.get_builtin_provider(provider_name, tenant_id) tool_configuration = ProviderConfigEncrypter( @@ -334,7 +334,7 @@ class BuiltinToolManageService: return {"result": "success"} @staticmethod - def get_builtin_tool_provider(tenant_id: str, user_id: str, provider: str, plugin_id: str): + def get_builtin_tool_oauth_client(tenant_id: str, provider: str, plugin_id: str): """ get builtin tool provider """ @@ -450,6 +450,7 @@ class BuiltinToolManageService: 1.if the default provider exists, return the default provider 2.if the default provider does not exist, return the oldest provider """ + def _query(provider_filters: list[ColumnExpressionArgument[bool]]): return ( db.session.query(BuiltinToolProvider)