Skip to content

Commit

Permalink
Fix namespace sync with missing avatar_sha256
Browse files Browse the repository at this point in the history
In case upstream does not provide an avatar digest, we just need to
mirror that benhaviour to provide we calculate the proper metadata
digest to compare the namespace object.

fixes pulp#1772

Co-authored-by: Gerrod <gerrodubben@gmail.com>
  • Loading branch information
mdellweg and gerrod3 committed Feb 27, 2024
1 parent 07eabfa commit b2a35dd
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGES/1772.bugfix
@@ -0,0 +1 @@
Fixed a bug failing sync on namespace metadata when avatar_sha256 is missing.
1 change: 1 addition & 0 deletions functest_requirements.txt
Expand Up @@ -7,3 +7,4 @@ numpy
requests
proxy.py~=2.4.3
trustme~=1.1.0
pulp-smash @ git+https://github.com/pulp/pulp-smash.git
13 changes: 3 additions & 10 deletions pulp_ansible/app/models.py
Expand Up @@ -332,16 +332,9 @@ class AnsibleNamespaceMetadata(Content):

@property
def avatar_artifact(self):
if self.avatar_sha256:
avatar = self._artifacts.filter(sha256=self.avatar_sha256).first()
if avatar is None:
log.debug(
f"Artifact({self.avatar_sha256}) is missing for namespace avatar "
f"{self.name}:{self.metadata_sha256}"
)
return avatar

return None
return self._artifacts.filter(
content_memberships__relative_path=f"{self.name}-avatar"
).first()

@hook(BEFORE_SAVE)
def calculate_metadata_sha256(self):
Expand Down
8 changes: 5 additions & 3 deletions pulp_ansible/app/serializers.py
Expand Up @@ -862,15 +862,17 @@ def get_avatar_url(self, obj):
return None

def validate(self, data):
"""Check that avatar_sha256 is set if avatar was present in upload."""
if self.instance:
if (name := data.get("name", None)) and name != self.instance.name:
raise serializers.ValidationError(_("Name can not be changed in an update"))

# Check that avatar_sha256 is set if avatar was present in upload.
if "artifact" in self.context:
if "avatar_sh256" not in data:
avatar_artifact = Artifact.objects.get(pk=self.context["artifact"])
avatar_artifact = Artifact.objects.get(pk=self.context["artifact"])
if data.get("avatar_sha256") is None:
data["avatar_sha256"] = avatar_artifact.sha256
elif data["avatar_sha256"] != avatar_artifact.sha256:
raise serializers.ValidationError(_("Avatar does not match expected digest."))

return super().validate(data)

Expand Down
7 changes: 3 additions & 4 deletions pulp_ansible/app/tasks/collections.py
Expand Up @@ -1154,13 +1154,12 @@ def _pre_save(self, batch):
d_content.content.namespace = namespace
if d_content.d_artifacts:
da = d_content.d_artifacts[0]
# Check to see if avatar failed to download, update metadata if so
# Check to see if avatar failed to download, update metadata if so,
# so that the avatar should be attemtped to be downloaded again.
if da.deferred_download:
d_content.d_artifacts = None
d_content.content.avatar_sha256 = None
# Check to see if upstream didn't have avatar_sha256 set
elif d_content.content.avatar_sha256 is None:
d_content.content.avatar_sha256 = da.artifact.sha256
d_content.content.metadata_sha256 = None

def _post_save(self, batch):
"""
Expand Down

0 comments on commit b2a35dd

Please sign in to comment.