Skip to content
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

Add support for SENTRY_RELEASE in bash-hook #1720

Merged
merged 4 commits into from Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/api.rs
Expand Up @@ -577,7 +577,7 @@ impl Api {
}

let pagination = resp.pagination();
rv.extend(resp.convert::<Vec<Artifact>>()?.into_iter());
rv.extend(resp.convert::<Vec<Artifact>>()?);
if let Some(next) = pagination.into_next_cursor() {
cursor = next;
} else {
Expand Down Expand Up @@ -1450,7 +1450,7 @@ impl Api {
}
}
let pagination = resp.pagination();
rv.extend(resp.convert::<Vec<Organization>>()?.into_iter());
rv.extend(resp.convert::<Vec<Organization>>()?);
if let Some(next) = pagination.into_next_cursor() {
cursor = next;
} else {
Expand Down Expand Up @@ -1478,7 +1478,7 @@ impl Api {
}
}
let pagination = resp.pagination();
rv.extend(resp.convert::<Vec<Monitor>>()?.into_iter());
rv.extend(resp.convert::<Vec<Monitor>>()?);
if let Some(next) = pagination.into_next_cursor() {
cursor = next;
} else {
Expand Down Expand Up @@ -1540,7 +1540,7 @@ impl Api {
}
}
let pagination = resp.pagination();
rv.extend(resp.convert::<Vec<Project>>()?.into_iter());
rv.extend(resp.convert::<Vec<Project>>()?);
if let Some(next) = pagination.into_next_cursor() {
cursor = next;
} else {
Expand Down Expand Up @@ -1580,7 +1580,7 @@ impl Api {
}

let pagination = resp.pagination();
rv.extend(resp.convert::<Vec<ProcessedEvent>>()?.into_iter());
rv.extend(resp.convert::<Vec<ProcessedEvent>>()?);

if requests_no == max_pages {
break;
Expand Down Expand Up @@ -1633,7 +1633,7 @@ impl Api {
}

let pagination = resp.pagination();
rv.extend(resp.convert::<Vec<Issue>>()?.into_iter());
rv.extend(resp.convert::<Vec<Issue>>()?);

if requests_no == max_pages {
break;
Expand Down Expand Up @@ -1664,7 +1664,7 @@ impl Api {
break;
} else {
let pagination = resp.pagination();
rv.extend(resp.convert::<Vec<Repo>>()?.into_iter());
rv.extend(resp.convert::<Vec<Repo>>()?);
if let Some(next) = pagination.into_next_cursor() {
cursor = next;
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/bashsupport.sh
Expand Up @@ -32,7 +32,7 @@ _sentry_err_trap() {
echo "@exit_code:${_exit_code}" >> "$_SENTRY_TRACEBACK_FILE"

: >> "$_SENTRY_LOG_FILE"
export SENTRY_LAST_EVENT=$(___SENTRY_CLI___ bash-hook --send-event --traceback "$_SENTRY_TRACEBACK_FILE" ___SENTRY_TAGS___ --log "$_SENTRY_LOG_FILE" ___SENTRY_NO_ENVIRON___)
export SENTRY_LAST_EVENT=$(___SENTRY_CLI___ bash-hook --send-event --traceback "$_SENTRY_TRACEBACK_FILE" ___SENTRY_TAGS___ ___SENTRY_RELEASE___ --log "$_SENTRY_LOG_FILE" ___SENTRY_NO_ENVIRON___)
rm -f "$_SENTRY_TRACEBACK_FILE" "$_SENTRY_LOG_FILE"
}

Expand Down
30 changes: 27 additions & 3 deletions src/commands/bash_hook.rs
Expand Up @@ -19,7 +19,7 @@ use crate::utils::releases::detect_release_name;

const BASH_SCRIPT: &str = include_str!("../bashsupport.sh");
lazy_static! {
static ref FRAME_RE: Regex = Regex::new(r#"^(.*?):(.*):(\d+)$"#).unwrap();
static ref FRAME_RE: Regex = Regex::new(r"^(.*?):(.*):(\d+)$").unwrap();
}

pub fn make_command(command: Command) -> Command {
Expand Down Expand Up @@ -68,15 +68,28 @@ pub fn make_command(command: Command) -> Command {
.action(ArgAction::Append)
.help("Add tags (key:value) to the event."),
)
.arg(
Arg::new("release")
.value_name("RELEASE")
.long("release")
.action(ArgAction::Set)
.help("Define release version for the event."),
)
.arg(Arg::new("log").long("log").value_name("PATH").hide(true))
}

fn send_event(traceback: &str, logfile: &str, tags: &[&String], environ: bool) -> Result<()> {
fn send_event(
traceback: &str,
logfile: &str,
tags: &[&String],
release: Option<String>,
environ: bool,
) -> Result<()> {
let config = Config::current();

let mut event = Event {
environment: config.get_environment().map(Into::into),
release: detect_release_name().ok().map(Into::into),
release: release.or(detect_release_name().ok()).map(Into::into),
sdk: Some(get_sdk_info()),
user: get_user_name().ok().map(|n| User {
username: Some(n),
Expand Down Expand Up @@ -192,6 +205,8 @@ fn send_event(traceback: &str, logfile: &str, tags: &[&String], environ: bool) -
}

pub fn execute(matches: &ArgMatches) -> Result<()> {
let release = Config::current().get_release(matches).ok();

let tags = matches
.get_many::<String>("tags")
.map(|v| v.collect())
Expand All @@ -202,6 +217,7 @@ pub fn execute(matches: &ArgMatches) -> Result<()> {
matches.get_one::<String>("traceback").unwrap(),
matches.get_one::<String>("log").unwrap(),
&tags,
release,
!matches.get_flag("no_environ"),
);
}
Expand All @@ -228,6 +244,14 @@ pub fn execute(matches: &ArgMatches) -> Result<()> {
.join(""),
);

script = match release {
Some(release) => script.replace(
" ___SENTRY_RELEASE___",
format!(" --release \"{}\"", release).as_str(),
),
None => script.replace(" ___SENTRY_RELEASE___", ""),
};

script = script.replace(
"___SENTRY_CLI___",
matches
Expand Down
1 change: 1 addition & 0 deletions tests/integration/_cases/bash_hook/bash_hook-help.trycmd
Expand Up @@ -18,6 +18,7 @@ Options:
flag is currently implemented only for selected subcommands.
[aliases: silent]
--tag <KEY:VALUE> Add tags (key:value) to the event.
--release <RELEASE> Define release version for the event.
-h, --help Print help

```
75 changes: 75 additions & 0 deletions tests/integration/_cases/bash_hook/bash_hook-release-env.trycmd
@@ -0,0 +1,75 @@
```
$ SENTRY_RELEASE=0.2.0 sentry-cli bash-hook
? success
set -e

_SENTRY_TRACEBACK_FILE="[..].traceback"
_SENTRY_LOG_FILE="[..].out"

if [ "${SENTRY_CLI_NO_EXIT_TRAP-0}" != 1 ]; then
trap _sentry_exit_trap EXIT
fi
trap _sentry_err_trap ERR

_sentry_shown_traceback=0

_sentry_exit_trap() {
local _exit_code="$?"
local _command="${BASH_COMMAND:-unknown}"
if [[ $_exit_code != 0 && "${_sentry_shown_traceback}" != 1 ]]; then
_sentry_err_trap "$_command" "$_exit_code"
fi
rm -f "$_SENTRY_TRACEBACK_FILE" "$_SENTRY_LOG_FILE"
exit $_exit_code
}

_sentry_err_trap() {
local _exit_code="$?"
local _command="${BASH_COMMAND:-unknown}"
if [ $# -ge 1 ] && [ "x$1" != x ]; then
_command="$1"
fi
if [ $# -ge 2 ] && [ "x$2" != x ]; then
_exit_code="$2"
fi
_sentry_traceback 1
echo "@command:${_command}" >> "$_SENTRY_TRACEBACK_FILE"
echo "@exit_code:${_exit_code}" >> "$_SENTRY_TRACEBACK_FILE"

: >> "$_SENTRY_LOG_FILE"
export SENTRY_LAST_EVENT=$([CWD]/target/debug/sentry-cli[EXE] bash-hook --send-event --traceback "$_SENTRY_TRACEBACK_FILE" --release "0.2.0" --log "$_SENTRY_LOG_FILE" )
rm -f "$_SENTRY_TRACEBACK_FILE" "$_SENTRY_LOG_FILE"
}

_sentry_traceback() {
_sentry_shown_traceback=1
local -i start=$(( ${1:-0} + 1 ))
local -i end=${#BASH_SOURCE[@]}
local -i i=0
local -i j=0

: > "$_SENTRY_TRACEBACK_FILE"
for ((i=${start}; i < ${end}; i++)); do
j=$(( $i - 1 ))
local function="${FUNCNAME[$i]}"
local file="${BASH_SOURCE[$i]}"
local line="${BASH_LINENO[$j]}"
echo "${function}:${file}:${line}" >> "$_SENTRY_TRACEBACK_FILE"
done
}

: > "$_SENTRY_LOG_FILE"

if command -v perl >/dev/null; then
exec /
1> >(tee >(perl '-MPOSIX' -ne '$|++; print strftime("%Y-%m-%d %H:%M:%S %z: ", localtime()), "stdout: ", $_;' >> "$_SENTRY_LOG_FILE")) /
2> >(tee >(perl '-MPOSIX' -ne '$|++; print strftime("%Y-%m-%d %H:%M:%S %z: ", localtime()), "stderr: ", $_;' >> "$_SENTRY_LOG_FILE") >&2)
else
exec /
1> >(tee >(awk '{ system(""); print strftime("%Y-%m-%d %H:%M:%S %z:"), "stdout:", $0; system(""); }' >> "$_SENTRY_LOG_FILE")) /
2> >(tee >(awk '{ system(""); print strftime("%Y-%m-%d %H:%M:%S %z:"), "stderr:", $0; system(""); }' >> "$_SENTRY_LOG_FILE") >&2)
fi


```

75 changes: 75 additions & 0 deletions tests/integration/_cases/bash_hook/bash_hook-release.trycmd
@@ -0,0 +1,75 @@
```
$ sentry-cli bash-hook --release "0.1.0"
? success
set -e

_SENTRY_TRACEBACK_FILE="[..].traceback"
_SENTRY_LOG_FILE="[..].out"

if [ "${SENTRY_CLI_NO_EXIT_TRAP-0}" != 1 ]; then
trap _sentry_exit_trap EXIT
fi
trap _sentry_err_trap ERR

_sentry_shown_traceback=0

_sentry_exit_trap() {
local _exit_code="$?"
local _command="${BASH_COMMAND:-unknown}"
if [[ $_exit_code != 0 && "${_sentry_shown_traceback}" != 1 ]]; then
_sentry_err_trap "$_command" "$_exit_code"
fi
rm -f "$_SENTRY_TRACEBACK_FILE" "$_SENTRY_LOG_FILE"
exit $_exit_code
}

_sentry_err_trap() {
local _exit_code="$?"
local _command="${BASH_COMMAND:-unknown}"
if [ $# -ge 1 ] && [ "x$1" != x ]; then
_command="$1"
fi
if [ $# -ge 2 ] && [ "x$2" != x ]; then
_exit_code="$2"
fi
_sentry_traceback 1
echo "@command:${_command}" >> "$_SENTRY_TRACEBACK_FILE"
echo "@exit_code:${_exit_code}" >> "$_SENTRY_TRACEBACK_FILE"

: >> "$_SENTRY_LOG_FILE"
export SENTRY_LAST_EVENT=$([CWD]/target/debug/sentry-cli[EXE] bash-hook --send-event --traceback "$_SENTRY_TRACEBACK_FILE" --release "0.1.0" --log "$_SENTRY_LOG_FILE" )
rm -f "$_SENTRY_TRACEBACK_FILE" "$_SENTRY_LOG_FILE"
}

_sentry_traceback() {
_sentry_shown_traceback=1
local -i start=$(( ${1:-0} + 1 ))
local -i end=${#BASH_SOURCE[@]}
local -i i=0
local -i j=0

: > "$_SENTRY_TRACEBACK_FILE"
for ((i=${start}; i < ${end}; i++)); do
j=$(( $i - 1 ))
local function="${FUNCNAME[$i]}"
local file="${BASH_SOURCE[$i]}"
local line="${BASH_LINENO[$j]}"
echo "${function}:${file}:${line}" >> "$_SENTRY_TRACEBACK_FILE"
done
}

: > "$_SENTRY_LOG_FILE"

if command -v perl >/dev/null; then
exec /
1> >(tee >(perl '-MPOSIX' -ne '$|++; print strftime("%Y-%m-%d %H:%M:%S %z: ", localtime()), "stdout: ", $_;' >> "$_SENTRY_LOG_FILE")) /
2> >(tee >(perl '-MPOSIX' -ne '$|++; print strftime("%Y-%m-%d %H:%M:%S %z: ", localtime()), "stderr: ", $_;' >> "$_SENTRY_LOG_FILE") >&2)
else
exec /
1> >(tee >(awk '{ system(""); print strftime("%Y-%m-%d %H:%M:%S %z:"), "stdout:", $0; system(""); }' >> "$_SENTRY_LOG_FILE")) /
2> >(tee >(awk '{ system(""); print strftime("%Y-%m-%d %H:%M:%S %z:"), "stderr:", $0; system(""); }' >> "$_SENTRY_LOG_FILE") >&2)
fi


```

10 changes: 10 additions & 0 deletions tests/integration/bash_hook.rs
Expand Up @@ -14,3 +14,13 @@ fn command_bash_hook() {
fn command_bash_hook_tags() {
register_test("bash_hook/bash_hook-tags.trycmd");
}

#[test]
fn command_bash_hook_release() {
register_test("bash_hook/bash_hook-release.trycmd");
}

#[test]
fn command_bash_hook_release_using_environment() {
register_test("bash_hook/bash_hook-release-env.trycmd");
}
2 changes: 1 addition & 1 deletion tests/integration/mod.rs
Expand Up @@ -26,7 +26,7 @@ use std::path::Path;
use mockito::{mock, server_url, Matcher, Mock};
use trycmd::TestCases;

pub const UTC_DATE_FORMAT: &str = r#"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{6,9}Z"#;
pub const UTC_DATE_FORMAT: &str = r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{6,9}Z";
const VERSION: &str = env!("CARGO_PKG_VERSION");

pub fn register_test(path: &str) -> TestCases {
Expand Down