-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix update command help text * Add support for CircleCI
- Loading branch information
Showing
6 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package parser | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/sethvargo/ratchet/resolver" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
type CircleCI struct{} | ||
|
||
// Parse pulls the CircleCI refs from the document. Unfortunately it does not | ||
// process "orbs" because there is no documented API for resolving orbs to an | ||
// absolute version. | ||
func (C *CircleCI) Parse(m *yaml.Node) (*RefsList, error) { | ||
var refs RefsList | ||
|
||
if m == nil { | ||
return nil, nil | ||
} | ||
|
||
if m.Kind != yaml.DocumentNode { | ||
return nil, fmt.Errorf("expected document node, got %v", m.Kind) | ||
} | ||
|
||
// Top-level object map | ||
for _, docMap := range m.Content { | ||
if docMap.Kind != yaml.MappingNode { | ||
continue | ||
} | ||
|
||
// jobs: and executors: keyword | ||
for i, jobsMap := range docMap.Content { | ||
if jobsMap.Value != "jobs" && jobsMap.Value != "executors" { | ||
continue | ||
} | ||
|
||
// Individual job names | ||
jobs := docMap.Content[i+1] | ||
if jobs.Kind != yaml.MappingNode { | ||
continue | ||
} | ||
|
||
for _, jobMap := range jobs.Content { | ||
if jobMap.Kind != yaml.MappingNode { | ||
continue | ||
} | ||
|
||
for j, sub := range jobMap.Content { | ||
// CI service container, should be resolved as a Docker reference. | ||
// This is a map, so the container value is nested a bit deeper. | ||
if sub.Value == "docker" { | ||
servicesMap := jobMap.Content[j+1] | ||
for _, subMap := range servicesMap.Content { | ||
if subMap.Kind != yaml.MappingNode { | ||
continue | ||
} | ||
|
||
for k, property := range subMap.Content { | ||
if property.Value == "image" { | ||
image := subMap.Content[k+1] | ||
ref := resolver.NormalizeContainerRef(image.Value) | ||
refs.Add(ref, image) | ||
break | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return &refs, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package parser | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestCircleCI_Parse(t *testing.T) { | ||
t.Parallel() | ||
|
||
cases := []struct { | ||
name string | ||
in string | ||
exp []string | ||
}{ | ||
{ | ||
name: "mostly_empty_file", | ||
in: ` | ||
executors: | ||
`, | ||
exp: []string{}, | ||
}, | ||
{ | ||
name: "executor", | ||
in: ` | ||
executors: | ||
my-executor: | ||
docker: | ||
- image: 'docker://ubuntu:20.04' | ||
`, | ||
exp: []string{ | ||
"container://ubuntu:20.04", | ||
}, | ||
}, | ||
{ | ||
name: "job", | ||
in: ` | ||
jobs: | ||
my-job: | ||
docker: | ||
- image: 'ubuntu:20.04' | ||
- image: 'ubuntu:22.04' | ||
`, | ||
exp: []string{ | ||
"container://ubuntu:20.04", | ||
"container://ubuntu:22.04", | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
tc := tc | ||
|
||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
m := helperStringToYAML(t, tc.in) | ||
|
||
refs, err := new(CircleCI).Parse(m) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if got, want := refs.Refs(), tc.exp; !reflect.DeepEqual(got, want) { | ||
t.Errorf("expected %q to be %q", got, want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
version: '2.1' | ||
|
||
orbs: | ||
my-orb: 'circleci/hello-build@0.0.5' | ||
my-other-orb: 'circleci/hello-build@0.0.3' | ||
|
||
executors: | ||
my-executor: | ||
docker: | ||
- image: 'cimg/base:2022.05-22.04' | ||
|
||
jobs: | ||
build: | ||
docker: | ||
- image: 'cimg/base:2022.05-22.04' | ||
- image: 'ubuntu:20.04' |