Repository navigation
fix(gitlab): restore ticket context in reviews - #2802
IsmaelMartinez merged 3 commits into
Conversation
PR Summary by QodoFix GitLab ticket context extraction for reviews
AI Description
Diagram
High-Level Assessment
Files changed (2)
|
Code Review by Qodo
1. Bypass GitLab project resolver
|
| project = git_provider.gl.projects.get(project_path) | ||
| issue = project.issues.get(issue_iid) | ||
| except Exception as e: | ||
| get_logger().error( |
There was a problem hiding this comment.
1. Bypass gitlab project resolver 🐞 Bug ≡ Correctness
extract_tickets() fetches GitLab issues via git_provider.gl.projects.get(project_path), bypassing GitLabProvider._project_by_path() which exists to handle encoded-path resolution and safer lookup strategies. This can cause valid GitLab issue references to be skipped at runtime (ticket context missing) when raw projects.get(project_path) does not resolve correctly for a given project path.
Agent Prompt
## Issue description
The GitLab ticket extraction path resolves projects using `git_provider.gl.projects.get(project_path)` directly, which is less robust than the provider’s existing `_project_by_path()` helper.
## Issue Context
`GitLabProvider` already implements `_project_by_path()` to handle multiple strategies (including URL-encoded paths and safer fallback behavior). The new ticket extraction should use that same resolver to avoid failing to fetch issues for valid project paths.
## Fix Focus Areas
- pr_agent/tools/ticket_pr_compliance_check.py[498-514]
- pr_agent/git_providers/gitlab_provider.py[243-283]
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
IsmaelMartinez
left a comment
There was a problem hiding this comment.
The fix is right and the coverage is genuine: all three reference forms go red on main without the source change. One change requested before merge: the URL branch of the new regex is quadratic on adversarial input, measured at 38 s for a 1 MB description (GitLab's documented cap) on input the MR author controls; details and a committable rewrite are inline. On Qodo's resolver finding: python-gitlab already URL-encodes string project IDs via EncodedId, and _project_by_path is the submodule resolver that swallows errors and returns None, so the direct projects.get call with logged errors looks right; no change needed there. Thanks @Fnine59 for the fast turnaround with tests included.
| GITLAB_TICKET_PATTERN = re.compile( | ||
| r"(?P<url>https?://[^\s<>()]+?/-/issues/(?P<url_issue>\d+))(?=$|[^\w])" | ||
| r"|(?<![\w./-])(?P<project>[\w.-]+(?:/[\w.-]+)+)#(?P<project_issue>\d+)\b" | ||
| r"|(?<![\w/])#(?P<local_issue>\d+)\b" | ||
| ) |
There was a problem hiding this comment.
The URL branch is quadratic: the lazy [^\s<>()]+? rescans to end-of-input from every https:// start. Measured ~4x per input doubling: 150 ms at 64 KB, 38 s at 1 MB. The rewrite below matches any URL greedily (linear, 0.3 ms at 64 KB) and moves the issue check onto the parsed path. Output matches on 27 realistic cases (trailing punctuation, backtick and bold wrappers, subpath installs, ports, #note_ fragments), and the three divergences are deliberate: comma-joined URL lists no longer yield a garbage project path, zero-padded iids resolve, and trailing text glued to the iid no longer counts. Your three tests stay green.
| GITLAB_TICKET_PATTERN = re.compile( | |
| r"(?P<url>https?://[^\s<>()]+?/-/issues/(?P<url_issue>\d+))(?=$|[^\w])" | |
| r"|(?<![\w./-])(?P<project>[\w.-]+(?:/[\w.-]+)+)#(?P<project_issue>\d+)\b" | |
| r"|(?<![\w/])#(?P<local_issue>\d+)\b" | |
| ) | |
| GITLAB_TICKET_PATTERN = re.compile( | |
| r"(?P<url>https?://[^\s<>(),;]+)" | |
| r"|(?<![\w./-])(?P<project>[\w.-]+(?:/[\w.-]+)+)#(?P<project_issue>\d+)\b" | |
| r"|(?<![\w/])#(?P<local_issue>\d+)\b" | |
| ) | |
| GITLAB_ISSUE_PATH_PATTERN = re.compile(r"/-/issues/(?P<iid>\d+)(?=/|$)") |
There was a problem hiding this comment.
Addressed in 65e0af41: URL matching now uses a greedy, delimiter-bounded token and leaves issue extraction to a separate path pattern. Added boundary regressions; the focused GitLab tests pass (9/9), the related ticket suite passes (157 passed, 1 xfailed), and the full unit suite passes (2097 passed, 1 skipped, 1 xfailed).
| if match.group("url"): | ||
| try: | ||
| parsed_ticket_url = urlparse(match.group("url")) | ||
| except ValueError: | ||
| continue | ||
| if not provider_host or parsed_ticket_url.hostname != provider_host: | ||
| continue | ||
|
|
||
| ticket_path = parsed_ticket_url.path | ||
| if provider_base_path: | ||
| if not ticket_path.startswith(f"{provider_base_path}/"): | ||
| continue | ||
| ticket_path = ticket_path[len(provider_base_path):] | ||
|
|
||
| issue_iid = int(match.group("url_issue")) | ||
| issue_suffix = f"/-/issues/{issue_iid}" | ||
| if not ticket_path.endswith(issue_suffix): | ||
| continue | ||
| issue_project = ticket_path[:-len(issue_suffix)].strip("/") |
There was a problem hiding this comment.
Code half of the linear rewrite, pairs with the pattern change above.
| if match.group("url"): | |
| try: | |
| parsed_ticket_url = urlparse(match.group("url")) | |
| except ValueError: | |
| continue | |
| if not provider_host or parsed_ticket_url.hostname != provider_host: | |
| continue | |
| ticket_path = parsed_ticket_url.path | |
| if provider_base_path: | |
| if not ticket_path.startswith(f"{provider_base_path}/"): | |
| continue | |
| ticket_path = ticket_path[len(provider_base_path):] | |
| issue_iid = int(match.group("url_issue")) | |
| issue_suffix = f"/-/issues/{issue_iid}" | |
| if not ticket_path.endswith(issue_suffix): | |
| continue | |
| issue_project = ticket_path[:-len(issue_suffix)].strip("/") | |
| if match.group("url"): | |
| try: | |
| parsed_ticket_url = urlparse(match.group("url").rstrip(".:!?'\"]}`*")) | |
| except ValueError: | |
| continue | |
| if not provider_host or parsed_ticket_url.hostname != provider_host: | |
| continue | |
| ticket_path = parsed_ticket_url.path | |
| if provider_base_path: | |
| if not ticket_path.startswith(f"{provider_base_path}/"): | |
| continue | |
| ticket_path = ticket_path[len(provider_base_path):] | |
| path_match = GITLAB_ISSUE_PATH_PATTERN.search(ticket_path) | |
| if not path_match: | |
| continue | |
| issue_iid = int(path_match.group("iid")) | |
| issue_project = ticket_path[:path_match.start()].strip("/") |
There was a problem hiding this comment.
Addressed in 65e0af41: the parser trims trailing wrappers, validates the host and configured base path, then extracts the project and IID from GITLAB_ISSUE_PATH_PATTERN. Coverage now includes wrappers, custom ports/subpaths, fragments, zero-padded IDs, glued suffixes, and comma-separated URLs.
|
Code review by qodo was updated up to the latest commit 65e0af4 |
IsmaelMartinez
left a comment
There was a problem hiding this comment.
Thanks for turning both points around within the hour. Verified the new pattern is linear, roughly 2x per input doubling on adversarial inputs. One character left: Qodo's remaining finding is real, I reproduced it, a Markdown heading reads as a reference, so ##7 matches as issue #7. Suggestion inline; with it applied this is ready to merge.
| GITLAB_TICKET_PATTERN = re.compile( | ||
| r"(?P<url>https?://[^\s<>(),;]+)" | ||
| r"|(?<![\w./-])(?P<project>[\w.-]+(?:/[\w.-]+)+)#(?P<project_issue>\d+)\b" | ||
| r"|(?<![\w/])#(?P<local_issue>\d+)\b" |
There was a problem hiding this comment.
Excludes # from the lookbehind so a Markdown heading no longer reads as a reference; the 38 tests stay green and group/proj#7 still extracts.
| r"|(?<![\w/])#(?P<local_issue>\d+)\b" | |
| r"|(?<![\w/#])#(?P<local_issue>\d+)\b" |
There was a problem hiding this comment.
Addressed in bc13b4a6: local shorthand references now exclude a preceding #. The regression verifies that ##7 is ignored while group/proj#7 and #7 still extract. Focused GitLab tests pass (12/12), the related ticket suite passes (160 passed, 1 xfailed), and the full unit suite passes (2100 passed, 1 skipped, 1 xfailed).
|
Code review by qodo was updated up to the latest commit bc13b4a |
IsmaelMartinez
left a comment
There was a problem hiding this comment.
The lookbehind fix and the three boundary cases are exactly right; 41 pass on the merged tree. Thanks for the quick turnarounds today.
Fixes #2799.
Summary
Tests
PYTHONPATH=. .venv/bin/pytest tests/unittest -q --color=no