Skip to content

Commit 4009138

Browse files
committed
Use request context from SDK within pipeline
1 parent bdddfc1 commit 4009138

File tree

9 files changed

+43
-4
lines changed

9 files changed

+43
-4
lines changed

backend/infrahub/context.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import Any
22

3+
from infrahub_sdk.context import ContextAccount, RequestContext
34
from pydantic import BaseModel, Field
45
from typing_extensions import Self
56

@@ -47,3 +48,6 @@ def set_event(self, name: str, id: str) -> None:
4748

4849
def to_event(self) -> dict[str, Any]:
4950
return self.model_dump(mode="json")
51+
52+
def to_request_context(self) -> RequestContext:
53+
return RequestContext(account=ContextAccount(id=self.account.account_id))

backend/infrahub/graphql/context.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ async def apply_external_context(graphql_context: GraphqlContext, context_input:
1717
if not context_input or not context_input.account:
1818
return
1919

20+
if graphql_context.active_account_session.account_id == context_input.account.id:
21+
# If the account_id from the request context is the same as the current account
22+
# there's no point moving forward with other checks to override the current
23+
# context we can just continue with what is already there.
24+
return
25+
2026
permission = define_global_permission_from_branch(
2127
permission=GlobalPermissions.OVERRIDE_CONTEXT, branch_name=graphql_context.branch.name
2228
)

backend/infrahub/message_bus/messages/check_generator_run.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from pydantic import Field
22

3+
from infrahub.context import InfrahubContext
34
from infrahub.generators.models import ProposedChangeGeneratorDefinition
45
from infrahub.message_bus import InfrahubMessage
56

@@ -22,3 +23,4 @@ class CheckGeneratorRun(InfrahubMessage):
2223
variables: dict = Field(..., description="Input variables when running the generator")
2324
validator_id: str = Field(..., description="The ID of the validator")
2425
proposed_change: str | None = Field(None, description="The unique ID of the Proposed Change")
26+
context: InfrahubContext = Field(..., description="The Infrahub context")

backend/infrahub/message_bus/operations/check/generator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ async def run(message: messages.CheckGeneratorRun, service: InfrahubServices) ->
7272
convert_query_response=generator_definition.convert_query_response,
7373
infrahub_node=InfrahubNode,
7474
)
75+
generator._init_client.request_context = message.context.to_request_context()
7576
await generator.run(identifier=generator_definition.name)
7677
generator_instance.status.value = GeneratorInstanceStatus.READY.value
7778
except ModuleImportError as exc:

