Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

feat(workspace): read Azure Service Bus queues and topics as topic contracts (#4520) (#4637)

Dipak Chaudhari0e8c084dbc27
diff --git a/packages/core/src/repowise/core/workspace/extractors/topic/__init__.py b/packages/core/src/repowise/core/workspace/extractors/topic/__init__.py
index 81bac93a8..68d356d97 100644
--- a/packages/core/src/repowise/core/workspace/extractors/topic/__init__.py
+++ b/packages/core/src/repowise/core/workspace/extractors/topic/__init__.py
@@ -12,6 +12,7 @@ from repowise.core.workspace.contracts import TOPIC_NAMES_UNRESOLVED_STAT
from ..dialect import ContractDialect, DialectExtractor
from .aws import SNS, SQS
+from .azure_servicebus import AZURE_SERVICE_BUS
from .bullmq import BULLMQ
from .csharp_events import CSHARP_EVENTS
from .dialect import topic_identity
@@ -34,6 +35,7 @@ DIALECTS: tuple[ContractDialect, ...] = (
BULLMQ,
SQS,
SNS,
+ AZURE_SERVICE_BUS,
NESTJS,
LARAVEL_QUEUES,
CSHARP_EVENTS,
diff --git a/packages/core/src/repowise/core/workspace/extractors/topic/azure_servicebus.py b/packages/core/src/repowise/core/workspace/extractors/topic/azure_servicebus.py
new file mode 100644
index 000000000..d0d4aaf2e
--- /dev/null
+++ b/packages/core/src/repowise/core/workspace/extractors/topic/azure_servicebus.py
@@ -0,0 +1,98 @@
+"""Azure Service Bus queues and topics: the .NET ``Azure.Messaging.ServiceBus`` client and Functions triggers.
+
+A ``ServiceBusClient`` sends with ``CreateSender("orders")`` and receives with
+``CreateProcessor``, ``CreateReceiver`` or ``CreateSessionProcessor`` (or their
+``CreateSessionReceiver`` / ``AcceptSessionAsync`` forms); an Azure Function
+receives with ``[ServiceBusTrigger("orders")]``. The first argument (or a
+``queueName`` / ``topicName`` keyword) names the entity.
+
+A receive that also names a subscription (a second argument that is a string
+literal or a name mentioning a subscription) reads a topic; any other call
+reads a queue. A sender names only the entity, so it is a queue. The broker
+matches by name, so a topic's sender and its subscription's processor still
+meet. The client calls are read only in a file importing the library; the
+trigger names the attribute itself.
+"""
+
+from __future__ import annotations
+
+import re
+
+from repowise.core.workspace.contracts import TOPIC_KIND_QUEUE, TOPIC_KIND_TOPIC
+from repowise.core.workspace.origins import ContractOrigin
+
+from ..langs import CSHARP
+from ..strings import Arg
+from .dialect import TopicCall, TopicDialect
+
+_ENTITY = Arg(keys=("queueName", "topicName"), pos=0)
+_CLIENT = ("Azure.Messaging.ServiceBus",)
+_TRIGGER = ("ServiceBusTrigger",)
+
+#: A second argument naming a subscription: a string literal, or a name that says so.
+_SUBSCRIPTION = (
+ r'(?=\s*[^,()]+,\s*(?:(?:"[^"]*"|@"[^"]*"|\w*[Ss]ubscription\w*)\s*[,)]|subscriptionName\s*:))'
+)
+_NO_SUBSCRIPTION = _SUBSCRIPTION.replace("(?=", "(?!", 1)
+
+_RECEIVERS = r"Create(?:Session)?(?:Processor|Receiver)|AcceptSessionAsync"
+
+
+def _call(
+ head: str,
+ role: str,
+ label: str,
+ kind: str,
+ requires: tuple[str, ...],
+ origin: ContractOrigin = ContractOrigin.CLIENT_CALL,
+) -> TopicCall:
+ return TopicCall(
+ head=re.compile(head),
+ role=role,
+ label=label,
+ extensions=CSHARP,
+ name=_ENTITY,
+ kind=kind,
+ origin=origin,
+ requires=requires,
+ )
+
+
+AZURE_SERVICE_BUS = TopicDialect(
+ name="azure_servicebus",
+ calls=(
+ _call(r"\.\s*CreateSender\s*\(", "provider", "CreateSender", TOPIC_KIND_QUEUE, _CLIENT),
+ _call(
+ rf"\.\s*(?:{_RECEIVERS})\s*\({_SUBSCRIPTION}",
+ "consumer",
+ "CreateProcessor",
+ TOPIC_KIND_TOPIC,
+ _CLIENT,
+ ),
+ _call(
+ rf"\.\s*(?:{_RECEIVERS})\s*\({_NO_SUBSCRIPTION}",
+ "consumer",
+ "CreateProcessor",
+ TOPIC_KIND_QUEUE,
+ _CLIENT,
+ ),
+ _call(
+ rf"\bServiceBusTrigger(?:Attribute)?\s*\({_SUBSCRIPTION}",
+ "consumer",
+ "ServiceBusTrigger",
+ TOPIC_KIND_TOPIC,
+ _TRIGGER,
+ ContractOrigin.ROUTE_REGISTRATION,
+ ),
+ _call(
+ rf"\bServiceBusTrigger(?:Attribute)?\s*\({_NO_SUBSCRIPTION}",
+ "consumer",
+ "ServiceBusTrigger",
+ TOPIC_KIND_QUEUE,
+ _TRIGGER,
+ ContractOrigin.ROUTE_REGISTRATION,
+ ),
+ ),
+)
+
+__all__ = ["AZURE_SERVICE_BUS"]
diff --git a/tests/unit/workspace/test_topic_dialects.py b/tests/unit/workspace/test_topic_dialects.py
index 3e9c80d02..c8c3eddd2 100644
--- a/tests/unit/workspace/test_topic_dialects.py
+++ b/tests/unit/workspace/test_topic_dialects.py
@@ -787,3 +787,61 @@ class TestGoKafka:
'r := kgo.Record{Topic: "c"}\n'
)
assert _extract(tmp_path, "main.go", src) == []
+
+
+_SB = "using Azure.Messaging.ServiceBus;\n"
+
+
+class TestAzureServiceBus:
+ def test_a_sender_and_a_processor_meet_on_the_queue(self, tmp_path: Path) -> None:
+ sender = _SB + 'var sender = client.CreateSender("orders");\nawait sender.SendMessageAsync(m);\n'
+ processor = (
+ _SB
+ + 'var processor = client.CreateProcessor("orders", new ServiceBusProcessorOptions());\n'
+ + 'var receiver = client.CreateReceiver("refunds");\n'
+ )
+ _extract(tmp_path, "sender/OrderPublisher.cs", sender)
+ rows = _extract(tmp_path, "billing/OrderWorker.cs", processor)
+ assert {(c.role, c.contract_id, c.meta["broker"], c.meta["kind"]) for c in rows} == {
+ ("provider", "topic::orders", "azure_servicebus", "queue"),
+ ("consumer", "topic::orders", "azure_servicebus", "queue"),
+ ("consumer", "topic::refunds", "azure_servicebus", "queue"),
+ }
+ assert {c.origin for c in rows} == {ContractOrigin.CLIENT_CALL}
+
+ @pytest.mark.parametrize(
+ "call",
+ [
+ 'client.CreateProcessor("orders", "billing")',
+ 'client.CreateProcessor("orders", "billing", new ServiceBusProcessorOptions())',
+ 'client.CreateReceiver("orders", billingSubscription)',
+ 'client.CreateSessionProcessor("orders", SubscriptionName, options)',
+ 'client.CreateReceiver(topicName: "orders", subscriptionName: "billing")',
+ ],
+ )
+ def test_a_receive_naming_a_subscription_reads_a_topic(self, tmp_path: Path, call: str) -> None:
+ c = _one(_extract(tmp_path, "Worker.cs", _SB + f"var r = {call};\n"), "consumer")
+ assert (c.contract_id, c.meta["kind"]) == ("topic::orders", "topic")
+
+ def test_functions_triggers_are_route_registrations(self, tmp_path: Path) -> None:
+ src = (
+ "public static class RefundFunction {\n"
+ ' [Function("Refund")]\n'
+ ' public static void Run([ServiceBusTrigger("refunds", Connection = "SB")] string msg) { }\n'
+ ' [FunctionName("Audit")]\n'
+ ' public static void Audit([ServiceBusTrigger("orders", "audit")] string msg) { }\n'
+ "}\n"
+ )
+ rows = _extract(tmp_path, "RefundFunction.cs", src)
+ assert {(c.role, c.contract_id, c.meta["kind"], c.origin) for c in rows} == {
+ ("consumer", "topic::refunds", "queue", ContractOrigin.ROUTE_REGISTRATION),
+ ("consumer", "topic::orders", "topic", ContractOrigin.ROUTE_REGISTRATION),
+ }
+
+ def test_a_commented_out_call_is_not_read(self, tmp_path: Path) -> None:
+ src = _SB + '// var sender = client.CreateSender("old-orders");\n/* client.CreateProcessor("x"); */\n'
+ assert _extract(tmp_path, "Old.cs", src) == []
+
+ def test_client_calls_need_the_library_import(self, tmp_path: Path) -> None:
+ src = 'var s = factory.CreateSender("orders");\nvar p = pool.CreateProcessor("jobs");\n'
+ assert _extract(tmp_path, "Other.cs", src) == []