Skip to content

Fix some missing query parameters on the API endpoints#103

Closed
borfast wants to merge 0 commit intomainfrom
bugfix/fix-dependency-endpoints-not-sending-project
Closed

Fix some missing query parameters on the API endpoints#103
borfast wants to merge 0 commit intomainfrom
bugfix/fix-dependency-endpoints-not-sending-project

Conversation

@borfast
Copy link
Copy Markdown
Contributor

@borfast borfast commented Mar 13, 2025

Some endpoints were missing the project and the granularity parameters, and thus were not able to fetch data from TinyBird.

I also took the opportunity to add a few improvements in terms of data bounds checking.

Summary by CodeRabbit

  • New Features

    • Enabled project-specific filtering for contributor and organization endpoints by automatically using the project identifier from the URL.
    • Introduced a granularity setting for dependency data that defaults to a quarterly view when not specified.
  • Improvements

    • Enhanced data handling to safely manage cases where expected values might be missing, ensuring more robust query responses.
    • Updated filter structures to include a project property for more targeted data retrieval.

@borfast borfast requested review from emlimlf and gaspergrom March 13, 2025 12:11
@borfast borfast self-assigned this Mar 13, 2025
@borfast borfast added the bug Something isn't working label Mar 13, 2025
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 13, 2025

Walkthrough

This pull request updates several API endpoints and their supporting Tinybird data source functions by extracting the project identifier from the URL slug rather than the query string. Additionally, a new granularity property (defaulting to FilterGranularity.QUARTERLY) is added to certain filter objects. The changes also propagate the new project field into the type definitions used for contributor and organization leaderboards, enhancing the filtering capabilities across the code base.

Changes

File(s) Change Summary
frontend/server/api/.../contributor-dependency.get.ts
frontend/server/api/.../organization-dependency.get.ts
Added import of FilterGranularity; extracted project from URL slug; added granularity property (defaulting to QUARTERLY) to the filter object; removed query-based project assignment.
frontend/server/api/.../contributor-leaderboard.get.ts
frontend/server/api/.../organization-leaderboard.get.ts
Extracted project from URL slug and injected it into the filter object to ensure the leaderboard queries are project-specific.
frontend/server/data/tinybird/...-dependency-data-source.ts
frontend/server/data/tinybird/...-leaderboard-source.ts
Added project property into the Tinybird query filter objects; improved data access using optional chaining for robust retrieval of metrics such as top contributor/organization percentages and counts.
frontend/server/data/types.ts Updated ContributorsLeaderboardFilter and OrganizationsLeaderboardFilter types by adding a new project: string property.

Sequence Diagram(s)

sequenceDiagram
  participant C as Client
  participant A as API Endpoint
  participant D as Data Source
  participant T as Tinybird Service

  C->>A: Send request (URL includes slug)
  A->>A: Extract slug as project and set filter (project, granularity)
  A->>D: Invoke data source function with filter object
  D->>T: Query Tinybird with filter criteria
  T-->>D: Return query results
  D-->>A: Provide data with safe access (optional chaining)
  A-->>C: Respond with filtered results
Loading

Possibly related PRs

  • Active organizations widget endpoint #78: The changes in the main PR and the retrieved PR are related through the introduction of the project variable in the filter objects, which enhances the filtering capabilities in both cases.
  • Repo switching #44: The changes in the main PR are related to those in the retrieved PR as both involve the introduction of a project variable extracted from the slug parameter and its inclusion in filter objects, enhancing project-specific data handling.
  • Add endpoint for the Retention widget data #96: The changes in the main PR and the retrieved PR are related through the introduction of the project variable in the filter objects, as both PRs modify how the project is utilized in their respective filtering logic.

Suggested reviewers

  • gaspergrom
  • emlimlf

Tip

⚡🧪 Multi-step agentic review comment chat (experimental)
  • We're introducing multi-step agentic chat in review comments. This experimental feature enhances review discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments.
    - To enable this feature, set early_access to true under in the settings.

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ff8d0106764af41a6276244fafa94d9aad01a30a and 4ad66d11b60dc08fcdbc2492c23f8d32d79ddc97.

