Skip to content

Commit

Permalink
split out api, test_utils, and bundlerepo from cli
Browse files Browse the repository at this point in the history
  • Loading branch information
dfrankland committed Oct 2, 2024
1 parent 9d7fa72 commit c72e8f9
Show file tree
Hide file tree
Showing 21 changed files with 741 additions and 703 deletions.
44 changes: 38 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["cli", "codeowners", "junit-mock", "context"]
members = ["api", "cli", "codeowners", "context", "junit-mock", "test_utils"]
resolver = "2"

[profile.release]
Expand Down
9 changes: 9 additions & 0 deletions api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "api"
version = "0.1.0"
edition = "2021"

[dependencies]
context = { path = "../context" }
serde = { version = "1.0.130", default-features = false, features = ["derive"] }
serde_json = "1.0.68"
42 changes: 42 additions & 0 deletions api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use std::collections::HashSet;

use context::repo::RepoUrlParts;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Clone, Deserialize, PartialEq, Eq)]
pub struct CreateBundleUploadRequest {
pub repo: RepoUrlParts,
#[serde(rename = "orgUrlSlug")]
pub org_url_slug: String,
}

#[derive(Debug, Serialize, Clone, Deserialize)]
pub struct CreateBundleUploadResponse {
pub id: String,
pub url: String,
pub key: String,
}

#[derive(Debug, Serialize, Clone, Deserialize, PartialEq, Eq)]
pub struct CreateRepoRequest {
pub repo: RepoUrlParts,
#[serde(rename = "orgUrlSlug")]
pub org_url_slug: String,
#[serde(rename = "remoteUrls")]
pub remote_urls: Vec<String>,
}

#[derive(Debug, Serialize, Clone, Deserialize, PartialEq, Eq)]
pub struct GetQuarantineBulkTestStatusRequest {
pub repo: RepoUrlParts,
#[serde(rename = "orgUrlSlug")]
pub org_url_slug: String,
}

#[derive(Debug, Serialize, Clone, Deserialize, Default)]
pub struct QuarantineConfig {
#[serde(rename = "isPreview")]
pub is_preview_mode: bool,
#[serde(rename = "testIds")]
pub quarantined_tests: HashSet<String>,
}
5 changes: 3 additions & 2 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ path = "src/lib.rs"

