Skip to content

Commit

Permalink
feat(rover): output service title for whoami (#299)
Browse files Browse the repository at this point in the history
* feat(rover): output service title for whoami

In our discussions we found that Name was not a useful piece of
information to show to Rover's users. Instead, this commit introduces
a Service title which is aligned with the information that Studio
displays to a user. This commit ensures that Service title is only
printed for graph keys. For user keys we do not display this service
title.

Co-authored-by: Jake Dawkins <[email protected]>
  • Loading branch information
lrlna and JakeDawkins authored Feb 23, 2021
1 parent 7f3fc65 commit df17531
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
4 changes: 3 additions & 1 deletion crates/rover-client/src/query/config/whoami.graphql
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
query WhoAmIQuery {
me {
__typename
name
... on Service {
title
}
id
asActor {
type
Expand Down
19 changes: 13 additions & 6 deletions crates/rover-client/src/query/config/whoami.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ pub struct WhoAmIQuery;

#[derive(Debug, PartialEq)]
pub struct RegistryIdentity {
pub name: String,
pub id: String,
pub graph_title: Option<String>,
pub key_actor_type: Actor,
}

Expand Down Expand Up @@ -48,14 +48,21 @@ fn get_identity_from_response_data(
// graphs as api key actors, since that's all we _should_ get.
// I think it's safe to only include those two kinds of actors in the enum
// more here: https://studio-staging.apollographql.com/graph/engine/schema/reference/enums/ActorType?variant=prod

let key_actor_type = match me.as_actor.type_ {
who_am_i_query::ActorType::GRAPH => Actor::GRAPH,
who_am_i_query::ActorType::USER => Actor::USER,
_ => Actor::OTHER,
};

let graph_title = match me.on {
who_am_i_query::WhoAmIQueryMeOn::Service(s) => Some(s.title),
_ => None,
};

Ok(RegistryIdentity {
id: me.id,
name: me.name,
graph_title,
key_actor_type,
})
} else {
Expand All @@ -72,7 +79,7 @@ mod tests {
let json_response = json!({
"me": {
"__typename": "User",
"name": "Yaboi",
"title": "SearchForTunaService",
"id": "gh.nobodydefinitelyhasthisusernamelol",
"asActor": {
"type": "USER"
Expand All @@ -83,8 +90,8 @@ mod tests {
let output = get_identity_from_response_data(data);

let expected_identity = RegistryIdentity {
name: "Yaboi".to_string(),
id: "gh.nobodydefinitelyhasthisusernamelol".to_string(),
graph_title: None,
key_actor_type: Actor::USER,
};
assert!(output.is_ok());
Expand All @@ -96,7 +103,7 @@ mod tests {
let json_response = json!({
"me": {
"__typename": "Service",
"name": "big-ol-graph",
"title": "GraphKeyService",
"id": "big-ol-graph-key-lolol",
"asActor": {
"type": "GRAPH"
Expand All @@ -107,8 +114,8 @@ mod tests {
let output = get_identity_from_response_data(data);

let expected_identity = RegistryIdentity {
name: "big-ol-graph".to_string(),
id: "big-ol-graph-key-lolol".to_string(),
graph_title: Some("GraphKeyService".to_string()),
key_actor_type: Actor::GRAPH,
};
assert!(output.is_ok());
Expand Down
31 changes: 26 additions & 5 deletions src/command/config/whoami.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// use ansi_term::Colour::{Cyan, Yellow};
use ansi_term::Colour::Green;
use serde::Serialize;
use structopt::StructOpt;

use rover_client::query::config::whoami;

use crate::anyhow;
use crate::command::RoverStdout;
use crate::utils::client::StudioClientConfig;
use crate::Result;
Expand All @@ -23,10 +24,30 @@ impl WhoAmI {

let identity = whoami::run(whoami::who_am_i_query::Variables {}, &client)?;

eprintln!(
"Key Info:\n- Name: {}\n- ID: {}\n- Key Type: {:?}",
identity.name, identity.id, identity.key_actor_type
);
let message = match identity.key_actor_type {
whoami::Actor::GRAPH => Ok(format!(
"Key Info\n{}: {}\n{}: {}\n{}: {:?}",
Green.normal().paint("Graph Title"),
identity.graph_title.unwrap(),
Green.normal().paint("Unique Graph ID"),
identity.id,
Green.normal().paint("Key Type"),
identity.key_actor_type
)),
whoami::Actor::USER => Ok(format!(
"Key Info\n{}: {}\n{}: {:?}",
Green.normal().paint("User ID"),
identity.id,
Green.normal().paint("Key Type"),
identity.key_actor_type
)),
_ => Err(anyhow!(
"The key provided is invalid. Rover only accepts personal and graph API keys"
)),
}?;

eprintln!("{}", message);

Ok(RoverStdout::None)
}
}

0 comments on commit df17531

Please sign in to comment.