Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add validation of yaml fields before loading them during import #2181

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

20wildmanj
Copy link
Contributor

@20wildmanj 20wildmanj commented Aug 23, 2024

Description

It is possible that after exporting an assessment, instructors may modify the yaml configuration file manually, leading to bad / blank values. Therefore, in order to ensure that if bad values / nil values are provided to assessment import, we must validate some important fields like name, handin_directory, handin_filename, display_name, and dates. There was some validation of dates, but the checks did not include the case where the dates could be empty strings which technically can be parsed but will cause an error later down the road.

For example there can exist these errors:

  • if the name field in the yaml file is different from the folder / yaml configuration filenames, then the name field of the assessment will become the one in the yaml file, but then Autolab can no longer find the folder of the assessment, which causes an error
  • if the handin_directory field is not provided, this may also cause an error when trying to execute assessment.rb:construct_folder (constructing a handin folder for the assessment)
  • if the handin_filename field is not provided, this causes a 500 error when trying to view the homepage
  • if display_name is not provided, the assessment import will not be successful
  • if dates provided are blank strings, the assessment import will fail validation of start / due dates

The below changes fix all of these cases.

Motivation and Context

Improves assessment import flow and overall robustness by handling invalid yaml file fields.

How Has This Been Tested?

  1. Use this configuration file: homework04.yml.txt
  2. create a folder homework04 within a course, and try using the import assessment feature, see that import is successful
  3. delete the assessment, and then go into homework04.yml and either delete the field or make it a blank string, see that import is successful and display name is the same as the assessment name
  4. delete the assessment, go into homework04.yml and change the name to different from the file name (for example homework05), see that import is successful
  5. delete the assessment, go into homework04.yml and remove the handin_directory and handin_filename fields, see that import is successful and handin_directory / handin_filename are given default values
  6. delete the assessment, go into homework04.yml and make all date fields empty string, import and see that dates are autofilled

Note: currently after deleting the assessment, the yaml fields may end up inconsistent with their corresponding values in the UI.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • I have run rubocop and erblint for style check. If you haven't, run overcommit --install && overcommit --sign to use pre-commit hook for linting

…ure that bad values aren't loaded in by either autofilling when blank or defaulting to a safe value
Copy link
Contributor

coderabbitai bot commented Aug 23, 2024

Walkthrough

Walkthrough

The changes introduce a new method validate_yaml in the Assessment class, which validates YAML configurations related to assessments. This method ensures the presence of specific fields and aligns them with the assessment's name, while also providing default values for missing entries. Additionally, modifications to the load_yaml and deserialize methods are made to integrate this validation process effectively.

Changes

Files Change Summary
app/models/assessment.rb Added the validate_yaml method for YAML validation. Modified load_yaml to call validate_yaml and updated deserialize with comments on date handling and validation.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Assessment
    participant YAMLLoader

    User->>YAMLLoader: Load YAML configuration
    YAMLLoader->>Assessment: load_yaml()
    Assessment->>Assessment: validate_yaml(yaml_map)
    Assessment-->>YAMLLoader: Return validated YAML
    YAMLLoader-->>User: Configuration loaded successfully
Loading

Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 0aa32cf and 846d0ec.

Files selected for processing (1)
  • app/models/assessment.rb (4 hunks)
Additional comments not posted (1)
app/models/assessment.rb (1)

340-342: Verify the impact of validate_yaml on existing functionality.

The addition of validate_yaml ensures that YAML fields are validated before loading. Ensure that this change does not break existing functionality or assumptions.

Run the following script to verify the usage of load_yaml and ensure it is compatible with existing code:

Verification successful

Ensure YAML files have a "general" section with "name" and "display_name".

The validate_yaml function checks for a "general" section and ensures "name" and "display_name" fields are present. This validation step could raise errors if these elements are missing, potentially affecting existing functionality if YAML files are not correctly formatted.

  • Ensure all YAML files used with load_yaml include a "general" section with the necessary fields.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of `load_yaml` to ensure compatibility with existing code.

# Test: Search for the function usage. Expect: Ensure compatibility with existing code.
rg --type ruby -A 5 $'load_yaml'

Length of output: 972


Script:

#!/bin/bash
# Description: Search for the definition of `validate_yaml` to understand its implementation.

# Test: Locate the `validate_yaml` function definition.
ast-grep --lang ruby --pattern 'def validate_yaml($_) { $$$ }'

Length of output: 409


Script:

#!/bin/bash
# Description: Search for the definition of `validate_yaml` using ripgrep to ensure it is located.

# Test: Locate the `validate_yaml` function definition.
rg --type ruby 'def validate_yaml'

Length of output: 91


Script:

#!/bin/bash
# Description: Retrieve the full implementation of `validate_yaml` to understand its impact.

# Test: Display the implementation of `validate_yaml`.
rg --type ruby -A 10 'def validate_yaml' app/models/assessment.rb

Length of output: 440


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Outside diff range, codebase verification and nitpick comments (1)
app/models/assessment.rb (1)

Line range hint 610-627: Consider adding validation for date formats.

The method handles date fields by parsing them and setting defaults if not provided. Consider adding validation to ensure the date formats are correct before parsing.

Implement a check to validate date formats and handle parsing errors gracefully.

Comment on lines +313 to +331
def validate_yaml(yaml_map)
unless yaml_map["general"]
raise "General section missing in yaml"
end

if yaml_map["general"]["name"] != name
yaml_map["general"]["name"] = name
end
# ensure display name is present, otherwise fill in with name
if yaml_map["general"]["display_name"].blank?
yaml_map["general"]["display_name"] = name
end
if yaml_map["general"]["handin_directory"].blank?
yaml_map["general"]["handin_directory"] = "handin"
end
return if yaml_map["general"]["handin_filename"].present?

yaml_map["general"]["handin_filename"] = "handin.c"
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add error handling for invalid YAML structures.

The method currently raises an exception if the "general" section is missing. Consider adding more robust error handling to manage invalid YAML structures gracefully.

You might want to log the error or provide a more descriptive error message to help with debugging.

@20wildmanj 20wildmanj requested review from a team, damianhxy and KesterTan and removed request for a team and damianhxy August 23, 2024 16:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant