Skip to content

Commit

Permalink
Move namespace as a sub-command under new registry command
Browse files Browse the repository at this point in the history
This includes `namespace create`, so:

$ circleci registry namespace create ....
  • Loading branch information
Zachary Scott committed Aug 1, 2018
1 parent c08f7ef commit 19f60ac
Show file tree
Hide file tree
Showing 6 changed files with 273 additions and 232 deletions.
44 changes: 0 additions & 44 deletions cmd/orb.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,42 +66,16 @@ func newOrbCommand() *cobra.Command {
Args: cobra.ExactArgs(1),
}

createNamespace := &cobra.Command{
Use: "create [name] [vcs] [org-name]",
Short: "create an orb namespace",
RunE: createOrbNamespace,
Args: cobra.ExactArgs(3),
}

// "org-name", "", "organization name (required)"
// "vcs", "github", "organization vcs, e.g. 'github', 'bitbucket'"

namespaceCommand := &cobra.Command{
Use: "namespace",
Short: "Operate on orb namespaces (create, etc.)",
}
namespaceCommand.AddCommand(createNamespace)
nsCommand := &cobra.Command{
Use: "ns",
Short: "Operate on orb namespaces (create, etc.)",
Hidden: true,
}
nsCommand.AddCommand(createNamespace)

orbCommand := &cobra.Command{
Use: "orb",
Short: "Operate on orbs",
}

orbCommand.AddCommand(listCommand)
orbCommand.AddCommand(orbCreate)

orbCommand.AddCommand(validateCommand)
orbCommand.AddCommand(expandCommand)
orbCommand.AddCommand(publishCommand)

orbCommand.AddCommand(namespaceCommand)
orbCommand.AddCommand(nsCommand)
orbCommand.AddCommand(sourceCommand)

return orbCommand
Expand Down Expand Up @@ -325,24 +299,6 @@ func createOrb(cmd *cobra.Command, args []string) error {
return nil
}

func createOrbNamespace(cmd *cobra.Command, args []string) error {
var err error
ctx := context.Background()

response, err := api.CreateNamespace(ctx, Logger, args[0], args[2], strings.ToUpper(args[1]))

if err != nil {
return err
}

if len(response.Errors) > 0 {
return response.ToError()
}

Logger.Info("Namespace created")
return nil
}

func showSource(cmd *cobra.Command, args []string) error {
orb := args[0]
source, err := api.OrbSource(context.Background(), Logger, orb)
Expand Down
187 changes: 0 additions & 187 deletions cmd/orb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,193 +452,6 @@ var _ = Describe("Orb integration tests", func() {
})
})

Describe("when using the full namespace command", func() {
BeforeEach(func() {
command = exec.Command(pathCLI,
"orb", "namespace", "create",
"-t", token,
"-e", testServer.URL(),
"foo-ns",
"BITBUCKET",
"test-org",
)
})

It("works with organizationName and organizationVcs", func() {
By("setting up a mock server")

gqlOrganizationResponse := `{
"organization": {
"name": "test-org",
"id": "bb604b45-b6b0-4b81-ad80-796f15eddf87"
}
}`

expectedOrganizationRequest := `{
"query": "\n\t\t\tquery($organizationName: String!, $organizationVcs: VCSType!) {\n\t\t\t\torganization(\n\t\t\t\t\tname: $organizationName\n\t\t\t\t\tvcsType: $organizationVcs\n\t\t\t\t) {\n\t\t\t\t\tid\n\t\t\t\t}\n\t\t\t}",
"variables": {
"organizationName": "test-org",
"organizationVcs": "BITBUCKET"
}
}`

gqlNsResponse := `{
"createNamespace": {
"errors": [],
"namespace": {
"id": "bb604b45-b6b0-4b81-ad80-796f15eddf87"
}
}
}`

expectedNsRequest := `{
"query": "\n\t\t\tmutation($name: String!, $organizationId: UUID!) {\n\t\t\t\tcreateNamespace(\n\t\t\t\t\tname: $name,\n\t\t\t\t\torganizationId: $organizationId\n\t\t\t\t) {\n\t\t\t\t\tnamespace {\n\t\t\t\t\t\tid\n\t\t\t\t\t}\n\t\t\t\t\terrors {\n\t\t\t\t\t\tmessage\n\t\t\t\t\t\ttype\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}",
"variables": {
"name": "foo-ns",
"organizationId": "bb604b45-b6b0-4b81-ad80-796f15eddf87"
}
}`

appendPostHandler(testServer, token, MockRequestResponse{
Status: http.StatusOK,
Request: expectedOrganizationRequest,
Response: gqlOrganizationResponse})
appendPostHandler(testServer, token, MockRequestResponse{
Status: http.StatusOK,
Request: expectedNsRequest,
Response: gqlNsResponse})

By("running the command")
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)

Expect(err).ShouldNot(HaveOccurred())
Eventually(session.Out).Should(gbytes.Say("Namespace created"))
Eventually(session).Should(gexec.Exit(0))
})
})