backend/infrahub/message_bus/operations/requests/generator_definition.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ async def check(message: messages.RequestGeneratorDefinitionCheck, service: Infr
8787
log.info(f"Trigger execution of {message.generator_definition.definition_name} for {member.display_label}")
8888
events.append(
8989
messages.CheckGeneratorRun(
90+
context=message.context,
9091
generator_definition=message.generator_definition,
9192
generator_instance=generator_instance,
9293
commit=repository.source_commit,

backend/infrahub/tasks/artifact.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ async def define_artifact(
3939
"content_type": model.content_type,
4040
},
4141
)
42-
await artifact.save()
42+
await artifact.save(request_context=model.context.to_request_context())
4343
created = True
4444
return artifact, created

backend/tests/integration/proposed_change/test_proposed_change_conflict.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
from uuid import uuid4
1010

1111
import pytest
12+
from infrahub_sdk.context import ContextAccount, RequestContext
1213
from infrahub_sdk.exceptions import GraphQLError
1314
from infrahub_sdk.protocols import CoreDataCheck, CoreProposedChange
1415

1516
from infrahub.core.constants import InfrahubKind, ValidatorConclusion
16-
from infrahub.core.initialization import create_branch
17+
from infrahub.core.initialization import create_account, create_branch
1718
from infrahub.core.manager import NodeManager
1819
from infrahub.core.merge import BranchMerger
1920
from infrahub.core.node import Node
@@ -203,11 +204,14 @@ async def test_conflict_pipeline(
203204
assert john.description.value == "Oh boy" # type: ignore[attr-defined]
204205

205206
async def test_happy_pipeline(self, db: InfrahubDatabase, happy_data_branch: str, client: InfrahubClient) -> None:
207+
proposed_change_user = await create_account(db=db, name="jimmy-change-user", password="Password123")
206208
proposed_change_create = await client.create(
207209
kind=CoreProposedChange,
208210
data={"source_branch": happy_data_branch, "destination_branch": "main", "name": "happy-test"},
209211
)
210-
await proposed_change_create.save()
212+
await proposed_change_create.save(
213+
request_context=RequestContext(account=ContextAccount(id=proposed_change_user.id))
214+
)
211215

212216
# -------------------------------------------------
213217
# Ensure that all validators have been executed and aren't reporting errors
@@ -292,6 +296,7 @@ async def test_happy_pipeline(self, db: InfrahubDatabase, happy_data_branch: str
292296
assert artifact_events["InfrahubEvent"]["count"] > 0
293297
latest_artifact_event = artifact_events["InfrahubEvent"]["edges"][0]["node"]
294298
assert sorted(latest_artifact_event.keys()) == [
299+
"account_id",
295300
"artifact_definition_id",
296301
"branch",
297302
"checksum",
@@ -348,6 +353,14 @@ async def test_happy_pipeline(self, db: InfrahubDatabase, happy_data_branch: str
348353
"CoreUserValidator",
349354
]
350355

356+
pr_account_events = await client.execute_graphql(
357+
query=QUERY_EVENT,
358+
variables={"account__ids": [proposed_change_user.id]},
359+
)
360+
pr_account_events_types = {event["node"]["event"] for event in pr_account_events["InfrahubEvent"]["edges"]}
361+
assert "infrahub.validator.passed" in pr_account_events_types
362+
assert "infrahub.artifact.updated" in pr_account_events_types
363+
351364
async def test_merge_failure(
352365
self,
353366
db: InfrahubDatabase,
@@ -389,6 +402,7 @@ async def test_connectivity(self, db: InfrahubDatabase, initial_dataset: str, cl
389402
$branch: [String!],
390403
$parent__ids: [String!],
391404
$event_type: [String!]
405+
$account__ids: [String!],
392406
$related_node__ids: [String!],
393407
$event_type_filter: EventTypeFilter
394408
) {
@@ -397,6 +411,7 @@ async def test_connectivity(self, db: InfrahubDatabase, initial_dataset: str, cl
397411
parent__ids: $parent__ids
398412
event_type: $event_type
399413
event_type_filter: $event_type_filter
414+
account__ids: $account__ids
400415
related_node__ids: $related_node__ids
401416
) {
402417
count
@@ -409,6 +424,7 @@ async def test_connectivity(self, db: InfrahubDatabase, initial_dataset: str, cl
409424
parent_id
410425
level
411426
occurred_at
427+
account_id
412428
primary_node {
413429
id
414430
kind
@@ -425,6 +441,14 @@ async def test_connectivity(self, db: InfrahubDatabase, initial_dataset: str, cl
425441
checksum_previous
426442
checksum
427443
}
444+
... on NodeMutatedEvent {
445+
id
446+
attributes {
447+
name
448+
value
449+
value_previous
450+
}
451+
}
428452
}
429453
}
430454
}

docs/docs/reference/message-bus-events.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ For more detailed explanations on how these events are used within Infrahub, see
4343
| **variables** | Input variables when running the generator | object | None |
4444
| **validator_id** | The ID of the validator | string | None |
4545
| **proposed_change** | The unique ID of the Proposed Change | N/A | None |
46+
| **context** | The Infrahub context | N/A | None |
4647
<!-- vale on -->
4748

4849
<!-- vale off -->

0 commit comments

Comments
 (0)