Skip to content

Commit

Permalink
Merge pull request #516 from CircleCI-Public/fix-create-orb-response
Browse files Browse the repository at this point in the history
Properly deserialize graphQL response when creating an imported orb
  • Loading branch information
kelvinkfli authored Nov 4, 2020
2 parents edd023d + 28aa724 commit 44f80c2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
23 changes: 16 additions & 7 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,15 @@ type CreateOrbResponse struct {
}
}

// ImportOrbResponse type matches the data shape of the GQL response for
// creating an orb
type ImportOrbResponse struct {
ImportOrb struct {
Orb Orb
Errors GQLErrorsCollection
}
}

// NamespaceOrbResponse type matches the result from GQL.
// So that we can use mapstructure to convert from nested maps to a strongly typed struct.
type NamespaceOrbResponse struct {
Expand Down Expand Up @@ -830,10 +839,10 @@ func organizationNotFound(name string, vcs string) error {
}

func DeleteNamespaceAlias(cl *graphql.Client, name string) error {
var response struct{
DeleteNamespaceAlias struct{
var response struct {
DeleteNamespaceAlias struct {
Deleted bool
Errors GQLErrorsCollection
Errors GQLErrorsCollection
}
}
query := `
Expand Down Expand Up @@ -1034,13 +1043,13 @@ func CreateOrb(cl *graphql.Client, namespace string, name string) (*CreateOrbRes
}

// CreateImportedOrb creates (reserves) an imported orb within the provided namespace.
func CreateImportedOrb(cl *graphql.Client, namespace string, name string) (*CreateOrbResponse, error) {
func CreateImportedOrb(cl *graphql.Client, namespace string, name string) (*ImportOrbResponse, error) {
res, err := GetNamespace(cl, namespace)
if err != nil {
return nil, err
}

var response CreateOrbResponse
var response ImportOrbResponse

query := `mutation($name: String!, $registryNamespaceId: UUID!){
importOrb(
Expand Down Expand Up @@ -1068,8 +1077,8 @@ func CreateImportedOrb(cl *graphql.Client, namespace string, name string) (*Crea
return nil, err
}

if len(response.CreateOrb.Errors) > 0 {
return nil, response.CreateOrb.Errors
if len(response.ImportOrb.Errors) > 0 {
return nil, response.ImportOrb.Errors
}

return &response, nil
Expand Down
10 changes: 5 additions & 5 deletions cmd/orb_import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -799,16 +799,16 @@ The following orb versions already exist:
}
}`

createOrbReq := `{
importOrbReq := `{
"query": "mutation($name: String!, $registryNamespaceId: UUID!){\n\t\t\t\timportOrb(\n\t\t\t\t\tname: $name,\n\t\t\t\t\tregistryNamespaceId: $registryNamespaceId\n\t\t\t\t){\n\t\t\t\t orb {\n\t\t\t\t id\n\t\t\t\t }\n\t\t\t\t errors {\n\t\t\t\t message\n\t\t\t\t type\n\t\t\t\t }\n\t\t\t\t}\n}",
"variables": {
"name": "orb",
"registryNamespaceId": "someid1"
}
}`

createOrbResp := `{
"createOrb": {
importOrbResp := `{
"importOrb": {
"errors": [{"message": "testerror"}]
}
}`
Expand All @@ -820,8 +820,8 @@ The following orb versions already exist:
})
cli.AppendPostHandler("", clitest.MockRequestResponse{
Status: http.StatusOK,
Request: createOrbReq,
Response: createOrbResp,
Request: importOrbReq,
Response: importOrbResp,
})

err := applyPlan(opts, plan)
Expand Down

0 comments on commit 44f80c2

Please sign in to comment.