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

[WEB-2615] fix: module date validation during chart distribution generation #5791

Merged
merged 2 commits into from
Oct 10, 2024

Conversation

gurusainath
Copy link
Collaborator

@gurusainath gurusainath commented Oct 10, 2024

Summary

In this PR date validation for modules when generating chart distributions.

Plane Issue:

[WEB-2615]

Summary by CodeRabbit

  • New Features

    • Improved permission management for module actions, enhancing clarity and security.
    • Enhanced permission handling for user favorites with a new permission class.
    • Adjusted logic for generating completion charts based on total issues.
  • Bug Fixes

    • Refined date range calculations for module plotting, ensuring accurate data representation.
    • Improved error handling for issue operations based on user roles.
  • Refactor

    • Restructured permission decorators for better readability and maintainability.
    • Enhanced query logic for returning relevant issues based on user membership and project status.

@gurusainath gurusainath added the 🐛bug Something isn't working label Oct 10, 2024
@gurusainath gurusainath self-assigned this Oct 10, 2024
Copy link
Contributor

coderabbitai bot commented Oct 10, 2024

Walkthrough

The changes in this pull request primarily focus on enhancing the ModuleViewSet class in the apiserver/plane/app/views/module/base.py file by restructuring permission decorators for several methods. Additionally, the burndown_plot function in apiserver/plane/utils/analytics_plot.py has been modified to improve date range handling for plotting. These updates aim to clarify permission management and streamline date calculations for module plots.

Changes

File Path Change Summary
apiserver/plane/app/views/module/base.py Restructured permission decorators for retrieve, partial_update, create, and list methods; added condition in retrieve to check modules.total_issues before generating completion_chart. Added ProjectLitePermission to ModuleFavoriteViewSet.
apiserver/plane/app/views/view/base.py Enhanced permission handling by adding decorator syntax for allow_permission in various methods; updated list, retrieve, partial_update, and destroy methods in IssueViewViewSet and IssueViewFavoriteViewSet to include project ID.
apiserver/plane/utils/analytics_plot.py Modified burndown_plot function to adjust date range calculations using queryset.target_date and simplified date handling in list comprehension.

Possibly related PRs

Suggested labels

🌟improvement, ⚙️backend

Suggested reviewers

  • sriramveeraghanta
  • pablohashescobar
  • SatishGandham

🐰 In the code we hop and play,
With permissions clear, we pave the way.
For charts that show our progress bright,
We plot our path, a joyful sight!
So let us code, with glee and cheer,
For every change brings us near! 🐇


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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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: 0

🧹 Outside diff range and nitpick comments (2)
apiserver/plane/utils/analytics_plot.py (2)

Line range hint 206-212: Approved: Improved date range calculation for modules

The changes to the date range calculation for modules are well-implemented and align with the module's structure. Using target_date instead of end_date is more appropriate for modules, and the simplified timedelta usage is more efficient.

For consistency, consider updating the date comparison later in the function:

if date > timezone.now().date():

to:

if date > timezone.now():

This change would make the comparison consistent with the new timedelta objects and eliminate the need for the .date() call.


Line range hint 206-212: Overall improvement: Enhanced module support in burndown_plot

The changes successfully improve the burndown_plot function's handling of modules by using the appropriate target_date and simplifying the date range calculation. This enhancement doesn't affect the existing cycle-related logic, maintaining the function's versatility.

To further improve code clarity and maintainability, consider extracting the date range calculation into a separate helper function for both cycle and module cases. This would reduce code duplication and make future modifications easier. For example:

def calculate_date_range(start_date, end_date):
    return [
        start_date + timedelta(days=x)
        for x in range((end_date - start_date).days + 1)
    ]

# Usage in the function:
if cycle_id:
    date_range = calculate_date_range(queryset.start_date, queryset.end_date) if queryset.end_date and queryset.start_date else []
elif module_id:
    date_range = calculate_date_range(queryset.start_date, queryset.target_date)

