Repository navigation
feat(workspace): read AWS EventBridge events as topic contracts (#4521) #4639
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
ANO3457
wants to merge
2
commits into
repowise-dev:main
Choose a base branch
from
ANO3457:feat/workspace-eventbridge-topics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| """AWS EventBridge topic dialect: PutEvents producers and rule pattern consumers.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from pathlib import Path | ||
|
|
||
| from repowise.core.workspace.extractors.topic import TopicExtractor | ||
| from repowise.core.workspace.matching import match_contracts | ||
| from repowise.core.workspace.origins import ContractOrigin | ||
|
|
||
|
|
||
| def _extract(root: Path, rel: str, content: str, alias: str = "svc"): | ||
| path = root / rel | ||
| path.parent.mkdir(parents=True, exist_ok=True) | ||
| path.write_text(content, encoding="utf-8") | ||
| return TopicExtractor().extract(root, alias) | ||
|
|
||
|
|
||
| def _ids(rows, role: str) -> list[str]: | ||
| return sorted(c.contract_id for c in rows if c.role == role) | ||
|
|
||
|
|
||
| def test_eventbridge_put_events_command_ts(tmp_path: Path) -> None: | ||
| src = ( | ||
| 'import { EventBridgeClient, PutEventsCommand } from "@aws-sdk/client-eventbridge";\n' | ||
| "const client = new EventBridgeClient({});\n" | ||
| "await client.send(new PutEventsCommand({\n" | ||
| " Entries: [\n" | ||
| " {\n" | ||
| ' Source: "com.shop.orders",\n' | ||
| ' DetailType: "OrderCreated",\n' | ||
| ' Detail: JSON.stringify({ id: "1" }),\n' | ||
| " }\n" | ||
| " ]\n" | ||
| "}));\n" | ||
| ) | ||
| [c] = _extract(tmp_path, "publish.ts", src) | ||
| assert (c.contract_id, c.role) == ("topic::com.shop.orders/ordercreated", "provider") | ||
| assert c.meta == {"topic": "com.shop.orders/OrderCreated", "broker": "eventbridge", "kind": "topic"} | ||
| assert c.origin == ContractOrigin.CLIENT_CALL | ||
|
|
||
|
|
||
| def test_eventbridge_put_events_python(tmp_path: Path) -> None: | ||
| src = ( | ||
| "import boto3\n" | ||
| "client = boto3.client('events')\n" | ||
| "client.put_events(\n" | ||
| " Entries=[\n" | ||
| " {\n" | ||
| " 'Source': 'com.shop.orders',\n" | ||
| " 'DetailType': 'OrderCreated',\n" | ||
| " 'Detail': '{}',\n" | ||
| " }\n" | ||
| " ]\n" | ||
| ")\n" | ||
| ) | ||
| [c] = _extract(tmp_path, "publish.py", src) | ||
| assert (c.contract_id, c.role) == ("topic::com.shop.orders/ordercreated", "provider") | ||
| assert c.origin == ContractOrigin.CLIENT_CALL | ||
|
|
||
|
|
||
| def test_eventbridge_rule_cdk(tmp_path: Path) -> None: | ||
| src = ( | ||
| "import { Rule } from 'aws-cdk-lib/aws-events';\n" | ||
| "new Rule(this, 'Rule', {\n" | ||
| " eventPattern: {\n" | ||
| " source: ['com.shop.orders'],\n" | ||
| " detailType: ['OrderCreated'],\n" | ||
| " },\n" | ||
| "});\n" | ||
| ) | ||
| [c] = _extract(tmp_path, "rule.ts", src) | ||
| assert (c.contract_id, c.role) == ("topic::com.shop.orders/ordercreated", "consumer") | ||
| assert c.origin == ContractOrigin.ROUTE_REGISTRATION | ||
|
|
||
|
|
||
| def test_eventbridge_sam_yaml(tmp_path: Path) -> None: | ||
| src = ( | ||
| "OrderCreatedRule:\n" | ||
| " Type: EventBridgeRule\n" | ||
| " Properties:\n" | ||
| " Pattern:\n" | ||
| " source:\n" | ||
| " - com.shop.orders\n" | ||
| " detail-type:\n" | ||
| " - OrderCreated\n" | ||
| ) | ||
| [c] = _extract(tmp_path, "template.yaml", src) | ||
| assert (c.contract_id, c.role) == ("topic::com.shop.orders/ordercreated", "consumer") | ||
| assert c.origin == ContractOrigin.ROUTE_REGISTRATION | ||
|
|
||
|
|
||
| def test_eventbridge_links_across_repos(tmp_path: Path) -> None: | ||
| pub = _extract( | ||
| tmp_path / "pub", | ||
| "send.py", | ||
| "import boto3\n" | ||
| "boto3.client('events').put_events(Entries=[{'Source': 'com.shop.orders', 'DetailType': 'OrderCreated'}])\n", | ||
| "pub", | ||
| ) | ||
| sub = _extract( | ||
| tmp_path / "sub", | ||
| "rule.ts", | ||
| "new Rule(this, 'R', { eventPattern: { source: ['com.shop.orders'], detailType: ['OrderCreated'] } });\n", | ||
| "sub", | ||
| ) | ||
| links = [lk for lk in match_contracts(pub + sub) if lk.contract_type == "topic"] | ||
| assert [(lk.provider_repo, lk.consumer_repo, lk.contract_id) for lk in links] == [ | ||
| ("pub", "sub", "topic::com.shop.orders/ordercreated") | ||
| ] | ||
|
|
||
|
|
||
| def test_eventbridge_isolated_from_sqs_and_sns(tmp_path: Path) -> None: | ||
| eb = _extract( | ||
| tmp_path / "eb", | ||
| "send.py", | ||
| "boto3.client('events').put_events(Entries=[{'Source': 'orders', 'DetailType': 'created'}])\n", | ||
| "eb", | ||
| ) | ||
| sns = _extract( | ||
| tmp_path / "sns", | ||
| "pub.py", | ||
| "boto3.client('sns').publish(TopicArn='arn:aws:sns:eu-west-1:123:orders/created')\n", | ||
| "sns", | ||
| ) | ||
| assert [lk for lk in match_contracts(eb + sns) if lk.contract_type == "topic"] == [] | ||
|
|
||
|
|
||
| def test_commented_out_eventbridge_call_is_skipped(tmp_path: Path) -> None: | ||
| src = ( | ||
| "// client.putEvents({ Entries: [{ Source: 'com.shop.orders', DetailType: 'OrderCreated' }] });\n" | ||
| ) | ||
| assert _extract(tmp_path, "c.ts", src) == [] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P1 · SAM templates keep only one rule
The YAML branch runs
searchonce over the whole file. A SAM or CloudFormation template with severalEventBridgeRuleresources therefore yields one consumer, built from the first rule'ssourceanddetail-type, at offset 0. Every other rule in that template is dropped from the topic graph with no warning, and its link to the publisher is lost. Iterate over eachEventBridgeRuleresource block withfinditer, as the CDK branch already does, and emit one consumer per block.Useful? React with 👍 or 👎. Details
Prompt for your agent