Skip to content

Commit b924e06

Browse files
authored
Split client and provider into separate packages (#2)
* Add client package * Added project import Fix token defaults * Add pipeline import * Add project tests * Refactoring * Add pipeline tests * Fix pipeline triggers * Added tests for pipeline triggers * Refactoring * Add original_yaml_string and runtime_environment to pipeline * Add pipeline revision * Added a test for pipeline revision * Fix SetVariables methods
1 parent 9f013fe commit b924e06

14 files changed

+1753
-610
lines changed

client/client.go

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package client
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"io/ioutil"
8+
"net/http"
9+
"strings"
10+
)
11+
12+
// Client token, host, htpp.Client
13+
type Client struct {
14+
Token string
15+
Host string
16+
Client *http.Client
17+
}
18+
19+
// RequestOptions path, method, etc
20+
type RequestOptions struct {
21+
Path string
22+
Method string
23+
Body []byte
24+
QS map[string]string
25+
}
26+
27+
// NewClient returns a new client configured to communicate on a server with the
28+
// given hostname and to send an Authorization Header with the value of
29+
// token
30+
func NewClient(hostname string, token string) *Client {
31+
return &Client{
32+
Host: hostname,
33+
Token: token,
34+
Client: &http.Client{},
35+
}
36+
37+
}
38+
39+
// RequestAPI http request to Codefresh API
40+
func (client *Client) RequestAPI(opt *RequestOptions) ([]byte, error) {
41+
finalURL := fmt.Sprintf("%s%s", client.Host, opt.Path)
42+
if opt.QS != nil {
43+
finalURL += ToQS(opt.QS)
44+
}
45+
request, err := http.NewRequest(opt.Method, finalURL, bytes.NewBuffer(opt.Body))
46+
if err != nil {
47+
return nil, err
48+
}
49+
50+
request.Header.Set("Authorization", client.Token)
51+
request.Header.Set("Content-Type", "application/json; charset=utf-8")
52+
53+
resp, err := client.Client.Do(request)
54+
55+
if err != nil {
56+
return nil, err
57+
}
58+
defer resp.Body.Close()
59+
60+
body, err := ioutil.ReadAll(resp.Body)
61+
if err != nil {
62+
return nil, fmt.Errorf("Failed to read body %v %v", resp.StatusCode, resp.Status)
63+
}
64+
65+
if resp.StatusCode != 200 {
66+
return nil, fmt.Errorf("%v, %s", resp.Status, string(body))
67+
}
68+
return body, nil
69+
}
70+
71+
// ToQS add extra parameters to path
72+
func ToQS(qs map[string]string) string {
73+
var arr = []string{}
74+
for k, v := range qs {
75+
arr = append(arr, fmt.Sprintf("%s=%s", k, v))
76+
}
77+
return "?" + strings.Join(arr, "&")
78+
}
79+
80+
// DecodeResponseInto json Unmarshall
81+
func DecodeResponseInto(body []byte, target interface{}) error {
82+
return json.Unmarshal(body, target)
83+
}
84+
85+
// EncodeToJSON json Marshal
86+
func EncodeToJSON(object interface{}) ([]byte, error) {
87+
body, err := json.Marshal(object)
88+
if err != nil {
89+
return nil, err
90+
}
91+
return body, nil
92+
}

client/pipeline.go

