From 13b2b612efcdfa05864c756cf6ad8c5e77be8284 Mon Sep 17 00:00:00 2001 From: Evan Purkhiser Date: Tue, 2 May 2023 11:51:15 -0700 Subject: [PATCH] feat(crons): Allow specifying checkin environment --- src/api.rs | 4 ++++ src/commands/monitors/run.rs | 9 +++++++++ .../_cases/monitors/monitors-run-environment.trycmd | 6 ++++++ .../_cases/monitors/monitors-run-help.trycmd | 1 + ...ors-run-env.trycmd => monitors-run-osenv.trycmd} | 0 .../monitors/post-monitors-environment.json | 6 ++++++ .../_responses/monitors/post-monitors.json | 1 + tests/integration/monitors/run.rs | 13 +++++++++++-- 8 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 tests/integration/_cases/monitors/monitors-run-environment.trycmd rename tests/integration/_cases/monitors/{monitors-run-env.trycmd => monitors-run-osenv.trycmd} (100%) create mode 100644 tests/integration/_responses/monitors/post-monitors-environment.json diff --git a/src/api.rs b/src/api.rs index e9eb95d0b4..761f4220a8 100644 --- a/src/api.rs +++ b/src/api.rs @@ -2490,11 +2490,13 @@ pub struct MonitorCheckIn { pub id: Uuid, pub status: Option, pub duration: Option, + pub environment: Option, } #[derive(Debug, Serialize)] pub struct CreateMonitorCheckIn { pub status: MonitorCheckinStatus, + pub environment: String, } #[derive(Debug, Serialize, Default)] @@ -2503,6 +2505,8 @@ pub struct UpdateMonitorCheckIn { pub status: Option, #[serde(skip_serializing_if = "Option::is_none")] pub duration: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub environment: Option, } #[derive(Deserialize, Debug)] diff --git a/src/commands/monitors/run.rs b/src/commands/monitors/run.rs index e6e1bb6aaf..303d8b6d37 100644 --- a/src/commands/monitors/run.rs +++ b/src/commands/monitors/run.rs @@ -19,6 +19,12 @@ pub fn make_command(command: Command) -> Command { .help("The monitor slug.") .required(true), ) + .arg( + Arg::new("environment") + .short('e') + .default_value("production") + .help("Specify the environment of the monitor."), + ) .arg( Arg::new("allow_failure") .short('f') @@ -47,6 +53,7 @@ pub fn execute(matches: &ArgMatches) -> Result<()> { } let monitor_slug = matches.get_one::("monitor_slug").unwrap(); + let environment = matches.get_one::("environment").unwrap(); let allow_failure = matches.get_flag("allow_failure"); let args: Vec<_> = matches.get_many::("args").unwrap().collect(); @@ -56,6 +63,7 @@ pub fn execute(matches: &ArgMatches) -> Result<()> { monitor_slug, &CreateMonitorCheckIn { status: MonitorCheckinStatus::InProgress, + environment: environment.to_string(), }, ); @@ -93,6 +101,7 @@ pub fn execute(matches: &ArgMatches) -> Result<()> { let elapsed = started.elapsed(); elapsed.as_secs() * 1000 + u64::from(elapsed.subsec_millis()) }), + environment: Some(environment.to_string()), }, ) .ok(); diff --git a/tests/integration/_cases/monitors/monitors-run-environment.trycmd b/tests/integration/_cases/monitors/monitors-run-environment.trycmd new file mode 100644 index 0000000000..0848da3d10 --- /dev/null +++ b/tests/integration/_cases/monitors/monitors-run-environment.trycmd @@ -0,0 +1,6 @@ +``` +$ sentry-cli monitors run -e dev-env foo-monitor -- echo 123 +? success +123 + +``` diff --git a/tests/integration/_cases/monitors/monitors-run-help.trycmd b/tests/integration/_cases/monitors/monitors-run-help.trycmd index 7d6e595c48..b68a5f5f61 100644 --- a/tests/integration/_cases/monitors/monitors-run-help.trycmd +++ b/tests/integration/_cases/monitors/monitors-run-help.trycmd @@ -10,6 +10,7 @@ Arguments: ... Options: + -e Specify the environment of the monitor. [default: production] -f, --allow-failure Run provided command even when Sentry reports an error. --header Custom headers that should be attached to all requests in key:value format. diff --git a/tests/integration/_cases/monitors/monitors-run-env.trycmd b/tests/integration/_cases/monitors/monitors-run-osenv.trycmd similarity index 100% rename from tests/integration/_cases/monitors/monitors-run-env.trycmd rename to tests/integration/_cases/monitors/monitors-run-osenv.trycmd diff --git a/tests/integration/_responses/monitors/post-monitors-environment.json b/tests/integration/_responses/monitors/post-monitors-environment.json new file mode 100644 index 0000000000..fd799892f5 --- /dev/null +++ b/tests/integration/_responses/monitors/post-monitors-environment.json @@ -0,0 +1,6 @@ +{ + "id": "85a34e5a-c0b6-11ec-9d64-0242ac120002", + "duration": 1337, + "environment": "dev-env", + "status": "in_progress" +} diff --git a/tests/integration/_responses/monitors/post-monitors.json b/tests/integration/_responses/monitors/post-monitors.json index 60f238e235..aa2c587279 100644 --- a/tests/integration/_responses/monitors/post-monitors.json +++ b/tests/integration/_responses/monitors/post-monitors.json @@ -1,5 +1,6 @@ { "id": "85a34e5a-c0b6-11ec-9d64-0242ac120002", "duration": 1337, + "environment": "production", "status": "in_progress" } diff --git a/tests/integration/monitors/run.rs b/tests/integration/monitors/run.rs index f334017d74..25c748e488 100644 --- a/tests/integration/monitors/run.rs +++ b/tests/integration/monitors/run.rs @@ -28,12 +28,21 @@ fn command_monitors_run_token_auth() { } #[test] -fn command_monitors_run_env() { +fn command_monitors_run_osenv() { let _server = mock_endpoint( EndpointOptions::new("POST", "/api/0/monitors/foo-monitor/checkins/", 200) .with_response_file("monitors/post-monitors.json"), ); - register_test("monitors/monitors-run-env.trycmd"); + register_test("monitors/monitors-run-osenv.trycmd"); +} + +#[test] +fn command_monitors_run_environment() { + let _server = mock_endpoint( + EndpointOptions::new("POST", "/api/0/monitors/foo-monitor/checkins/", 200) + .with_response_file("monitors/post-monitors-environment.json"), + ); + register_test("monitors/monitors-run-environment.trycmd"); } #[test]