Skip to content

Commit

Permalink
Add wait_until_completion
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasG0 committed Nov 13, 2024
1 parent 0bddb1e commit ab2f144
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
2 changes: 1 addition & 1 deletion backend/infrahub/git/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ async def create_branch_in_graph(self, branch_name: str) -> BranchData:
"""

# TODO need to handle the exception properly
branch = await self.sdk.branch.create(branch_name=branch_name, background_execution=True)
branch = await self.sdk.branch.create(branch_name=branch_name)

log.debug(f"Branch {branch_name} created in the Graph", repository=self.name, branch=branch_name)
return branch
Expand Down
23 changes: 16 additions & 7 deletions backend/infrahub/graphql/mutations/branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ class BranchCreateInput(InputObjectType):
class BranchCreate(Mutation):
class Arguments:
data = BranchCreateInput(required=True)
# TODO: We might want to change `background_execution` to `wait_until_completion` to keep consistent
# with other mutations. It would be a breaking change though.
background_execution = Boolean(required=False)
background_execution = Boolean(required=False, deprecation_reason="Please use `wait_until_completion` instead")
wait_until_completion = Boolean(required=False)

ok = Boolean()
object = Field(BranchType)
Expand All @@ -58,21 +57,31 @@ class Arguments:
@retry_db_transaction(name="branch_create")
@trace.get_tracer(__name__).start_as_current_span("branch_create")
async def mutate(
cls, root: dict, info: GraphQLResolveInfo, data: BranchCreateInput, background_execution: bool = False
cls,
root: dict,
info: GraphQLResolveInfo,
data: BranchCreateInput,
background_execution: bool = False,
wait_until_completion: bool = True,
) -> Self:
context: GraphqlContext = info.context
task: dict | None = None

model = BranchCreateModel(**data)

if background_execution or not wait_until_completion:
workflow = await context.active_service.workflow.submit_workflow(
workflow=BRANCH_CREATE, parameters={"model": model}
)
task = {"id": workflow.id}
return cls(ok=True, task=task)

await context.active_service.workflow.execute_workflow(workflow=BRANCH_CREATE, parameters={"model": model})

# Retrieve created branch
obj = await Branch.get_by_name(db=context.db, name=model.name)
ok = True
fields = await extract_fields(info.field_nodes[0].selection_set)

return cls(object=await obj.to_graphql(fields=fields.get("object", {})), ok=ok, task=task)
return cls(object=await obj.to_graphql(fields=fields.get("object", {})), ok=True, task=task)


class BranchNameInput(InputObjectType):
Expand Down
22 changes: 22 additions & 0 deletions backend/tests/functional/branch/test_graphql_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,25 @@ async def test_branch_create(self, initial_dataset: str, client: InfrahubClient)
assert result["BranchCreate"]["ok"] is True
assert result["BranchCreate"]["object"]["id"] is not None
assert result["BranchCreate"]["task"] is None

async def test_branch_create_async(self, initial_dataset: str, client: InfrahubClient) -> None:
query = Mutation(
mutation="BranchCreate",
input_data={"data": {"name": "branch-3"}, "wait_until_completion": False},
query={"ok": None, "task": {"id": None}, "object": {"id": None}},
)
result = await client.execute_graphql(query=query.render())
assert result["BranchCreate"]["ok"] is True
assert result["BranchCreate"]["object"]["id"] is not None
assert result["BranchCreate"]["task"] is not None

async def test_branch_create_async_deprecated(self, initial_dataset: str, client: InfrahubClient) -> None:
query = Mutation(
mutation="BranchCreate",
input_data={"data": {"name": "branch-4"}, "background_execution": True},
query={"ok": None, "task": {"id": None}, "object": {"id": None}},
)
result = await client.execute_graphql(query=query.render())
assert result["BranchCreate"]["ok"] is True
assert result["BranchCreate"]["object"]["id"] is not None
assert result["BranchCreate"]["task"] is not None

0 comments on commit ab2f144

Please sign in to comment.