Skip to content

Potential fixes for 2 code quality findings#61

Merged
PythonSmall-Q merged 4 commits intomasterfrom
ai-findings-autofix/Source-index.ts
Mar 20, 2026
Merged

Potential fixes for 2 code quality findings#61
PythonSmall-Q merged 4 commits intomasterfrom
ai-findings-autofix/Source-index.ts

Conversation

@langningchen
Copy link
Member

@langningchen langningchen commented Mar 19, 2026

This PR applies 2/5 suggestions from code quality AI findings. 3 suggestions were skipped to avoid creating conflicts.

Summary by Sourcery

Improve Cloudflare Worker type safety and harden username parsing from profile pages.

Bug Fixes:

  • Prevent ParseUsernameFromProfile from throwing when the expected user_id pattern or closing quote is missing, returning an empty string instead.

Enhancements:

  • Type the Cloudflare Workers AI binding using the Ai type from @cloudflare/workers-types instead of any to strengthen environment typing.

Summary by cubic

Typed the Cloudflare Workers AI binding using Ai from @cloudflare/workers-types and hardened username parsing to prevent runtime errors.

  • Bug Fixes

    • ParseUsernameFromProfile now returns an empty string when user_id= or the closing quote is missing instead of throwing.
  • Refactors

    • Changed AI: any to AI: Ai in Environment and used import type from @cloudflare/workers-types to improve type safety and avoid bundling types.

Written for commit 9e0a888. Summary will update on new commits.

langningchen and others added 2 commits March 20, 2026 06:22
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
Signed-off-by: Langning Chen <chenlangning2009@outlook.com>
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
Signed-off-by: Langning Chen <chenlangning2009@outlook.com>
@sourcery-ai
Copy link

sourcery-ai bot commented Mar 19, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Typed the Cloudflare AI environment binding using the Ai type from @cloudflare/workers-types and made username parsing from profile pages more defensive against missing or malformed user_id data to avoid runtime errors.

Class diagram for updated Environment interface and ParseUsernameFromProfile function

classDiagram
    class Environment {
      +API_TOKEN string
      +CaptchaSiteKey string
      +Hostname string
      +KV KVNamespace
      +CaptchaTrustScoreThreshold number
      +CloudflareTurnstileOn string
      +CaptchaSecretKey string
      +DB D1Database
      +logdb AnalyticsEngineDataset
      +AI Ai
      +NOTIFICATIONS DurableObjectNamespace
      +NOTIFICATION_PUSH_TOKEN string
    }

    class ParseUsernameFromProfile {
      +ParseUsernameFromProfile(profilePage string) string
    }
Loading

File-Level Changes

Change Details Files
Strengthened typing for the Cloudflare AI environment binding.
  • Imported the Ai type from @cloudflare/workers-types alongside existing Cloudflare worker types.
  • Updated the Environment interface to type the AI field as Ai instead of any to improve type safety and editor support.
Source/index.ts
Hardened username extraction logic from profile HTML.
  • Replaced direct substring operations that assumed user_id was always present and properly quoted with guarded index checks.
  • Added early returns to safely handle missing user_id= and missing closing quote, returning an empty string instead of throwing.
  • Refactored the parsing logic to compute the start index using the length of the user_id= token and operate on the remaining substring for clarity.
Source/index.ts

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. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the 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 exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

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

@langningchen langningchen marked this pull request as ready for review March 19, 2026 22:23
Copilot AI review requested due to automatic review settings March 19, 2026 22:23
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 - I've left some high level feedback:

  • In ParseUsernameFromProfile, consider handling both single- and double-quoted user_id attributes (or using a small regex) so the parser is resilient to minor HTML/template changes.
  • You might want to extract the "user_id=" literal into a constant (e.g., const USER_ID_PREFIX = 'user_id=';) to avoid magic strings and make future adjustments to the profile markup easier.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `ParseUsernameFromProfile`, consider handling both single- and double-quoted `user_id` attributes (or using a small regex) so the parser is resilient to minor HTML/template changes.
- You might want to extract the `"user_id="` literal into a constant (e.g., `const USER_ID_PREFIX = 'user_id=';`) to avoid magic strings and make future adjustments to the profile markup easier.

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.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses two code-quality findings by tightening Cloudflare Workers AI binding typing and making profile-page username parsing more defensive to avoid malformed input causing incorrect parsing behavior.

Changes:

  • Type the Workers AI environment binding as Ai (from @cloudflare/workers-types) instead of any.
  • Harden ParseUsernameFromProfile to return "" when user_id= or the expected closing quote is missing.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link

@cubic-dev-ai cubic-dev-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.

No issues found across 1 file


Since this is your first cubic review, here's how it works:

  • cubic automatically reviews your code and comments on bugs and improvements
  • Teach cubic by replying to its comments. cubic learns from your replies and gets better over time
  • Add one-off context when rerunning by tagging @cubic-dev-ai with guidance or docs links (including llms.txt)
  • Ask questions if you need clarification on any suggestion

@PythonSmall-Q
Copy link
Member

@langningchen maybe you can consider the suggestions given by Copilot?

langningchen and others added 2 commits March 20, 2026 16:29
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Langning Chen <chenlangning2009@outlook.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Langning Chen <chenlangning2009@outlook.com>
@langningchen
Copy link
Member Author

@PythonSmall-Q Done

@PythonSmall-Q PythonSmall-Q requested a review from boomzero March 20, 2026 12:10
@boomzero
Copy link
Member

Merge?

@langningchen
Copy link
Member Author

OK 但是我好久都没有 merge 过 PR,我不知道在 XMOJ-Script 应该按照什么规范做了(

@PythonSmall-Q PythonSmall-Q merged commit 1771d77 into master Mar 20, 2026
7 checks passed
@PythonSmall-Q PythonSmall-Q deleted the ai-findings-autofix/Source-index.ts branch March 20, 2026 13:13
@boomzero
Copy link
Member

OK 但是我好久都没有 merge 过 PR,我不知道在 XMOJ-Script 应该按照什么规范做了(

有啥规范(

@langningchen
Copy link
Member Author

OK 但是我好久都没有 merge 过 PR,我不知道在 XMOJ-Script 应该按照什么规范做了(

有啥规范(

例如应该使用 Squash Merge 还是 Commit Merge,Merge 日志应该怎么写,Status Check 状态应该怎么样等等……

@boomzero
Copy link
Member

OK 但是我好久都没有 merge 过 PR,我不知道在 XMOJ-Script 应该按照什么规范做了(

有啥规范(

例如应该使用 Squash Merge 还是 Commit Merge,Merge 日志应该怎么写,Status Check 状态应该怎么样等等……

On this repo do whatever you want, on XMOJ-Script use merge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants