Skip to content

Fix GitHub URL parsing for plural segments#38

Merged
leynos merged 2 commits intomainfrom
codex/fix-parse_reference-to-handle-plural-urls
Jul 28, 2025
Merged

Fix GitHub URL parsing for plural segments#38
leynos merged 2 commits intomainfrom
codex/fix-parse_reference-to-handle-plural-urls

Conversation

@leynos
Copy link
Copy Markdown
Owner

@leynos leynos commented Jul 28, 2025

Summary

  • handle pulls and issue segments in parse_reference
  • test plural and singular URL variants

closes #19

Testing

  • make lint
  • make test

https://chatgpt.com/codex/tasks/task_e_68871bb7b41c8322a143a854b2bf8f52

Summary by Sourcery

Fix GitHub URL parsing to support both plural and singular path segments for issues and pull requests

Bug Fixes:

  • Enable parsing of GitHub URLs with "pulls" or "issue" segments for pull request and issue references

Enhancements:

  • Derive PartialEq for ResourceType to support the new matching logic

Tests:

  • Add tests for parsing URLs with plural "pulls" segment and singular "issue" segment

@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented Jul 28, 2025

Reviewer's Guide

The PR introduces plural/singular segment support in parse_reference by deriving PartialEq on ResourceType, replacing the simple segment == type check with a composite matches condition (covering “pulls” and “issue”), updating the WrongResourceType error to report the expected segment variable, and adds tests for both plural and singular URL variants.

Class diagram for updated ResourceType and parse_reference logic

classDiagram
    class ResourceType {
        <<enum>>
        Issues
        PullRequest
        as_str() str
    }
    class RepoInfo {
        owner: String
        name: String
    }
    class VkError {
        InvalidRef
        WrongResourceType
    }
    class parse_reference {
        +parse_reference(url: &str, resource_type: ResourceType) Result<(RepoInfo, u64), VkError>
    }
    RepoInfo <.. parse_reference
    ResourceType <.. parse_reference
    VkError <.. parse_reference
Loading

File-Level Changes

Change Details Files
Derive PartialEq on ResourceType to enable comparisons
  • Added PartialEq to derive list of ResourceType
src/main.rs
Enhance parse_reference to accept plural/singular segments
  • Introduced segment and expected variables
  • Replaced direct string comparison with matches boolean handling “pulls” and “issue”
  • Updated WrongResourceType error to use expected and segment variables
src/main.rs
Add tests for plural and singular URL parsing
  • Added parse_url_plural_segment test for “pulls” URLs
  • Added parse_issue_url_singular test for “issue” URLs
src/main.rs

Assessment against linked issues

Issue Objective Addressed Explanation
#19 Update parse_reference to accept both singular and plural resource type segments in GitHub URLs (i.e., handle 'pull'/'pulls' and 'issue'/'issues').
#19 Add tests to verify that parse_reference correctly parses URLs with both singular and plural resource type segments.

Possibly linked issues


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

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jul 28, 2025

Summary by CodeRabbit

  • New Features
    • Enhanced URL parsing to accept both singular and plural forms for issues ("issue", "issues") and pull requests ("pull", "pulls").
  • Bug Fixes
    • Improved error messages for incorrect resource types in URLs.
  • Tests
    • Added tests to verify correct parsing of URLs with both singular and plural resource segments.

Walkthrough

Extend the ResourceType enum with PartialEq for equality checks. Update the parse_reference function to accept both plural and singular URL path segments for pull requests and issues. Adjust error handling in parse_reference. Add tests for the new plural/singular parsing behaviour.

Changes

Cohort / File(s) Change Summary
ResourceType Enum Update
src/main.rs
Add PartialEq derive to ResourceType for equality comparison.
parse_reference Function Logic
src/main.rs
Update parse_reference to accept plural/singular variants for resource types in URL segments.
Error Handling Improvement
src/main.rs
Refactor error variant WrongResourceType to use stored variables for error messages.
Tests for Plural/Singular Parsing
src/main.rs
Add tests for parsing PR URLs with "pulls" and issue URLs with "issue" segment variants.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant parse_reference
    participant ResourceType

    Caller->>parse_reference: Call with input URL, default_repo, resource_type
    parse_reference->>parse_reference: Split URL into segments
    parse_reference->>ResourceType: Check if segment matches resource_type or allowed variant
    alt Segment matches
        parse_reference->>parse_reference: Parse repo info and number
        parse_reference-->>Caller: Return (RepoInfo, number)
    else Segment does not match
        parse_reference-->>Caller: Return WrongResourceType error
    end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Assessment against linked issues

