Skip to content

Commit

Permalink
circleci orb publish should not require a UUID to work
Browse files Browse the repository at this point in the history
We can use the get-orb-by-name query to get the UUID by name instead.

Here's the output from help for this command following this commit:

```
publish a version of an orb

Usage:
  circleci orb publish <namespace>/<orb> <orb.yml> [flags]

Flags:
  -h, --help                 help for publish
  -o, --orb-version string   version of orb to publish (required)

Global Flags:
  ...
```
  • Loading branch information
Zachary Scott committed Jul 31, 2018
1 parent d99aa3d commit 7d31118
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 29 deletions.
38 changes: 37 additions & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,12 @@ func OrbQuery(ctx context.Context, logger *logger.Logger, configPath string) (*C

// OrbPublish publishes a new version of an orb
func OrbPublish(ctx context.Context, logger *logger.Logger,
configPath string, orbVersion string, orbID string) (*PublishOrbResponse, error) {
name string, configPath string, orbVersion string) (*PublishOrbResponse, error) {
orbID, err := getOrbID(ctx, logger, name)
if err != nil {
return nil, err
}

var response struct {
PublishOrb struct {
PublishOrbResponse
Expand Down Expand Up @@ -194,6 +199,37 @@ func OrbPublish(ctx context.Context, logger *logger.Logger,
return &response.PublishOrb.PublishOrbResponse, err
}

func getOrbID(ctx context.Context, logger *logger.Logger, name string) (string, error) {
var response struct {
Orb struct {
ID string
}
}

query := `query($name: String!) {
orb(name: $name) {
id
}
}`

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

graphQLclient := client.NewClient(viper.GetString("endpoint"), logger)

err := graphQLclient.Run(ctx, request, &response)

if err != nil {
return "", err
}

if response.Orb.ID == "" {
return "", fmt.Errorf("the %s orb could not be found", name)
}

return response.Orb.ID, nil
}

func createNamespaceWithOwnerID(ctx context.Context, logger *logger.Logger, name string, ownerID string) (*CreateNamespaceResponse, error) {
var response struct {
CreateNamespace struct {
Expand Down
19 changes: 5 additions & 14 deletions cmd/orb.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
)

var orbVersion string
var orbID string

func newOrbCommand() *cobra.Command {

Expand All @@ -42,19 +41,15 @@ func newOrbCommand() *cobra.Command {
}

publishCommand := &cobra.Command{
Use: "publish [orb.yml]",
Use: "publish <namespace>/<orb> <orb.yml>",
Short: "publish a version of an orb",
RunE: publishOrb,
Args: cobra.MaximumNArgs(1),
Args: cobra.ExactArgs(2),
}

publishCommand.Flags().StringVarP(&orbVersion, "orb-version", "o", "", "version of orb to publish (required)")
publishCommand.Flags().StringVarP(&orbID, "orb-id", "i", "", "id of orb to publish (required)")

for _, flag := range [2]string{"orb-version", "orb-id"} {
if err := publishCommand.MarkFlagRequired(flag); err != nil {
panic(err)
}
if err := publishCommand.MarkFlagRequired("orb-version"); err != nil {
panic(err)
}

sourceCommand := &cobra.Command{
Expand Down Expand Up @@ -291,12 +286,8 @@ func expandOrb(cmd *cobra.Command, args []string) error {

func publishOrb(cmd *cobra.Command, args []string) error {
ctx := context.Background()
orbPath := defaultOrbPath
if len(args) == 1 {
orbPath = args[0]
}

response, err := api.OrbPublish(ctx, Logger, orbPath, orbVersion, orbID)
response, err := api.OrbPublish(ctx, Logger, args[0], args[1], orbVersion)

if err != nil {
return err
Expand Down
60 changes: 46 additions & 14 deletions cmd/orb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,9 @@ var _ = Describe("Orb integration tests", func() {
"orb", "publish",
"-t", token,
"-e", testServer.URL(),
"my/orb",
orb.Path,
"--orb-version", "0.0.1",
"--orb-id", "bb604b45-b6b0-4b81-ad80-796f15eddf87",
)
})

Expand All @@ -348,7 +348,20 @@ var _ = Describe("Orb integration tests", func() {
// assert write to test file successful
Expect(err).ToNot(HaveOccurred())

gqlResponse := `{
gqlOrbIDResponse := `{
"orb": {
"id": "bb604b45-b6b0-4b81-ad80-796f15eddf87"
}
}`

expectedOrbIDRequest := `{
"query": "query($name: String!) {\n\t\t\t orb(name: $name) {\n\t\t\t id\n\t\t\t }\n\t\t }",
"variables": {
"name": "my/orb"
}
}`

gqlPublishResponse := `{
"publishOrb": {
"errors": [],
"orb": {
Expand All @@ -357,7 +370,7 @@ var _ = Describe("Orb integration tests", func() {
}
}`

expectedRequestJson := `{
expectedPublishRequest := `{
"query": "\n\t\tmutation($config: String!, $orbId: UUID!, $version: String!) {\n\t\t\tpublishOrb(\n\t\t\t\torbId: $orbId,\n\t\t\t\torbYaml: $config,\n\t\t\t\tversion: $version\n\t\t\t) {\n\t\t\t\torb {\n\t\t\t\t\tversion\n\t\t\t\t}\n\t\t\t\terrors { message }\n\t\t\t}\n\t\t}\n\t",
"variables": {
"config": "some orb",
Expand All @@ -368,9 +381,12 @@ var _ = Describe("Orb integration tests", func() {

appendPostHandler(testServer, token, MockRequestResponse{
Status: http.StatusOK,
Request: expectedRequestJson,
Response: gqlResponse,
})
Request: expectedOrbIDRequest,
Response: gqlOrbIDResponse})
appendPostHandler(testServer, token, MockRequestResponse{
Status: http.StatusOK,
Request: expectedPublishRequest,
Response: gqlPublishResponse})

By("running the command")
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expand All @@ -385,17 +401,30 @@ var _ = Describe("Orb integration tests", func() {
err := orb.write(`some orb`)
Expect(err).ToNot(HaveOccurred())

gqlResponse := `{
"publishOrb": {
gqlOrbIDResponse := `{
"orb": {
"id": "bb604b45-b6b0-4b81-ad80-796f15eddf87"
}
}`

expectedOrbIDRequest := `{
"query": "query($name: String!) {\n\t\t\t orb(name: $name) {\n\t\t\t id\n\t\t\t }\n\t\t }",
"variables": {
"name": "my/orb"
}
}`

gqlPublishResponse := `{
"publishOrb": {
"errors": [
{"message": "error1"},
{"message": "error2"}
],
"orb": null
}
}`
}
}`

expectedRequestJson := `{
expectedPublishRequest := `{
"query": "\n\t\tmutation($config: String!, $orbId: UUID!, $version: String!) {\n\t\t\tpublishOrb(\n\t\t\t\torbId: $orbId,\n\t\t\t\torbYaml: $config,\n\t\t\t\tversion: $version\n\t\t\t) {\n\t\t\t\torb {\n\t\t\t\t\tversion\n\t\t\t\t}\n\t\t\t\terrors { message }\n\t\t\t}\n\t\t}\n\t",
"variables": {
"config": "some orb",
Expand All @@ -406,9 +435,12 @@ var _ = Describe("Orb integration tests", func() {

appendPostHandler(testServer, token, MockRequestResponse{
Status: http.StatusOK,
Request: expectedRequestJson,
Response: gqlResponse,
})
Request: expectedOrbIDRequest,
Response: gqlOrbIDResponse})
appendPostHandler(testServer, token, MockRequestResponse{
Status: http.StatusOK,
Request: expectedPublishRequest,
Response: gqlPublishResponse})

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

0 comments on commit 7d31118

Please sign in to comment.