-
-
Notifications
You must be signed in to change notification settings - Fork 240
Expand file tree
/
Copy pathupload.rs
More file actions
168 lines (143 loc) · 5.95 KB
/
upload.rs
File metadata and controls
168 lines (143 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use std::borrow::Cow;
use std::ffi::OsStr;
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::path::Path;
use anyhow::{bail, Context as _, Result};
use clap::Args;
use crate::api::{Api, ChunkUploadCapability};
use crate::config::Config;
use crate::constants::{DEFAULT_MAX_DIF_SIZE, DEFAULT_MAX_WAIT};
use crate::utils::chunks::{upload_chunked_objects, Assemblable, ChunkOptions, Chunked};
use crate::utils::dif::DifFile;
use symbolic::common::ByteView;
use symbolic::common::DebugId;
struct DartSymbolMapObject<'a> {
bytes: &'a [u8],
name: &'a str,
debug_id: DebugId,
}
impl AsRef<[u8]> for DartSymbolMapObject<'_> {
fn as_ref(&self) -> &[u8] {
self.bytes
}
}
impl Display for DartSymbolMapObject<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
write!(f, "dartsymbolmap {}", self.name)
}
}
impl Assemblable for DartSymbolMapObject<'_> {
fn name(&self) -> Cow<'_, str> {
Cow::Borrowed(self.name)
}
fn debug_id(&self) -> Option<DebugId> {
Some(self.debug_id)
}
}
#[derive(Args, Clone)]
pub(crate) struct DartSymbolMapUploadArgs {
#[arg(short = 'o', long = "org")]
#[arg(help = "The organization ID or slug.")]
pub(super) org: Option<String>,
#[arg(short = 'p', long = "project")]
#[arg(help = "The project ID or slug.")]
pub(super) project: Option<String>,
#[arg(value_name = "MAPPING")]
#[arg(
help = "Path to the dartsymbolmap JSON file (e.g. dartsymbolmap.json). Must be a JSON array of strings with an even number of entries (pairs)."
)]
pub(super) mapping: String,
#[arg(value_name = "DEBUG_FILE")]
#[arg(
help = "Path to the corresponding debug file to extract the Debug ID from. The file must contain exactly one Debug ID."
)]
pub(super) debug_file: String,
}
pub(super) fn execute(args: DartSymbolMapUploadArgs) -> Result<()> {
let mapping_path = &args.mapping;
let debug_file_path = &args.debug_file;
// Extract Debug ID(s) from the provided debug file
let dif = DifFile::open_path(debug_file_path, None)?;
let mut ids: Vec<DebugId> = dif.ids().filter(|id| !id.is_nil()).collect();
// Ensure a single, unambiguous Debug ID
ids.sort();
ids.dedup();
match ids.len() {
0 => bail!(
"No debug identifier found in the provided debug file ({debug_file_path}). Ensure the file contains an embedded Debug ID."
),
1 => {
let debug_id = ids.remove(0);
// Validate the dartsymbolmap JSON: must be a JSON array of strings with even length
let mapping_file_bytes = ByteView::open(mapping_path)
.with_context(|| format!("Failed to read mapping file at {mapping_path}"))?;
let mapping_entries: Vec<Cow<'_, str>> =
serde_json::from_slice(mapping_file_bytes.as_ref())
.context("Invalid dartsymbolmap: expected a JSON array of strings")?;
if !mapping_entries.len().is_multiple_of(2) {
bail!(
"Invalid dartsymbolmap: expected an even number of entries, got {}",
mapping_entries.len()
);
}
// Prepare upload object
let file_name = Path::new(mapping_path)
.file_name()
.and_then(OsStr::to_str)
.unwrap_or(mapping_path)
;
let mapping_len = mapping_file_bytes.len();
let object = DartSymbolMapObject {
bytes: mapping_file_bytes.as_ref(),
name: file_name,
debug_id,
};
// Prepare chunked upload
let api = Api::current();
// Resolve org and project, with org also checking auth token payload
let config = Config::current();
let org = config.get_org_with_cli_input(args.org.as_deref())?;
let project = args
.project
.clone()
.or_else(|| std::env::var("SENTRY_PROJECT").ok())
.or_else(|| config.get_project_default().ok())
.ok_or_else(|| {
anyhow::anyhow!(
"No project specified. Use --project or set a default in config."
)
})?;
let chunk_upload_options = api
.authenticated()?
.get_chunk_upload_options(&org)?;
if !chunk_upload_options.supports(ChunkUploadCapability::DartSymbolMap) {
bail!(
"Server does not support uploading Dart symbol maps via chunked upload. Please update your Sentry server."
);
}
// Early file size check against server or default limits (same as debug files)
let effective_max_file_size = if chunk_upload_options.max_file_size > 0 {
chunk_upload_options.max_file_size
} else {
DEFAULT_MAX_DIF_SIZE
};
if (mapping_len as u64) > effective_max_file_size {
bail!(
"The dartsymbolmap '{mapping_path}' exceeds the maximum allowed size ({mapping_len} bytes > {effective_max_file_size} bytes)."
);
}
let options = ChunkOptions::new(chunk_upload_options, &org, &project)
.with_max_wait(DEFAULT_MAX_WAIT);
let chunked = Chunked::from(object, options.server_options().chunk_size);
let (_uploaded, has_processing_errors) = upload_chunked_objects(&[chunked], options)?;
if has_processing_errors {
bail!("Some symbol maps did not process correctly");
}
Ok(())
}
_ => bail!(
"Multiple debug identifiers found in the provided debug file ({debug_file_path}): {}. Please provide a file that contains a single Debug ID.",
ids.into_iter().map(|id| id.to_string()).collect::<Vec<_>>().join(", ")
),
}
}