-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add tail logs option #615
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
Add tail logs option #615
Changes from 2 commits
7d06c88
ec50b5e
36236f5
0867d89
305391c
2573620
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -584,6 +584,10 @@ func GetJobLogs(getClient GetClientFn, t translations.TranslationHelperFunc) (to | |
mcp.WithBoolean("return_content", | ||
mcp.Description("Returns actual log content instead of URLs"), | ||
), | ||
mcp.WithNumber("tail_lines", | ||
mcp.Description("Number of lines to return from the end of the log"), | ||
mcp.DefaultNumber(50), | ||
), | ||
), | ||
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { | ||
owner, err := RequiredParam[string](request, "owner") | ||
|
@@ -612,6 +616,14 @@ func GetJobLogs(getClient GetClientFn, t translations.TranslationHelperFunc) (to | |
if err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
tailLines, err := OptionalIntParam(request, "tail_lines") | ||
if err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
// Default to 50 lines if not specified | ||
if tailLines == 0 { | ||
tailLines = 50 | ||
} | ||
|
||
client, err := getClient(ctx) | ||
if err != nil { | ||
|
@@ -628,18 +640,18 @@ func GetJobLogs(getClient GetClientFn, t translations.TranslationHelperFunc) (to | |
|
||
if failedOnly && runID > 0 { | ||
// Handle failed-only mode: get logs for all failed jobs in the workflow run | ||
return handleFailedJobLogs(ctx, client, owner, repo, int64(runID), returnContent) | ||
return handleFailedJobLogs(ctx, client, owner, repo, int64(runID), returnContent, tailLines) | ||
} else if jobID > 0 { | ||
// Handle single job mode | ||
return handleSingleJobLogs(ctx, client, owner, repo, int64(jobID), returnContent) | ||
return handleSingleJobLogs(ctx, client, owner, repo, int64(jobID), returnContent, tailLines) | ||
} | ||
|
||
return mcp.NewToolResultError("Either job_id must be provided for single job logs, or run_id with failed_only=true for failed job logs"), nil | ||
} | ||
} | ||
|
||
// handleFailedJobLogs gets logs for all failed jobs in a workflow run | ||
func handleFailedJobLogs(ctx context.Context, client *github.Client, owner, repo string, runID int64, returnContent bool) (*mcp.CallToolResult, error) { | ||
func handleFailedJobLogs(ctx context.Context, client *github.Client, owner, repo string, runID int64, returnContent bool, tailLines int) (*mcp.CallToolResult, error) { | ||
// First, get all jobs for the workflow run | ||
jobs, resp, err := client.Actions.ListWorkflowJobs(ctx, owner, repo, runID, &github.ListWorkflowJobsOptions{ | ||
Filter: "latest", | ||
|
@@ -671,7 +683,7 @@ func handleFailedJobLogs(ctx context.Context, client *github.Client, owner, repo | |
// Collect logs for all failed jobs | ||
var logResults []map[string]any | ||
for _, job := range failedJobs { | ||
jobResult, resp, err := getJobLogData(ctx, client, owner, repo, job.GetID(), job.GetName(), returnContent) | ||
jobResult, resp, err := getJobLogData(ctx, client, owner, repo, job.GetID(), job.GetName(), returnContent, tailLines) | ||
if err != nil { | ||
// Continue with other jobs even if one fails | ||
jobResult = map[string]any{ | ||
|
@@ -704,8 +716,8 @@ func handleFailedJobLogs(ctx context.Context, client *github.Client, owner, repo | |
} | ||
|
||
// handleSingleJobLogs gets logs for a single job | ||
func handleSingleJobLogs(ctx context.Context, client *github.Client, owner, repo string, jobID int64, returnContent bool) (*mcp.CallToolResult, error) { | ||
jobResult, resp, err := getJobLogData(ctx, client, owner, repo, jobID, "", returnContent) | ||
func handleSingleJobLogs(ctx context.Context, client *github.Client, owner, repo string, jobID int64, returnContent bool, tailLines int) (*mcp.CallToolResult, error) { | ||
jobResult, resp, err := getJobLogData(ctx, client, owner, repo, jobID, "", returnContent, tailLines) | ||
if err != nil { | ||
return ghErrors.NewGitHubAPIErrorResponse(ctx, "failed to get job logs", resp, err), nil | ||
} | ||
|
@@ -719,7 +731,7 @@ func handleSingleJobLogs(ctx context.Context, client *github.Client, owner, repo | |
} | ||
|
||
// getJobLogData retrieves log data for a single job, either as URL or content | ||
func getJobLogData(ctx context.Context, client *github.Client, owner, repo string, jobID int64, jobName string, returnContent bool) (map[string]any, *github.Response, error) { | ||
func getJobLogData(ctx context.Context, client *github.Client, owner, repo string, jobID int64, jobName string, returnContent bool, tailLines int) (map[string]any, *github.Response, error) { | ||
// Get the download URL for the job logs | ||
url, resp, err := client.Actions.GetWorkflowJobLogs(ctx, owner, repo, jobID, 1) | ||
if err != nil { | ||
|
@@ -736,7 +748,7 @@ func getJobLogData(ctx context.Context, client *github.Client, owner, repo strin | |
|
||
if returnContent { | ||
// Download and return the actual log content | ||
content, httpResp, err := downloadLogContent(url.String()) //nolint:bodyclose // Response body is closed in downloadLogContent, but we need to return httpResp | ||
content, httpResp, err := downloadLogContent(url.String(), tailLines) //nolint:bodyclose // Response body is closed in downloadLogContent, but we need to return httpResp | ||
if err != nil { | ||
// To keep the return value consistent wrap the response as a GitHub Response | ||
ghRes := &github.Response{ | ||
|
@@ -757,7 +769,7 @@ func getJobLogData(ctx context.Context, client *github.Client, owner, repo strin | |
} | ||
|
||
// downloadLogContent downloads the actual log content from a GitHub logs URL | ||
func downloadLogContent(logURL string) (string, *http.Response, error) { | ||
func downloadLogContent(logURL string, tailLines int) (string, *http.Response, error) { | ||
httpResp, err := http.Get(logURL) //nolint:gosec // URLs are provided by GitHub API and are safe | ||
if err != nil { | ||
return "", httpResp, fmt.Errorf("failed to download logs: %w", err) | ||
|
@@ -775,6 +787,16 @@ func downloadLogContent(logURL string) (string, *http.Response, error) { | |
|
||
// Clean up and format the log content for better readability | ||
logContent := strings.TrimSpace(string(content)) | ||
|
||
// Truncate to tail_lines if specified | ||
if tailLines > 0 { | ||
lines := strings.Split(logContent, "\n") | ||
if len(lines) > tailLines { | ||
lines = lines[len(lines)-tailLines:] | ||
logContent = strings.Join(lines, "\n") | ||
} | ||
} | ||
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. Nerdy, but you could build from reverse parsing the string maybe, for efficiency perhaps?
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. Could also add an offset from the bottom, so repeat calls could search upwards without getting the same data twice? 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. Yeah it's a good call not to split the log content and move from backwards. Regarding |
||
|
||
return logContent, httpResp, nil | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.
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 can encourage LLM using this option in the description. But I think the default behavior should be return all if
tail_lines
not specified?