+198
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
package client
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"strings"
7+
)
8+
9+
type ErrorResponse struct {
10+
Status int `json:"status,omitempty"`
11+
Message string `json:"message,omitempty"`
12+
Error string `json:"error,omitempty"`
13+
}
14+
15+
type Labels struct {
16+
Tags []string `json:"tags,omitempty"`
17+
}
18+
19+
type Metadata struct {
20+
Name string `json:"name,omitempty"`
21+
ID string `json:"id,omitempty"`
22+
Labels Labels `json:"labels,omitempty"`
23+
OriginalYamlString string `json:"originalYamlString,omitempty"`
24+
Project string `json:"project,omitempty"`
25+
ProjectId string `json:"projectId,omitempty"`
26+
Revision int `json:"revision,omitempty"`
27+
}
28+
29+
type SpecTemplate struct {
30+
Location string `json:"location,omitempty"`
31+
Repo string `json:"repo,omitempty"`
32+
Path string `json:"path,omitempty"`
33+
Revision string `json:"revision,omitempty"`
34+
Context string `json:"context,omitempty"`
35+
}
36+
37+
type Trigger struct {
38+
Name string `json:"name,omitempty"`
39+
Description string `json:"description,omitempty"`
40+
Type string `json:"type,omitempty"`
41+
Repo string `json:"repo,omitempty"`
42+
Events []string `json:"events,omitempty"`
43+
BranchRegex string `json:"branchRegex,omitempty"`
44+
ModifiedFilesGlob string `json:"modifiedFilesGlob,omitempty"`
45+
Provider string `json:"provider,omitempty"`
46+
Disabled bool `json:"disabled,omitempty"`
47+
Context string `json:"context,omitempty"`
48+
Variables []Variable `json:"variables,omitempty"`
49+
}
50+
51+
type RuntimeEnvironment struct {
52+
Name string `json:"name,omitempty"`
53+
Memory string `json:"memory,omitempty"`
54+
CPU string `json:"cpu,omitempty"`
55+
DindStorage string `json:"dindStorage,omitempty"`
56+
}
57+
58+
func (t *Trigger) SetVariables(variables map[string]string) {
59+
for key, value := range variables {
60+
t.Variables = append(t.Variables, Variable{Key: key, Value: value})
61+
}
62+
}
63+
64+
type Spec struct {
65+
Variables []Variable `json:"variables,omitempty"`
66+
SpecTemplate *SpecTemplate `json:"specTemplate,omitempty"`
67+
Triggers []Trigger `json:"triggers,omitempty"`
68+
Priority int `json:"priority,omitempty"`
69+
Concurrency int `json:"concurrency,omitempty"`
70+
Contexts []interface{} `json:"contexts,omitempty"`
71+
Steps map[string]interface{} `json:"steps,omitempty"`
72+
Stages []interface{} `json:"stages,omitempty"`
73+
Mode string `json:"mode,omitempty"`
74+
RuntimeEnvironment RuntimeEnvironment `json:"runtimeEnvironment,omitempty"`
75+
}
76+
77+
type Pipeline struct {
78+
Metadata Metadata `json:"metadata,omitempty"`
79+
Spec Spec `json:"spec,omitempty"`
80+
Version string `json:"version,omitempty"`
81+
}
82+
83+
func (p *Pipeline) SetVariables(variables map[string]string) {
84+
for key, value := range variables {
85+
p.Spec.Variables = append(p.Spec.Variables, Variable{Key: key, Value: value})
86+
}
87+
}
88+
89+
func (pipeline *Pipeline) GetID() string {
90+
if pipeline.Metadata.ID != "" {
91+
return pipeline.Metadata.ID
92+
} else {
93+
return pipeline.Metadata.Name
94+
}
95+
}
96+
97+
func (client *Client) GetPipeline(name string) (*Pipeline, error) {
98+
fullPath := fmt.Sprintf("/pipelines/%s", strings.Replace(name, "/", "%2F", 1))
99+
opts := RequestOptions{
100+
Path: fullPath,
101+
Method: "GET",
102+
}
103+
104+
resp, err := client.RequestAPI(&opts)
105+
106+
if err != nil {
107+
return nil, err
108+
}
109+
110+
var pipeline Pipeline
111+
112+
err = DecodeResponseInto(resp, &pipeline)
113+
if err != nil {
114+
return nil, err
115+
}
116+
117+
return &pipeline, nil
118+
}
119+
120+
func (client *Client) CreatePipeline(pipeline *Pipeline) (*Pipeline, error) {
121+
122+
body, err := EncodeToJSON(pipeline)
123+
124+
if err != nil {
125+
return nil, err
126+
}
127+
opts := RequestOptions{
128+
Path: "/pipelines",
129+
Method: "POST",
130+
Body: body,
131+
}
132+
133+
resp, err := client.RequestAPI(&opts)
134+
135+
if err != nil {
136+
return nil, err
137+
}
138+
139+
var respPipeline Pipeline
140+
err = DecodeResponseInto(resp, &respPipeline)
141+
if err != nil {
142+
return nil, err
143+
}
144+
145+
return &respPipeline, nil
146+
147+
}
148+
149+
func (client *Client) UpdatePipeline(pipeline *Pipeline) (*Pipeline, error) {
150+
151+
body, err := EncodeToJSON(pipeline)
152+
153+
if err != nil {
154+
return nil, err
155+
}
156+
157+
id := pipeline.GetID()
158+
if id == "" {
159+
return nil, errors.New("[ERROR] Both Pipeline ID and Name are empty")
160+
}
161+
162+
fullPath := fmt.Sprintf("/pipelines/%s", strings.Replace(id, "/", "%2F", 1))
163+
opts := RequestOptions{
164+
Path: fullPath,
165+
Method: "PUT",
166+
Body: body,
167+
}
168+
169+
resp, err := client.RequestAPI(&opts)
170+
if err != nil {
171+
return nil, err
172+
}
173+
174+
var respPipeline Pipeline
175+
err = DecodeResponseInto(resp, &respPipeline)
176+
if err != nil {
177+
return nil, err
178+
}
179+
180+
return &respPipeline, nil
181+
}
182+
183+
func (client *Client) DeletePipeline(name string) error {
184+
185+
fullPath := fmt.Sprintf("/pipelines/%s", strings.Replace(name, "/", "%2F", 1))
186+
opts := RequestOptions{
187+
Path: fullPath,
188+
Method: "DELETE",
189+
}
190+
191+
_, err := client.RequestAPI(&opts)
192+
193+
if err != nil {
194+
return err
195+
}
196+
197+
return nil
198+
}

0 commit comments

Comments
 (0)