Skip to content

Commit

Permalink
Merge pull request #109 from CircleCI-Public/orb-list-certified
Browse files Browse the repository at this point in the history
CIRCLE-13449 Add --uncertified flag to orbs list which defaults to false
  • Loading branch information
Zachary Scott authored Sep 12, 2018
2 parents af75d4d + da2d5a5 commit dc65e6f
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 8 deletions.
7 changes: 4 additions & 3 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ func OrbSource(ctx context.Context, logger *logger.Logger, namespace string, orb
// ListOrbs queries the API to find all orbs.
// Returns a collection of Orb objects containing their relevant data. Logs
// request and parse errors to the supplied logger.
func ListOrbs(ctx context.Context, logger *logger.Logger) ([]Orb, error) {
func ListOrbs(ctx context.Context, logger *logger.Logger, uncertified bool) ([]Orb, error) {
// Define a structure that matches the result of the GQL
// query, so that we can use mapstructure to convert from
// nested maps to a strongly typed struct.
Expand All @@ -765,8 +765,8 @@ func ListOrbs(ctx context.Context, logger *logger.Logger) ([]Orb, error) {
}

query := `
query ListOrbs ($after: String!) {
orbs(first: 20, after: $after) {
query ListOrbs ($after: String!, $certifiedOnly: Boolean!) {
orbs(first: 20, after: $after, certifiedOnly: $certifiedOnly) {
totalCount,
edges {
cursor
Expand Down Expand Up @@ -799,6 +799,7 @@ query ListOrbs ($after: String!) {
for {
request := client.NewAuthorizedRequest(viper.GetString("token"), query)
request.Var("after", currentCursor)
request.Var("certifiedOnly", !uncertified)

err := graphQLclient.Run(ctx, request, &result)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion cmd/orb.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ var orbAnnotations = map[string]string{
"ORB": "The name of your orb (i.e. rails)",
}

var orbListUncertified bool

func newOrbCommand() *cobra.Command {

listCommand := &cobra.Command{
Expand All @@ -27,6 +29,7 @@ func newOrbCommand() *cobra.Command {
Annotations: make(map[string]string),
}
listCommand.Annotations["NAMESPACE"] = orbAnnotations["NAMESPACE"] + " (Optional)"
listCommand.PersistentFlags().BoolVarP(&orbListUncertified, "uncertified", "u", false, "include uncertified orbs")

validateCommand := &cobra.Command{
Use: "validate PATH",
Expand Down Expand Up @@ -146,7 +149,7 @@ func listOrbs(cmd *cobra.Command, args []string) error {
}

ctx := context.Background()
orbs, err := api.ListOrbs(ctx, Logger)
orbs, err := api.ListOrbs(ctx, Logger, orbListUncertified)
if err != nil {
return errors.Wrapf(err, "Failed to list orbs")
}
Expand Down
51 changes: 47 additions & 4 deletions cmd/orb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1040,12 +1040,10 @@ var _ = Describe("Orb integration tests", func() {
It("sends multiple requests when there are more than 1 page of orbs", func() {
By("setting up a mock server")

tmpBytes, err := ioutil.ReadFile(filepath.Join("testdata/gql_orb_list", "first_response.json"))
Expect(err).ShouldNot(HaveOccurred())
tmpBytes := golden.Get(GinkgoT(), filepath.FromSlash("gql_orb_list/first_response.json"))
firstGqlResponse := string(tmpBytes)

tmpBytes, err = ioutil.ReadFile(filepath.Join("testdata/gql_orb_list", "second_response.json"))
Expect(err).ShouldNot(HaveOccurred())
tmpBytes = golden.Get(GinkgoT(), filepath.FromSlash("gql_orb_list/second_response.json"))
secondGqlResponse := string(tmpBytes)

// Use Gomega's default matcher instead of our custom appendPostHandler
Expand All @@ -1072,6 +1070,51 @@ var _ = Describe("Orb integration tests", func() {

})

Describe("when listing all orbs with --uncertified", func() {
BeforeEach(func() {
command = exec.Command(pathCLI,
"orb", "list",
"--uncertified",
"--host", testServer.URL(),
"--verbose",
)
})

It("sends a GraphQL request with 'uncertifiedOnly: false'", func() {
By("setting up a mock server")

tmpBytes := golden.Get(GinkgoT(), filepath.FromSlash("gql_orb_list_uncertified/request.json"))
gqlRequest := string(tmpBytes)

tmpBytes = golden.Get(GinkgoT(), filepath.FromSlash("gql_orb_list_uncertified/response.json"))
response := string(tmpBytes)

// Use Gomega's default matcher instead of our custom appendPostHandler
// since this query doesn't pass in a token.
// Skip checking the content type field to make this test simpler.
testServer.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("POST", "/graphql-unstable"),
func(w http.ResponseWriter, req *http.Request) {
body, error := ioutil.ReadAll(req.Body)
req.Body.Close()
Expect(error).ShouldNot(HaveOccurred())
Expect(body).Should(MatchJSON(gqlRequest), "JSON Mismatch")
},
ghttp.RespondWith(http.StatusOK, response),
),
)

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

Expect(err).ShouldNot(HaveOccurred())
Eventually(session).Should(gexec.Exit(0))
Expect(testServer.ReceivedRequests()).Should(HaveLen(1))
})

})

Describe("when listing orbs with a namespace argument", func() {
BeforeEach(func() {
command = exec.Command(pathCLI,
Expand Down
7 changes: 7 additions & 0 deletions cmd/testdata/gql_orb_list_uncertified/request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"variables": {
"after": "",
"certifiedOnly": false
},
"query": "\nquery ListOrbs ($after: String!, $certifiedOnly: Boolean!) {\n orbs(first: 20, after: $after, certifiedOnly: $certifiedOnly) {\n\ttotalCount,\n edges {\n\t\tcursor\n\t node {\n\t name\n\t\t versions(count: 1) {\n\t\t\tversion,\n\t\t\tsource\n\t\t }\n\t\t}\n\t}\n pageInfo {\n hasNextPage\n }\n }\n}\n\t"
}
59 changes: 59 additions & 0 deletions cmd/testdata/gql_orb_list_uncertified/response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"data": {
"registryNamespace": {
"name": "circleci",
"orbs": {
"edges": [
{
"cursor": "circleci/aws-cli",
"node": {
"versions": [],
"name": "circleci/aws-cli"
}
},
{
"cursor": "circleci/aws-code-deploy",
"node": {
"versions": [],
"name": "circleci/aws-code-deploy"
}
},
{
"cursor": "circleci/aws-s3",
"node": {
"versions": [],
"name": "circleci/aws-s3"
}
},
{
"cursor": "circleci/circleci-cli",
"node": {
"versions": [],
"name": "circleci/circleci-cli"
}
},
{
"cursor": "circleci/codecov-clojure",
"node": {
"versions": [
{
"source": "{}",
"version": "0.0.3"
},
{
"source": "description: provides a job for reporting code coverage of clojure projects to codecov\ncommands:\n upload:\n parameters:\n path:\n description: Path to the code coverage data file to upload.\n type: string\n steps:\n - run:\n name: Upload Coverage Results\n command: |\n curl --request POST --retry 3 --silent --show-error --fail --data-binary @<< parameters.path >> \\\n \"https://codecov.io/upload/v2?service=circleci\\\n &token=$CODECOV_TOKEN\\\n &commit=$CIRCLE_SHA1\\\n &branch=$CIRCLE_BRANCH\\\n &build=$CIRCLE_BUILD_NUM\\\n &job=$CIRCLE_NODE_INDEX\\\n &build_url=$CIRCLE_BUILD_URL\\\n &slug=$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME\\\n &pr=$CIRCLE_PR_NUMBER\"\njobs:\n code-coverage:\n parameters:\n coverage-file:\n description: the path to coverage.json\n type: string\n default: target/coverage/codecov.json\n cache-key:\n description: cache key to use, if this is likely to share cache with another job. currently only supports one key.\n type: string\n default: code-coverage-{{ checksum \"project.clj\" }}\n circlecitest:\n description: tests are run with the circleci.test test runner instead of clojure.test\n type: boolean\n default: false\n docker:\n - image: circleci/clojure\n steps:\n - checkout\n - restore_cache:\n keys:\n - << parameters.cache-key >>\n - run: lein cloverage --codecov <<# parameters.circlecitest >> --runner circleci.test <</ parameters.circlecitest >>\n - save_cache:\n paths:\n - ~/.m2\n - ~/.lein\n key: << parameters.cache-key >>\n - store_artifacts:\n path: target/coverage\n - upload:\n path: << parameters.coverage-file >>\n",
"version": "0.0.2"
}
],
"name": "circleci/codecov-clojure"
}
}
],
"totalCount": 10,
"pageInfo": {
"hasNextPage": true
}
}
}
}
}

0 comments on commit dc65e6f

Please sign in to comment.