Skip to content

Commit 828b1e8

Browse files
Merge pull request #45 from codefresh-io/CR-5420
Cr 5420
2 parents 7812eed + 3de2a89 commit 828b1e8

File tree

5 files changed

+318
-4
lines changed

5 files changed

+318
-4
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.32.6
1+
0.33.0

pkg/codefresh/codefresh.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ type (
3636
Runtime() IRuntimeAPI
3737
GitSource() IGitSourceAPI
3838
Component() IComponentAPI
39+
Workflow() IWorkflowV2API
40+
Pipeline() IPipelineV2API
3941
}
4042
)
4143

@@ -108,6 +110,14 @@ func (c *codefresh) Component() IComponentAPI {
108110
return newComponentAPI(c)
109111
}
110112

113+
func (c *codefresh) Workflow() IWorkflowV2API {
114+
return newWorkflowV2API(c)
115+
}
116+
117+
func (c *codefresh) Pipeline() IPipelineV2API {
118+
return newPipelineV2API(c)
119+
}
120+
111121
func (c *codefresh) requestAPI(opt *requestOptions) (*http.Response, error) {
112122
return c.requestAPIWithContext(context.Background(), opt)
113123
}

pkg/codefresh/model/models_gen.go

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/codefresh/pipeline_v2.go

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package codefresh
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/codefresh-io/go-sdk/pkg/codefresh/model"
8+
)
9+
10+
type (
11+
IPipelineV2API interface {
12+
Get(ctx context.Context, name, namespace, runtime string) (*model.Pipeline, error)
13+
List(ctx context.Context, filterArgs model.PipelinesFilterArgs) ([]model.Pipeline, error)
14+
}
15+
16+
pipelineV2 struct {
17+
codefresh *codefresh
18+
}
19+
20+
graphqlListPipelinesResponse struct {
21+
Data struct {
22+
Pipelines model.PipelinePage
23+
}
24+
Errors []graphqlError
25+
}
26+
27+
graphqlGetPipelineResponse struct {
28+
Data struct {
29+
Pipeline model.Pipeline
30+
}
31+
Errors []graphqlError
32+
}
33+
)
34+
35+
func newPipelineV2API(codefresh *codefresh) IPipelineV2API {
36+
return &pipelineV2{codefresh: codefresh}
37+
}
38+
39+
func (p *pipelineV2) Get(ctx context.Context, name, namespace, runtime string) (*model.Pipeline, error) {
40+
jsonData := map[string]interface{}{
41+
"query": `
42+
query Pipeline(
43+
$runtime: String!
44+
$name: String!
45+
$namespace: String
46+
) {
47+
pipeline(name: $name, namespace: $namespace, runtime: $runtime) {
48+
metadata {
49+
name
50+
namespace
51+
runtime
52+
}
53+
self {
54+
healthStatus
55+
syncStatus
56+
version
57+
}
58+
projects
59+
spec {
60+
trigger
61+
}
62+
}
63+
}`,
64+
"variables": map[string]interface{}{
65+
"runtime": runtime,
66+
"name": name,
67+
"namespace": namespace,
68+
},
69+
}
70+
71+
res := &graphqlGetPipelineResponse{}
72+
err := p.codefresh.graphqlAPI(ctx, jsonData, res)
73+
if err != nil {
74+
return nil, fmt.Errorf("failed getting pipeline: %w", err)
75+
}
76+
77+
if len(res.Errors) > 0 {
78+
return nil, graphqlErrorResponse{errors: res.Errors}
79+
}
80+
81+
if res.Data.Pipeline.Metadata == nil {
82+
return nil, err
83+
}
84+
85+
return &res.Data.Pipeline, nil
86+
}
87+
88+
func (p *pipelineV2) List(ctx context.Context, filterArgs model.PipelinesFilterArgs) ([]model.Pipeline, error) {
89+
jsonData := map[string]interface{}{
90+
"query": `
91+
query Pipelines($filters: PipelinesFilterArgs) {
92+
pipelines(filters: $filters) {
93+
edges {
94+
node {
95+
metadata {
96+
name
97+
namespace
98+
runtime
99+
}
100+
self {
101+
healthStatus
102+
syncStatus
103+
version
104+
}
105+
projects
106+
spec {
107+
trigger
108+
}
109+
}
110+
}
111+
}
112+
}`,
113+
"variables": map[string]interface{}{
114+
"filters": filterArgs,
115+
},
116+
}
117+
118+
res := &graphqlListPipelinesResponse{}
119+
err := p.codefresh.graphqlAPI(ctx, jsonData, res)
120+
if err != nil {
121+
return nil, fmt.Errorf("failed getting pipeline list: %w", err)
122+
}
123+
124+
if len(res.Errors) > 0 {
125+
return nil, graphqlErrorResponse{errors: res.Errors}
126+
}
127+
128+
pipelines := make([]model.Pipeline, len(res.Data.Pipelines.Edges))
129+
for i := range res.Data.Pipelines.Edges {
130+
pipelines[i] = *res.Data.Pipelines.Edges[i].Node
131+
}
132+
133+
return pipelines, nil
134+
}

