Skip to content

Commit

Permalink
minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
lokst committed Aug 14, 2020
1 parent e5c9a6d commit 977d126
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 32 deletions.
26 changes: 12 additions & 14 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ type AddOrRemoveOrbCategorizationData struct {
Errors GQLErrorsCollection
}

// OrbListResponse type matches the result from GQL.
// OrbCategoryListResponse type matches the result from GQL.
// So that we can use mapstructure to convert from nested maps to a strongly typed struct.
type OrbCategoryListResponse struct {
OrbCategories struct {
Expand Down Expand Up @@ -1001,11 +1001,11 @@ func OrbInfo(cl *graphql.Client, orbRef string) (*OrbVersion, error) {
orb {
id
createdAt
name
categories {
id
name
}
name
categories {
id
name
}
statistics {
last30DaysBuildCount,
last30DaysProjectCount,
Expand Down Expand Up @@ -1250,11 +1250,9 @@ func OrbCategoryID(cl *graphql.Client, name string) (*OrbCategoryIDResponse, err
orbCategoryByName(name: $name) {
id
}
}
`
}`

request := graphql.NewRequest(query)
request.SetToken(cl.Token)

request.Var("name", name)

Expand All @@ -1268,8 +1266,8 @@ func OrbCategoryID(cl *graphql.Client, name string) (*OrbCategoryIDResponse, err
return nil, fmt.Errorf("the '%s' category does not exist. Did you misspell the category name? To see the list of category names, please run 'circleci orb list-categories'.", name)
}

// OrbAddOrRemoveOrbCategorization adds or removes an orb categorization
func OrbAddOrRemoveOrbCategorization(cl *graphql.Client, namespace string, orb string, categoryName string, updateType UpdateOrbCategorizationRequestType) error {
// AddOrRemoveOrbCategorization adds or removes an orb categorization
func AddOrRemoveOrbCategorization(cl *graphql.Client, namespace string, orb string, categoryName string, updateType UpdateOrbCategorizationRequestType) error {
orbId, err := OrbID(cl, namespace, orb)
if err != nil {
return err
Expand All @@ -1293,9 +1291,9 @@ func OrbAddOrRemoveOrbCategorization(cl *graphql.Client, namespace string, orb s
return fmt.Errorf("Internal error - invalid update type %d", updateType)
}

query := `
query := fmt.Sprintf(`
mutation($orbId: UUID!, $categoryId: UUID!) {
` + mutationName + `(
%s(
orbId: $orbId,
categoryId: $categoryId
) {
Expand All @@ -1307,7 +1305,7 @@ func OrbAddOrRemoveOrbCategorization(cl *graphql.Client, namespace string, orb s
}
}
}
`
`, mutationName)

request := graphql.NewRequest(query)
request.SetToken(cl.Token)
Expand Down
6 changes: 3 additions & 3 deletions cmd/orb.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ Please note that at this time all orbs created in the registry are world-readabl

listCategoriesCommand := &cobra.Command{
Use: "list-categories",
Short: "List categories",
Short: "List orb categories",
Args: cobra.ExactArgs(0),
RunE: func(_ *cobra.Command, _ []string) error {
return listOrbCategories(opts)
Expand Down Expand Up @@ -505,7 +505,7 @@ func orbCategoryCollectionToString(orbCategoryCollection *api.OrbCategoriesForLi
if opts.listJSON {
orbCategoriesJSON, err := json.MarshalIndent(orbCategoryCollection, "", " ")
if err != nil {
return "", errors.Wrapf(err, "Failed to convert to convert to JSON")
return "", errors.Wrapf(err, "Failed to convert to JSON")
}
result = string(orbCategoriesJSON)
} else {
Expand Down Expand Up @@ -850,7 +850,7 @@ func addOrRemoveOrbCategorization(opts orbOptions, updateType api.UpdateOrbCateg
return err
}

err = api.OrbAddOrRemoveOrbCategorization(opts.cl, namespace, orb, opts.args[1], updateType)
err = api.AddOrRemoveOrbCategorization(opts.cl, namespace, orb, opts.args[1], updateType)

if err != nil {
var errorString = "Failed to add orb %s to category %s"
Expand Down
36 changes: 21 additions & 15 deletions cmd/orb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2378,11 +2378,11 @@ foo.bar/account/api`))
orb {
id
createdAt
name
categories {
id
name
}
name
categories {
id
name
}
statistics {
last30DaysBuildCount,
last30DaysProjectCount,
Expand Down Expand Up @@ -2600,7 +2600,7 @@ https://circleci.com/orbs/registry/orb/my/orb
})
})

Describe("orb categories", func() {
Describe("list orb categories", func() {
Context("with mock server", func() {
DescribeTable("sends multiple requests when there are more than 1 page of orb categories",
func(json bool) {
Expand Down Expand Up @@ -2651,7 +2651,11 @@ https://circleci.com/orbs/registry/orb/my/orb
tmpBytes = golden.Get(GinkgoT(), filepath.FromSlash("gql_orb_category_list/second_response.json"))
secondResponse := string(tmpBytes)

tmpBytes = golden.Get(GinkgoT(), filepath.FromSlash("gql_orb_category_list/pretty_json_output.json"))
expectedOutputPath := "gql_orb_category_list/text_output.txt"
if json {
expectedOutputPath = "gql_orb_category_list/pretty_json_output.json"
}
tmpBytes = golden.Get(GinkgoT(), filepath.FromSlash(expectedOutputPath))
expectedOutput := string(tmpBytes)

tempSettings.AppendPostHandler("", clitest.MockRequestResponse{
Expand All @@ -2672,9 +2676,11 @@ https://circleci.com/orbs/registry/orb/my/orb
Eventually(session).Should(gexec.Exit(0))
Expect(tempSettings.TestServer.ReceivedRequests()).Should(HaveLen(2))

completeOutput := string(session.Wait().Out.Contents())
displayedOutput := string(session.Wait().Out.Contents())
if json {
Expect(completeOutput).Should(MatchJSON(expectedOutput))
Expect(displayedOutput).Should(MatchJSON(expectedOutput))
} else {
Expect(displayedOutput).To(Equal(expectedOutput))
}
},
Entry("with --json", true),
Expand Down Expand Up @@ -2703,7 +2709,7 @@ https://circleci.com/orbs/registry/orb/my/orb
})

Context("with mock server", func() {
DescribeTable("add/remove orb categorization",
DescribeTable("add/remove a valid orb to/from a valid category",
func(mockErrorResponse bool, updateType api.UpdateOrbCategorizationRequestType) {
commandName := "add-to-category"
operationName := "addCategorizationToOrb"
Expand Down Expand Up @@ -2736,7 +2742,7 @@ https://circleci.com/orbs/registry/orb/my/orb
}`, orbFullName, orbNamespaceName)

expectedCategoryIDRequest := fmt.Sprintf(`{
"query": "\n\tquery ($name: String!) {\n\t\torbCategoryByName(name: $name) {\n\t\t id\n\t\t}\n\t}\n\t ",
"query": "\n\tquery ($name: String!) {\n\t\torbCategoryByName(name: $name) {\n\t\t id\n\t\t}\n\t}",
"variables": {
"name": "%s"
}
Expand All @@ -2749,7 +2755,7 @@ https://circleci.com/orbs/registry/orb/my/orb
}`, categoryId)

expectedOrbCategorizationRequest := fmt.Sprintf(`{
"query": "\n\t\tmutation($orbId: UUID!, $categoryId: UUID!) {\n\t%s(\n\t\t\t\torbId: $orbId,\n\t\t\t\tcategoryId: $categoryId\n\t\t\t) {\n\t\t\t\torbId\n\t\t\t\tcategoryId\n\t\t\t\terrors {\n\t\t\t\t\tmessage\n\t\t\t\t\ttype\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t",
"query": "\n\t\tmutation($orbId: UUID!, $categoryId: UUID!) {\n\t\t\t%s(\n\t\t\t\torbId: $orbId,\n\t\t\t\tcategoryId: $categoryId\n\t\t\t) {\n\t\t\t\torbId\n\t\t\t\tcategoryId\n\t\t\t\terrors {\n\t\t\t\t\tmessage\n\t\t\t\t\ttype\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t",
"variables": {
"categoryId": "%s",
"orbId": "%s"
Expand Down Expand Up @@ -2782,7 +2788,7 @@ https://circleci.com/orbs/registry/orb/my/orb
Request: expectedOrbIDRequest,
Response: gqlOrbIDResponse})

tempSettings.AppendPostHandler(token, clitest.MockRequestResponse{
tempSettings.AppendPostHandler("", clitest.MockRequestResponse{
Status: http.StatusOK,
Request: expectedCategoryIDRequest,
Response: gqlCategoryIDResponse})
Expand Down Expand Up @@ -2892,7 +2898,7 @@ https://circleci.com/orbs/registry/orb/my/orb
}`, orbId)

expectedCategoryIDRequest := fmt.Sprintf(`{
"query": "\n\tquery ($name: String!) {\n\t\torbCategoryByName(name: $name) {\n\t\t id\n\t\t}\n\t}\n\t ",
"query": "\n\tquery ($name: String!) {\n\t\torbCategoryByName(name: $name) {\n\t\t id\n\t\t}\n\t}",
"variables": {
"name": "%s"
}
Expand All @@ -2907,7 +2913,7 @@ https://circleci.com/orbs/registry/orb/my/orb
Request: expectedOrbIDRequest,
Response: gqlOrbIDResponse})

tempSettings.AppendPostHandler(token, clitest.MockRequestResponse{
tempSettings.AppendPostHandler("", clitest.MockRequestResponse{
Status: http.StatusOK,
Request: expectedCategoryIDRequest,
Response: gqlCategoryIDResponse})
Expand Down
17 changes: 17 additions & 0 deletions cmd/testdata/gql_orb_category_list/text_output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Artifacts/Registry
Build
Cloud Platform
Code Analysis
Collaboration
Containers
Deployment
Infra Automation
Kubernetes
Language/Framework
Monitoring
Notifications
Reporting
Security
Testing
Windows Server 2003
Windows Server 2010

0 comments on commit 977d126

Please sign in to comment.