|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/harness/harness-mcp/client/dto" |
| 8 | +) |
| 9 | + |
| 10 | +const ( |
| 11 | + logDownloadPath = "log-service/blob/download" |
| 12 | +) |
| 13 | + |
| 14 | +// LogService handles operations related to pipeline logs |
| 15 | +type LogService struct { |
| 16 | + client *Client |
| 17 | +} |
| 18 | + |
| 19 | +// DownloadLogs fetches a download URL for pipeline execution logs |
| 20 | +func (l *LogService) DownloadLogs(ctx context.Context, scope dto.Scope, planExecutionID string) (string, error) { |
| 21 | + // First, get the pipeline execution details to determine the prefix format |
| 22 | + pipelineService := &PipelineService{client: l.client} |
| 23 | + execution, err := pipelineService.GetExecution(ctx, scope, planExecutionID) |
| 24 | + if err != nil { |
| 25 | + return "", fmt.Errorf("failed to get execution details: %w", err) |
| 26 | + } |
| 27 | + |
| 28 | + // Build the prefix based on the execution details |
| 29 | + var prefix string |
| 30 | + if execution.Data.ShouldUseSimplifiedBaseKey { |
| 31 | + // Simplified key format |
| 32 | + prefix = fmt.Sprintf("%s/pipeline/%s/%d/-%s", |
| 33 | + scope.AccountID, |
| 34 | + execution.Data.PipelineIdentifier, |
| 35 | + execution.Data.RunSequence, |
| 36 | + planExecutionID) |
| 37 | + } else { |
| 38 | + // Standard key format |
| 39 | + prefix = fmt.Sprintf("accountId:%s/orgId:%s/projectId:%s/pipelineId:%s/runSequence:%d/level0:pipeline", |
| 40 | + scope.AccountID, |
| 41 | + execution.Data.OrgIdentifier, |
| 42 | + execution.Data.ProjectIdentifier, |
| 43 | + execution.Data.PipelineIdentifier, |
| 44 | + execution.Data.RunSequence) |
| 45 | + } |
| 46 | + |
| 47 | + // Prepare query parameters |
| 48 | + params := make(map[string]string) |
| 49 | + params["accountID"] = scope.AccountID |
| 50 | + params["prefix"] = prefix |
| 51 | + |
| 52 | + // Initialize the response object |
| 53 | + response := &dto.LogDownloadResponse{} |
| 54 | + |
| 55 | + // Make the POST request |
| 56 | + err = l.client.Post(ctx, logDownloadPath, params, nil, response) |
| 57 | + if err != nil { |
| 58 | + return "", fmt.Errorf("failed to fetch log download URL: %w", err) |
| 59 | + } |
| 60 | + |
| 61 | + return response.Link, nil |
| 62 | +} |
0 commit comments