Describe("when creating / reserving a namespace", func() {
BeforeEach(func() {
command = exec.Command(pathCLI,
"orb", "ns", "create",
"-t", token,
"-e", testServer.URL(),
"foo-ns",
"BITBUCKET",
"test-org",
)
})

It("works with organizationName and organizationVcs", func() {
By("setting up a mock server")

gqlOrganizationResponse := `{
"organization": {
"name": "test-org",
"id": "bb604b45-b6b0-4b81-ad80-796f15eddf87"
}
}`

expectedOrganizationRequest := `{
"query": "\n\t\t\tquery($organizationName: String!, $organizationVcs: VCSType!) {\n\t\t\t\torganization(\n\t\t\t\t\tname: $organizationName\n\t\t\t\t\tvcsType: $organizationVcs\n\t\t\t\t) {\n\t\t\t\t\tid\n\t\t\t\t}\n\t\t\t}",
"variables": {
"organizationName": "test-org",
"organizationVcs": "BITBUCKET"
}
}`

gqlNsResponse := `{
"createNamespace": {
"errors": [],
"namespace": {
"id": "bb604b45-b6b0-4b81-ad80-796f15eddf87"
}
}
}`

expectedNsRequest := `{
"query": "\n\t\t\tmutation($name: String!, $organizationId: UUID!) {\n\t\t\t\tcreateNamespace(\n\t\t\t\t\tname: $name,\n\t\t\t\t\torganizationId: $organizationId\n\t\t\t\t) {\n\t\t\t\t\tnamespace {\n\t\t\t\t\t\tid\n\t\t\t\t\t}\n\t\t\t\t\terrors {\n\t\t\t\t\t\tmessage\n\t\t\t\t\t\ttype\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}",
"variables": {
"name": "foo-ns",
"organizationId": "bb604b45-b6b0-4b81-ad80-796f15eddf87"
}
}`

appendPostHandler(testServer, token, MockRequestResponse{
Status: http.StatusOK,
Request: expectedOrganizationRequest,
Response: gqlOrganizationResponse})
appendPostHandler(testServer, token, MockRequestResponse{
Status: http.StatusOK,
Request: expectedNsRequest,
Response: gqlNsResponse})

By("running the command")
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)

Expect(err).ShouldNot(HaveOccurred())
Eventually(session.Out).Should(gbytes.Say("Namespace created"))
Eventually(session).Should(gexec.Exit(0))
})

It("prints all errors returned by the GraphQL API", func() {
By("setting up a mock server")

gqlOrganizationResponse := `{
"organization": {
"name": "test-org",
"id": "bb604b45-b6b0-4b81-ad80-796f15eddf87"
}
}`

expectedOrganizationRequest := `{
"query": "\n\t\t\tquery($organizationName: String!, $organizationVcs: VCSType!) {\n\t\t\t\torganization(\n\t\t\t\t\tname: $organizationName\n\t\t\t\t\tvcsType: $organizationVcs\n\t\t\t\t) {\n\t\t\t\t\tid\n\t\t\t\t}\n\t\t\t}",
"variables": {
"organizationName": "test-org",
"organizationVcs": "BITBUCKET"
}
}`

gqlResponse := `{
"createNamespace": {
"errors": [
{"message": "error1"},
{"message": "error2"}
],
"namespace": null
}
}`

expectedRequestJson := `{
"query": "\n\t\t\tmutation($name: String!, $organizationId: UUID!) {\n\t\t\t\tcreateNamespace(\n\t\t\t\t\tname: $name,\n\t\t\t\t\torganizationId: $organizationId\n\t\t\t\t) {\n\t\t\t\t\tnamespace {\n\t\t\t\t\t\tid\n\t\t\t\t\t}\n\t\t\t\t\terrors {\n\t\t\t\t\t\tmessage\n\t\t\t\t\t\ttype\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}",
"variables": {
"name": "foo-ns",
"organizationId": "bb604b45-b6b0-4b81-ad80-796f15eddf87"
}
}`

appendPostHandler(testServer, token,
MockRequestResponse{
Status: http.StatusOK,
Request: expectedOrganizationRequest,
Response: gqlOrganizationResponse,
})
appendPostHandler(testServer, token,
MockRequestResponse{
Status: http.StatusOK,
Request: expectedRequestJson,
Response: gqlResponse,
})

By("running the command")
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)

Expect(err).ShouldNot(HaveOccurred())
Eventually(session.Err).Should(gbytes.Say("Error: error1: error2"))
Eventually(session).ShouldNot(gexec.Exit(0))
})
})

Describe("when creating / reserving an orb", func() {
BeforeEach(func() {
command = exec.Command(pathCLI,
Expand Down
55 changes: 55 additions & 0 deletions cmd/registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package cmd

import (
"context"
"strings"

"github.com/CircleCI-Public/circleci-cli/api"
"github.com/spf13/cobra"
)

func newRegistryCommand() *cobra.Command {
registryCmd := &cobra.Command{
Use: "registry",
Short: "Operate on the registry",
}

namespaceCommand := &cobra.Command{
Use: "namespace",
Short: "Operate on orb namespaces (create, etc.)",
}

createNamespaceCommand := &cobra.Command{
Use: "create [name] [vcs] [org-name]",
Short: "create an namespace",
RunE: createNamespace,
Args: cobra.ExactArgs(3),
}

// "org-name", "", "organization name (required)"
// "vcs", "github", "organization vcs, e.g. 'github', 'bitbucket'"

namespaceCommand.AddCommand(createNamespaceCommand)

registryCmd.AddCommand(namespaceCommand)

return registryCmd
}

func createNamespace(cmd *cobra.Command, args []string) error {
var err error
ctx := context.Background()

response, err := api.CreateNamespace(ctx, Logger, args[0], args[2], strings.ToUpper(args[1]))

if err != nil {
return err
}

if len(response.Errors) > 0 {
return response.ToError()
}

Logger.Info("Namespace created")
return nil
}
Loading

0 comments on commit 19f60ac

Please sign in to comment.