Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/commands/code_mappings/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use anyhow::Result;
use clap::{ArgMatches, Command};

use crate::utils::args::ArgExt as _;

pub mod upload;

macro_rules! each_subcommand {
($mac:ident) => {
$mac!(upload);
};
}

pub fn make_command(mut command: Command) -> Command {
macro_rules! add_subcommand {
($name:ident) => {{
command = command.subcommand(crate::commands::code_mappings::$name::make_command(
Command::new(stringify!($name).replace('_', "-")),
));
}};
}

command = command
.about("Manage code mappings for Sentry.")
.subcommand_required(true)
.arg_required_else_help(true)
.org_arg()
.project_arg(false);
each_subcommand!(add_subcommand);
command
}

pub fn execute(matches: &ArgMatches) -> Result<()> {
macro_rules! execute_subcommand {
($name:ident) => {{
if let Some(sub_matches) =
matches.subcommand_matches(&stringify!($name).replace('_', "-"))
{
return crate::commands::code_mappings::$name::execute(&sub_matches);
}
}};
}
each_subcommand!(execute_subcommand);
unreachable!();
}
62 changes: 62 additions & 0 deletions src/commands/code_mappings/upload.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use std::fs;

use anyhow::{bail, Context as _, Result};
use clap::{Arg, ArgMatches, Command};
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
struct CodeMapping {
stack_root: String,
source_root: String,
}

pub fn make_command(command: Command) -> Command {
command
.about("Upload code mappings for a project from a JSON file.")
.arg(
Arg::new("path")
.value_name("PATH")
.required(true)
.help("Path to a JSON file containing code mappings."),
)
.arg(
Arg::new("repo")
.long("repo")
.value_name("REPO")
.help("The repository name (e.g. owner/repo). Defaults to the git remote."),
)
.arg(
Arg::new("default_branch")
.long("default-branch")
.value_name("BRANCH")
.default_value("main")
.help("The default branch name."),
)
}

pub fn execute(matches: &ArgMatches) -> Result<()> {
#[expect(clippy::unwrap_used, reason = "path is a required argument")]
let path = matches.get_one::<String>("path").unwrap();
let data = fs::read(path).with_context(|| format!("Failed to read mappings file '{path}'"))?;

let mappings: Vec<CodeMapping> =
serde_json::from_slice(&data).context("Failed to parse mappings JSON")?;

if mappings.is_empty() {
bail!("Mappings file contains an empty array. Nothing to upload.");
}

for (i, mapping) in mappings.iter().enumerate() {
if mapping.stack_root.is_empty() {
bail!("Mapping at index {i} has an empty stackRoot.");
}
if mapping.source_root.is_empty() {
bail!("Mapping at index {i} has an empty sourceRoot.");
}
}

println!("Found {} code mapping(s) in {path}", mappings.len());

Ok(())
}
2 changes: 2 additions & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::utils::value_parsers::auth_token_parser;

mod bash_hook;
mod build;
mod code_mappings;
mod dart_symbol_map;
mod debug_files;
mod deploys;
Expand Down Expand Up @@ -52,6 +53,7 @@ macro_rules! each_subcommand {
($mac:ident) => {
$mac!(bash_hook);
$mac!(build);
$mac!(code_mappings);
$mac!(debug_files);
$mac!(deploys);
$mac!(events);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
```
$ sentry-cli code-mappings --help
? success
Manage code mappings for Sentry.

Usage: sentry-cli[EXE] code-mappings [OPTIONS] <COMMAND>

Commands:
upload Upload code mappings for a project from a JSON file.
help Print this message or the help of the given subcommand(s)

Options:
-o, --org <ORG> The organization ID or slug.
--header <KEY:VALUE> Custom headers that should be attached to all requests
in key:value format.
-p, --project <PROJECT> The project ID or slug.
--auth-token <AUTH_TOKEN> Use the given Sentry auth token.
--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

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
```
$ sentry-cli code-mappings
? failed
Manage code mappings for Sentry.

Usage: sentry-cli[EXE] code-mappings [OPTIONS] <COMMAND>

Commands:
upload Upload code mappings for a project from a JSON file.
help Print this message or the help of the given subcommand(s)

Options:
-o, --org <ORG> The organization ID or slug.
--header <KEY:VALUE> Custom headers that should be attached to all requests
in key:value format.
-p, --project <PROJECT> The project ID or slug.
--auth-token <AUTH_TOKEN> Use the given Sentry auth token.
--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

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
```
$ sentry-cli code-mappings upload --help
? success
Upload code mappings for a project from a JSON file.

Usage: sentry-cli[EXE] code-mappings upload [OPTIONS] <PATH>

Arguments:
<PATH> Path to a JSON file containing code mappings.

Options:
-o, --org <ORG> The organization ID or slug.
--repo <REPO> The repository name (e.g. owner/repo). Defaults to the git remote.
--default-branch <BRANCH> The default branch name. [default: main]
--header <KEY:VALUE> Custom headers that should be attached to all requests
in key:value format.
-p, --project <PROJECT> The project ID or slug.
--auth-token <AUTH_TOKEN> Use the given Sentry auth token.
--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

```
1 change: 1 addition & 0 deletions tests/integration/_cases/help/help-windows.trycmd
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Usage: sentry-cli[EXE] [OPTIONS] <COMMAND>
Commands:
completions Generate completions for the specified shell.
build Manage builds.
code-mappings Manage code mappings for Sentry.
debug-files Locate, analyze or upload debug information files. [aliases: dif]
deploys Manage deployments for Sentry releases.
events Manage events on Sentry.
Expand Down
1 change: 1 addition & 0 deletions tests/integration/_cases/help/help.trycmd
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Usage: sentry-cli[EXE] [OPTIONS] <COMMAND>
Commands:
completions Generate completions for the specified shell.
build Manage builds.
code-mappings Manage code mappings for Sentry.
debug-files Locate, analyze or upload debug information files. [aliases: dif]
deploys Manage deployments for Sentry releases.
events Manage events on Sentry.
Expand Down
16 changes: 16 additions & 0 deletions tests/integration/code_mappings/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::integration::TestManager;

#[test]
fn command_code_mappings_help() {
TestManager::new().register_trycmd_test("code_mappings/code-mappings-help.trycmd");
}

#[test]
fn command_code_mappings_no_subcommand() {
TestManager::new().register_trycmd_test("code_mappings/code-mappings-no-subcommand.trycmd");
}

#[test]
fn command_code_mappings_upload_help() {
TestManager::new().register_trycmd_test("code_mappings/code-mappings-upload-help.trycmd");
}
1 change: 1 addition & 0 deletions tests/integration/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod bash_hook;
mod build;
mod code_mappings;
mod debug_files;
mod deploys;
mod events;
Expand Down
Loading