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 #1772

Co-authored-by: Gerrod <[email protected]>
(cherry picked from commit 3a5e961)
  • Loading branch information
mdellweg committed Feb 28, 2024
1 parent 4fddd8d commit 3f27f89
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGES/1772.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed a bug failing sync on namespace metadata when avatar_sha256 is missing.
13 changes: 3 additions & 10 deletions pulp_ansible/app/models.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -1149,13 +1149,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 3f27f89

Please sign in to comment.