diff --git a/sde_collections/management/commands/push_config_to_github b/sde_collections/management/commands/push_config_to_github deleted file mode 100644 index baa98ec8..00000000 --- a/sde_collections/management/commands/push_config_to_github +++ /dev/null @@ -1,24 +0,0 @@ -from django.core.management.base import BaseCommand - - -class Command(BaseCommand): - help = ( - "Push config to github. Takes comma-separated config_folder list as argument." - ) - - def add_arguments(self, parser): - parser.add_argument("config_folders", nargs="*", type=str, default=[]) - - def handle(self, *args, **options): - config_folders = options["config_folders"] - for config_folder in config_folders: - print("jere") - # collection = Collection.objects.get(config_folder=config_folder) - # collection.push_configs_to_github() - - self.stdout.write( - self.style.SUCCESS( - 'Successfully pushed the following collection to github from "%s"' - % config_folders - ) - ) diff --git a/sde_collections/management/commands/push_to_github.py b/sde_collections/management/commands/push_to_github.py new file mode 100644 index 00000000..37aed37f --- /dev/null +++ b/sde_collections/management/commands/push_to_github.py @@ -0,0 +1,44 @@ +from django.core.management.base import BaseCommand + +from sde_collections.models.collection import Collection +from sde_collections.utils.github_helper import GitHubHandler + + +class Command(BaseCommand): + help = ( + "Push config to github. Takes comma-separated config_folder list as argument." + ) + + def add_arguments(self, parser): + parser.add_argument("config_folders", nargs="*", type=str, default=[]) + + def handle(self, *args, **options): + config_folders = options["config_folders"] + + # curation status 5 is Curated + collections = Collection.objects.filter( + config_folder__in=config_folders + ).filter(curation_status=5) + + cant_push = Collection.objects.filter(config_folder__in=config_folders).exclude( + curation_status=5 + ) + cant_push = list(cant_push.values_list("name", flat=True)) + + gh = GitHubHandler(collections) + gh.push_to_github() + + self.stdout.write( + self.style.SUCCESS( + "Successfully pushed: %s" + % list(collections.values_list("name", flat=True)) + ) + ) + + if cant_push: + self.stdout.write( + self.style.ERROR( + "Can't push since status is not Curated (choice_id:5) %s" + % cant_push + ) + ) diff --git a/sde_collections/utils/github_helper.py b/sde_collections/utils/github_helper.py index 2ff709c8..724366b4 100644 --- a/sde_collections/utils/github_helper.py +++ b/sde_collections/utils/github_helper.py @@ -40,13 +40,14 @@ def _update_file_contents(self, collection): """ contents = self._get_file_contents(collection) FILE_CONTENTS = contents.decoded_content.decode("utf-8") + updated_xml = collection.update_config_xml(FILE_CONTENTS) COMMIT_MESSAGE = f"Webapp: Update {collection.name}" self.repo.update_file( contents.path, COMMIT_MESSAGE, - FILE_CONTENTS, + updated_xml, contents.sha, branch=self.github_branch, ) @@ -66,6 +67,7 @@ def create_pull_request(self): def push_to_github(self): for collection in self.collections: + print(f"Pushing {collection.name} to GitHub.") self._update_file_contents(collection) collection.curation_status = CurationStatusChoices.GITHUB_PR_CREATED collection.save() diff --git a/sde_collections/views.py b/sde_collections/views.py index 148591c3..c38b81cb 100644 --- a/sde_collections/views.py +++ b/sde_collections/views.py @@ -292,7 +292,7 @@ def post(self, request): "collection_ids can't be empty.", status=status.HTTP_400_BAD_REQUEST ) - push_to_github_task(collection_ids) + push_to_github_task.delay(collection_ids) return Response( {"Success": "Started pushing collections to github"},