-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat: support CI artifact context injection and workflow_run trigger #2494
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
MahmoudHaouachi
wants to merge
2
commits into
The-PR-Agent:main
Choose a base branch
from
idealo:feat/ci-artifact-context
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.
Open
Changes from all commits
Commits
Show all changes
2 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
Some comments aren't visible on the classic Files Changed page.
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 |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| #!/bin/bash | ||
| set -e | ||
| python /app/pr_agent/servers/github_action_runner.py |
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 |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| import os | ||
| from pathlib import Path | ||
| from typing import Optional | ||
|
|
||
| from pr_agent.config_loader import get_settings | ||
| from pr_agent.log import get_logger | ||
|
|
||
| DEFAULT_ARTIFACT_INSTRUCTIONS = ( | ||
| "Consider this CI artifact as additional context when analyzing the PR. " | ||
| "It was produced by a prior CI step." | ||
| ) | ||
|
|
||
|
|
||
| def resolve_artifact_path(path: str) -> Optional[Path]: | ||
| if not path: | ||
| return None | ||
| try: | ||
| workspace = os.environ.get("GITHUB_WORKSPACE", "") | ||
|
|
||
| artifact_path = Path(path) | ||
| if artifact_path.is_absolute(): | ||
| resolved = artifact_path.resolve() | ||
| elif workspace: | ||
| resolved = (Path(workspace) / artifact_path).resolve() | ||
| else: | ||
| resolved = artifact_path.resolve() | ||
|
|
||
| if workspace: | ||
| workspace_resolved = Path(workspace).resolve() | ||
| under_workspace = resolved == workspace_resolved or resolved.is_relative_to(workspace_resolved) | ||
| if not under_workspace: | ||
| get_logger().warning( | ||
| f"Artifact path '{path}' resolves outside GITHUB_WORKSPACE: {resolved}" | ||
| ) | ||
| return None | ||
|
|
||
| return resolved if resolved.is_file() else None | ||
| except OSError as e: | ||
| get_logger().warning(f"Failed to resolve artifact path '{path}': {e}") | ||
| return None | ||
|
|
||
|
|
||
| _TRUNCATION_MARKER = "\n\n[... content truncated due to size limit ...]" | ||
|
|
||
|
|
||
| def _read_and_truncate(path: Path, max_size: int) -> str: | ||
| try: | ||
| with open(path, "r", encoding="utf-8", errors="replace") as f: | ||
| content = f.read(max_size + 1) | ||
| except (OSError, IOError) as e: | ||
| get_logger().warning(f"Failed to read artifact file {path}: {e}") | ||
| return "" | ||
|
|
||
| if len(content) > max_size: | ||
| available = max_size - len(_TRUNCATION_MARKER) | ||
| content = content[:available] + _TRUNCATION_MARKER if available > 0 else content[:max_size] | ||
| return content | ||
|
qodo-free-for-open-source-projects[bot] marked this conversation as resolved.
MahmoudHaouachi marked this conversation as resolved.
|
||
|
|
||
|
|
||
| def format_artifact_content(content: str, label: str, instructions: str) -> str: | ||
| header = f"CI Artifact: {label}" if label else "CI Artifact" | ||
| instructions = (instructions or "").strip() or DEFAULT_ARTIFACT_INSTRUCTIONS | ||
| return ( | ||
| f"{header}\n" | ||
| f"=====\n" | ||
| f"{content}\n" | ||
| f"=====\n" | ||
| f"{instructions}" | ||
| ) | ||
|
|
||
|
|
||
| def load_artifact() -> str: | ||
| try: | ||
| artifacts_settings = get_settings().get("ARTIFACTS", {}) | ||
| except AttributeError: | ||
| return "" | ||
|
|
||
| if not artifacts_settings: | ||
| return "" | ||
|
|
||
| enable = artifacts_settings.get("enable", False) | ||
| if isinstance(enable, str): | ||
| enable = enable.lower() == "true" | ||
| if not enable: | ||
| return "" | ||
|
|
||
| artifact_path_str = artifacts_settings.get("artifact_path", "") | ||
| if not artifact_path_str: | ||
| return "" | ||
|
|
||
| artifact_path = resolve_artifact_path(artifact_path_str) | ||
| if not artifact_path: | ||
| get_logger().warning( | ||
| f"Artifact file not found or path rejected: '{artifact_path_str}' " | ||
| f"(GITHUB_WORKSPACE={os.environ.get('GITHUB_WORKSPACE', 'not set')})" | ||
| ) | ||
| return "" | ||
|
|
||
| try: | ||
| max_size = int(artifacts_settings.get("max_artifact_size", 50000)) | ||
| except (TypeError, ValueError): | ||
| max_size = 50000 | ||
| if max_size <= 0: | ||
| max_size = 50000 | ||
| content = _read_and_truncate(artifact_path, max_size) | ||
| if not content: | ||
| return "" | ||
|
|
||
| label = artifacts_settings.get("artifact_label", "") or artifact_path.name | ||
| instructions = artifacts_settings.get("artifact_instructions", "") | ||
| return format_artifact_content(content, label, instructions) | ||
|
MahmoudHaouachi marked this conversation as resolved.
|
||
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.