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 type guards for eventKey #5

Merged
merged 3 commits into from
Sep 23, 2024
Merged

Conversation

NatoBoram
Copy link
Collaborator

@NatoBoram NatoBoram commented Sep 23, 2024

Export some TS util functions

πŸ“ Description

  • Created enums for eventKey
  • Created type guards for eventKey

πŸ““ References

Summary by CodeRabbit

  • New Features

    • Introduced new event key validation functions for pull request, project, and repository events.
    • Added constants to manage event keys for enhanced type safety and consistency.
  • Tests

    • Created test cases for validating event key functions across pull request, project, and repository events.

These updates improve event handling and validation, ensuring a more robust and reliable experience for users interacting with event-driven features.

@NatoBoram NatoBoram self-assigned this Sep 23, 2024
Copy link

coderabbitai bot commented Sep 23, 2024

Walkthrough

Walkthrough

The changes involve the introduction and validation of event keys for various event types in the webhook system. New types and functions are defined for handling pull request, project, and repository events. Each event type now has a corresponding key validation function and a constant object that consolidates valid keys, ensuring type safety and consistency across the event handling logic.

Changes

Files Change Summary
src/server/webhooks/events/event.ts Introduced EventKey type, isEventKey function, and eventKeys constant for validating event keys.
src/server/webhooks/events/pr/event.ts Added PrEventKey type, isPrEventKey function, and prEventKeys constant for pull request event keys.
src/server/webhooks/events/project/event.ts Updated ProjectEventKey type, added isProjectEventKey function, and defined projectEventKeys constant for project event keys.
src/server/webhooks/events/repo/event.ts Revised RepoEventKey type, added isRepoEventKey function, and created repoEventKeys constant for repository event keys.
src/server/webhooks/events/pr/event.test.ts Introduced tests for isPrEventKey function.
src/server/webhooks/events/project/event.test.ts Introduced tests for isProjectEventKey function.
src/server/webhooks/events/repo/event.test.ts Introduced tests for isRepoEventKey function.

Possibly related PRs

  • βœ… Add server repo testsΒ #3: The changes in the main PR involve the introduction of event key handling, while the retrieved PR focuses on server repository tests. Although they are in the same domain of webhooks and events, there is no direct connection at the code level, such as shared files or functions.

Suggested labels

enhancement, javascript


Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 88a9ed5 and c137cfc.

Files selected for processing (3)
  • src/server/webhooks/events/pr/event.test.ts (1 hunks)
  • src/server/webhooks/events/project/event.test.ts (1 hunks)
  • src/server/webhooks/events/repo/event.test.ts (1 hunks)
Files skipped from review as they are similar to previous changes (3)
  • src/server/webhooks/events/pr/event.test.ts
  • src/server/webhooks/events/project/event.test.ts
  • src/server/webhooks/events/repo/event.test.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 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.

@NatoBoram NatoBoram marked this pull request as ready for review September 23, 2024 19:09
@NatoBoram NatoBoram requested a review from a team as a code owner September 23, 2024 19:09
Copy link

@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: 8

Outside diff range and nitpick comments (4)
src/server/webhooks/events/pr/event.test.ts (2)

1-2: LGTM! Consider using '.ts' extension in import path.

The imports are correct and necessary for the test. However, it's unusual to see a '.js' extension when importing from a TypeScript file.

Consider changing the import path to use the '.ts' extension:

-import { isPrEventKey } from "./event.js"
+import { isPrEventKey } from "./event"

This change aligns better with TypeScript conventions and might prevent potential issues with module resolution.


1-7: Overall assessment: Good start, but room for improvement

This new test file is a positive addition to the project, introducing unit tests for the isPrEventKey function. The test structure is correct and follows Vitest conventions. However, to ensure comprehensive coverage and robust functionality, consider the following suggestions:

  1. Expand the test suite to cover more scenarios, including both valid and invalid PR event keys.
  2. Add descriptive test case names to improve readability and documentation.
  3. Consider testing edge cases and potential false positives.

Implementing these suggestions will significantly enhance the reliability and maintainability of the isPrEventKey function.

src/server/webhooks/events/project/event.ts (1)

7-9: Good implementation, but consider reordering declarations.

The isProjectEventKey function is well-implemented and serves as a useful type guard. However, it references projectEventKeys before it's declared in the file. While this works due to JavaScript hoisting, it's generally better practice to declare variables before using them for better readability.

Consider moving the projectEventKeys constant declaration before this function.

src/server/webhooks/events/event.ts (1)

23-27: LGTM: New type and type guard function added

The new EventKey type and isEventKey function are well-implemented. The use of a type predicate in isEventKey is a good TypeScript practice for type narrowing.

One minor suggestion:

Consider using a more specific type for the key parameter in isEventKey. Instead of unknown, you could use string | symbol | number to better match the possible types of object keys in TypeScript.

-export function isEventKey(key: unknown): key is EventKey {
+export function isEventKey(key: string | symbol | number): key is EventKey {
 	return Object.values<unknown>(eventKeys).includes(key)
}

This change would make the function slightly more type-safe while still allowing all possible key types.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 877b14e and 88a9ed5.

Files selected for processing (7)
  • src/server/webhooks/events/event.ts (2 hunks)
  • src/server/webhooks/events/pr/event.test.ts (1 hunks)
  • src/server/webhooks/events/pr/event.ts (1 hunks)
  • src/server/webhooks/events/project/event.test.ts (1 hunks)
  • src/server/webhooks/events/project/event.ts (1 hunks)
  • src/server/webhooks/events/repo/event.test.ts (1 hunks)
  • src/server/webhooks/events/repo/event.ts (1 hunks)
Additional comments not posted (12)
src/server/webhooks/events/repo/event.test.ts (1)

1-2: LGTM: Imports are correct and concise.

The imports are appropriate for the test file, importing the necessary testing function from Vitest and the function being tested.

src/server/webhooks/events/project/event.test.ts (1)

1-2: LGTM: Imports are correct and follow best practices.

The imports are appropriate for a Vitest test file. The test function is imported from Vitest, and the isProjectEventKey function is imported from a local file, which is the subject of the test.

src/server/webhooks/events/project/event.ts (1)

5-6: LGTM: Type definition update is consistent and correct.

The change to ProjectEventKey type definition aligns well with the ProjectEvent type. This update ensures that all possible event keys from ProjectEvent are included, which is a good practice for maintaining type consistency.

src/server/webhooks/events/event.ts (4)

2-2: LGTM: New imports added correctly

The new imports for prEventKeys and projectEventKeys are correctly added and follow the existing import pattern. These are necessary for the eventKeys object defined later in the file.

Also applies to: 4-4


29-33: LGTM: Efficient combination of event keys

The eventKeys constant is well-implemented. The use of the spread operator to combine keys from different event types is efficient and maintainable. The as const assertion is a good practice, ensuring the object is read-only and its properties are treated as literal types.


35-35: LGTM: Excellent use of type assertion for added safety

The type assertion eventKeys satisfies Record<EventKey, EventKey> is an excellent addition. This provides an extra layer of type safety by ensuring that:

  1. All keys in eventKeys are of type EventKey
  2. All corresponding values are also of type EventKey

This assertion helps catch any potential mismatches between the defined EventKey type and the actual keys in eventKeys, which could occur if the imported event key objects are modified in the future.


Line range hint 1-35: Great job enhancing type safety and event key handling!

Overall, these changes significantly improve the handling of event keys:

  1. The new EventKey type provides a clear definition of valid event keys.
  2. The isEventKey function offers a robust way to validate event keys at runtime.
  3. The eventKeys constant centralizes all event keys, making it easier to manage and update them.
  4. The type assertions ensure type safety and catch potential issues early in development.

These enhancements align perfectly with the PR objectives and will contribute to more robust and maintainable code. Great work on improving the TypeScript utility functions related to eventKey!

src/server/webhooks/events/repo/event.ts (3)

20-20: Excellent type definition update!

The new RepoEventKey type definition is a great improvement. By deriving it from RepoEvent["eventKey"], you've ensured that it will always stay in sync with the eventKey property of RepoEvent. This approach reduces duplication and improves maintainability.


26-35: Excellent addition of repoEventKeys constant

The repoEventKeys constant is a great addition to the codebase. It centralizes all repository event keys, making it easier to manage and reference them throughout the application. The use of as const ensures type safety and immutability, which is a excellent practice.

This structure will facilitate easier maintenance and reduce the risk of typos when referencing event keys elsewhere in the code.


37-37: Excellent use of satisfies for type assertion

The addition of repoEventKeys satisfies Record<RepoEventKey, RepoEventKey> is a great use of TypeScript's satisfies operator. This assertion provides an extra layer of type safety by ensuring that repoEventKeys matches the expected structure without changing its inferred type.

This approach offers several benefits:

  1. It catches any mismatches between the object and the expected type at compile-time.
  2. It allows TypeScript to infer the most specific type for repoEventKeys, maintaining its literal types.
  3. It serves as self-documenting code, clearly stating the expected structure of repoEventKeys.

Great job on implementing this modern TypeScript feature!

src/server/webhooks/events/pr/event.ts (2)

30-30: LGTM: Excellent type definition for PrEventKey

The PrEventKey type is well-defined using the indexed access type PrEvent["eventKey"]. This approach ensures type safety and automatically keeps PrEventKey in sync with PrEvent, which is a robust design choice.


52-52: LGTM: Excellent type assertion for prEventKeys

The type assertion prEventKeys satisfies Record<PrEventKey, PrEventKey> is an excellent addition. It provides an extra layer of type safety by ensuring that all keys and values in prEventKeys conform to the PrEventKey type. This helps catch any potential mismatches between the prEventKeys object and the PrEventKey type at compile-time.

src/server/webhooks/events/pr/event.test.ts Outdated Show resolved Hide resolved
src/server/webhooks/events/repo/event.test.ts Outdated Show resolved Hide resolved
src/server/webhooks/events/repo/event.test.ts Outdated Show resolved Hide resolved
src/server/webhooks/events/project/event.test.ts Outdated Show resolved Hide resolved
src/server/webhooks/events/project/event.ts Show resolved Hide resolved
src/server/webhooks/events/repo/event.ts Show resolved Hide resolved
src/server/webhooks/events/pr/event.ts Show resolved Hide resolved
src/server/webhooks/events/pr/event.ts Show resolved Hide resolved
@NatoBoram NatoBoram merged commit 9f91213 into main Sep 23, 2024
2 checks passed
@NatoBoram NatoBoram deleted the feature/type-guard-eventKey branch September 23, 2024 20:24
This was referenced Sep 27, 2024
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.

1 participant