Skip to content

Commit

Permalink
Merge pull request #123 from CircleCI-Public/orb-source-version
Browse files Browse the repository at this point in the history
Use orbVersionRef query when fetching source
  • Loading branch information
Zachary Scott authored Sep 24, 2018
2 parents 6fe1dfc + d038130 commit 80aa375
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 20 deletions.
45 changes: 31 additions & 14 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -730,28 +730,45 @@ func OrbPromote(ctx context.Context, logger *logger.Logger, namespace string, or
return &response.PromoteOrb.OrbPromoteResponse.Orb, err
}

// orbVersionRef is designed to ensure an orb reference fits the orbVersion query where orbVersionRef argument requires a version
func orbVersionRef(orb string) string {
split := strings.Split(orb, "@")
// We're expecting the API to tell us the reference is acceptable
// Without performing a lot of client-side validation
if len(split) > 1 {
return orb
}

// If no version was supplied, append @volatile to the reference
return fmt.Sprintf("%s@%s", split[0], "volatile")
}

// OrbSource gets the source or an orb
func OrbSource(ctx context.Context, logger *logger.Logger, namespace string, orb string) (string, error) {
name := namespace + "/" + orb
func OrbSource(ctx context.Context, logger *logger.Logger, orbRef string) (string, error) {
ref := orbVersionRef(orbRef)

var response struct {
Orb struct {
Versions []struct {
Source string
OrbVersion struct {
ID string
Version string
Orb struct {
ID string
}
Source string
}
}

query := `query($name: String!) {
orb(name: $name) {
versions(count: 1) {
source
}
query := `query($orbVersionRef: String!) {
orbVersion(orbVersionRef: $orbVersionRef) {
id
version
orb { id }
source
}
}`

request := client.NewAuthorizedRequest(viper.GetString("token"), query)
request.Var("name", name)
request.Var("orbVersionRef", ref)

address, err := GraphQLServerAddress(EnvEndpointHost())
if err != nil {
Expand All @@ -765,11 +782,11 @@ func OrbSource(ctx context.Context, logger *logger.Logger, namespace string, orb
return "", err
}

if len(response.Orb.Versions) != 1 {
return "", fmt.Errorf("the %s orb has never published a revision", name)
if response.OrbVersion.ID == "" {
return "", fmt.Errorf("the %s orb has never published a revision", orbRef)
}

return response.Orb.Versions[0].Source, nil
return response.OrbVersion.Source, nil
}

// ListOrbs queries the API to find all orbs.
Expand Down
26 changes: 26 additions & 0 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,32 @@ import (
"testing"
)

func TestOrbVersionRef(t *testing.T) {
var (
orbRef string
expected string
)

orbRef = orbVersionRef("foo/bar@baz")

expected = "foo/bar@baz"
if orbRef != expected {
t.Errorf("Expected %s, got %s", expected, orbRef)
}

orbRef = orbVersionRef("omg/bbq")
expected = "omg/bbq@volatile"
if orbRef != expected {
t.Errorf("Expected %s, got %s", expected, orbRef)
}

orbRef = orbVersionRef("omg/bbq@too@many@ats")
expected = "omg/bbq@too@many@ats"
if orbRef != expected {
t.Errorf("Expected %s, got %s", expected, orbRef)
}
}

func TestGraphQLServerAddress(t *testing.T) {
var (
addr string
Expand Down
21 changes: 15 additions & 6 deletions cmd/orb.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,15 @@ func newOrbCommand() *cobra.Command {
publishCommand.AddCommand(incrementCommand)

sourceCommand := &cobra.Command{
Use: "source NAMESPACE ORB",
Use: "source ORB",
Short: "Show the source of an orb",
RunE: showSource,
Args: cobra.ExactArgs(2),
Args: cobra.MaximumNArgs(2),
Annotations: make(map[string]string),
}
sourceCommand.Annotations["NAMESPACE"] = orbAnnotations["NAMESPACE"]
sourceCommand.Annotations["ORB"] = orbAnnotations["ORB"]
sourceCommand.Annotations["ORB"] = "The name of your orb, including namespace (i.e. circleci/rails)"
sourceCommand.Example = ` circleci orb source circleci/[email protected] # grab the source at version 0.1.4
circleci orb source my-ns/foo-orb@dev:latest # grab the source of dev release "latest"`

orbCreate := &cobra.Command{
Use: "create NAMESPACE ORB",
Expand Down Expand Up @@ -328,9 +329,17 @@ func createOrb(cmd *cobra.Command, args []string) error {
}

func showSource(cmd *cobra.Command, args []string) error {
source, err := api.OrbSource(context.Background(), Logger, args[0], args[1])
var ref string

if len(args) > 1 {
ref = fmt.Sprintf("%s/%s", args[0], args[1])
} else {
ref = args[0]
}

source, err := api.OrbSource(context.Background(), Logger, ref)
if err != nil {
return errors.Wrapf(err, "Failed to get source for '%s/%s'", args[1], args[0])
return errors.Wrapf(err, "Failed to get source for '%s'", ref)
}
Logger.Info(source)
return nil
Expand Down

0 comments on commit 80aa375

Please sign in to comment.