Changeset View
Changeset View
Standalone View
Standalone View
swh/graphql/resolvers/snapshot_branch.py
Show All 19 Lines | class BaseSnapshotBranchNode(BaseNode): | ||||
def _get_node_from_data(self, node_data: tuple): | def _get_node_from_data(self, node_data: tuple): | ||||
# node_data is a tuple as returned by _get_paged_result in | # node_data is a tuple as returned by _get_paged_result in | ||||
# SnapshotBranchConnection and _get_node_data in AliasSnapshotBranchNode | # SnapshotBranchConnection and _get_node_data in AliasSnapshotBranchNode | ||||
# overriding to support this special data structure | # overriding to support this special data structure | ||||
branch_name, branch_obj = node_data | branch_name, branch_obj = node_data | ||||
node = { | node = { | ||||
"name": branch_name, | "name": branch_name, | ||||
"type": branch_obj.target_type.value, | "target_type": branch_obj.target_type.value, | ||||
"target_hash": branch_obj.target, | "target_id": branch_obj.target, | ||||
} | } | ||||
return namedtuple("NodeObj", node.keys())(*node.values()) | return namedtuple("NodeObj", node.keys())(*node.values()) | ||||
def is_type_of(self): | def is_type_of(self): | ||||
return "Branch" | return "Branch" | ||||
@property | @property | ||||
def targetType(self): # To support the schema naming convention | def targetType(self): # To support the schema naming convention | ||||
return self._node.type | return self._node.target_type | ||||
def snapshot_swhid(self): | def snapshot_swhid(self): | ||||
""" | """ | ||||
Logic to handle multiple branch alias redirects | Logic to handle multiple branch alias redirects | ||||
Alias redirects can be any level deep. Hence the parent snapshot can be | Alias redirects can be any level deep. Hence the parent snapshot can be | ||||
reached only by a loop | reached only by a loop | ||||
This code expects every BranchNode to have a Snapshot parent in the GraphQL query | This code expects every BranchNode to have a Snapshot parent in the GraphQL query | ||||
Show All 13 Lines | |||||
class AliasSnapshotBranchNode(BaseSnapshotBranchNode): | class AliasSnapshotBranchNode(BaseSnapshotBranchNode): | ||||
obj: BaseSnapshotBranchNode | obj: BaseSnapshotBranchNode | ||||
def _get_node_data(self): | def _get_node_data(self): | ||||
snapshot_swhid = self.snapshot_swhid() | snapshot_swhid = self.snapshot_swhid() | ||||
target_branch = self.obj.target_hash | target_branch = self.obj.target_id | ||||
alias_branch = self.archive.get_snapshot_branches( | alias_branch = self.archive.get_snapshot_branches( | ||||
snapshot_swhid.object_id, first=1, name_include=target_branch | snapshot_swhid.object_id, first=1, name_include=target_branch | ||||
) | ) | ||||
if target_branch not in alias_branch["branches"]: | if target_branch not in alias_branch["branches"]: | ||||
raise ObjectNotFoundError( | raise ObjectNotFoundError( | ||||
f"Branch name with {target_branch.decode()} is not available" | f"Branch name with {target_branch.decode()} is not available" | ||||
) | ) | ||||
▲ Show 20 Lines • Show All 53 Lines • Show Last 20 Lines |