[dependencies]
anyhow = "1.0.44"
api = { path = "../api" }
chrono = { version = "0.4.33", default-features = false, features = ["clock"] }
clap = { version = "4.4.18", features = ["derive", "env"] }
context = { path = "../context" }
env_logger = { version = "0.11.0", default-features = false }
log = "0.4.14"
exitcode = "1.1.1"
Expand All @@ -24,7 +26,6 @@ tokio = { version = "*", default-features = false, features = [
] }
tempfile = "3.2.0"
tokio-retry = { version = "0.3", default-features = false }
gix = { version = "0.63.0", default-features = false, features = [] }
glob = "0.3.0"
regex = { version = "1.10.3", default-features = false, features = ["std"] }
reqwest = { version = "0.12.5", default-features = false, features = [
Expand All @@ -46,8 +47,8 @@ uuid = { version = "1.10.0", features = ["v5"] }
assert_cmd = "2.0.16"
assert_matches = "1.5.0"
axum = { version = "0.7.5", features = ["macros"] }
git2 = "0.19.0" # Used for creating test repos with libgit2
junit-mock = { path = "../junit-mock" }
test_utils = { path = "../test_utils" }

[build-dependencies]
vergen = { version = "8.3.1", features = [
Expand Down
16 changes: 8 additions & 8 deletions cli/src/clients.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::format;
use std::path::PathBuf;
use std::{format, path::PathBuf};

use anyhow::Context;

use crate::types::{
use api::{
CreateBundleUploadRequest, CreateBundleUploadResponse, CreateRepoRequest,
GetQuarantineBulkTestStatusRequest, QuarantineConfig, Repo,
GetQuarantineBulkTestStatusRequest, QuarantineConfig,
};
use context::repo::RepoUrlParts;

use crate::utils::status_code_help;

pub const TRUNK_API_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(30);
Expand All @@ -16,7 +16,7 @@ pub async fn create_trunk_repo(
origin: &str,
api_token: &str,
org_slug: &str,
repo: &Repo,
repo: &RepoUrlParts,
remote_urls: &[String],
) -> anyhow::Result<()> {
let client = reqwest::Client::new();
Expand Down Expand Up @@ -52,7 +52,7 @@ pub async fn create_bundle_upload_intent(
origin: &str,
api_token: &str,
org_slug: &str,
repo: &Repo,
repo: &RepoUrlParts,
) -> anyhow::Result<CreateBundleUploadResponse> {
let client = reqwest::Client::new();
let resp = match client
Expand Down Expand Up @@ -87,7 +87,7 @@ pub async fn get_quarantining_config(
origin: &str,
api_token: &str,
org_slug: &str,
repo: &Repo,
repo: &RepoUrlParts,
) -> anyhow::Result<QuarantineConfig> {
let client = reqwest::Client::new();
let resp = match client
Expand Down
28 changes: 14 additions & 14 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::io::Write;
use std::time::{SystemTime, UNIX_EPOCH};

use clap::{Args, Parser, Subcommand};
use context::repo::BundleRepo;
use tokio_retry::strategy::ExponentialBackoff;
use tokio_retry::Retry;
use trunk_analytics_cli::bundler::BundlerUtil;
Expand All @@ -16,11 +17,11 @@ use trunk_analytics_cli::constants::{
use trunk_analytics_cli::runner::{
build_filesets, extract_failed_tests, run_quarantine, run_test_command,
};
use trunk_analytics_cli::scanner::{BundleRepo, EnvScanner};
use trunk_analytics_cli::scanner::EnvScanner;
use trunk_analytics_cli::types::{
BundleMeta, QuarantineBulkTestStatus, QuarantineRunResult, RunResult, META_VERSION,
};
use trunk_analytics_cli::utils::{from_non_empty_or_default, parse_custom_tags};
use trunk_analytics_cli::utils::parse_custom_tags;

#[derive(Debug, Parser)]
#[command(
Expand Down Expand Up @@ -159,7 +160,7 @@ async fn run_upload(
codeowners_path,
} = upload_args;

let repo = BundleRepo::try_read_from_root(
let repo = BundleRepo::new(
repo_root,
repo_url,
repo_head_sha,
Expand All @@ -171,11 +172,7 @@ async fn run_upload(
return Err(anyhow::anyhow!("No junit paths provided."));
}

let api_address = from_non_empty_or_default(
std::env::var(TRUNK_PUBLIC_API_ADDRESS_ENV).ok(),
DEFAULT_ORIGIN.to_string(),
|s| s,
);
let api_address = get_api_address();

let codeowners =
codeowners.or_else(|| CodeOwners::find_file(&repo.repo_root, &codeowners_path));
Expand Down Expand Up @@ -340,7 +337,7 @@ async fn run_test(test_args: TestArgs) -> anyhow::Result<i32> {
..
} = &upload_args;

let repo = BundleRepo::try_read_from_root(
let repo = BundleRepo::new(
repo_root.clone(),
repo_url.clone(),
repo_head_sha.clone(),
Expand All @@ -352,11 +349,7 @@ async fn run_test(test_args: TestArgs) -> anyhow::Result<i32> {
return Err(anyhow::anyhow!("No junit paths provided."));
}

let api_address = from_non_empty_or_default(
std::env::var(TRUNK_PUBLIC_API_ADDRESS_ENV).ok(),
DEFAULT_ORIGIN.to_string(),
|s| s,
);
let api_address = get_api_address();

let codeowners = CodeOwners::find_file(&repo.repo_root, codeowners_path);

Expand Down Expand Up @@ -456,3 +449,10 @@ fn setup_logger() -> anyhow::Result<()> {
builder.init();
Ok(())
}

fn get_api_address() -> String {
std::env::var(TRUNK_PUBLIC_API_ADDRESS_ENV)
.ok()
.and_then(|s| if s.is_empty() { None } else { Some(s) })
.unwrap_or_else(|| DEFAULT_ORIGIN.to_string())
}
8 changes: 4 additions & 4 deletions cli/src/runner.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use std::process::{Command, Stdio};
use std::time::SystemTime;

use api::QuarantineConfig;
use context::repo::BundleRepo;
use junit_parser;
use tokio_retry::strategy::ExponentialBackoff;
use tokio_retry::Retry;

use crate::clients::get_quarantining_config;
use crate::codeowners::CodeOwners;
use crate::constants::{EXIT_FAILURE, EXIT_SUCCESS};
use crate::scanner::{BundleRepo, FileSet, FileSetCounter};
use crate::types::{
QuarantineBulkTestStatus, QuarantineConfig, QuarantineRunResult, RunResult, Test,
};
use crate::scanner::{FileSet, FileSetCounter};
use crate::types::{QuarantineBulkTestStatus, QuarantineRunResult, RunResult, Test};

pub async fn run_test_command(
repo: &BundleRepo,
Expand Down
Loading

0 comments on commit c72e8f9

Please sign in to comment.