|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log/slog" |
| 7 | + |
| 8 | + "github.com/harness/harness-mcp/client/dto" |
| 9 | +) |
| 10 | + |
| 11 | +const ( |
| 12 | + acmCreateTaskPath = "/api/v1/accounts/%s/tasks" |
| 13 | + acmRunTaskPath = "/api/v1/accounts/%s/executions" |
| 14 | +) |
| 15 | + |
| 16 | +// ACMService provides methods to interact with the Autonomous Code Maintenance API |
| 17 | +type ACMService struct { |
| 18 | + Client *Client |
| 19 | +} |
| 20 | + |
| 21 | +// CreateTask creates a new autonomous code maintenance task |
| 22 | +func (s *ACMService) CreateTask(ctx context.Context, scope dto.Scope, params *dto.CreateACMTaskRequest) (*dto.ACMTaskResponse, error) { |
| 23 | + // Build the path |
| 24 | + path := fmt.Sprintf(acmCreateTaskPath, scope.AccountID) |
| 25 | + |
| 26 | + // Set up query parameters |
| 27 | + queryParams := make(map[string]string) |
| 28 | + |
| 29 | + headers := make(map[string]string) |
| 30 | + addHarnessAccountToHeaders(scope, headers) |
| 31 | + |
| 32 | + // Make the request |
| 33 | + result := new(dto.ACMTaskResponse) |
| 34 | + err := s.Client.Post(ctx, path, queryParams, params, headers, result) |
| 35 | + if err != nil { |
| 36 | + return nil, fmt.Errorf("failed to create ACM task: %w", err) |
| 37 | + } |
| 38 | + |
| 39 | + return result, nil |
| 40 | +} |
| 41 | + |
| 42 | +// ListTaskExecutions lists executions of an autonomous code maintenance task |
| 43 | +func (s *ACMService) ListTaskExecutions(ctx context.Context, scope dto.Scope, params *dto.GetACMExecutionsRequest) (*dto.ACMExecutionsListResponse, error) { |
| 44 | + // Build the path |
| 45 | + path := fmt.Sprintf(acmRunTaskPath, scope.AccountID) |
| 46 | + |
| 47 | + // Set up query parameters |
| 48 | + queryParams := make(map[string]string) |
| 49 | + |
| 50 | + headers := make(map[string]string) |
| 51 | + addHarnessAccountToHeaders(scope, headers) |
| 52 | + |
| 53 | + // Add specific query parameters for this endpoint |
| 54 | + queryParams["task_id"] = params.TaskID |
| 55 | + queryParams["page"] = fmt.Sprintf("%d", params.Page) |
| 56 | + queryParams["limit"] = fmt.Sprintf("%d", params.Limit) |
| 57 | + |
| 58 | + // Make the request |
| 59 | + result := new(dto.ACMExecutionsListResponse) |
| 60 | + err := s.Client.Get(ctx, path, queryParams, headers, result) |
| 61 | + if err != nil { |
| 62 | + return nil, fmt.Errorf("failed to list ACM task executions: %w", err) |
| 63 | + } |
| 64 | + |
| 65 | + return result, nil |
| 66 | +} |
| 67 | + |
| 68 | +// TriggerTaskExecution triggers execution of an autonomous code maintenance task |
| 69 | +func (s *ACMService) TriggerTaskExecution(ctx context.Context, scope dto.Scope, params *dto.TriggerACMTaskExecutionRequest) (*dto.ACMExecution, error) { |
| 70 | + // Build the path |
| 71 | + path := fmt.Sprintf(acmRunTaskPath, scope.AccountID) |
| 72 | + |
| 73 | + // Set up query parameters |
| 74 | + queryParams := make(map[string]string) |
| 75 | + |
| 76 | + headers := make(map[string]string) |
| 77 | + addHarnessAccountToHeaders(scope, headers) |
| 78 | + |
| 79 | + slog.Info("triggering ACM task execution", "task_id", params.TaskID, "repository_id", params.RepositoryID, "source_branch", params.SourceBranch) |
| 80 | + |
| 81 | + // Make the request |
| 82 | + result := new(dto.ACMExecution) |
| 83 | + err := s.Client.Post(ctx, path, queryParams, params, headers, result) |
| 84 | + if err != nil { |
| 85 | + return nil, fmt.Errorf("failed to trigger ACM task execution: %w", err) |
| 86 | + } |
| 87 | + |
| 88 | + return result, nil |
| 89 | +} |
0 commit comments