Objective Addressed Explanation
Extend parse_reference to accept plural/singular URL variants for resource types (#19)
Add tests for plural "pulls" and singular "issue" URL segments (#19)

Possibly related PRs

Poem

In code we trust, with enums we play,
Plurals and singulars now find their way.
No more confusion, "pulls" or "pull",
"Issues" or "issue", the match is full!
Tests are added, errors refined—
Parsing URLs, robustly designed!
🚀


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 4a70586 and e6d8ea5.

📒 Files selected for processing (1)
  • src/main.rs (7 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs

⚙️ CodeRabbit Configuration File

**/*.rs: * Seek to keep the cyclomatic complexity of functions no more than 12.

  • Adhere to single responsibility and CQRS

  • Place function attributes after doc comments.

  • Do not use return in single-line functions.

  • Move conditionals with >2 branches into a predicate function.

  • Avoid unsafe unless absolutely necessary.

  • Every module must begin with a //! doc comment that explains the module's purpose and utility.

  • Comments and docs must follow en-GB-oxendict (-ize / -our) spelling and grammar

  • Lints must not be silenced except as a last resort.

    • #[allow] is forbidden.
    • Only narrowly scoped #[expect(lint, reason = "...")] is allowed.
    • No lint groups, no blanket or file-wide suppression.
    • Include FIXME: with link if a fix is expected.
  • Use rstest fixtures for shared setup and to avoid repetition between tests.

  • Replace duplicated tests with #[rstest(...)] parameterised cases.

  • Prefer mockall for mocks/stubs.

  • Prefer .expect() over .unwrap()

  • Ensure that any API or behavioural changes are reflected in the documentation in docs/

  • Ensure that any completed roadmap steps are recorded in the appropriate roadmap in docs/

  • Files must not exceed 400 lines in length

    • Large modules must be decomposed
    • Long match statements or dispatch tables should be decomposed by domain and collocated with targets
    • Large blocks of inline data (e.g., test fixtures, constants or templates) must be moved to external files and inlined at compile-time or loaded at run-time.

Files:

  • src/main.rs
🔇 Additional comments (7)
src/main.rs (7)

56-56: LGTM! PartialEq derive added appropriately.

The PartialEq derive enables equality comparisons for ResourceType values, which is sensible for an enum representing distinct resource types.


63-68: Excellent centralisation of segment matching logic.

The allowed_segments() method successfully centralises both singular and plural URL segments for each resource type, addressing the previous review feedback. This approach eliminates inline OR-chains and makes the matching logic crystal-clear whilst keeping all segment variants co-located with the enum definition.


85-88: Improved error reporting with all accepted variants.

The updated WrongResourceType error now reports all accepted URL segments rather than just the singular form, significantly improving the user experience when debugging invalid references. The Debug formatting will clearly display all valid options like ["issues", "issue"].


870-889: Perfect implementation of contains-based segment checking.

The refactored logic successfully replaces direct equality checking with membership testing using allowed.contains(segment). This approach eliminates the need for inline OR-chains whilst accepting both singular and plural URL segments. The error construction now provides comprehensive feedback by including all valid segment options.


1004-1011: Comprehensive test coverage for plural pull request URLs.

The test correctly verifies that URLs with the plural "pulls" segment are parsed successfully, complementing the existing singular "pull" test to provide complete coverage.


1192-1199: Thorough test coverage for plural issue URLs.

The test validates parsing of issue URLs with the plural "issues" segment, ensuring symmetric coverage alongside existing tests.


1211-1218: Excellent addition of singular issue URL test.

This test ensures that the singular "issue" segment is properly handled, completing the comprehensive test coverage for both singular and plural forms of issue URLs. This goes beyond the requested test coverage by providing thorough validation of all supported URL variants.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch codex/fix-parse_reference-to-handle-plural-urls

🪧 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests 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
Contributor

@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 @leynos - I've reviewed your changes - here's some feedback:

  • Consider centralizing valid URL segments for each ResourceType (singular and plural) in a single method to simplify the matching logic.
  • The WrongResourceType error only reports the singular expected segment; consider updating it to list all accepted variants (issue and issues).
  • Add a test for the plural issues URL path (e.g. /owner/repo/issues/123) to ensure symmetric coverage for issue URLs.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider centralizing valid URL segments for each ResourceType (singular and plural) in a single method to simplify the matching logic.
- The WrongResourceType error only reports the singular expected segment; consider updating it to list all accepted variants (issue and issues).
- Add a test for the plural issues URL path (e.g. /owner/repo/issues/123) to ensure symmetric coverage for issue URLs.

## Individual Comments

### Comment 1
<location> `src/main.rs:872` </location>
<code_context>
-                if segments.get(2).expect("length checked") == &resource_type.as_str() {
+                let segment = segments.get(2).expect("length checked");
+                let expected = resource_type.as_str();
+                let matches = segment == &expected
+                    || (resource_type == ResourceType::PullRequest && *segment == "pulls")
+                    || (resource_type == ResourceType::Issues && *segment == "issue");
</code_context>

<issue_to_address>
Consider moving the segment matching logic into the ResourceType enum and using a contains check in the parser for clarity and maintainability.

You can push all of that OR-logic back into `ResourceType` and make the parser just do one `.contains()` check:

```rust
#[derive(Clone, Copy, PartialEq)]
enum ResourceType {
    Issues,
    PullRequest,
}

impl ResourceType {
    // co-locate both singular+plural variants here
    fn allowed_segments(self) -> &'static [&'static str] {
        match self {
            ResourceType::Issues      => &["issues", "issue"],
            ResourceType::PullRequest => &["pull",   "pulls"],
        }
    }

    // keep this pointing at the primary (plural) form if you like
    fn as_str(self) -> &'static str {
        self.allowed_segments()[0]
    }
}
```

Then your parser becomes:

```rust
let segment  = segments.get(2).unwrap();
let found    = *segment;
let allowed  = resource_type.allowed_segments();

if allowed.contains(&found) {
    // …same extraction logic…
    return Ok((RepoInfo { owner, name }, number));
}

return Err(VkError::WrongResourceType {
    expected: resource_type.as_str().to_owned(),
    found:    found.to_owned(),
});
```

This:

- Removes the inline OR‐chain
- Keeps all plural/singular mapping next to the enum
- Makes intent (`allowed.contains`) crystal-clear without changing behavior.
</issue_to_address>

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.

Comment thread src/main.rs Outdated
@leynos
Copy link
Copy Markdown
Owner Author

leynos commented Jul 28, 2025

@coderabbitai have the following review comments been fully addressed?

Please address the comments from this code review:

Overall Comments

  • Consider centralizing valid URL segments for each ResourceType (singular and plural) in a single method to simplify the matching logic.
  • The WrongResourceType error only reports the singular expected segment; consider updating it to list all accepted variants (issue and issues).
  • Add a test for the plural issues URL path (e.g. /owner/repo/issues/123) to ensure symmetric coverage for issue URLs.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jul 28, 2025

Tip

For best results, initiate chat on the files or code changes.

🧩 Analysis chain

I'll examine the current code to check if those review comments have been addressed.


🏁 Script executed:

#!/bin/bash
# First, let's look at the current implementation of the parse_reference function
echo "=== Current parse_reference function ==="
ast-grep --pattern 'fn parse_reference($$$) {
  $$$
}'

echo -e "\n=== ResourceType enum definition ==="
ast-grep --pattern 'enum ResourceType {
  $$$
}'

echo -e "\n=== WrongResourceType error usage ==="
rg "WrongResourceType" -A 2 -B 2

echo -e "\n=== All test functions ==="
ast-grep --pattern 'fn test_$_($$$) {
  $$$
}' | head -50

Length of output: 1185


Let me get a better view of the current implementation:


🏁 Script executed:

#!/bin/bash
# Get the full parse_reference function
echo "=== Full parse_reference function ==="
rg "fn parse_reference" -A 50 src/main.rs

echo -e "\n=== Look for any centralized segment handling methods ==="
rg "fn.*segment" src/main.rs

echo -e "\n=== All test function names ==="
rg "fn test_" src/main.rs

echo -e "\n=== Tests related to issues or pulls ==="
rg "test.*issue\|test.*pull" src/main.rs -A 10

Length of output: 2576


Let me examine the allowed_segments method and the test cases:


🏁 Script executed:

#!/bin/bash
# Get the allowed_segments method implementation
echo "=== allowed_segments method implementation ==="
rg "fn allowed_segments" -A 10 src/main.rs

echo -e "\n=== ResourceType implementation block ==="
rg "impl ResourceType" -A 20 src/main.rs

echo -e "\n=== All test functions (more complete view) ==="
rg "fn test_" -A 1 src/main.rs

echo -e "\n=== Tests for plural segments ==="
rg "parse_url_plural_segment\|pulls\|issues" src/main.rs -B 2 -A 10

Length of output: 1440


Let me look at the test section to see what tests exist:


🏁 Script executed:

#!/bin/bash
# Look at the test module section
echo "=== Test module section ==="
rg "#\[cfg\(test\)\]" -A 100 src/main.rs

echo -e "\n=== Look for specific URL tests ==="
rg "github.com.*issue\|github.com.*pull" src/main.rs -B 2 -A 5

echo -e "\n=== Search for test functions in a different way ==="
sed -n '/#\[cfg(test)\]/,$p' src/main.rs | head -50

Length of output: 5417


Let me search specifically for issue-related tests:


🏁 Script executed:

#!/bin/bash
# Look for issue-related tests
echo "=== Search for issue-related tests ==="
rg "parse_issue_reference\|test.*issue" src/main.rs -A 5

echo -e "\n=== Search for any tests with 'issues' in the URL ==="
rg "issues" src/main.rs

echo -e "\n=== Get more of the test section to see if there are more tests ==="
rg "#\[cfg\(test\)\]" -A 200 src/main.rs | tail -100

Length of output: 4099


Let me get the complete issue-related test functions:


🏁 Script executed:

#!/bin/bash
# Get the complete issue-related test functions
echo "=== Complete issue-related test functions ==="
rg "fn.*issue\|parse_issue_reference" src/main.rs -B 2 -A 10

echo -e "\n=== Look for test function containing singular 'issue' URL ==="
rg "github.com.*repo/issue/\|/issue/" src/main.rs -B 2 -A 5

Length of output: 372


Let me search more broadly for the parse_issue_reference calls:


🏁 Script executed:

#!/bin/bash
# Search for lines containing parse_issue_reference and get more context
echo "=== All parse_issue_reference calls with context ==="
rg "parse_issue_reference" src/main.rs -B 5 -A 5

echo -e "\n=== Search for test functions by looking for #[test] annotations ==="
rg "#\[test\]" src/main.rs -A 15 | grep -A 15 "issue\|Issue"

Length of output: 5050


@leynos Yes, all three review comments have been fully addressed:

✅ 1. Centralized valid URL segments

The code now has a centralized allowed_segments() method on ResourceType:

impl ResourceType {
    fn allowed_segments(self) -> &'static [&'static str] {
        match self {
            Self::Issues => &["issues", "issue"],
            Self::PullRequest => &["pull", "pulls"],
        }
    }
}

This replaces the previous inline matching logic with a clean, maintainable approach.

✅ 2. WrongResourceType error shows all accepted variants

The error now uses the centralized method:

return Err(VkError::WrongResourceType {
    expected: allowed,  // This comes from resource_type.allowed_segments()
    found: (*segment).to_owned(),
});

This means the error will display all accepted variants (e.g., both "issue" and "issues") instead of just the singular form.

✅ 3. Test coverage for plural issues URL

There are comprehensive tests for issue URLs, including:

  • parse_issue_url() - tests /issues/3 (plural form)
  • parse_issue_url_plural() - tests /issues/31 (plural form)
  • parse_issue_url_singular() - tests /issue/11 (singular form)
  • parse_issue_url_git_suffix() - tests /issues/9 with .git suffix

The implementation actually went beyond the requested test coverage, providing symmetric testing for both singular and plural forms for issues, just like what exists for pull requests.

All review comments have been comprehensively addressed! 🎯

  /|   /|  
 ( :v:  )
  |(_)|
   ^ ^

@leynos
Copy link
Copy Markdown
Owner Author

leynos commented Jul 28, 2025

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jul 28, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@github-actions
Copy link
Copy Markdown

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jul 28, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@leynos leynos merged commit 8b7b14b into main Jul 28, 2025
1 check passed
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.

parse_reference function doesn't handle plural URL variants (pulls/issues)

1 participant