📒 Files selected for processing (9)
  • frontend/server/api/project/[slug]/contributors/contributor-dependency.get.ts (2 hunks)
  • frontend/server/api/project/[slug]/contributors/contributor-leaderboard.get.ts (1 hunks)
  • frontend/server/api/project/[slug]/contributors/organization-dependency.get.ts (2 hunks)
  • frontend/server/api/project/[slug]/contributors/organization-leaderboard.get.ts (1 hunks)
  • frontend/server/data/tinybird/contributors-dependency-data-source.ts (2 hunks)
  • frontend/server/data/tinybird/contributors-leaderboard-source.ts (2 hunks)
  • frontend/server/data/tinybird/organizations-dependency-data-source.ts (2 hunks)
  • frontend/server/data/tinybird/organizations-leaderboard-source.ts (1 hunks)
  • frontend/server/data/types.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (9)
  • frontend/server/data/tinybird/organizations-leaderboard-source.ts
  • frontend/server/api/project/[slug]/contributors/contributor-leaderboard.get.ts
  • frontend/server/data/tinybird/contributors-leaderboard-source.ts
  • frontend/server/api/project/[slug]/contributors/organization-leaderboard.get.ts
  • frontend/server/data/tinybird/organizations-dependency-data-source.ts
  • frontend/server/api/project/[slug]/contributors/contributor-dependency.get.ts
  • frontend/server/data/tinybird/contributors-dependency-data-source.ts
  • frontend/server/api/project/[slug]/contributors/organization-dependency.get.ts
  • frontend/server/data/types.ts

🪧 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 generate docstrings to generate docstrings for this 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
Copy Markdown

@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 comments (1)
frontend/server/data/tinybird/contributors-dependency-data-source.ts (1)

64-65: ⚠️ Potential issue

Potential division by zero risk.

Since totalContributorCount might be undefined or zero, there's a risk of calculating incorrect values for otherContributors.count and otherContributors.percentage. The missing fallback value for totalContributorCount could lead to unexpected results.

Add defensive checks to ensure calculations don't produce NaN or negative values:

    otherContributors: {
-      count: totalContributorCount - topContributorsCount,
-      percentage: 100 - topContributorsPercentage
+      count: Math.max(0, (totalContributorCount || 0) - topContributorsCount),
+      percentage: Math.max(0, 100 - topContributorsPercentage)
    },
🧹 Nitpick comments (3)
frontend/server/data/tinybird/contributors-leaderboard-source.ts (1)

64-64: Consider adding optional chaining for data.rows

Add optional chaining when accessing data.rows to prevent potential runtime errors if data is undefined.

-      total: data.rows
+      total: data?.rows || 0
frontend/server/data/tinybird/contributors-dependency-data-source.ts (1)

54-56: Good use of optional chaining for safer data access.

The changes implement optional chaining (?.) with fallback values to prevent potential runtime errors when accessing data properties that might be undefined.

However, there's an inconsistency in the implementation. Line 56 doesn't provide a fallback value (|| 0) unlike the other similar changes.

-  const totalContributorCount = tinybirdTopContributorsResponse.data[0]?.totalContributorCount;
+  const totalContributorCount = tinybirdTopContributorsResponse.data[0]?.totalContributorCount || 0;
frontend/server/api/project/[slug]/contributors/organization-dependency.get.ts (1)

28-33: Update API documentation to match implementation.

The API documentation comment still lists project as a query parameter, but the implementation now extracts it from the URL path. Consider updating the documentation to reflect this change.

 /**
  * Query params:
  * - metric: 'all' | 'commits' | (see metrics options)
- * - project: string
  * - repository: string
  * - time-period: string
+ * - granularity: string (optional, defaults to QUARTERLY)
  */
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0875920 and af963d026752a68b24690e331f654fd45169f1d8.

📒 Files selected for processing (9)
  • frontend/server/api/project/[slug]/contributors/contributor-dependency.get.ts (2 hunks)
  • frontend/server/api/project/[slug]/contributors/contributor-leaderboard.get.ts (1 hunks)
  • frontend/server/api/project/[slug]/contributors/organization-dependency.get.ts (2 hunks)
  • frontend/server/api/project/[slug]/contributors/organization-leaderboard.get.ts (1 hunks)
  • frontend/server/data/tinybird/contributors-dependency-data-source.ts (2 hunks)
  • frontend/server/data/tinybird/contributors-leaderboard-source.ts (1 hunks)
  • frontend/server/data/tinybird/organizations-dependency-data-source.ts (2 hunks)
  • frontend/server/data/tinybird/organizations-leaderboard-source.ts (1 hunks)
  • frontend/server/data/types.ts (2 hunks)
