Repository: hydroshare/hydroshare

Issue:
Enable management command to fix file issues on published resource
Currently, running `repair_resource` on published resources will fail.

Reproduce by:
* Create a resource and add some files
* Use the database or shell to remove some of the files from django to put the resource in a "broken" state
* Publish the resource
* Run the repair_resource --published management command and see that the published resource cannot be fixed by this script

**Additional context**
HS v2.11.3



Discussion:

Resolving patch:
diff --git a/hs_core/management/commands/repair_resource.py b/hs_core/management/commands/repair_resource.py
--- a/hs_core/management/commands/repair_resource.py
+++ b/hs_core/management/commands/repair_resource.py
@@ -11,6 +11,7 @@
 """
 
 from django.core.management.base import BaseCommand, CommandError
+from django.core.exceptions import ValidationError
 from hs_core.models import BaseResource
 from hs_core.management.utils import repair_resource
 from hs_core.views.utils import get_default_admin_user
@@ -95,6 +96,7 @@ def handle(self, *args, **options):
         total_files_dangling_in_django = 0
         resources_with_missing_django = []
         resources_with_missing_irods = []
+        failed_resources = []
         for resource in resources.iterator():
             current_resource += 1
             res_url = site_url + resource.absolute_url
@@ -106,7 +108,13 @@ def handle(self, *args, **options):
                     print("Command running with --admin. Published resources will be repaired if needed.")
                 else:
                     print("Command running without --admin. Fixing a published resource raise ValidationError")
-            _, missing_in_django, dangling_in_django = repair_resource(resource, logger, dry_run=dry_run, user=user)
+            try:
+                _, missing_in_django, dangling_in_django = repair_resource(resource, logger, dry_run=dry_run, user=user)
+            except ValidationError as ve:
+                failed_resources.append(res_url)
+                print("Exception while attempting to repair resource:")
+                print(ve)
+                continue
             if dangling_in_django > 0 or missing_in_django > 0:
                 impacted_resources += 1
                 total_files_missing_in_django += missing_in_django
@@ -139,7 +147,13 @@ def handle(self, *args, **options):
             print(res)
 
         # Make it simple to detect clean/fail run in Jenkins
-        if impacted_resources:
-            raise CommandError("repair_resources detected problems")
+        if impacted_resources and dry_run:
+            raise CommandError("repair_resources detected resources in need of repair during dry run")
         else:
-            print("Completed run without detecting issues")
+            print("Completed run of repair_resource")
+        if failed_resources:
+            print("*" * 100)
+            print("Repair was attempted but failed for the following resources:")
+            for res in resources_with_missing_irods:
+                print(res)
+            raise CommandError("Repair was attempted but failed on at least one resource")


Test patch: