Skip to content

Conversation

@cernymatej
Copy link
Collaborator

@cernymatej cernymatej commented Oct 15, 2024

This PR brings a small refactor to the original code and implements spam detection.
It was necessary to downgrade nuxt hub to 0.7.26 because of an issue explained here: #32

I optimized how the issue body is sent to the AI model - previously, the AI wouldn't process anything if the issue had more than 200 words. Now, unnecessary information like the environment, logs, etc., is stripped away, and the total content length is capped at 5000 characters. (This might be too much, not sure)

I've tested it on a couple of issues, and it seems to work reasonably well.

One potential enhancement to optimize AI requests could be to store the number of spam issues created by a certain user in a KV. If that count hits a certain threshold within 10 minutes or so, we could automatically consider every subsequent issue as spam without sending the contents to the LLM (with an expiry date, of course).

Btw. Would it be worth maybe generating the json schema from the zod schema using something like https://www.npmjs.com/package/zod-to-json-schema, for example?

Summary by CodeRabbit

  • New Features

    • Introduced enhanced validation for GitHub webhook events, improving data integrity and processing.
    • Added new functions for normalizing issue content and language codes.
    • Expanded conditions for labeling issues based on specific types.
    • Added new environment variable for handling spam issues.
    • Introduced a new configuration property for managing target repository node ID.
  • Bug Fixes

    • Improved error handling with more specific messages for webhook processing failures.
  • Documentation

    • Enhanced schema properties with comments for better clarity and understanding.
  • Chores

    • Updated dependencies to ensure compatibility and stability.

@coderabbitai
Copy link

coderabbitai bot commented Oct 15, 2024

Walkthrough

The changes include updates to the package.json file, where the version of the @nuxthub/core package was downgraded and a new dependency, zod, was added. The webhook.post.ts file was significantly enhanced to improve validation and processing of GitHub webhook events, introducing new schemas and enums for better structure and error handling. Additionally, the normalization.ts file added two functions for normalizing issue content and language codes, which are now available for export. The .env.example and nuxt.config.ts files were also updated to include new environment variable declarations and configuration properties.

Changes

File Change Summary
package.json - Downgraded @nuxthub/core from ^0.7.32 to ^0.7.26
- Added zod with version ^3.23.8
- Reformatted nuxt-webhook-validators with a trailing comma
server/api/webhook.post.ts - Enhanced webhook handling with validation using zod
- Introduced new enums: IssueLabel, IssueType
- Added schemas: githubWebhookSchema, aiResponseSchema, analyzedIssueSchema
- Improved error handling and labeling logic
server/utils/normalization.ts - Added getNormalizedIssueContent for processing issue text
- Added getNormalizedLanguage for normalizing language codes
.env.example - Added variable NUXT_GITHUB_TARGET_REPOSITORY_NODE_ID
- Added comments for NUXT_GITHUB_TOKEN
nuxt.config.ts - Added property targetRepositoryNodeId in runtimeConfig.github

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant WebhookHandler
    participant Validator
    participant Normalizer
    participant ResponseHandler

    Client->>WebhookHandler: Sends GitHub webhook event
    WebhookHandler->>Validator: Validate incoming data
    Validator-->>WebhookHandler: Validated data
    WebhookHandler->>Normalizer: Normalize issue content
    Normalizer-->>WebhookHandler: Normalized content
    WebhookHandler->>ResponseHandler: Process and respond to event
    ResponseHandler-->>Client: Send response
Loading

🐰 In the garden where changes bloom,
New dependencies chase away the gloom.
With validation strong and errors few,
We hop along with a clearer view!
🥕 Let's celebrate this code so bright,
For every bug, we’ll fix it right! 🌟


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 2b564c3 and 39816c5.

📒 Files selected for processing (2)
  • server/api/webhook.post.ts (4 hunks)
  • server/utils/normalization.ts (1 hunks)

Note

🎁 Summarized by CodeRabbit Free

Your organization is on the Free plan. CodeRabbit will generate a high-level summary and a walkthrough for each pull request. For a comprehensive line-by-line review, please upgrade your subscription to CodeRabbit Pro by visiting https://app.coderabbit.ai/login.

🪧 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.

# Conflicts:
#	package.json
#	pnpm-lock.yaml
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Oct 15, 2024

Deploying carpenter-uh62 with  Cloudflare Pages  Cloudflare Pages

Latest commit: 39816c5
Status: ✅  Deploy successful!
Preview URL: https://dc7877ed.carpenter-uh62.pages.dev
Branch Preview URL: https://feat-spam-detection.carpenter-uh62.pages.dev

View logs

@cernymatej cernymatej requested a review from danielroe October 15, 2024 16:23
@danielroe danielroe changed the title feat: Add spam detection feat: add spam detection Oct 15, 2024
Comment on lines 156 to 169
enum IssueLabel {
NeedsReproduction = 'needs reproduction',
PossibleRegression = 'possible regression',
Nitro = 'nitro',
Documentation = 'documentation',
PossibleSpam = 'spam',
}

enum IssueType {
Bug = 'bug',
Feature = 'feature',
Documentation = 'documentation',
Spam = 'spam',
}
Copy link
Owner

Choose a reason for hiding this comment

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

I'm not a massive fan of enums but willing to give it a go

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Why not? 👀

})).optional(),
})

// TODO: generate AI model schema from this?
Copy link
Owner

Choose a reason for hiding this comment

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

or maybe do it the other way - generate the zod schema from the json schema above?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That's a good idea, but I'm not sure how we would create the transforms then. 🤔 They're there to provide some recovery from a non-optimal AI response.
Sometimes, it would respond with null for the boolean fields and also invalid language codes, which would then cause an error in the translation prompt.

I liked the idea of transforming the data using zod so that we don't have to think about it further down in the code.

@danielroe
Copy link
Owner

now I think I just need to check that the token carpenter has is able to transfer issues ... it probably only has permissions right now to read/write issues ...

Copy link
Owner

@danielroe danielroe left a comment

Choose a reason for hiding this comment

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

beautiful work - thank you ❤️

now I think it's time to try in production

@danielroe danielroe merged commit 12df9fb into main Oct 16, 2024
6 checks passed
@danielroe danielroe deleted the feat-spam-detection branch October 16, 2024 09:23
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.

3 participants