From ffc61b41ccaf31719e1136df3860f495098f62e8 Mon Sep 17 00:00:00 2001 From: Ayush Rathore Date: Sat, 12 Aug 2023 00:22:40 +0530 Subject: [PATCH] [BugFix] Fix validate challenge for missing/extra YAML files (#4021) * [BugFix] Fix Validate challenge throwing errors in case of wrong/missing YAML files. This fixes internal server errors and returns the error directly and avoid further checks in case of any issues with the YAML file. Signed-off-by: AyushR1 * Check only for challenge_config.yaml file Signed-off-by: AyushR1 * Fix validate challenge config Signed-off-by: AyushR1 * Fix challenge zip file Signed-off-by: AyushR1 --------- Signed-off-by: AyushR1 Co-authored-by: Gunjan Chhablani --- apps/challenges/challenge_config_utils.py | 32 ++++++++++++++-------- examples/example1/test_zip_file.zip | Bin 22538 -> 22490 bytes 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/apps/challenges/challenge_config_utils.py b/apps/challenges/challenge_config_utils.py index 310f2ad8a9..65040b0db4 100644 --- a/apps/challenges/challenge_config_utils.py +++ b/apps/challenges/challenge_config_utils.py @@ -53,8 +53,9 @@ def get_yaml_files_from_challenge_config(zip_ref): yaml_file_name = None extracted_folder_name = None for name in zip_ref.namelist(): - if (name.endswith(".yaml") or name.endswith(".yml")) and ( - not name.startswith("__MACOSX") + if ( + (name == "challenge_config.yaml" or name == "challenge_config.yml") + and not name.startswith("__MACOSX") ): yaml_file_name = name extracted_folder_name = yaml_file_name.split( @@ -222,7 +223,7 @@ def get_value_from_field(data, base_location, field_name): error_message_dict = { "no_yaml_file": "There is no YAML file in the zip file you uploaded!", - "multiple_yaml_files": "There are {} YAML files instead of one in the zip file!", + "multiple_yaml_files": "There are {} challenge config YAML files instead of 1 in the zip file!", "yaml_file_read_error": "\n{} in line {}, column {}\n", "missing_challenge_title": "Please add the challenge title", "missing_challenge_description": "Please add the challenge description", @@ -322,11 +323,13 @@ def __init__( self.extracted_folder_name, ) = get_yaml_files_from_challenge_config(self.zip_ref) - self.challenge_config_location = join( - self.base_location, - self.unique_folder_name, - self.extracted_folder_name, - ) + self.valid_yaml = self.read_and_validate_yaml() + if self.valid_yaml: + self.challenge_config_location = join( + self.base_location, + self.unique_folder_name, + self.extracted_folder_name, + ) self.phase_ids = [] self.leaderboard_ids = [] @@ -334,12 +337,14 @@ def read_and_validate_yaml(self): if not self.yaml_file_count: message = self.error_messages_dict.get("no_yaml_file") self.error_messages.append(message) + return False if self.yaml_file_count > 1: message = self.error_messages_dict.get( "multiple_yaml_files" ).format(self.yaml_file_count) self.error_messages.append(message) + return False # YAML Read Error try: @@ -347,6 +352,7 @@ def read_and_validate_yaml(self): self.base_location, self.unique_folder_name, self.yaml_file ) self.yaml_file_data = read_yaml_file(self.yaml_file_path, "r") + return True except (yaml.YAMLError, ScannerError) as exc: ( error_description, @@ -357,6 +363,7 @@ def read_and_validate_yaml(self): "yaml_file_read_error" ).format(error_description, line_number, column_number) self.error_messages.append(message) + return False def validate_challenge_title(self): challenge_title = self.yaml_file_data.get("title") @@ -1012,9 +1019,12 @@ def validate_challenge_config_util( zip_ref, current_challenge, ) - - # Read and validate YAML file - val_config_util.read_and_validate_yaml() + if not val_config_util.valid_yaml: + return ( + val_config_util.error_messages, + val_config_util.yaml_file_data, + val_config_util.files, + ) # # Validate challenge title val_config_util.validate_challenge_title() diff --git a/examples/example1/test_zip_file.zip b/examples/example1/test_zip_file.zip index 78c6a26f68dd4a4a83278325ff3d0cecce47e44e..2107142aa7f449bd7aafe675cf0fc6c3a269c612 100644 GIT binary patch delta 792 zcmeC$z<6stV|{=(Gm8i_0|N(x%Fg!S86Wx+J64=x=x*ls)aa?bkbLQ0Vf~M3iV+SE&QD-#sq3pX zWwS}1Fn8y{m(H_o9lvop*PIR8DK`J{gf)p3Q`08>^mm%BOcJA~73ucRkFd4rT#v^qO+LnK^dfr?( z(sua!v$l_GKcAJ(xRYYdbMw;@&yJ(Jk`yv$$){-BoIa`YkXcmVZttP=|M|z#c#m3~ z=-9aK-ICbYli|Pezv^#n-5<2uOE2rI_mQL$L%+EfZQGX{+`asA$I-Zm(u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