This refactoring would make the code more DRY (Don't Repeat Yourself) and easier to maintain.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between c1f881b and 71b1307.

📒 Files selected for processing (2)
  • apiserver/plane/app/views/module/base.py (2 hunks)
  • apiserver/plane/utils/analytics_plot.py (1 hunks)
🧰 Additional context used
🔇 Additional comments (5)
apiserver/plane/app/views/module/base.py (5)

438-438: LGTM: Permission decorator added

The addition of the @allow_permission([ROLE.ADMIN, ROLE.MEMBER]) decorator is a good practice. It ensures that only users with admin or member roles can access this method, improving security and access control.


669-675: Optimization: Conditional chart generation

The new condition modules.total_issues > 0 is a good optimization. It prevents unnecessary chart generation when there are no issues in the module, potentially improving performance.


Line range hint 696-696: LGTM: Consistent permission control

The addition of the @allow_permission([ROLE.ADMIN, ROLE.MEMBER]) decorator is consistent with the retrieve method. This ensures uniform access control across different operations on modules.


Line range hint 301-301: LGTM: Consistent permission control

The addition of the @allow_permission([ROLE.ADMIN, ROLE.MEMBER]) decorator to the create method maintains consistency in access control across the ModuleViewSet class.


Line range hint 1-1000: Summary: Improved security and performance

The changes in this file focus on two main areas:

  1. Security: Consistent application of permission decorators (@allow_permission) across multiple methods in the ModuleViewSet class, ensuring proper access control.
  2. Performance: Addition of a condition to prevent unnecessary chart generation when there are no issues in a module.

These changes enhance the overall security and efficiency of the module management system without introducing any apparent issues or inconsistencies.

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: 5

🧹 Outside diff range and nitpick comments (3)
apiserver/plane/app/views/view/base.py (3)

Line range hint 471-479: Use appropriate HTTP status code and error message

In the retrieve method, when a guest user is not allowed to view the issue view, the response returns a status code of 400 Bad Request. It's more appropriate to return a 403 Forbidden status code to indicate that the server understood the request but refuses to authorize it. Additionally, update the error message to accurately reflect the resource.

Proposed change:

              return Response(
-                 {"error": "You are not allowed to view this issue"},
-                 status=status.HTTP_400_BAD_REQUEST,
+                 {"error": "You are not allowed to view this view"},
+                 status=status.HTTP_403_FORBIDDEN,
              )

Line range hint 509-513: Return 403 Forbidden instead of 400 Bad Request for unauthorized access

When a user who is not the owner attempts to update the view, the response returns a 400 Bad Request. It's more appropriate to return a 403 Forbidden status code in this case.

Proposed change:

          if issue_view.owned_by_id != request.user.id:
              return Response(
                  {
                      "error": "Only the owner of the view can update the view"
                  },
-                 status=status.HTTP_400_BAD_REQUEST,
+                 status=status.HTTP_403_FORBIDDEN,
              )

Line range hint 542-546: Return 403 Forbidden instead of 400 Bad Request when deletion is unauthorized

If a user who is neither an admin nor the owner attempts to delete a view, a 400 Bad Request is returned. Update the status code to 403 Forbidden to correctly indicate the authorization error.

Proposed change:

          else:
              return Response(
                  {"error": "Only admin or owner can delete the view"},
-                 status=status.HTTP_400_BAD_REQUEST,
+                 status=status.HTTP_403_FORBIDDEN,
              )
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 71b1307 and d9ceac5.

📒 Files selected for processing (2)
  • apiserver/plane/app/views/module/base.py (6 hunks)
  • apiserver/plane/app/views/view/base.py (6 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • apiserver/plane/app/views/module/base.py
🧰 Additional context used
🔇 Additional comments (1)
apiserver/plane/app/views/view/base.py (1)

588-588: ⚠️ Potential issue

Correct the keyword argument when retrieving UserFavorite

In the destroy method, the UserFavorite.objects.get uses project=project_id as a keyword argument. However, the correct field is project_id.

Proposed change:

      view_favorite = UserFavorite.objects.get(
-         project=project_id,
+         project_id=project_id,
          user=request.user,
          workspace__slug=slug,
          entity_type="view",
          entity_identifier=view_id,
      )

Likely invalid or redundant comment.

apiserver/plane/app/views/view/base.py Show resolved Hide resolved
apiserver/plane/app/views/view/base.py Show resolved Hide resolved
apiserver/plane/app/views/view/base.py Show resolved Hide resolved
apiserver/plane/app/views/view/base.py Show resolved Hide resolved
apiserver/plane/app/views/view/base.py Show resolved Hide resolved
@pushya22 pushya22 merged commit e9158f8 into preview Oct 10, 2024
14 of 15 checks passed
@pushya22 pushya22 deleted the fix-module-chart-distribution branch October 10, 2024 13:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants