Repository: langgenius/dify
File: api/services/trigger/trigger_provider_service.py (Python)

Code before:
        """
        Create a subscription builder for rebuilding an existing subscription.

        This method creates a builder pre-filled with data from the rebuild request,
        keeping the same subscription_id and endpoint_id so the webhook URL remains unchanged.

        :param tenant_id: Tenant ID
        :param name: Name for the subscription
        :param subscription_id: Subscription ID
        :param provider_id: Provider identifier
        :param credentials: Credentials for the subscription
        :param parameters: Parameters for the subscription
        :return: SubscriptionBuilderApiEntity
        """
        provider_controller = TriggerManager.get_trigger_provider(tenant_id, provider_id)
        if not provider_controller:
            raise ValueError(f"Provider {provider_id} not found")

        subscription = TriggerProviderService.get_subscription_by_id(
            tenant_id=tenant_id,
            subscription_id=subscription_id,
        )
        if not subscription:
            raise ValueError(f"Subscription {subscription_id} not found")

        credential_type = CredentialType.of(subscription.credential_type)
        if credential_type not in [CredentialType.OAUTH2, CredentialType.API_KEY]:
            raise ValueError(f"Credential type {credential_type} not supported for auto creation")

        # Delete the previous subscription
        user_id = subscription.user_id
        unsubscribe_result = TriggerManager.unsubscribe_trigger(
            tenant_id=tenant_id,
            user_id=user_id,
            provider_id=provider_id,
            subscription=subscription.to_entity(),
            credentials=subscription.credentials,
            credential_type=credential_type,
        )
        if not unsubscribe_result.success:
            raise ValueError(f"Failed to delete previous subscription: {unsubscribe_result.message}")

        # Create a new subscription with the same subscription_id and endpoint_id
        new_credentials: dict[str, Any] = {
            key: value if value != HIDDEN_VALUE else subscription.credentials.get(key, UNKNOWN_VALUE)
            for key, value in credentials.items()
        }
        new_subscription: TriggerSubscriptionEntity = TriggerManager.subscribe_trigger(
            tenant_id=tenant_id,
            user_id=user_id,
            provider_id=provider_id,

Review comment (security):
There is no need to use `CredentialType.of` as `CredentialType` is a kind of `StrEnum`

Code after:
        """
        Create a subscription builder for rebuilding an existing subscription.

        This method rebuild the subscription by call DELETE and CREATE API of the third party provider(e.g. GitHub)
        keeping the same subscription_id and endpoint_id so the webhook URL remains unchanged.

        :param tenant_id: Tenant ID
        :param name: Name for the subscription
        :param subscription_id: Subscription ID
        :param provider_id: Provider identifier
        :param credentials: Credentials for the subscription
        :param parameters: Parameters for the subscription
        :return: SubscriptionBuilderApiEntity
        """
        provider_controller = TriggerManager.get_trigger_provider(tenant_id, provider_id)
        if not provider_controller:
            raise ValueError(f"Provider {provider_id} not found")

        subscription = TriggerProviderService.get_subscription_by_id(
            tenant_id=tenant_id,
            subscription_id=subscription_id,
        )
        if not subscription:
            raise ValueError(f"Subscription {subscription_id} not found")

        credential_type = CredentialType.of(subscription.credential_type)
        if credential_type not in {CredentialType.OAUTH2, CredentialType.API_KEY}:
            raise ValueError(f"Credential type {credential_type} not supported for auto creation")

        # Delete the previous subscription
        user_id = subscription.user_id
        unsubscribe_result = TriggerManager.unsubscribe_trigger(
            tenant_id=tenant_id,
            user_id=user_id,
            provider_id=provider_id,
            subscription=subscription.to_entity(),
            credentials=subscription.credentials,
            credential_type=credential_type,
        )
        if not unsubscribe_result.success:
            raise ValueError(f"Failed to delete previous subscription: {unsubscribe_result.message}")

        # Create a new subscription with the same subscription_id and endpoint_id
        new_credentials: dict[str, Any] = {
            key: value if value != HIDDEN_VALUE else subscription.credentials.get(key, UNKNOWN_VALUE)
            for key, value in credentials.items()
        }
        new_subscription: TriggerSubscriptionEntity = TriggerManager.subscribe_trigger(
            tenant_id=tenant_id,
            user_id=user_id,
            provider_id=provider_id,