fix: add None safety for optional fields in GitLab webhook handlers#2276
fix: add None safety for optional fields in GitLab webhook handlers#2276wishhyt wants to merge 1 commit intoqodo-ai:mainfrom
Conversation
`is_draft` calls `'Draft:' in .get('title')` which raises TypeError
when 'title' is absent (returns None). Similarly, `should_process_pr_logic`
passes potentially None values from `.get('source_branch')`,
`.get('target_branch')`, and `.get('title')` into `re.search()`, which
raises TypeError on None input.
Add empty string defaults to all `.get()` calls for these optional fields
and guard `re.search` calls with truthiness checks.
Made-with: Cursor
Review Summary by QodoAdd None safety for optional fields in GitLab webhook handlers
WalkthroughsDescription• Add None safety for optional fields in GitLab webhook handlers • Provide empty string defaults to .get() calls for title, source_branch, and target_branch • Guard re.search() calls with truthiness checks to prevent TypeError • Prevent incorrect fallback behavior when optional fields are missing from payload Diagramflowchart LR
A["GitLab Webhook Payload"] -->|Missing optional fields| B["is_draft & should_process_pr_logic"]
B -->|Before: TypeError| C["Incorrect fallback behavior"]
B -->|After: Empty string defaults| D["Safe field access"]
D -->|Truthiness guards| E["Correct processing logic"]
File Changes1. pr_agent/servers/gitlab_webhook.py
|
Code Review by Qodo
1. should_process_pr_logic() title None
|
| if not data.get('object_attributes', {}): | ||
| return False | ||
| title = data['object_attributes'].get('title') | ||
| title = data['object_attributes'].get('title', '') |
There was a problem hiding this comment.
1. should_process_pr_logic() title none 📘 Rule violation ⛯ Reliability
The updated title = ...get('title', '') assignment still allows title to be None when the
payload explicitly provides null, and later re.search(regex, title) will raise TypeError. The
exception handler then returns True, potentially processing MRs that should be ignored.
Agent Prompt
## Issue description
`should_process_pr_logic()` still allows `title` to be `None` when the payload includes an explicit null, which can trigger `TypeError` inside `re.search()`.
## Issue Context
The PR intent is None-safety for optional webhook fields; `.get(key, '')` does not protect against `key` being present with a `None` value.
## Fix Focus Areas
- pr_agent/servers/gitlab_webhook.py[111-167]
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
| source_branch = data['object_attributes'].get('source_branch', '') | ||
| if source_branch and any(re.search(regex, source_branch) for regex in ignore_mr_source_branches): | ||
| get_logger().info( | ||
| f"Ignoring MR with source branch '{source_branch}' due to gitlab.ignore_mr_source_branches settings") | ||
| return False | ||
|
|
||
| if ignore_mr_target_branches: | ||
| target_branch = data['object_attributes'].get('target_branch') | ||
| if any(re.search(regex, target_branch) for regex in ignore_mr_target_branches): | ||
| target_branch = data['object_attributes'].get('target_branch', '') | ||
| if target_branch and any(re.search(regex, target_branch) for regex in ignore_mr_target_branches): | ||
| get_logger().info( |
There was a problem hiding this comment.
2. None-safety lacks pytest coverage 📘 Rule violation ⛯ Reliability
This PR changes GitLab webhook logic to handle missing title/branch fields, but existing pytest coverage does not validate behavior when these fields are absent/None under the ignore-pattern settings. Without targeted tests, regressions in MR filtering behavior are likely to go unnoticed.
Agent Prompt
## Issue description
The GitLab webhook filtering logic was changed for None/missing safety, but there are no targeted pytest tests that exercise these new edge cases.
## Issue Context
Current tests for GitLab webhook processing focus on repository ignore behavior and use a payload that always includes a non-empty `title`, so regressions for missing/None fields can slip through.
## Fix Focus Areas
- tests/unittest/test_ignore_repositories.py[31-81]
- pr_agent/servers/gitlab_webhook.py[75-167]ర్వ
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
Summary
Changes
pr_agent/servers/gitlab_webhook.py: Added empty string defaults to all .get() calls for optional fields and added truthiness guards before re.search calls
Test plan