-
Notifications
You must be signed in to change notification settings - Fork 8
Add save_job_artifacts utility for CI artifact collection #48
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
EmilienM
wants to merge
1
commit into
main
Choose a base branch
from
artifact-collection
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -305,3 +305,48 @@ def run_skill( | |
| verdict.get("verdict", "unknown"), | ||
| ) | ||
| return 0 | ||
|
|
||
|
|
||
| def save_job_artifacts( | ||
| work_dir: Path, | ||
| repo_dir: Path | None = None, | ||
| prompt: str = "", | ||
| ) -> None: | ||
| """Collect debugging artifacts into work_dir for CI artifact upload.""" | ||
| try: | ||
| work_dir.mkdir(parents=True, exist_ok=True) | ||
| if prompt: | ||
| (work_dir / "prompt.txt").write_text(prompt, encoding="utf-8") | ||
| for candidate in [repo_dir, work_dir]: | ||
| if candidate is None: | ||
| continue | ||
| output_file = candidate / "claude-output.txt" | ||
| if output_file.exists(): | ||
| dest = work_dir / "claude_output.txt" | ||
| if output_file.resolve() != dest.resolve(): | ||
| shutil.copy2(str(output_file), str(dest)) | ||
| content = output_file.read_text(encoding="utf-8", errors="replace") | ||
| lines = content.splitlines() | ||
| if len(lines) > 500: | ||
| (work_dir / "claude_output_tail.txt").write_text( | ||
| "\n".join(lines[-500:]), encoding="utf-8" | ||
| ) | ||
| break | ||
| if repo_dir and (repo_dir / ".git").is_dir(): | ||
| _collect_git_artifacts(work_dir, repo_dir) | ||
| except Exception as exc: | ||
| log.debug("Artifact collection failed (non-fatal): %s", exc) | ||
|
|
||
|
|
||
| def _collect_git_artifacts(work_dir: Path, repo_dir: Path) -> None: | ||
| """Capture git diff and log from repo_dir into work_dir.""" | ||
| from agentic_ci.git import get_default_branch, git_output | ||
|
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. Move this to be a file-level import. |
||
|
|
||
| default_branch = get_default_branch(repo_dir) | ||
| base_ref = f"origin/{default_branch}" | ||
| diff = git_output(repo_dir, "diff", f"{base_ref}...HEAD") | ||
| if diff: | ||
| (work_dir / "diff.patch").write_text(diff, encoding="utf-8") | ||
| log_text = git_output(repo_dir, "log", "--oneline", f"{base_ref}..HEAD") | ||
| if log_text: | ||
| (work_dir / "git-log.txt").write_text(log_text, encoding="utf-8") | ||
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,71 @@ | ||
| """Tests for save_job_artifacts in agentic_ci.skill.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from pathlib import Path | ||
| from unittest.mock import patch | ||
|
|
||
| from agentic_ci.skill import save_job_artifacts | ||
|
|
||
|
|
||
| def test_prompt_saved(tmp_path: Path) -> None: | ||
| """save_job_artifacts writes prompt.txt when prompt is provided.""" | ||
| save_job_artifacts(tmp_path, prompt="hello world") | ||
| assert (tmp_path / "prompt.txt").read_text() == "hello world" | ||
|
|
||
|
|
||
| def test_output_copied(tmp_path: Path) -> None: | ||
| """Claude output is copied from repo_dir to work_dir.""" | ||
| repo = tmp_path / "repo" | ||
| repo.mkdir() | ||
| work = tmp_path / "work" | ||
| (repo / "claude-output.txt").write_text("some output", encoding="utf-8") | ||
|
|
||
| save_job_artifacts(work, repo_dir=repo) | ||
|
|
||
| assert (work / "claude_output.txt").read_text() == "some output" | ||
| assert not (work / "claude_output_tail.txt").exists() | ||
|
|
||
|
|
||
| def test_tail_created_for_long_output(tmp_path: Path) -> None: | ||
| """A tail file is created when output exceeds 500 lines.""" | ||
| repo = tmp_path / "repo" | ||
| repo.mkdir() | ||
| work = tmp_path / "work" | ||
| lines = [f"line {i}" for i in range(600)] | ||
| (repo / "claude-output.txt").write_text("\n".join(lines), encoding="utf-8") | ||
|
|
||
| save_job_artifacts(work, repo_dir=repo) | ||
|
|
||
| assert (work / "claude_output.txt").exists() | ||
| tail = (work / "claude_output_tail.txt").read_text() | ||
| tail_lines = tail.splitlines() | ||
| assert len(tail_lines) == 500 | ||
| assert tail_lines[-1] == "line 599" | ||
| assert tail_lines[0] == "line 100" | ||
|
|
||
|
|
||
| def test_graceful_on_exception(tmp_path: Path) -> None: | ||
| """save_job_artifacts does not raise even on internal errors.""" | ||
| with patch("agentic_ci.skill.Path.mkdir", side_effect=OSError("disk full")): | ||
| # Should not raise | ||
| save_job_artifacts(tmp_path / "nonexistent", prompt="test") | ||
|
|
||
|
|
||
| def test_no_prompt_no_file(tmp_path: Path) -> None: | ||
| """No prompt.txt is created when prompt is empty.""" | ||
| save_job_artifacts(tmp_path) | ||
| assert not (tmp_path / "prompt.txt").exists() | ||
|
|
||
|
|
||
| def test_output_in_work_dir(tmp_path: Path) -> None: | ||
| """Claude output in work_dir is handled when repo_dir has none.""" | ||
| work = tmp_path / "work" | ||
| work.mkdir() | ||
| (work / "claude-output.txt").write_text("work output", encoding="utf-8") | ||
|
|
||
| save_job_artifacts(work) | ||
|
|
||
| # Output file already in work_dir, resolve() should match, so no copy | ||
| # but no error either | ||
| assert (work / "claude-output.txt").read_text() == "work output" |
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.
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.
We should probably make this harness-agnostic. I feel like we should tackle https://redhat.atlassian.net/browse/RHAIFIRST-59 first so we have a standardized way to refer to artifacts and then this function becomes a lot simpler where it copies known files into an artifacts directory instead of hardcoding things like
claude-output.txthere.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.
nice catch. I think you're right, let's fix that one first and come back to it.