pkg/codefresh/workflow_v2.go

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
package codefresh
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/codefresh-io/go-sdk/pkg/codefresh/model"
8+
)
9+
10+
type (
11+
IWorkflowV2API interface {
12+
Get(ctx context.Context, uid string) (*model.Workflow, error)
13+
List(ctx context.Context, filterArgs model.WorkflowsFilterArgs) ([]model.Workflow, error)
14+
}
15+
16+
workflowV2 struct {
17+
codefresh *codefresh
18+
}
19+
20+
graphqlListWorkflowsResponse struct {
21+
Data struct {
22+
Workflows model.WorkflowPage
23+
}
24+
Errors []graphqlError
25+
}
26+
27+
graphqlGetWorkflowResponse struct {
28+
Data struct {
29+
Workflow model.Workflow
30+
}
31+
Errors []graphqlError
32+
}
33+
)
34+
35+
func newWorkflowV2API(codefresh *codefresh) IWorkflowV2API {
36+
return &workflowV2{codefresh: codefresh}
37+
}
38+
39+
func (w *workflowV2) Get(ctx context.Context, uid string) (*model.Workflow, error) {
40+
jsonData := map[string]interface{}{
41+
"query": `
42+
query Workflow(
43+
$uid: String!
44+
) {
45+
workflow(uid: $uid) {
46+
metadata {
47+
uid
48+
name
49+
namespace
50+
runtime
51+
}
52+
projects
53+
spec {
54+
entrypoint
55+
templates {
56+
name
57+
}
58+
workflowTemplateRef {
59+
name
60+
namespace
61+
}
62+
}
63+
status {
64+
phase
65+
progress {
66+
total
67+
done
68+
}
69+
nodes {
70+
type
71+
name
72+
}
73+
}
74+
pipeline {
75+
metadata {
76+
name
77+
namespace
78+
}
79+
}
80+
}
81+
}`,
82+
"variables": map[string]interface{}{
83+
"uid": uid,
84+
},
85+
}
86+
87+
res := &graphqlGetWorkflowResponse{}
88+
err := w.codefresh.graphqlAPI(ctx, jsonData, res)
89+
if err != nil {
90+
return nil, fmt.Errorf("failed getting workflow: %w", err)
91+
}
92+
93+
if len(res.Errors) > 0 {
94+
return nil, graphqlErrorResponse{errors: res.Errors}
95+
}
96+
97+
if res.Data.Workflow.Metadata == nil {
98+
return nil, err
99+
}
100+
101+
return &res.Data.Workflow, nil
102+
}
103+
104+
func (w *workflowV2) List(ctx context.Context, filterArgs model.WorkflowsFilterArgs) ([]model.Workflow, error) {
105+
jsonData := map[string]interface{}{
106+
"query": `
107+
query Workflows($filters: WorkflowsFilterArgs) {
108+
workflows(filters: $filters) {
109+
edges {
110+
node {
111+
metadata {
112+
uid
113+
name
114+
namespace
115+
runtime
116+
}
117+
projects
118+
spec {
119+
entrypoint
120+
templates {
121+
name
122+
}
123+
workflowTemplateRef {
124+
name
125+
namespace
126+
}
127+
}
128+
status {
129+
phase
130+
progress {
131+
total
132+
done
133+
}
134+
nodes {
135+
type
136+
name
137+
}
138+
}
139+
pipeline {
140+
metadata {
141+
name
142+
namespace
143+
}
144+
}
145+
}
146+
}
147+
}
148+
}`,
149+
"variables": map[string]interface{}{
150+
"filters": filterArgs,
151+
},
152+
}
153+
154+
res := &graphqlListWorkflowsResponse{}
155+
err := w.codefresh.graphqlAPI(ctx, jsonData, res)
156+
if err != nil {
157+
return nil, fmt.Errorf("failed getting workflow list: %w", err)
158+
}
159+
160+
if len(res.Errors) > 0 {
161+
return nil, graphqlErrorResponse{errors: res.Errors}
162+
}
163+
164+
workflows := make([]model.Workflow, len(res.Data.Workflows.Edges))
165+
for i := range res.Data.Workflows.Edges {
166+
workflows[i] = *res.Data.Workflows.Edges[i].Node
167+
}
168+
169+
return workflows, nil
170+
}

0 commit comments

Comments
 (0)