Skip to content

fix(bitbucket): propagate repo context fetch errors - #2791

Merged
IsmaelMartinez merged 3 commits into
The-PR-Agent:mainfrom
junnhwan:fix/bitbucket-repo-context-errors
Aug 24, 2026
Merged

IsmaelMartinez merged 3 commits into
The-PR-Agent:mainfrom
junnhwan:fix/bitbucket-repo-context-errors

Conversation

@junnhwan

@junnhwan junnhwan commented Aug 24, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2790.

Summary

  • propagate non-404 Bitbucket source errors through repo-context loading;
  • make error propagation the default for direct get_pr_file_content callers, whose higher-level callers handle the exception; and
  • add a regression test for a 500 source response.

Tests

  • PYTHONPATH=. /tmp/pr-agent-full-venv/bin/pytest tests/unittest/test_bitbucket_provider.py tests/unittest/test_repo_context.py -q — 76 passed
  • PYTHONPATH=. /tmp/pr-agent-full-venv/bin/pytest tests/unittest/test_bitbucket_provider.py tests/unittest/test_pr_update_changelog.py tests/unittest/test_repo_context.py -q — 92 passed
  • PYTHONPATH=. /tmp/pr-agent-full-venv/bin/pytest tests/unittest -q — 2035 passed, 1 skipped, 1 xfailed, 90 warnings
  • git diff --check

@qodo-code-review

Copy link
Copy Markdown
Contributor

PR Summary by Qodo

Propagate Bitbucket repo-context fetch errors (keep empty-string for direct calls)

🐞 Bug fix 🧪 Tests 🕐 20-40 Minutes

Grey Divider

AI Description

• Propagate non-404 Bitbucket fetch errors when loading repo context.
• Keep existing empty-string behavior for direct file-content callers.
• Add regression test covering 500 upstream response handling.
Diagram

graph TD
  A["Repo-context loader"] --> B["BitbucketProvider.get_repo_file_content"] --> C["get_pr_file_content(propagate_errors=true)"] --> D{{"Bitbucket HTTP API"}} --> E["File text"]
  D --> F["HTTPError (non-404)"]
  F --> A

  subgraph Legend
    direction LR
    _call["Caller"] ~~~ _prov["Provider method"] ~~~ _ext{{"External API"}} ~~~ _err["Error"}
  end
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Always raise on non-404 in get_pr_file_content
  • ➕ Simpler API surface (no propagate_errors flag).
  • ➕ Avoids any future call sites accidentally swallowing real failures.
  • ➖ Behavior change could break existing callers that rely on empty-string fallback.
  • ➖ Requires auditing and updating all call sites to handle exceptions.
2. Return a structured result (content + status/error) instead of exceptions
  • ➕ Makes 'missing file' vs 'failed fetch' explicit without exceptions.
  • ➕ Callers can decide policy per use-case.
  • ➖ Larger refactor across providers/callers; higher churn.
  • ➖ More boilerplate at call sites compared to exception flow.

Recommendation: The chosen opt-in propagation flag is a good balance: repo-context loading gets correct failure semantics (non-404 raises) while preserving backward-compatible empty-string behavior for existing direct callers.

Files changed (2) +25 / -4

Bug fix (1) +8 / -2
bitbucket_provider.pyAdd opt-in HTTP error propagation for Bitbucket file fetches +8/-2

Add opt-in HTTP error propagation for Bitbucket file fetches

• Extends get_pr_file_content with a propagate_errors flag. When enabled (used by get_repo_file_content), non-404 HTTP responses raise and exceptions are re-thrown instead of returning an empty string.

pr_agent/git_providers/bitbucket_provider.py

Tests (1) +17 / -2
test_bitbucket_provider.pyUpdate repo-file tests and add regression coverage for 500 errors +17/-2

Update repo-file tests and add regression coverage for 500 errors

• Adjusts assertions to ensure repo file reads enable propagate_errors. Adds a test verifying that a 500 response during repo-file content fetch raises an HTTPError (and calls raise_for_status).

tests/unittest/test_bitbucket_provider.py

@qodo-code-review

qodo-code-review Bot commented Aug 24, 2026 •

Copy link
Copy Markdown
Contributor

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📜 Skill insights (0)

