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

refactor: ♻️ switch between schemas for codemeta.json version #126

Merged
merged 11 commits into from
Jan 4, 2025

Conversation

slugb0t
Copy link
Member

@slugb0t slugb0t commented Jan 4, 2025

Summary by Sourcery

Support validating different versions of the codemeta schema.

New Features:

  • Add support for validating Codemeta 2.0 and 3.0 schemas.

Tests:

  • Update tests to reflect the new schema versioning support.

megasanjay and others added 11 commits December 13, 2024 16:36
* 🐛 fix: update sorting on updated time

* ⚰️ chore: remove yarn.lock

* 💄 style: uipdate profile ui
* ✨ feat: add a dashboard page

* 🐛 fix: remove old code

* 🐛 fix: add response checks

* refactor: ♻️ dashboard link in nav directs to /dashboard

* refactor: ♻️ update variables for GET /dashboard api

---------

Co-authored-by: slugb0t <wheresdorian@gmail.com>
Copy link

Thank you for submitting this pull request! We appreciate your contribution to the project. Before we can merge it, we need to review the changes you've made to ensure they align with our code standards and meet the requirements of the project. We'll get back to you as soon as we can with feedback. Thanks again!

Copy link

sourcery-ai bot commented Jan 4, 2025

Reviewer's Guide by Sourcery

This pull request refactors the validator API to support multiple schemas for different CodeMeta versions. It implements version switching logic and error handling for schema validation. Additionally, the UI is updated to only show the "Okay" button after publishing to Zenodo, simplifying the user interaction.

Sequence diagram for CodeMeta validation with version support

Loading
sequenceDiagram
    participant Client
    participant API
    participant SchemaValidator

    Client->>API: POST /validate with codemeta.json
    API->>API: Extract codemeta_version
    alt version not supported
        API-->>Client: Return 400 error
    else version == 2.0
        API->>SchemaValidator: Load codemeta-schema2.0.json
        SchemaValidator->>SchemaValidator: Validate content
    else version == 3.0
        API->>SchemaValidator: Load codemeta-schema.json
        SchemaValidator->>SchemaValidator: Validate content
    end
    alt validation successful
        API-->>Client: Return valid status (200)
    else validation error
        API-->>Client: Return invalid status with error (200)
    else unexpected error
        API-->>Client: Return error status (400)
    end

State diagram for Zenodo publish button visibility

Loading
stateDiagram-v2
    [*] --> Hidden: Initial State
    Hidden --> Visible: When status is 'published' or 'error'
    Visible --> [*]: After clicking Okay

    note right of Hidden: Button not shown during processing
    note right of Visible: Button enables navigation to dashboard

File-Level Changes

Change Details Files
Implement schema switching logic based on CodeMeta version
  • Added conditional logic to load different schema files based on the provided CodeMeta version.
  • Handles versions 2.0 and 3.0, loading 'codemeta-schema2.0.json' and 'codemeta-schema.json' respectively.
  • Maintains existing error handling and validation logic within each version branch.
validator/apis/__init__.py
Updated UI to display "Okay" button after Zenodo publishing
  • Modified the visibility condition of the "Okay" button.
  • The button is now displayed only when the Zenodo publishing status is either 'published' or 'error'.
ui/pages/dashboard/[owner]/[repo]/release/zenodo.vue

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

Thanks for making updates to your pull request. Our team will take a look and provide feedback as soon as possible. Please wait for any GitHub Actions to complete before editing your pull request. If you have any additional questions or concerns, feel free to let us know. Thank you for your contributions!

Copy link

what-the-diff bot commented Jan 4, 2025

PR Summary

  • Improved Visibility Controls for Zenodo Button
    The visibility conditions for the Zenodo button embedded in our application have been updated. The button will now only appear if a publishing status flags it as 'published' or if there is an 'error'. This change removes its previous display when it was in a 'disabled' state.

  • Strengthened API Validation Logic
    We have made enhancements in the logic used for the validation of data submitted via API. Previously, we didn't distinguish between codemeta version 2.0 and 3.0. Now, the system loads different schemas based on the version and keeps track of validation errors for both versions of the codemeta. This will allow users to have a more accurate and efficient validation process when interacting with our API.

@slugb0t slugb0t merged commit 744dcf2 into main Jan 4, 2025
4 checks passed
Copy link

Thanks for closing this pull request! If you have any further questions, please feel free to open a new issue. We are always happy to help!

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @slugb0t - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider reducing code duplication by extracting the schema validation logic into a separate function that takes the schema filename as a parameter. This would make the code more maintainable and easier to extend for future codemeta versions.
Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@@ -218,23 +218,46 @@ def post(self):
"message": "Validation Error",
"error": "Unsupported codemeta version",
}, 400
Copy link

Choose a reason for hiding this comment

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

suggestion: Consider extracting the validation logic into a separate function to avoid code duplication

The validation logic for both version 2.0 and 3.0 is identical except for the schema file name. Consider creating a helper function that takes the schema file name as a parameter to reduce duplication and improve maintainability.

Suggested implementation:

        def validate_schema(schema_filename: str, version: str) -> tuple[dict, int]:
            """Validate file content against the specified schema file.

            Args:
                schema_filename: Path to the schema file
                version: Version string to include in the response

            Returns:
                Tuple of (response dict, status code)
            """
            try:
                with open(schema_filename, "r", encoding="utf-8") as f:
                    schema = json.load(f)
                    jsonschema.validate(file_content, schema)

                return {
                    "message": "valid", 
                    "version": version,
                }, 200
            except jsonschema.exceptions.ValidationError as e:
                raise e

        if codemeta_version == "2.0":
            return validate_schema("./codemeta-schema2.0.json", codemeta_version)

The developer will need to:

  1. Update the code handling version 3.0 to use the new validate_schema() function in the same way
  2. Ensure the error handling for ValidationError is properly implemented in the calling code since we're re-raising the exception

Comment on lines +240 to +242
"message": "Validation Error",
"error": f"Unexpected error: {str(e)}",
}, 400
Copy link

Choose a reason for hiding this comment

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

suggestion (bug_risk): Consider using a 500 status code for unexpected errors instead of 400

400 Bad Request implies client error, but unexpected exceptions might be server-side issues. Using 500 Internal Server Error would be more appropriate for unexpected exceptions.

Suggested change
"message": "Validation Error",
"error": f"Unexpected error: {str(e)}",
}, 400
"message": "Internal Server Error",
"error": f"Unexpected error: {str(e)}",
}, 500

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

2 participants