refactor(trigger): update trigger error handling and response types

- Renamed `WebhookValidationError` to `TriggerValidationError` for consistency in error handling across trigger operations.
- Updated references to the new `TriggerValidationError` in the GitHub provider and trigger interfaces.
- Enhanced the `TriggerProvider` class to raise the new validation error for webhook signature issues.
- Adjusted the `IssueCommentTrigger` to return the correct event response type.
- Improved type hints and imports in various modules for better clarity and organization.
This commit is contained in:
Harry
2025-09-03 14:04:33 +08:00
parent cae5c910be
commit 1fa18bfd1c
9 changed files with 49 additions and 48 deletions
@@ -6,13 +6,13 @@ import urllib.parse
from collections.abc import Mapping
from typing import Any
from dify_plugin.entities import I18nObject, ParameterOption
import requests
from werkzeug import Request, Response
from dify_plugin.entities import I18nObject, ParameterOption
from dify_plugin.entities.trigger import Subscription, TriggerDispatch, Unsubscription
from dify_plugin.errors.tool import ToolProviderCredentialValidationError, ToolProviderOAuthError
from dify_plugin.errors.trigger import SubscriptionError, TriggerDispatchError, WebhookValidationError
from dify_plugin.errors.trigger import SubscriptionError, TriggerDispatchError, TriggerValidationError
from dify_plugin.interfaces.trigger import TriggerProvider
@@ -77,7 +77,7 @@ class GithubProvider(TriggerProvider):
if webhook_secret:
signature = request.headers.get("X-Hub-Signature-256")
if not signature:
raise WebhookValidationError("Missing webhook signature")
raise TriggerValidationError("Missing webhook signature")
# Verify the signature
expected_signature = (
@@ -85,7 +85,7 @@ class GithubProvider(TriggerProvider):
)
if not hmac.compare_digest(signature, expected_signature):
raise WebhookValidationError("Invalid webhook signature")
raise TriggerValidationError("Invalid webhook signature")
event_type = request.headers.get("X-GitHub-Event")
if not event_type:
@@ -96,7 +96,7 @@ class GithubProvider(TriggerProvider):
if not payload:
raise TriggerDispatchError("Empty request body")
except Exception as e:
raise TriggerDispatchError(f"Failed to parse JSON payload: {e}")
raise TriggerDispatchError(f"Failed to parse JSON payload: {e}") from e
response = Response(response='{"status": "ok"}', status=200, mimetype="application/json")
@@ -134,7 +134,7 @@ class GithubProvider(TriggerProvider):
try:
owner, repo = repository.split("/")
except ValueError:
raise ValueError("repository must be in format 'owner/repo'")
raise ValueError("repository must be in format 'owner/repo'") from None
# Create webhook using GitHub API
url = f"https://api.github.com/repos/{owner}/{repo}/hooks"
@@ -179,7 +179,7 @@ class GithubProvider(TriggerProvider):
external_response=response.json(),
)
except requests.RequestException as e:
raise SubscriptionError(f"Network error while creating webhook: {e}", error_code="NETWORK_ERROR")
raise SubscriptionError(f"Network error while creating webhook: {e}", error_code="NETWORK_ERROR") from e
def _unsubscribe(self, endpoint: str, subscription: Subscription, credentials: Mapping[str, Any]) -> Unsubscription:
"""
@@ -33,9 +33,9 @@ class IssueCommentTrigger(TriggerEvent):
# Apply action filter if specified
action_filter = parameters.get("action_filter", "any")
if action_filter != "any" and action != action_filter:
if action_filter not in ("any", action):
# Skip this event if it doesn't match the filter
return TriggerEventResponse(variables={})
return Event(variables={})
# Extract issue comment information
comment = payload.get("comment", {})
@@ -49,7 +49,7 @@ class IssueCommentTrigger(TriggerEvent):
issue_number = issue.get("number")
if issue_number != int(issue_filter):
# Skip this event if it doesn't match the issue filter
return TriggerEventResponse(variables={})
return Event(variables={})
# Check if this is a pull request
is_pull_request = "pull_request" in issue
@@ -119,4 +119,4 @@ class IssueCommentTrigger(TriggerEvent):
},
}
return TriggerEventResponse(variables=variables)
return Event(variables=variables)