-
-
Notifications
You must be signed in to change notification settings - Fork 245
feat(snapshots): Add download command for baseline snapshots #3310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
NicoHinderling
wants to merge
1
commit into
feat/snapshots-diff
Choose a base branch
from
feat/snapshots-download
base: feat/snapshots-diff
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| use std::fs; | ||
| use std::io::{self, Seek as _}; | ||
| use std::path::PathBuf; | ||
|
|
||
| use anyhow::{bail, Result}; | ||
| use clap::{Arg, ArgMatches, Command}; | ||
|
|
||
| use crate::api::Api; | ||
| use crate::config::Config; | ||
| use crate::utils::args::ArgExt as _; | ||
| use crate::utils::fs::path_as_url; | ||
|
|
||
| const EXPERIMENTAL_WARNING: &str = | ||
| "[EXPERIMENTAL] The \"snapshots download\" command is experimental. \ | ||
| The command is subject to breaking changes, including removal, in any Sentry CLI release."; | ||
|
|
||
| pub fn make_command(command: Command) -> Command { | ||
| command | ||
| .about("[EXPERIMENTAL] Download baseline snapshot images from Sentry.") | ||
| .long_about(format!( | ||
| "Download baseline snapshot images from Sentry's preprod system to a local directory.\n\n\ | ||
| {EXPERIMENTAL_WARNING}" | ||
| )) | ||
| .org_arg() | ||
| .arg( | ||
| Arg::new("app_id") | ||
| .long("app-id") | ||
| .value_name("APP_ID") | ||
| .help("App identifier (e.g. sentry-frontend). Mutually exclusive with --snapshot-id.") | ||
| .conflicts_with("snapshot_id"), | ||
| ) | ||
| .arg( | ||
| Arg::new("snapshot_id") | ||
| .long("snapshot-id") | ||
| .value_name("ID") | ||
| .help("Direct snapshot artifact ID. Mutually exclusive with --app-id.") | ||
| .conflicts_with("app_id"), | ||
| ) | ||
| .arg( | ||
| Arg::new("branch") | ||
| .long("branch") | ||
| .value_name("NAME") | ||
| .help("Git branch filter (only with --app-id).") | ||
| .requires("app_id"), | ||
| ) | ||
| .arg( | ||
| Arg::new("output") | ||
| .long("output") | ||
| .value_name("DIR") | ||
| .help("Directory for extracted images.") | ||
| .default_value("./snapshots-base/"), | ||
| ) | ||
| } | ||
|
|
||
| pub fn execute(matches: &ArgMatches) -> Result<()> { | ||
| eprintln!("{EXPERIMENTAL_WARNING}"); | ||
|
|
||
| let config = Config::current(); | ||
| let org = config.get_org(matches)?; | ||
| let api_ref = Api::current(); | ||
| let api = api_ref.authenticated()?; | ||
|
|
||
| let app_id = matches.get_one::<String>("app_id"); | ||
| let snapshot_id_arg = matches.get_one::<String>("snapshot_id"); | ||
| let branch = matches.get_one::<String>("branch").map(|s| s.as_str()); | ||
| let output_dir = PathBuf::from( | ||
| matches | ||
| .get_one::<String>("output") | ||
| .expect("output has a default value"), | ||
| ); | ||
|
|
||
| let snapshot_id = match (app_id, snapshot_id_arg) { | ||
| (Some(app_id), None) => { | ||
| eprintln!("Resolving latest baseline snapshot for app '{app_id}'..."); | ||
| match api.get_latest_base_snapshot(&org, app_id, branch)? { | ||
| Some(resp) => { | ||
| eprintln!( | ||
| "Found snapshot {} ({} images)", | ||
| resp.head_artifact_id, resp.image_count | ||
| ); | ||
| resp.head_artifact_id | ||
| } | ||
| None => { | ||
| let branch_msg = branch | ||
| .map(|b| format!(" on branch '{b}'")) | ||
| .unwrap_or_default(); | ||
| bail!("No baseline snapshot found for app '{app_id}'{branch_msg}"); | ||
| } | ||
| } | ||
| } | ||
| (None, Some(id)) => id.clone(), | ||
| _ => bail!("Exactly one of --app-id or --snapshot-id must be provided"), | ||
| }; | ||
|
|
||
| eprintln!("Downloading snapshot {snapshot_id}..."); | ||
| let mut tmp = tempfile::tempfile()?; | ||
| let response = api.download_snapshot_zip(&org, &snapshot_id, &mut tmp)?; | ||
|
|
||
| if response.failed() { | ||
| bail!( | ||
| "Failed to download snapshot (server returned status {}).", | ||
| response.status() | ||
| ); | ||
| } | ||
|
|
||
| tmp.seek(io::SeekFrom::Start(0))?; | ||
| let mut archive = zip::ZipArchive::new(&mut tmp)?; | ||
|
|
||
| fs::create_dir_all(&output_dir)?; | ||
|
|
||
| let mut extracted = 0usize; | ||
| for i in 0..archive.len() { | ||
| let mut entry = archive.by_index(i)?; | ||
| if entry.is_dir() { | ||
| continue; | ||
| } | ||
| let Some(enclosed_name) = entry.enclosed_name() else { | ||
| continue; | ||
| }; | ||
| let out_path = output_dir.join(&enclosed_name); | ||
| if let Some(parent) = out_path.parent() { | ||
| fs::create_dir_all(parent)?; | ||
| } | ||
| let mut out_file = fs::File::create(&out_path)?; | ||
| io::copy(&mut entry, &mut out_file)?; | ||
| extracted += 1; | ||
| } | ||
|
|
||
| eprintln!( | ||
| "\nDownloaded {extracted} images from snapshot {snapshot_id} to {}", | ||
| path_as_url(&output_dir) | ||
| ); | ||
|
|
||
| Ok(()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
tests/integration/_cases/snapshots/snapshots-download-help.trycmd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| ``` | ||
| $ sentry-cli snapshots download --help | ||
| ? success | ||
| Download baseline snapshot images from Sentry's preprod system to a local directory. | ||
|
|
||
| [EXPERIMENTAL] The "snapshots download" command is experimental. The command is subject to breaking | ||
| changes, including removal, in any Sentry CLI release. | ||
|
|
||
| Usage: sentry-cli[EXE] snapshots download [OPTIONS] | ||
|
|
||
| Options: | ||
| -o, --org <ORG> | ||
| The organization ID or slug. | ||
|
|
||
| --app-id <APP_ID> | ||
| App identifier (e.g. sentry-frontend). Mutually exclusive with --snapshot-id. | ||
|
|
||
| --header <KEY:VALUE> | ||
| Custom headers that should be attached to all requests | ||
| in key:value format. | ||
|
|
||
| --auth-token <AUTH_TOKEN> | ||
| Use the given Sentry auth token. | ||
|
|
||
| --snapshot-id <ID> | ||
| Direct snapshot artifact ID. Mutually exclusive with --app-id. | ||
|
|
||
| --branch <NAME> | ||
| Git branch filter (only with --app-id). | ||
|
|
||
| --log-level <LOG_LEVEL> | ||
| Set the log output verbosity. [possible values: trace, debug, info, warn, error] | ||
|
|
||
| --output <DIR> | ||
| Directory for extracted images. | ||
|
|
||
| [default: ./snapshots-base/] | ||
|
|
||
| --quiet | ||
| Do not print any output while preserving correct exit code. This flag is currently | ||
| implemented only for selected subcommands. | ||
|
|
||
| [aliases: --silent] | ||
|
|
||
| -h, --help | ||
| Print help (see a summary with '-h') | ||
|
|
||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.