Skip to content

Commit

Permalink
Merge pull request #112 from CircleCI-Public/whoami
Browse files Browse the repository at this point in the history
Add whoami details to diagnostic check
  • Loading branch information
Zachary Scott authored Sep 20, 2018
2 parents e9c2278 + cc4eb66 commit b4a0c54
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
31 changes: 31 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ type CreateNamespaceResponse struct {
GQLResponseErrors
}

// WhoamiResponse type matches the data shape of the GQL response for the current user
type WhoamiResponse struct {
Me struct {
Name string
}

GQLResponseErrors
}

// CreateOrbResponse type matches the data shape of the GQL response for
// creating an orb
type CreateOrbResponse struct {
Expand Down Expand Up @@ -202,6 +211,28 @@ func loadYaml(path string) (string, error) {
return string(config), nil
}

// WhoamiQuery returns the result of querying the `/me` endpoint of the API
func WhoamiQuery(ctx context.Context, logger *logger.Logger) (*WhoamiResponse, error) {
response := WhoamiResponse{}
query := `query { me { name } }`

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

address, err := GraphQLServerAddress(EnvEndpointHost())
if err != nil {
return nil, err
}
graphQLclient := client.NewClient(address, logger)

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

if err != nil {
return nil, err
}

return &response, nil
}

func buildAndOrbQuery(ctx context.Context, logger *logger.Logger, configPath string, response interface{}, query string) error {
config, err := loadYaml(configPath)
if err != nil {
Expand Down
9 changes: 8 additions & 1 deletion cmd/diagnostic.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func diagnostic(cmd *cobra.Command, args []string) error {
token := viper.GetString("token")

Logger.Infoln("\n---\nCircleCI CLI Diagnostics\n---")
Logger.Infof("Verbose mode: %v\n", viper.GetBool("verbose"))

Logger.Infof("Config found: %v\n", viper.ConfigFileUsed())

Logger.Infof("GraphQL API address: %s\n", address)
Expand All @@ -36,7 +38,6 @@ func diagnostic(cmd *cobra.Command, args []string) error {
return errors.New("please set a token")
}
Logger.Infoln("OK, got a token.")
Logger.Infof("Verbose mode: %v\n", viper.GetBool("verbose"))

Logger.Infoln("Trying an introspection query on API... ")
response, err := api.IntrospectionQuery(context.Background(), Logger)
Expand All @@ -50,5 +51,11 @@ func diagnostic(cmd *cobra.Command, args []string) error {
Logger.Infoln("Ok.")

Logger.Debug("Introspection query result with Schema.QueryType of %s", response.Schema.QueryType.Name)

who, err := api.WhoamiQuery(context.Background(), Logger)
if err == nil && who != nil && who.Me.Name != "" {
Logger.Infof("Hello, %s.\n", who.Me.Name)
}

return nil
}
26 changes: 26 additions & 0 deletions cmd/diagnostic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ var _ = Describe("Diagnostic", func() {
ghttp.RespondWith(http.StatusOK, `{ "data": `+mockResponse+`}`),
),
)
testServer.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("POST", "/"),
ghttp.VerifyContentType("application/json; charset=utf-8"),
ghttp.RespondWith(http.StatusOK, `{ "data": { "me": { "name": "zzak" } } }`),
),
)
})

AfterEach(func() {
Expand Down Expand Up @@ -155,5 +162,24 @@ token: mytoken
Eventually(session).Should(gexec.Exit(0))
})
})

Describe("whoami returns a user", func() {
BeforeEach(func() {
_, err := config.Write([]byte(`token: mytoken`))
Expect(err).ToNot(HaveOccurred())
Expect(config.Close()).To(Succeed())
})

It("print success", func() {
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).ShouldNot(HaveOccurred())
Eventually(session.Err.Contents()).Should(BeEmpty())
Eventually(session.Out).Should(gbytes.Say(
fmt.Sprintf("GraphQL API address: %s", testServer.URL())))
Eventually(session.Out).Should(gbytes.Say("OK, got a token."))
Eventually(session.Out).Should(gbytes.Say("Hello, zzak."))
Eventually(session).Should(gexec.Exit(0))
})
})
})
})
13 changes: 13 additions & 0 deletions cmd/testdata/me/query.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
query {
me {
name
avatarURL
accounts {
edges {
node {
id
}
}
}
}
}

0 comments on commit b4a0c54

Please sign in to comment.