-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix(gerrit): use action parameter for dispatch and clean up temp repos #2331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gvago
wants to merge
3
commits into
The-PR-Agent:main
Choose a base branch
from
gvago:fix/gerrit-action-param-and-temp-cleanup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+60
−8
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
|
|
||
| from pr_agent.agent.pr_agent import PRAgent | ||
| from pr_agent.config_loader import get_settings, global_settings | ||
| from pr_agent.git_providers.gerrit_provider import GerritProvider | ||
| from pr_agent.log import get_logger, setup_logger | ||
|
|
||
| setup_logger() | ||
|
|
@@ -29,24 +30,54 @@ class Action(str, Enum): | |
| class Item(BaseModel): | ||
| refspec: str | ||
| project: str | ||
| msg: str | ||
| msg: str = "" | ||
|
|
||
|
|
||
| @router.post("/api/v1/gerrit/{action}") | ||
| async def handle_gerrit_request(action: Action, item: Item): | ||
| get_logger().debug("Received a Gerrit request") | ||
| context["settings"] = copy.deepcopy(global_settings) | ||
|
|
||
| # For the "ask" action, the question must come from item.msg. | ||
| # For all other actions, use the action path parameter as the command. | ||
| if action == Action.ask: | ||
| if not item.msg: | ||
| return HTTPException( | ||
| raise HTTPException( | ||
| status_code=400, | ||
| detail="msg is required for ask command" | ||
| ) | ||
| await PRAgent().handle_request( | ||
| f"{item.project}:{item.refspec}", | ||
| f"/{item.msg.strip()}" | ||
| ) | ||
| command = f"/{action.value} {item.msg.strip()}" | ||
| else: | ||
| command = f"/{action.value}" | ||
|
|
||
| try: | ||
| await PRAgent().handle_request( | ||
| f"{item.project}:{item.refspec}", | ||
| command | ||
| ) | ||
| finally: | ||
| # Clean up the cloned temp repo created by GerritProvider. | ||
| # The provider is cached in the starlette context during | ||
| # get_git_provider_with_context(). | ||
| # | ||
| # We guard against two failure modes: | ||
| # 1. The starlette context is inaccessible (e.g. middleware not | ||
| # active) — caught by the outer try/except. | ||
| # 2. The provider was never stored in the context (e.g. an error | ||
| # occurred before get_git_provider_with_context ran) — the | ||
| # dict will simply be empty, and the GerritProvider.__del__ | ||
| # safety net handles cleanup on garbage collection. | ||
| try: | ||
| git_providers = context.get("git_provider", {}) | ||
| if isinstance(git_providers, dict): | ||
| for provider in git_providers.values(): | ||
| if isinstance(provider, GerritProvider): | ||
| provider.cleanup() | ||
|
Comment on lines
+70
to
+75
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2. Ask temp repo leak The new finally cleanup only cleans GerritProvider instances stored in starlette_context, but the ask command path constructs its provider via get_git_provider() (not get_git_provider_with_context()), so its mkdtemp clone is not cleaned by the finally block. Agent Prompt
|
||
| except (LookupError, RuntimeError): | ||
| get_logger().debug( | ||
| "Could not retrieve GerritProvider for cleanup; " | ||
| "temp directory will be cleaned up by __del__" | ||
| ) | ||
|
|
||
|
|
||
| async def get_body(request): | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1. ask allows blank msg
📘 Rule violation≡ CorrectnessAgent Prompt
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools