Skip to content

Commit

Permalink
Merge pull request #1091 from CircleCI-Public/develop
Browse files Browse the repository at this point in the history
Remove introspection query from `setup` & `diagnostic` commands
  • Loading branch information
EnoshAnwar authored Jan 20, 2025
2 parents 591d7b2 + cb22920 commit ebcbd1f
Show file tree
Hide file tree
Showing 13 changed files with 209 additions and 542 deletions.
8 changes: 4 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ orbs:
executors:
go:
docker:
- image: cimg/go:1.21.5
- image: cimg/go:1.23.4
resource_class: large
environment:
CGO_ENABLED: 0
Expand Down Expand Up @@ -103,8 +103,8 @@ jobs:
steps:
- checkout
- run: |
curl -OL https://go.dev/dl/go1.21.5.darwin-arm64.pkg
sudo installer -pkg ./go1.21.5.darwin-arm64.pkg -target /
curl -OL https://go.dev/dl/go1.23.4.darwin-arm64.pkg
sudo installer -pkg ./go1.23.4.darwin-arm64.pkg -target /
echo 'export PATH="/usr/local/go/bin:$PATH"' >> ~/.bash_profile
- gomod
- run: make test
Expand Down Expand Up @@ -177,7 +177,7 @@ jobs:

lint:
docker:
- image: golangci/golangci-lint:v1.51.0-alpine
- image: golangci/golangci-lint:v1.63.4-alpine
resource_class: large
steps:
- checkout
Expand Down
5 changes: 1 addition & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
---
service:
golangci-lint-version: 1.46.x
golangci-lint-version: 1.63.x

linters:
enable:
- deadcode
- errcheck
- goconst
- gofmt
Expand All @@ -18,12 +17,10 @@ linters:
- nakedret
# - revive
- staticcheck
- structcheck
- typecheck
- unconvert
# - unparam
- unused
- varcheck
- vet
- vetshadow

Expand Down
56 changes: 2 additions & 54 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,6 @@ type GQLResponseError struct {
Type string
}

// IntrospectionResponse matches the result from making an introspection query
type IntrospectionResponse struct {
Schema struct {
MutationType struct {
Name string
}
QueryType struct {
Name string
}
Types []struct {
Description string
Fields []struct {
Name string
}
Kind string
Name string
}
} `json:"__schema"`
}

// ConfigResponse is 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 Down Expand Up @@ -1457,7 +1437,7 @@ query ListOrbs ($after: String!, $certifiedOnly: Boolean!) {
err := yaml.Unmarshal([]byte(edge.Node.Versions[0].Source), &edge.Node)

if err != nil {
l.Printf(errors.Wrapf(err, "Corrupt Orb %s %s", edge.Node.Name, v.Version).Error())
l.Printf("%s", errors.Wrap(err, fmt.Sprintf("Corrupt Orb %s %s", edge.Node.Name, v.Version)).Error())
continue Orbs
}

Expand Down Expand Up @@ -1626,7 +1606,7 @@ query namespaceOrbs ($namespace: String, $after: String!, $view: OrbListViewType

err := yaml.Unmarshal([]byte(edge.Node.Versions[0].Source), &edge.Node)
if err != nil {
l.Printf(errors.Wrapf(err, "Corrupt Orb %s %s", edge.Node.Name, v.Version).Error())
l.Printf("%s", errors.Wrap(err, fmt.Sprintf("Corrupt Orb %s %s", edge.Node.Name, v.Version)).Error())
continue NamespaceOrbs
}
} else {
Expand All @@ -1644,38 +1624,6 @@ query namespaceOrbs ($namespace: String, $after: String!, $view: OrbListViewType
return &orbs, nil
}

// IntrospectionQuery makes a query on the API asking for bits of the schema
// This query isn't intended to get the entire schema, there are better tools for that.
func IntrospectionQuery(cl *graphql.Client) (*IntrospectionResponse, error) {
var response IntrospectionResponse

query := `query IntrospectionQuery {
__schema {
queryType { name }
mutationType { name }
types {
...FullType
}
}
}
fragment FullType on __Type {
kind
name
description
fields(includeDeprecated: true) {
name
}
}`

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

err := cl.Run(request, &response)

return &response, err
}

// OrbCategoryID fetches an orb returning the ID
func OrbCategoryID(cl *graphql.Client, name string) (*OrbCategoryIDResponse, error) {
var response OrbCategoryIDResponse
Expand Down
2 changes: 1 addition & 1 deletion api/graphql/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func (cl *Client) Run(request *Request, resp interface{}) error {
defer func() {
responseBodyCloseErr := res.Body.Close()
if responseBodyCloseErr != nil {
l.Printf(responseBodyCloseErr.Error())
l.Printf("%s", responseBodyCloseErr.Error())
}
}()

Expand Down
30 changes: 15 additions & 15 deletions api/graphql/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestDoJSON(t *testing.T) {

b, err := io.ReadAll(r.Body)
if err != nil {
t.Errorf(err.Error())
t.Errorf("%s", err.Error())
}

if string(b) != `{"query":"query {}","variables":null}`+"\n" {
Expand All @@ -67,7 +67,7 @@ func TestDoJSON(t *testing.T) {
}
}`)
if err != nil {
t.Errorf(err.Error())
t.Errorf("%s", err.Error())
}
}))
defer srv.Close()
Expand All @@ -79,7 +79,7 @@ func TestDoJSON(t *testing.T) {
}
err := client.Run(&Request{Query: "query {}"}, &resp)
if err != nil {
t.Errorf(err.Error())
t.Errorf("%s", err.Error())
}

if calls != 1 {
Expand All @@ -97,14 +97,14 @@ func TestQueryJSON(t *testing.T) {
calls++
b, err := io.ReadAll(r.Body)
if err != nil {
t.Errorf(err.Error())
t.Errorf("%s", err.Error())
}
if string(b) != `{"query":"query {}","variables":{"username":"matryer"}}`+"\n" {
t.Errorf("expected %s", b)
}
_, err = io.WriteString(w, `{"data":{"value":"some data"}}`)
if err != nil {
t.Errorf(err.Error())
t.Errorf("%s", err.Error())
}
}))
defer srv.Close()
Expand All @@ -128,7 +128,7 @@ func TestQueryJSON(t *testing.T) {
}
err := client.Run(req, &resp)
if err != nil {
t.Errorf(err.Error())
t.Errorf("%s", err.Error())
}

if calls != 1 {
Expand All @@ -147,7 +147,7 @@ func TestDoJSONErr(t *testing.T) {

body, err := io.ReadAll(req.Body)
if err != nil {
t.Errorf(err.Error())
t.Errorf("%s", err.Error())
}

if string(body) != `{"query":"query {}","variables":null}`+"\n" {
Expand All @@ -166,7 +166,7 @@ func TestDoJSONErr(t *testing.T) {
]
}`)
if err != nil {
t.Errorf(err.Error())
t.Errorf("%s", err.Error())
}
}))

