Skip to content

Commit 4003978

Browse files
chore: move GitContext to rover-client
1 parent e8154f4 commit 4003978

File tree

19 files changed

+116
-106
lines changed

19 files changed

+116
-106
lines changed

Cargo.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ camino = { version = "1.0.2", features = ["serde1"] }
4949
chrono = "0.4"
5050
console = "0.14.0"
5151
crossterm = "0.19.0"
52-
git-url-parse = "0.3.1"
53-
git2 = { version = "0.13.20", default-features = false, features = ["vendored-openssl"] }
5452
harmonizer = { version = "0.2.5", optional = true }
5553
heck = "0.3.3"
5654
humantime = "2.1.0"

crates/rover-client/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ houston = {path = "../houston"}
1414
anyhow = "1"
1515
camino = "1"
1616
chrono = "0.4"
17+
git-url-parse = "0.3.1"
18+
git2 = { version = "0.13.20", default-features = false, features = ["vendored-openssl"] }
1719
graphql-parser = "0.3.0"
1820
graphql_client = "0.9"
1921
http = "0.2"

crates/rover-client/build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use uuid::Uuid;
1010
///
1111
/// If the user is offline and the schema already exists in the file system, the script does nothing.
1212
///
13-
/// The URL to fetch the schema can be overriden with the APOLLO_GPAPHQL_SCHEMA_URL environment variable.
13+
/// The URL to fetch the schema can be overridden with the APOLLO_GPAPHQL_SCHEMA_URL environment variable.
1414
///
1515
/// Note: eprintln! statements only show up with `cargo build -vv`
1616
fn main() -> std::io::Result<()> {

crates/rover-client/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ pub mod query;
2121

2222
/// Module for getting release info
2323
pub mod releases;
24+
25+
/// Module for shared functionality
26+
pub mod utils;

src/utils/git.rs crates/rover-client/src/utils/git.rs

+81-87
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,101 @@
1-
use crate::utils::env::{RoverEnv, RoverEnvKey};
2-
use crate::Result;
3-
use rover_client::query::{graph, subgraph};
1+
use crate::query::{graph, subgraph};
42

53
use std::env;
64

75
use git2::{Reference, Repository};
86
use git_url_parse::GitUrl;
97

10-
#[derive(Debug, PartialEq)]
8+
#[derive(Debug, Clone, PartialEq)]
119
pub struct GitContext {
1210
pub branch: Option<String>,
1311
pub author: Option<String>,
1412
pub commit: Option<String>,
15-
pub message: Option<String>,
1613
pub remote_url: Option<String>,
1714
}
1815

1916
impl GitContext {
20-
pub fn try_from_rover_env(env: &RoverEnv) -> Result<Self> {
21-
let repo = Repository::discover(env::current_dir()?);
22-
match repo {
23-
Ok(repo) => {
24-
let head = repo.head().ok();
25-
Ok(Self {
26-
branch: GitContext::get_branch(env, head.as_ref())?,
27-
commit: GitContext::get_commit(env, head.as_ref())?,
28-
author: GitContext::get_author(env, head.as_ref())?,
29-
remote_url: GitContext::get_remote_url(env, Some(&repo))?,
30-
message: None,
31-
})
17+
pub fn new(override_git_context: GitContext) -> Self {
18+
let repo = GitContext::get_repo();
19+
20+
let mut remote_url = override_git_context.remote_url;
21+
22+
if let Some(repo) = repo {
23+
remote_url = remote_url.or_else(|| GitContext::get_remote_url(&repo));
24+
if let Ok(head) = repo.head() {
25+
let branch = override_git_context
26+
.branch
27+
.or_else(|| GitContext::get_branch(&head));
28+
29+
let author = override_git_context
30+
.author
31+
.or_else(|| GitContext::get_author(&head));
32+
33+
let commit = override_git_context
34+
.commit
35+
.or_else(|| GitContext::get_commit(&head));
36+
37+
return GitContext {
38+
branch,
39+
author,
40+
commit,
41+
remote_url,
42+
};
3243
}
33-
Err(_) => Ok(Self {
34-
branch: GitContext::get_branch(env, None)?,
35-
commit: GitContext::get_commit(env, None)?,
36-
author: GitContext::get_author(env, None)?,
37-
remote_url: GitContext::get_remote_url(env, None)?,
38-
message: None,
39-
}),
44+
}
45+
46+
GitContext {
47+
branch: override_git_context.branch,
48+
author: override_git_context.author,
49+
commit: override_git_context.commit,
50+
remote_url,
4051
}
4152
}
4253

43-
fn get_branch(env: &RoverEnv, head: Option<&Reference>) -> Result<Option<String>> {
44-
Ok(env.get(RoverEnvKey::VcsBranch)?.or_else(|| {
45-
let mut branch = None;
46-
if let Some(head) = head {
47-
branch = head.shorthand().map(|s| s.to_string())
48-
}
49-
branch
50-
}))
54+
pub fn default() -> Self {
55+
GitContext::new(GitContext {
56+
author: None,
57+
branch: None,
58+
commit: None,
59+
remote_url: None,
60+
})
5161
}
5262

53-
fn get_commit(env: &RoverEnv, head: Option<&Reference>) -> Result<Option<String>> {
54-
Ok(env.get(RoverEnvKey::VcsCommit)?.or_else(|| {
55-
let mut commit = None;
56-
if let Some(head) = head {
57-
if let Ok(head_commit) = head.peel_to_commit() {
58-
commit = Some(head_commit.id().to_string())
59-
}
60-
}
61-
commit
62-
}))
63+
fn get_repo() -> Option<Repository> {
64+
env::current_dir()
65+
.map(|d| Repository::discover(d).ok())
66+
.ok()
67+
.flatten()
6368
}
6469

65-
fn get_author(env: &RoverEnv, head: Option<&Reference>) -> Result<Option<String>> {
66-
Ok(env.get(RoverEnvKey::VcsAuthor)?.or_else(|| {
67-
let mut author = None;
68-
if let Some(head) = head {
69-
if let Ok(head_commit) = head.peel_to_commit() {
70-
author = Some(head_commit.author().to_string())
71-
}
72-
}
73-
author
74-
}))
70+
fn get_branch(head: &Reference) -> Option<String> {
71+
head.shorthand().map(|s| s.to_string())
7572
}
7673

77-
fn get_remote_url(env: &RoverEnv, repo: Option<&Repository>) -> Result<Option<String>> {
78-
let remote_url = env.get(RoverEnvKey::VcsRemoteUrl)?.or_else(|| {
79-
let mut remote_url = None;
80-
if let Some(repo) = repo {
81-
if let Ok(remote) = repo.find_remote("origin") {
82-
remote_url = remote.url().map(|r| r.to_string())
83-
}
84-
}
85-
remote_url
86-
});
74+
fn get_commit(head: &Reference) -> Option<String> {
75+
if let Ok(head_commit) = head.peel_to_commit() {
76+
Some(head_commit.id().to_string())
77+
} else {
78+
None
79+
}
80+
}
8781

88-
Ok(if let Some(remote_url) = remote_url {
89-
GitContext::sanitize_remote_url(&remote_url)
82+
fn get_author(head: &Reference) -> Option<String> {
83+
if let Ok(head_commit) = head.peel_to_commit() {
84+
Some(head_commit.author().to_string())
9085
} else {
9186
None
92-
})
87+
}
88+
}
89+
90+
fn get_remote_url(repo: &Repository) -> Option<String> {
91+
let remote_url = if let Ok(remote) = repo.find_remote("origin") {
92+
remote.url().map(|r| r.to_string())
93+
} else {
94+
None
95+
};
96+
remote_url
97+
.map(|r| GitContext::sanitize_remote_url(&r))
98+
.flatten()
9399
}
94100

95101
// Parses and sanitizes git remote urls according to the same rules as
@@ -139,7 +145,7 @@ impl From<GitContext> for GraphPublishContextInput {
139145
commit: git_context.commit,
140146
committer: git_context.author,
141147
remote_url: git_context.remote_url,
142-
message: git_context.message,
148+
message: None,
143149
}
144150
}
145151
}
@@ -152,7 +158,7 @@ impl From<GitContext> for GraphCheckContextInput {
152158
commit: git_context.commit,
153159
committer: git_context.author,
154160
remote_url: git_context.remote_url,
155-
message: git_context.message,
161+
message: None,
156162
}
157163
}
158164
}
@@ -166,7 +172,7 @@ impl From<GitContext> for SubgraphPublishContextInput {
166172
commit: git_context.commit,
167173
committer: git_context.author,
168174
remote_url: git_context.remote_url,
169-
message: git_context.message,
175+
message: None,
170176
}
171177
}
172178
}
@@ -180,15 +186,14 @@ impl From<GitContext> for SubgraphCheckContextInput {
180186
commit: git_context.commit,
181187
committer: git_context.author,
182188
remote_url: git_context.remote_url,
183-
message: git_context.message,
189+
message: None,
184190
}
185191
}
186192
}
187193

188194
#[cfg(test)]
189195
mod tests {
190196
use super::*;
191-
use crate::PKG_NAME;
192197

193198
#[test]
194199
fn removed_user_from_remote_with_only_user() {
@@ -319,30 +324,21 @@ mod tests {
319324
let commit = "f84b32caddddfdd9fa87d7ce2140d56eabe805ee".to_string();
320325
let remote_url = "[email protected]:roku/theworstremoteintheworld.git".to_string();
321326

322-
let mut rover_env = RoverEnv::new();
323-
rover_env.insert(RoverEnvKey::VcsBranch, &branch);
324-
rover_env.insert(RoverEnvKey::VcsAuthor, &author);
325-
rover_env.insert(RoverEnvKey::VcsCommit, &commit);
326-
rover_env.insert(RoverEnvKey::VcsRemoteUrl, &remote_url);
327-
328-
let expected_git_context = GitContext {
327+
let override_git_context = GitContext {
329328
branch: Some(branch),
330329
author: Some(author),
331330
commit: Some(commit),
332-
message: None,
333331
remote_url: Some(remote_url),
334332
};
335333

336-
let actual_git_context = GitContext::try_from_rover_env(&rover_env)
337-
.expect("Could not create GitContext from RoverEnv");
334+
let actual_git_context = GitContext::new(override_git_context.clone());
338335

339-
assert_eq!(expected_git_context, actual_git_context);
336+
assert_eq!(override_git_context, actual_git_context);
340337
}
341338

342339
#[test]
343-
fn it_can_create_git_context_committ_author_remote_url() {
344-
let git_context =
345-
GitContext::try_from_rover_env(&RoverEnv::new()).expect("Could not create git context");
340+
fn it_can_create_git_context_commit_author_remote_url() {
341+
let git_context = GitContext::default();
346342

347343
assert!(git_context.branch.is_some());
348344
assert!(git_context.author.is_some());
@@ -353,10 +349,8 @@ mod tests {
353349
panic!("Could not find the commit hash");
354350
}
355351

356-
assert!(git_context.message.is_none());
357-
358352
if let Some(remote_url) = git_context.remote_url {
359-
assert!(remote_url.contains(PKG_NAME));
353+
assert!(remote_url.contains("apollographql"));
360354
} else {
361355
panic!("GitContext could not find the remote url");
362356
}

crates/rover-client/src/utils/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mod git;
2+
3+
pub use git::GitContext;

installers/binstall/scripts/nix/install.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ BINARY_DOWNLOAD_PREFIX="https://github.com/apollographql/rover/releases/download
1616
# Rover version defined in root cargo.toml
1717
# Note: this line is built automatically
1818
# in build.rs. Don't touch it!
19-
PACKAGE_VERSION="v0.1.6"
19+
PACKAGE_VERSION="v0.1.3"
2020

2121
download_binary_and_run_installer() {
2222
downloader --check

installers/binstall/scripts/windows/install.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# version found in Rover's Cargo.toml
1010
# Note: this line is built automatically
1111
# in build.rs. Don't touch it!
12-
$package_version = 'v0.1.6'
12+
$package_version = 'v0.1.3'
1313

1414
function Install-Binary() {
1515
$old_erroractionpreference = $ErrorActionPreference

installers/npm/package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

installers/npm/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@apollo/rover",
3-
"version": "0.1.6",
3+
"version": "0.1.3",
44
"description": "The new Apollo CLI",
55
"main": "index.js",
66
"bin": {

src/cli.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ use crate::command::{self, RoverStdout};
55
use crate::utils::{
66
client::StudioClientConfig,
77
env::{RoverEnv, RoverEnvKey},
8-
git::GitContext,
98
stringify::from_display,
109
version,
1110
};
1211
use crate::Result;
1312
use config::Config;
1413
use houston as config;
14+
use rover_client::utils::GitContext;
1515
use timber::{Level, LEVELS};
1616

1717
use camino::Utf8PathBuf;
@@ -87,7 +87,14 @@ impl Rover {
8787

8888
pub(crate) fn get_git_context(&self) -> Result<GitContext> {
8989
// constructing GitContext with a set of overrides from env vars
90-
let git_context = GitContext::try_from_rover_env(&self.env_store)?;
90+
let override_git_context = GitContext {
91+
branch: self.env_store.get(RoverEnvKey::VcsBranch).ok().flatten(),
92+
commit: self.env_store.get(RoverEnvKey::VcsCommit).ok().flatten(),
93+
author: self.env_store.get(RoverEnvKey::VcsAuthor).ok().flatten(),
94+
remote_url: self.env_store.get(RoverEnvKey::VcsRemoteUrl).ok().flatten(),
95+
};
96+
97+
let git_context = GitContext::new(override_git_context);
9198
tracing::debug!(?git_context);
9299
Ok(git_context)
93100
}

src/command/graph/check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ use serde::Serialize;
22
use structopt::StructOpt;
33

44
use rover_client::query::graph::check;
5+
use rover_client::utils::GitContext;
56

67
use crate::command::RoverStdout;
78
use crate::utils::client::StudioClientConfig;
8-
use crate::utils::git::GitContext;
99
use crate::utils::loaders::load_schema_from_flag;
1010
use crate::utils::parsers::{
1111
parse_graph_ref, parse_query_count_threshold, parse_query_percentage_threshold,

0 commit comments

Comments
 (0)