Grey Divider


Remediation recommended

1. Non-imperative error comment ✓ Resolved 📘 Rule violation ⚙ Maintainability
Description
The newly added inline comment is not written in imperative mood as required, which reduces
consistency across the codebase. Rephrase it as a direct instruction (imperative) rather than a
descriptive statement about callers.
Code

pr_agent/git_providers/bitbucket_provider.py[R608-609]

+            # Callers must distinguish an unavailable file from a failed request; otherwise an
+            # error response body can be treated as repository instructions or changelog content.
Relevance

●●● Strong

Recent history accepts consistency and clarity fixes for newly added comments and style-rule
violations.

PR-#2776
PR-#2617

ⓘ Recommendations generated based on similar findings in past PRs

Evidence
PR Compliance ID 2694688 requires newly added comments to use imperative phrasing. The added comment
at bitbucket_provider.py lines 608-609 is phrased as "Callers must ..." rather than an imperative
instruction.

Rule 2694688: Docstrings and comments must use imperative phrasing
pr_agent/git_providers/bitbucket_provider.py[608-609]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
A newly added inline comment does not use imperative phrasing, which violates the project comment/docstring style requirement.

## Issue Context
The comment currently reads as a requirement about callers ("Callers must ...") rather than an imperative instruction.

## Fix Focus Areas
- pr_agent/git_providers/bitbucket_provider.py[608-609]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


  • Author self-review: I have reviewed the code review findings, and addressed the relevant ones.

Grey Divider

Context sources
✅ Compliance rules (platform): 34 rules
Review mode: ⚖️ Balanced

Grey Divider

Tip of the day
💡 Did you know, you can switch off images and animations for a plain-text comment

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

@IsmaelMartinez IsmaelMartinez left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the second Bitbucket fix today. The verdict is yes, and the case is stronger than the description makes it: measured on main, a 500 puts the error page into <instruction_files> as project guidance and caches it for the 15-minute TTL, so it outlives the outage that caused it.

# or from the repository default branch when from_default_branch is requested.
branch = self.get_repo_default_branch() if from_default_branch else self.pr.destination_branch
return self.get_pr_file_content(file_path, branch)
return self.get_pr_file_content(file_path, branch, propagate_errors=True)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This matches GitHub and Gitea, which already return "" only on 404 and let the rest propagate, for the reason spelled out in the comment above each. GitLab, Azure DevOps and Bitbucket Server still return "" when the fetch itself fails rather than only when the file is missing, so they cache an empty context instead. Worth a follow-up issue rather than widening this PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed. I kept the GitLab, Azure DevOps, and Bitbucket Server behavior out of this PR and prepared a separate follow-up issue covering their non-404 fetch failures being converted to empty repo context. The scope stays limited to those three providers, while GitHub and Gitea already propagate unexpected fetch errors.

return self._get_repo().pullrequests.get(self.pr_num)

def get_pr_file_content(self, file_path: str, branch: str) -> str:
def get_pr_file_content(self, file_path: str, branch: str, propagate_errors: bool = False) -> str:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Does the flag need to exist? /update_changelog is the only other caller and it already wraps this in try/except, falling back to an empty changelog. As it stands it still takes a 500 error page as the existing CHANGELOG.md, which is the same defect one call away.

Defaulting propagate_errors to True closes that as well, and the suite stays green.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, agreed. In follow-up commit b906b60d, I changed propagate_errors to default to True, so direct CHANGELOG.md reads no longer treat a non-404 error page as file content. /update_changelog already catches the propagated exception and falls back to an empty changelog.

I also added a regression test for the direct get_pr_file_content() path, while preserving the 404-as-missing behavior. The scoped provider/changelog/repo-context tests pass (92 tests), and the full unit suite passes (2035 passed). I kept the GitLab, Azure DevOps, and Bitbucket Server behavior out of this PR and prepared a separate follow-up issue.

@qodo-code-review

Copy link
Copy Markdown
Contributor

Code review by qodo was updated up to the latest commit b906b60

@qodo-code-review

Copy link
Copy Markdown
Contributor

Code review by qodo was updated up to the latest commit b4f0bb6

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Bitbucket repo context treats HTTP error responses as instruction files

2 participants