🔇 Additional comments (16)
frontend/server/api/project/[slug]/contributors/contributor-leaderboard.get.ts (2)

38-39: Good addition of project extraction from URL parameters

Extracting the project slug from the URL parameters is a good approach as it makes the endpoint more robust by not relying on query parameters for this essential value.


41-41: LGTM: Project parameter added to filter object

This change correctly adds the project parameter to the filter object, ensuring that the required project identifier is included in the request to TinyBird.

frontend/server/api/project/[slug]/contributors/organization-leaderboard.get.ts (2)

37-38: Good addition of project extraction from URL parameters

Similar to the contributor-leaderboard endpoint, extracting the project slug from the URL parameters is a consistent and reliable approach.


46-46: LGTM: Project parameter added to filter object

This change correctly adds the project parameter to the filter object for the organization leaderboard, ensuring consistency with the contributor leaderboard endpoint.

frontend/server/data/tinybird/contributors-leaderboard-source.ts (1)

35-35: LGTM: Project parameter added to TinyBird query

This change correctly passes the project parameter to the TinyBird query, ensuring that data is filtered by the appropriate project.

frontend/server/data/types.ts (2)

40-40: LGTM: Project property added to ContributorsLeaderboardFilter

This change correctly adds the required project property to the ContributorsLeaderboardFilter type, aligning it with other filter types in the file.


49-49: LGTM: Project property added to OrganizationsLeaderboardFilter

This change correctly adds the required project property to the OrganizationsLeaderboardFilter type, ensuring consistency with other filter types.

frontend/server/data/tinybird/organizations-leaderboard-source.ts (1)

35-40: Adding the project parameter ensures correct data retrieval.

Including the project identifier in the Tinybird query now allows the API to filter data correctly. This change is aligned with the PR objective of fixing missing query parameters.

frontend/server/data/tinybird/organizations-dependency-data-source.ts (2)

40-47: Project parameter added correctly to the leaderboard filter.

Including the project identifier in the filter object ensures data is filtered properly when fetching from Tinybird.


54-57: Good use of optional chaining for safer data access.

The changes implement optional chaining (?.) with fallback values to prevent potential runtime errors when accessing data properties that might be undefined. This is a defensive programming approach that makes the code more robust.

frontend/server/api/project/[slug]/contributors/contributor-dependency.get.ts (2)

3-3: Appropriate import for the FilterGranularity enum.

Adding the FilterGranularity import supports the new default granularity parameter in the filter object.


37-46: Improved parameter extraction and query building.

These changes correctly:

  1. Extract the project identifier from the URL slug instead of relying on query parameters
  2. Add the project to the filter object
  3. Set a default granularity value using the FilterGranularity enum

This implementation aligns with the PR objective of ensuring all necessary parameters are included in API calls to Tinybird.

frontend/server/data/tinybird/contributors-dependency-data-source.ts (1)

39-46: Project parameter added correctly to the leaderboard filter.

Including the project identifier in the filter object ensures data is filtered properly when fetching from Tinybird.

frontend/server/api/project/[slug]/contributors/organization-dependency.get.ts (3)

2-2: Good addition of FilterGranularity import.

The addition of the FilterGranularity import aligns with the PR objective of properly supporting granularity parameters in the API endpoints. This typed enum provides better type safety compared to using string literals.


37-38: Good implementation of project extraction from URL.

Extracting the project identifier from the URL slug rather than query parameters is a good change. This approach is more REST-compliant and makes the API more consistent.


40-41: Properly updated filter with required parameters.

The changes appropriately add the required parameters to the filter object:

  1. The project property is now correctly set from the URL slug
  2. The granularity property has a sensible default value of FilterGranularity.QUARTERLY

These changes directly address the PR objective of fixing the missing parameters needed for TinyBird data fetching.

Copy link
Copy Markdown

@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

🧹 Nitpick comments (3)
frontend/server/data/tinybird/organizations-leaderboard-source.ts (1)

