-
-
Notifications
You must be signed in to change notification settings - Fork 244
feat(build): Add build download command
#3221
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
551c53d
feat(build): Add `build download` command (EME-272)
runningcode 38241aa
docs(changelog): Add entry for build download command
runningcode fc5832a
fix(build): Error on unsupported download format
runningcode f79f33d
fix(build): Use expect instead of unwrap for clippy
runningcode dd61971
test(build): Add integration tests for build download command
runningcode 532d72c
fix(build): Fix clippy and Windows CI for download tests
runningcode 3b0c857
ref(build): Remove project flag from download and mark experimental
runningcode 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
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,106 @@ | ||
| use std::fs; | ||
| use std::path::PathBuf; | ||
|
|
||
| use anyhow::{bail, Result}; | ||
| use clap::{Arg, ArgMatches, Command}; | ||
| use log::info; | ||
|
|
||
| use crate::api::Api; | ||
| use crate::config::Config; | ||
| use crate::utils::args::ArgExt as _; | ||
| use crate::utils::fs::TempFile; | ||
|
|
||
| const EXPERIMENTAL_WARNING: &str = | ||
| "[EXPERIMENTAL] The \"build 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 a build artifact.") | ||
| .long_about(format!( | ||
| "Download a build artifact.\n\n{EXPERIMENTAL_WARNING}" | ||
| )) | ||
| .org_arg() | ||
| .arg( | ||
| Arg::new("build_id") | ||
| .long("build-id") | ||
| .short('b') | ||
| .required(true) | ||
| .help("The ID of the build to download."), | ||
| ) | ||
| .arg(Arg::new("output").long("output").help( | ||
|
szokeasaurusrex marked this conversation as resolved.
|
||
| "The output file path. Defaults to \ | ||
| 'preprod_artifact_<build_id>.<ext>' in the current directory, \ | ||
| where ext is ipa or apk depending on the platform.", | ||
| )) | ||
| } | ||
|
|
||
| /// For iOS builds, the install URL points to a plist manifest. | ||
| /// Replace the response_format to download the actual IPA binary instead. | ||
| fn ensure_binary_format(url: &str) -> String { | ||
| url.replace("response_format=plist", "response_format=ipa") | ||
| } | ||
|
|
||
| /// Extract the file extension from the response_format query parameter. | ||
| fn extension_from_url(url: &str) -> Result<&str> { | ||
| if url.contains("response_format=ipa") { | ||
| Ok("ipa") | ||
| } else if url.contains("response_format=apk") { | ||
| Ok("apk") | ||
| } else { | ||
| bail!("Unsupported build format in download URL.") | ||
| } | ||
| } | ||
|
|
||
| pub fn execute(matches: &ArgMatches) -> Result<()> { | ||
| eprintln!("{EXPERIMENTAL_WARNING}"); | ||
| let config = Config::current(); | ||
| let org = config.get_org(matches)?; | ||
| let build_id = matches | ||
| .get_one::<String>("build_id") | ||
| .expect("build_id is required"); | ||
|
|
||
| let api = Api::current(); | ||
| let authenticated_api = api.authenticated()?; | ||
|
|
||
| info!("Fetching install details for build {build_id}"); | ||
| let details = authenticated_api.get_build_install_details(&org, build_id)?; | ||
|
|
||
| if !details.is_installable { | ||
| bail!("Build {build_id} is not installable."); | ||
| } | ||
|
|
||
| let install_url = details | ||
| .install_url | ||
| .ok_or_else(|| anyhow::anyhow!("Build {build_id} has no install URL."))?; | ||
|
|
||
| let download_url = ensure_binary_format(&install_url); | ||
|
|
||
| let output_path = match matches.get_one::<String>("output") { | ||
| Some(path) => PathBuf::from(path), | ||
| None => { | ||
| let ext = extension_from_url(&download_url)?; | ||
| PathBuf::from(format!("preprod_artifact_{build_id}.{ext}")) | ||
| } | ||
| }; | ||
|
|
||
| info!("Downloading build {build_id} to {}", output_path.display()); | ||
|
|
||
| let tmp = TempFile::create()?; | ||
| let mut file = tmp.open()?; | ||
| let response = authenticated_api.download_installable_build(&download_url, &mut file)?; | ||
|
|
||
| if response.failed() { | ||
| bail!( | ||
| "Failed to download build (server returned status {}).", | ||
| response.status() | ||
| ); | ||
| } | ||
|
|
||
| drop(file); | ||
| fs::copy(tmp.path(), &output_path)?; | ||
|
|
||
| println!("Successfully downloaded build to {}", output_path.display()); | ||
|
|
||
| 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
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,44 @@ | ||
| ``` | ||
| $ sentry-cli build download --help | ||
| ? success | ||
| Download a build artifact. | ||
|
|
||
| [EXPERIMENTAL] The "build download" command is experimental. The command is subject to breaking | ||
| changes, including removal, in any Sentry CLI release. | ||
|
|
||
| Usage: sentry-cli[EXE] build download [OPTIONS] --build-id <build_id> | ||
|
|
||
| Options: | ||
| -o, --org <ORG> | ||
| The organization ID or slug. | ||
|
|
||
| -b, --build-id <build_id> | ||
| The ID of the build to download. | ||
|
|
||
| --header <KEY:VALUE> | ||
| Custom headers that should be attached to all requests | ||
| in key:value format. | ||
|
|
||
| -p, --project <PROJECT> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are builds project or org scoped?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, the project arg is optional here i just tested. |
||
| The project ID or slug. | ||
|
|
||
| --auth-token <AUTH_TOKEN> | ||
| Use the given Sentry auth token. | ||
|
|
||
| --output <output> | ||
| The output file path. Defaults to 'preprod_artifact_<build_id>.<ext>' in the current | ||
| directory, where ext is ipa or apk depending on the platform. | ||
|
|
||
| --log-level <LOG_LEVEL> | ||
| Set the log output verbosity. [possible values: trace, debug, info, warn, error] | ||
|
|
||
| --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
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,129 @@ | ||
| use crate::integration::{AssertCommand, MockEndpointBuilder, TestManager}; | ||
|
|
||
| #[test] | ||
| fn command_build_download_help() { | ||
| TestManager::new().register_trycmd_test("build/build-download-help.trycmd"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn command_build_download_not_installable() { | ||
| TestManager::new() | ||
| .mock_endpoint( | ||
| MockEndpointBuilder::new( | ||
| "GET", | ||
| "/api/0/organizations/wat-org/preprodartifacts/123/install-details/", | ||
| ) | ||
| .with_response_body(r#"{"isInstallable": false, "installUrl": null}"#), | ||
| ) | ||
| .assert_cmd(vec!["build", "download", "--build-id", "123"]) | ||
| .with_default_token() | ||
| .run_and_assert(AssertCommand::Failure); | ||
| } | ||
|
|
||
| #[test] | ||
| fn command_build_download_apk() { | ||
| let manager = TestManager::new(); | ||
| let server_url = manager.server_url(); | ||
| let download_path = format!("{server_url}/download/build.apk?response_format=apk"); | ||
| let install_details_response = serde_json::json!({ | ||
| "isInstallable": true, | ||
| "installUrl": download_path, | ||
| }) | ||
| .to_string(); | ||
|
|
||
| let output = tempfile::NamedTempFile::new().expect("Failed to create temp file"); | ||
| let output_path = output.path().to_str().unwrap().to_owned(); | ||
|
|
||
| manager | ||
| .mock_endpoint( | ||
| MockEndpointBuilder::new( | ||
| "GET", | ||
| "/api/0/organizations/wat-org/preprodartifacts/456/install-details/", | ||
| ) | ||
| .with_response_body(install_details_response), | ||
| ) | ||
| .mock_endpoint( | ||
| MockEndpointBuilder::new("GET", "/download/build.apk?response_format=apk") | ||
| .with_response_body("fake apk content"), | ||
| ) | ||
| .assert_cmd(vec![ | ||
| "build", | ||
| "download", | ||
| "--build-id", | ||
| "456", | ||
| "--output", | ||
| &output_path, | ||
| ]) | ||
| .with_default_token() | ||
| .run_and_assert(AssertCommand::Success); | ||
|
|
||
| let content = std::fs::read_to_string(&output_path).expect("Failed to read downloaded file"); | ||
| assert_eq!(content, "fake apk content"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn command_build_download_ipa_converts_plist_format() { | ||
| let manager = TestManager::new(); | ||
| let server_url = manager.server_url(); | ||
| // The install URL uses plist format, which should be converted to ipa | ||
| let install_url = format!("{server_url}/download/build.ipa?response_format=plist"); | ||
| let install_details_response = serde_json::json!({ | ||
| "isInstallable": true, | ||
| "installUrl": install_url, | ||
| }) | ||
| .to_string(); | ||
|
|
||
| let output = tempfile::NamedTempFile::new().expect("Failed to create temp file"); | ||
| let output_path = output.path().to_str().unwrap().to_owned(); | ||
|
|
||
| manager | ||
| .mock_endpoint( | ||
| MockEndpointBuilder::new( | ||
| "GET", | ||
| "/api/0/organizations/wat-org/preprodartifacts/789/install-details/", | ||
| ) | ||
| .with_response_body(install_details_response), | ||
| ) | ||
| // The mock should receive the converted URL with response_format=ipa | ||
| .mock_endpoint( | ||
| MockEndpointBuilder::new("GET", "/download/build.ipa?response_format=ipa") | ||
| .with_response_body("fake ipa content"), | ||
| ) | ||
| .assert_cmd(vec![ | ||
| "build", | ||
| "download", | ||
| "--build-id", | ||
| "789", | ||
| "--output", | ||
| &output_path, | ||
| ]) | ||
| .with_default_token() | ||
| .run_and_assert(AssertCommand::Success); | ||
|
|
||
| let content = std::fs::read_to_string(&output_path).expect("Failed to read downloaded file"); | ||
| assert_eq!(content, "fake ipa content"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn command_build_download_unsupported_format() { | ||
| let manager = TestManager::new(); | ||
| let server_url = manager.server_url(); | ||
| let download_path = format!("{server_url}/download/build.zip?response_format=zip"); | ||
| let install_details_response = serde_json::json!({ | ||
| "isInstallable": true, | ||
| "installUrl": download_path, | ||
| }) | ||
| .to_string(); | ||
|
|
||
| manager | ||
| .mock_endpoint( | ||
| MockEndpointBuilder::new( | ||
| "GET", | ||
| "/api/0/organizations/wat-org/preprodartifacts/999/install-details/", | ||
| ) | ||
| .with_response_body(install_details_response), | ||
| ) | ||
| .assert_cmd(vec!["build", "download", "--build-id", "999"]) | ||
| .with_default_token() | ||
| .run_and_assert(AssertCommand::Failure); | ||
| } |
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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| use crate::integration::TestManager; | ||
|
|
||
| mod download; | ||
| mod upload; | ||
|
|
||
| #[test] | ||
|
|
||
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.