diff --git a/VERSION b/VERSION index 8344134..be386c9 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.32.6 +0.33.0 diff --git a/pkg/codefresh/codefresh.go b/pkg/codefresh/codefresh.go index 67b4198..5e3553b 100644 --- a/pkg/codefresh/codefresh.go +++ b/pkg/codefresh/codefresh.go @@ -36,6 +36,8 @@ type ( Runtime() IRuntimeAPI GitSource() IGitSourceAPI Component() IComponentAPI + Workflow() IWorkflowV2API + Pipeline() IPipelineV2API } ) @@ -108,6 +110,14 @@ func (c *codefresh) Component() IComponentAPI { return newComponentAPI(c) } +func (c *codefresh) Workflow() IWorkflowV2API { + return newWorkflowV2API(c) +} + +func (c *codefresh) Pipeline() IPipelineV2API { + return newPipelineV2API(c) +} + func (c *codefresh) requestAPI(opt *requestOptions) (*http.Response, error) { return c.requestAPIWithContext(context.Background(), opt) } diff --git a/pkg/codefresh/model/models_gen.go b/pkg/codefresh/model/models_gen.go index d7c7477..f8203c2 100644 --- a/pkg/codefresh/model/models_gen.go +++ b/pkg/codefresh/model/models_gen.go @@ -949,6 +949,8 @@ type ObjectMeta struct { LastUpdated *string `json:"lastUpdated"` // Created Created *string `json:"created"` + // K8s object uid + UID *string `json:"uid"` } // Information about current page @@ -1226,10 +1228,8 @@ type Runtime struct { Self *AppProject `json:"self"` // Projects Projects []string `json:"projects"` - // K8s cluster where the runtime is running + // Cluster Cluster *string `json:"cluster"` - // Ingress host of the runtime - IngressHost *string `json:"ingressHost"` // Runtime version RuntimeVersion *string `json:"runtimeVersion"` } diff --git a/pkg/codefresh/pipeline_v2.go b/pkg/codefresh/pipeline_v2.go new file mode 100644 index 0000000..75c1e5b --- /dev/null +++ b/pkg/codefresh/pipeline_v2.go @@ -0,0 +1,134 @@ +package codefresh + +import ( + "context" + "fmt" + + "github.com/codefresh-io/go-sdk/pkg/codefresh/model" +) + +type ( + IPipelineV2API interface { + Get(ctx context.Context, name, namespace, runtime string) (*model.Pipeline, error) + List(ctx context.Context, filterArgs model.PipelinesFilterArgs) ([]model.Pipeline, error) + } + + pipelineV2 struct { + codefresh *codefresh + } + + graphqlListPipelinesResponse struct { + Data struct { + Pipelines model.PipelinePage + } + Errors []graphqlError + } + + graphqlGetPipelineResponse struct { + Data struct { + Pipeline model.Pipeline + } + Errors []graphqlError + } +) + +func newPipelineV2API(codefresh *codefresh) IPipelineV2API { + return &pipelineV2{codefresh: codefresh} +} + +func (p *pipelineV2) Get(ctx context.Context, name, namespace, runtime string) (*model.Pipeline, error) { + jsonData := map[string]interface{}{ + "query": ` + query Pipeline( + $runtime: String! + $name: String! + $namespace: String + ) { + pipeline(name: $name, namespace: $namespace, runtime: $runtime) { + metadata { + name + namespace + runtime + } + self { + healthStatus + syncStatus + version + } + projects + spec { + trigger + } + } + }`, + "variables": map[string]interface{}{ + "runtime": runtime, + "name": name, + "namespace": namespace, + }, + } + + res := &graphqlGetPipelineResponse{} + err := p.codefresh.graphqlAPI(ctx, jsonData, res) + if err != nil { + return nil, fmt.Errorf("failed getting pipeline: %w", err) + } + + if len(res.Errors) > 0 { + return nil, graphqlErrorResponse{errors: res.Errors} + } + + if res.Data.Pipeline.Metadata == nil { + return nil, err + } + + return &res.Data.Pipeline, nil +} + +func (p *pipelineV2) List(ctx context.Context, filterArgs model.PipelinesFilterArgs) ([]model.Pipeline, error) { + jsonData := map[string]interface{}{ + "query": ` + query Pipelines($filters: PipelinesFilterArgs) { + pipelines(filters: $filters) { + edges { + node { + metadata { + name + namespace + runtime + } + self { + healthStatus + syncStatus + version + } + projects + spec { + trigger + } + } + } + } + }`, + "variables": map[string]interface{}{ + "filters": filterArgs, + }, + } + + res := &graphqlListPipelinesResponse{} + err := p.codefresh.graphqlAPI(ctx, jsonData, res) + if err != nil { + return nil, fmt.Errorf("failed getting pipeline list: %w", err) + } + + if len(res.Errors) > 0 { + return nil, graphqlErrorResponse{errors: res.Errors} + } + + pipelines := make([]model.Pipeline, len(res.Data.Pipelines.Edges)) + for i := range res.Data.Pipelines.Edges { + pipelines[i] = *res.Data.Pipelines.Edges[i].Node + } + + return pipelines, nil +} diff --git a/pkg/codefresh/workflow_v2.go b/pkg/codefresh/workflow_v2.go new file mode 100644 index 0000000..60c5d9e --- /dev/null +++ b/pkg/codefresh/workflow_v2.go @@ -0,0 +1,170 @@ +package codefresh + +import ( + "context" + "fmt" + + "github.com/codefresh-io/go-sdk/pkg/codefresh/model" +) + +type ( + IWorkflowV2API interface { + Get(ctx context.Context, uid string) (*model.Workflow, error) + List(ctx context.Context, filterArgs model.WorkflowsFilterArgs) ([]model.Workflow, error) + } + + workflowV2 struct { + codefresh *codefresh + } + + graphqlListWorkflowsResponse struct { + Data struct { + Workflows model.WorkflowPage + } + Errors []graphqlError + } + + graphqlGetWorkflowResponse struct { + Data struct { + Workflow model.Workflow + } + Errors []graphqlError + } +) + +func newWorkflowV2API(codefresh *codefresh) IWorkflowV2API { + return &workflowV2{codefresh: codefresh} +} + +func (w *workflowV2) Get(ctx context.Context, uid string) (*model.Workflow, error) { + jsonData := map[string]interface{}{ + "query": ` + query Workflow( + $uid: String! + ) { + workflow(uid: $uid) { + metadata { + uid + name + namespace + runtime + } + projects + spec { + entrypoint + templates { + name + } + workflowTemplateRef { + name + namespace + } + } + status { + phase + progress { + total + done + } + nodes { + type + name + } + } + pipeline { + metadata { + name + namespace + } + } + } + }`, + "variables": map[string]interface{}{ + "uid": uid, + }, + } + + res := &graphqlGetWorkflowResponse{} + err := w.codefresh.graphqlAPI(ctx, jsonData, res) + if err != nil { + return nil, fmt.Errorf("failed getting workflow: %w", err) + } + + if len(res.Errors) > 0 { + return nil, graphqlErrorResponse{errors: res.Errors} + } + + if res.Data.Workflow.Metadata == nil { + return nil, err + } + + return &res.Data.Workflow, nil +} + +func (w *workflowV2) List(ctx context.Context, filterArgs model.WorkflowsFilterArgs) ([]model.Workflow, error) { + jsonData := map[string]interface{}{ + "query": ` + query Workflows($filters: WorkflowsFilterArgs) { + workflows(filters: $filters) { + edges { + node { + metadata { + uid + name + namespace + runtime + } + projects + spec { + entrypoint + templates { + name + } + workflowTemplateRef { + name + namespace + } + } + status { + phase + progress { + total + done + } + nodes { + type + name + } + } + pipeline { + metadata { + name + namespace + } + } + } + } + } + }`, + "variables": map[string]interface{}{ + "filters": filterArgs, + }, + } + + res := &graphqlListWorkflowsResponse{} + err := w.codefresh.graphqlAPI(ctx, jsonData, res) + if err != nil { + return nil, fmt.Errorf("failed getting workflow list: %w", err) + } + + if len(res.Errors) > 0 { + return nil, graphqlErrorResponse{errors: res.Errors} + } + + workflows := make([]model.Workflow, len(res.Data.Workflows.Edges)) + for i := range res.Data.Workflows.Edges { + workflows[i] = *res.Data.Workflows.Edges[i].Node + } + + return workflows, nil +}