Expand All @@ -178,7 +178,7 @@ func TestDoJSONErr(t *testing.T) {

err := client.Run(&Request{Query: "query {}"}, &responseData)
if err.Error() != "Something went wrong\nSomething else went wrong" {
t.Errorf(err.Error())
t.Errorf("%s", err.Error())
}
}

Expand All @@ -192,7 +192,7 @@ func TestHeader(t *testing.T) {

_, err := io.WriteString(w, `{"data":{"value":"some data"}}`)
if err != nil {
t.Errorf(err.Error())
t.Errorf("%s", err.Error())
}
}))
defer srv.Close()
Expand All @@ -207,7 +207,7 @@ func TestHeader(t *testing.T) {
}
err := client.Run(req, &resp)
if err != nil {
t.Errorf(err.Error())
t.Errorf("%s", err.Error())
}

if calls != 1 {
Expand All @@ -228,7 +228,7 @@ func TestStatusCode200(t *testing.T) {

_, err := io.WriteString(w, `{"data":{"value":"some data"}}`)
if err != nil {
t.Errorf(err.Error())
t.Errorf("%s", err.Error())
}
}))
defer srv.Close()
Expand All @@ -241,7 +241,7 @@ func TestStatusCode200(t *testing.T) {

err := client.Run(req, &resp)
if err != nil {
t.Errorf(err.Error())
t.Errorf("%s", err.Error())
}

if calls != 1 {
Expand All @@ -258,7 +258,7 @@ func TestStatusCode500(t *testing.T) {

_, err := io.WriteString(w, "some: data")
if err != nil {
t.Errorf(err.Error())
t.Errorf("%s", err.Error())
}
}))
defer srv.Close()
Expand Down Expand Up @@ -292,7 +292,7 @@ func TestStatusCode413(t *testing.T) {

_, err := io.WriteString(w, "some: data")
if err != nil {
t.Errorf(err.Error())
t.Errorf("%s", err.Error())
}
}))
defer srv.Close()
Expand Down
20 changes: 0 additions & 20 deletions cmd/diagnostic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"fmt"
"os"

"github.com/CircleCI-Public/circleci-cli/api"
"github.com/CircleCI-Public/circleci-cli/api/graphql"
Expand Down Expand Up @@ -57,25 +56,6 @@ func diagnostic(opts diagnosticOptions) error {

fmt.Println("OK, got a token.")

fmt.Println("Trying an introspection query on API... ")

responseIntro, err := api.IntrospectionQuery(opts.cl)
if responseIntro.Schema.QueryType.Name == "" {
fmt.Println("Unable to make a query against the GraphQL API, please check your settings")
if err != nil {
return err
}
}

fmt.Println("Ok.")

if opts.cfg.Debug {
_, err = fmt.Fprintf(os.Stderr, "Introspection query result with Schema.QueryType of %s", responseIntro.Schema.QueryType.Name)
if err != nil {
return err
}
}

responseWho, err := api.WhoamiQuery(opts.cl)

if err != nil {
Expand Down
Loading

0 comments on commit ebcbd1f

Please sign in to comment.