29-41: Consider addressing the security TODO comment

There's an existing TODO comment about unchecked query parameters being passed to TinyBird. While this PR is focused on adding missing parameters, it might be worth considering how to address this security concern in a follow-up PR.

// Potential approach to validate parameters:
const sanitizedQuery = {
  project: filter.project, // Ensure this is a valid project ID/slug
  repository: filter.repository ? sanitizeRepository(filter.repository) : undefined,
  startDate: filter.startDate, // DateTime objects are already validated
  endDate: filter.endDate,
};
frontend/server/data/tinybird/contributors-leaderboard-source.ts (1)

28-41: Consider addressing the security TODO comment

Similar to the organizations leaderboard file, there's an existing TODO comment about unchecked query parameters being passed to TinyBird. While this PR is focused on adding missing parameters, it might be worth considering how to address this security concern in a follow-up PR.

frontend/server/data/tinybird/contributors-dependency-data-source.ts (1)

55-56: Added optional chaining for safer property access.

The optional chaining improves robustness when accessing properties, preventing potential null/undefined errors.

Consider adding a check for empty data array before accessing indexed elements:

- const topContributorsCount = tinybirdTopContributorsResponse.data.length;
- const lastContributor = tinybirdTopContributorsResponse.data[topContributorsCount - 1];
- const topContributorsPercentage = lastContributor?.contributionPercentageRunningTotal || 0;
- const totalContributorCount = tinybirdTopContributorsResponse.data[0]?.totalContributorCount;
+ const topContributorsCount = tinybirdTopContributorsResponse.data.length;
+ const lastContributor = topContributorsCount > 0 ? tinybirdTopContributorsResponse.data[topContributorsCount - 1] : null;
+ const topContributorsPercentage = lastContributor?.contributionPercentageRunningTotal || 0;
+ const totalContributorCount = tinybirdTopContributorsResponse.data[0]?.totalContributorCount || 0;
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0875920 and af963d026752a68b24690e331f654fd45169f1d8.

📒 Files selected for processing (9)
  • frontend/server/api/project/[slug]/contributors/contributor-dependency.get.ts (2 hunks)
  • frontend/server/api/project/[slug]/contributors/contributor-leaderboard.get.ts (1 hunks)
  • frontend/server/api/project/[slug]/contributors/organization-dependency.get.ts (2 hunks)
  • frontend/server/api/project/[slug]/contributors/organization-leaderboard.get.ts (1 hunks)
  • frontend/server/data/tinybird/contributors-dependency-data-source.ts (2 hunks)
  • frontend/server/data/tinybird/contributors-leaderboard-source.ts (1 hunks)
  • frontend/server/data/tinybird/organizations-dependency-data-source.ts (2 hunks)
  • frontend/server/data/tinybird/organizations-leaderboard-source.ts (1 hunks)
  • frontend/server/data/types.ts (2 hunks)
🔇 Additional comments (17)
frontend/server/api/project/[slug]/contributors/contributor-leaderboard.get.ts (1)

38-41: Good implementation of project parameter extraction from URL path

The change correctly extracts the project identifier from the URL slug parameter rather than relying on the query string. This approach is more RESTful since the endpoint is already project-specific based on its URL structure.

For slightly improved type safety, consider using Nuxt's typed context params:

- const project = (event.context.params as { slug: string }).slug;
+ const { slug: project } = event.context.params as { slug: string };
frontend/server/api/project/[slug]/contributors/organization-leaderboard.get.ts (2)

37-38: Good implementation of project parameter extraction from URL path

The change correctly extracts the project identifier from the URL slug parameter rather than relying on the query string. This matches the approach used in the contributor-leaderboard.get.ts file, ensuring consistency across endpoints.

For slightly improved type safety, consider using Nuxt's typed context params:

- const project = (event.context.params as { slug: string }).slug;
+ const { slug: project } = event.context.params as { slug: string };

45-47: Correctly added project parameter to filter object

The addition of the project parameter to the filter object ensures that the query to TinyBird includes the necessary project context, aligning with the PR objective to fix missing query parameters.

frontend/server/data/tinybird/organizations-leaderboard-source.ts (1)

35-37: Good addition of project parameter to TinyBird query

The change correctly adds the project parameter to the organizations leaderboard query, ensuring that TinyBird receives all required parameters to filter the data properly. This aligns with the PR objective to address missing query parameters.

frontend/server/data/tinybird/contributors-leaderboard-source.ts (2)

34-36: Good addition of project parameter to TinyBird query

The change correctly adds the project parameter to the contributors leaderboard query, ensuring that TinyBird receives all required parameters to filter the data properly. This aligns with the PR objective to address missing query parameters.


60-67: Verify the hardcoded pagination values

The meta object contains hardcoded pagination values (offset: 0, limit: 10) which might not match the actual query parameters. Ensure these values are accurate or consider using the actual pagination values from the query:

  return {
    meta: {
-      offset: 0,
-      limit: 10,
+      offset: filter.offset || 0,
+      limit: filter.limit || 10,
      total: data.rows
    },
    data: processedData
  };
frontend/server/api/project/[slug]/contributors/contributor-dependency.get.ts (3)

3-3: Added necessary import for FilterGranularity.

The addition of FilterGranularity import supports the new granularity filter parameter.


37-38: Extracting project from URL slug instead of query parameter.

Good change - extracting the project from the URL slug makes the API more RESTful and consistent with path-based resource identification.


39-42: Added project and granularity to the filter object.

The changes properly include the project from URL slug and add granularity with a sensible default (QUARTERLY), which should help with getting the right data from TinyBird.

The comment in lines 28-33 still mentions project as a query parameter, but it's now extracted from the URL path. Consider updating the comment to reflect this change:

 /**
  * Query params:
  * - metric: 'all' | 'commits' | (see metrics options)
- * - project: string
  * - repository: string
  * - time-period: string
+ * - granularity: string (optional, defaults to 'quarterly')
  */
frontend/server/data/tinybird/contributors-dependency-data-source.ts (1)

39-41: Added project to leaderboardFilter.

Good addition of the project parameter to ensure consistent filtering across the application.

frontend/server/data/types.ts (2)

39-41: Added project field to ContributorsLeaderboardFilter type.

Good addition of the required project field to ensure type safety and consistency with implementation.


48-50: Added project field to OrganizationsLeaderboardFilter type.

Good addition of the required project field to ensure type safety and consistency with implementation.

frontend/server/api/project/[slug]/contributors/organization-dependency.get.ts (3)

2-2: Added necessary import for FilterGranularity.

The addition of FilterGranularity import supports the new granularity filter parameter.


37-38: Extracting project from URL slug instead of query parameter.

Good change - extracting the project from the URL slug makes the API more RESTful and consistent with path-based resource identification.


39-42: Added project and granularity to the filter object.

The changes properly include the project from URL slug and add granularity with a sensible default (QUARTERLY), which should help with getting the right data from TinyBird.

The comment in lines 28-33 still mentions project as a query parameter, but it's now extracted from the URL path. Consider updating the comment to reflect this change:

 /**
  * Query params:
  * - metric: 'all' | 'commits' | (see metrics options)
- * - project: string
  * - repository: string
  * - time-period: string
+ * - granularity: string (optional, defaults to 'quarterly')
  */
frontend/server/data/tinybird/organizations-dependency-data-source.ts (2)

41-41: Added important project parameter to the leaderboard filter.

This change addresses the main issue of missing query parameters mentioned in the PR objectives, ensuring that the project identifier is properly passed to the TinyBird data source for filtering organization data.


55-57: Improved error handling with optional chaining.

The refactored code properly handles potential undefined values in the TinyBird response using optional chaining (?.) and fallback values (|| 0). This defensive programming approach prevents potential runtime errors if the response data is empty or malformed.

@borfast borfast force-pushed the bugfix/fix-dependency-endpoints-not-sending-project branch 3 times, most recently from ff8d010 to 4ad66d1 Compare March 13, 2025 18:28
@borfast borfast closed this Mar 14, 2025
@borfast borfast force-pushed the bugfix/fix-dependency-endpoints-not-sending-project branch from 4ad66d1 to d603be0 Compare March 14, 2025 10:15
@borfast borfast deleted the bugfix/fix-dependency-endpoints-not-sending-project branch March 14, 2025 10:22
@borfast borfast assigned borfast and unassigned borfast May 2, 2025
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.

2 participants