@@ -17,13 +17,11 @@ limitations under the License.
1717package crd
1818
1919import (
20- "encoding/json"
2120 "fmt"
2221 "io"
2322 "net/http"
2423 "os"
2524 "path/filepath"
26- "regexp"
2725 "sort"
2826 "strings"
2927
@@ -32,31 +30,9 @@ import (
3230 "github.com/triggermesh/tmctl/pkg/log"
3331)
3432
35- const (
36- crdsURL = "https://github.com/triggermesh/triggermesh/releases/download/$VERSION/triggermesh-crds.yaml"
37- commitStatusURL = "https://api.github.com/repos/triggermesh/triggermesh/commits/$REF/status"
38- ciArtifactsURL = "https://circleci.com/api/v2/project/gh/triggermesh/triggermesh/$JOB/artifacts"
39-
40- ciBuildImageContext = "ci/circleci: publish-image"
41- )
42-
43- var semverRegexp = regexp .MustCompile (`^v(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$` )
44-
45- type ghJobStatus struct {
46- SHA string `json:"sha"`
47- Statuses []struct {
48- State string `json:"state"`
49- TargetURL string `json:"target_url"`
50- Context string `json:"context"`
51- } `json:"statuses"`
52- }
53-
54- type ciArtifacts struct {
55- Items []struct {
56- URL string `json:"url"`
57- } `json:"items"`
58- }
33+ const crdsURL = "https://github.com/triggermesh/triggermesh/releases/download/$VERSION/triggermesh-crds.yaml"
5934
35+ // CRD represents the custom resource definition for CLI functions.
6036type CRD struct {
6137 APIVersion string `yaml:"apiVersion"`
6238 Kind string `yaml:"kind"`
@@ -121,15 +97,9 @@ func Fetch(configDir, version string) (map[string]CRD, error) {
12197 }
12298 defer out .Close ()
12399
124- var resp * http.Response
125- if release := semverRegexp .MatchString (version ); release {
126- if resp , err = http .Get (strings .ReplaceAll (crdsURL , "$VERSION" , version )); err != nil {
127- return nil , err
128- }
129- } else {
130- if resp , err = getBuildArtifact (version ); err != nil {
131- return nil , fmt .Errorf ("%q build: %w" , version , err )
132- }
100+ resp , err := http .Get (strings .ReplaceAll (crdsURL , "$VERSION" , version ))
101+ if err != nil {
102+ return nil , err
133103 }
134104 defer resp .Body .Close ()
135105
@@ -148,6 +118,7 @@ func Fetch(configDir, version string) (map[string]CRD, error) {
148118 return Parse (f )
149119}
150120
121+ // Parse reads the CRD file contents into the map.
151122func Parse (reader io.ReadCloser ) (map [string ]CRD , error ) {
152123 var crds []CRD
153124 decoder := yaml .NewDecoder (reader )
@@ -203,52 +174,3 @@ func ListTargets(crds map[string]CRD) ([]string, error) {
203174 sort .Strings (result )
204175 return result , nil
205176}
206-
207- func getBuildArtifact (ref string ) (* http.Response , error ) {
208- ghCommit , err := http .Get (strings .Replace (commitStatusURL , "$REF" , ref , - 1 ))
209- if err != nil {
210- return nil , err
211- }
212- defer ghCommit .Body .Close ()
213-
214- var ghJob ghJobStatus
215- if err := json .NewDecoder (ghCommit .Body ).Decode (& ghJob ); err != nil {
216- return nil , err
217- }
218- ciJobID := ""
219- for _ , status := range ghJob .Statuses {
220- if status .Context == ciBuildImageContext {
221- if status .State != "success" {
222- return nil , fmt .Errorf ("dev build is not ready" )
223- }
224- path := strings .Split (status .TargetURL , "/" )
225- ciJobID = path [len (path )- 1 ]
226- break
227- }
228- }
229- if ciJobID == "" {
230- return nil , fmt .Errorf ("CI build job not found" )
231- }
232-
233- ciBuild , err := http .Get (strings .Replace (ciArtifactsURL , "$JOB" , ciJobID , - 1 ))
234- if err != nil {
235- return nil , err
236- }
237- defer ciBuild .Body .Close ()
238-
239- var artifacts ciArtifacts
240- if err := json .NewDecoder (ciBuild .Body ).Decode (& artifacts ); err != nil {
241- return nil , err
242- }
243- crdURL := ""
244- for _ , artifact := range artifacts .Items {
245- if strings .HasSuffix (artifact .URL , "/triggermesh-crds.yaml" ) {
246- crdURL = artifact .URL
247- break
248- }
249- }
250- if crdURL == "" {
251- return nil , fmt .Errorf ("CRD artifact not found" )
252- }
253- return http .Get (crdURL )
254- }
0 commit comments