-
Notifications
You must be signed in to change notification settings - Fork 38
Add GetPipelineJobLogTool for reading CI job logs #686
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
martinhoyer
wants to merge
2
commits into
packit:main
Choose a base branch
from
martinhoyer:feature/pipeline-job-logs
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
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 |
|---|---|---|
|
|
@@ -866,6 +866,62 @@ def get_latest_pipeline_jobs(): | |
| raise ToolError(f"Failed to get failed jobs from merge request: {e}") from e | ||
|
|
||
|
|
||
| MAX_LOG_LINES = 500 | ||
|
|
||
|
|
||
| class GetPipelineJobLogToolInput(BaseModel): | ||
| project_path: str = Field(description="GitLab project path (e.g. 'redhat/rhel/rpms/curl')") | ||
| job_id: str = Field(description="GitLab job ID (from get_failed_pipeline_jobs_from_merge_request output)") | ||
|
|
||
|
|
||
| class GetPipelineJobLogTool(Tool[GetPipelineJobLogToolInput, ToolRunOptions, StringToolOutput]): | ||
| name = "get_pipeline_job_log" | ||
| description = ( | ||
| "Fetches the raw log output from a specific GitLab CI job. " | ||
| "Use after get_failed_pipeline_jobs_from_merge_request to read WHY a job failed. " | ||
| "Returns the last 500 lines of log output." | ||
|
Member
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. we can start with this, but could also consider the approach used for Copr build logs previously - download the full trace to disk and let the agent view the content iteratively with offset/limit |
||
| ) | ||
| input_schema = GetPipelineJobLogToolInput | ||
|
|
||
| def _create_emitter(self) -> Emitter: | ||
| return Emitter.root().child( | ||
| namespace=["tool", "gitlab", self.name], | ||
| creator=self, | ||
| ) | ||
|
|
||
| async def _run( | ||
| self, | ||
| tool_input: GetPipelineJobLogToolInput, | ||
| options: ToolRunOptions | None, | ||
| context: RunContext, | ||
| ) -> StringToolOutput: | ||
| encoded_path = quote(tool_input.project_path, safe="") | ||
| url = f"https://gitlab.com/api/v4/projects/{encoded_path}/jobs/{tool_input.job_id}/trace" | ||
| headers = _get_auth_headers(f"https://gitlab.com/{tool_input.project_path}") | ||
|
|
||
| try: | ||
| async with ( | ||
| aiohttp.ClientSession(timeout=AIOHTTP_TIMEOUT) as session, | ||
|
martinhoyer marked this conversation as resolved.
|
||
| aiohttp_get_with_retries(session, url, headers=headers) as response, | ||
| ): | ||
| response.raise_for_status() | ||
| raw = await response.read() | ||
| log_text = raw.decode("utf-8", errors="replace") | ||
| except (aiohttp.ClientError, TimeoutError) as e: | ||
| raise ToolError( | ||
| f"Failed to fetch log for job {tool_input.job_id} in {tool_input.project_path}: {e}" | ||
| ) from e | ||
|
martinhoyer marked this conversation as resolved.
|
||
|
|
||
| lines = log_text.splitlines() | ||
| if len(lines) > MAX_LOG_LINES: | ||
| log_text = ( | ||
| f"[... truncated, showing last {MAX_LOG_LINES} of {len(lines)} lines ...]\n" | ||
| + "\n".join(lines[-MAX_LOG_LINES:]) | ||
| ) | ||
|
|
||
| return StringToolOutput(result=log_text) | ||
|
|
||
|
|
||
| def _get_authorized_member_ids(project: GitlabProject) -> set[int]: | ||
| """ | ||
| Fetch all project members and return a set of IDs for members | ||
|
|
@@ -1056,7 +1112,7 @@ async def _run( | |
| ) -> StringToolOutput: | ||
| patch_url = tool_input.patch_url | ||
| request_url = _get_api_diff_url(patch_url) | ||
| headers = _get_auth_headers(request_url) | ||
| headers = _get_auth_headers(patch_url) | ||
|
|
||
| try: | ||
| async with ( | ||
|
|
@@ -1117,7 +1173,7 @@ async def _run( | |
| ) -> StringToolOutput: | ||
| encoded_project = quote(input.project, safe="") | ||
| url = f"https://gitlab.com/api/v4/projects/{encoded_project}/merge_requests/{input.mr_iid}/notes" | ||
| headers = _get_auth_headers(url) | ||
| headers = _get_auth_headers(f"https://gitlab.com/{input.project}") | ||
| logger.info("Fetching MR notes from %s", url) | ||
|
|
||
| try: | ||
|
|
||
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
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.
currently, this MCP tool isn't wired to any agent, is this supposed to be used by preliminary agent here?
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.
Yeah, alongside the
get_failed_pipeline_jobs_from_merge_request, but it's not necessarily meant to be used by a specific agent.