|
| 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