omni-dev 0.37.0

AI-powered git commit rewriter, PR generator, and MCP server for Jira, Confluence, and Datadog.
Documentation
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//! CLI commands for JIRA project components.

use anyhow::Result;
use clap::{Parser, Subcommand};

use crate::atlassian::client::AtlassianClient;
use crate::atlassian::jira_types::JiraComponent;
use crate::cli::atlassian::confirm::{guard_destructive_with_io, GuardOptions, GuardOutcome};
use crate::cli::atlassian::format::{output_as, OutputFormat};
use crate::cli::atlassian::helpers::create_client;

/// Manages JIRA project components.
#[derive(Parser)]
pub struct ComponentCommand {
    /// The component subcommand to execute.
    #[command(subcommand)]
    pub command: ComponentSubcommands,
}

/// Component subcommands.
#[derive(Subcommand)]
pub enum ComponentSubcommands {
    /// Lists components for a project (mirrors the `jira_component_list` MCP tool).
    List(ListCommand),
    /// Creates a new project component (mirrors the `jira_component_create` MCP tool).
    Create(CreateCommand),
    /// Updates a component's name/description (mirrors the `jira_component_update` MCP tool).
    Update(UpdateCommand),
    /// Deletes a component (mirrors the `jira_component_delete` MCP tool).
    Delete(DeleteCommand),
}

impl ComponentCommand {
    /// Executes the component command.
    pub async fn execute(self) -> Result<()> {
        match self.command {
            ComponentSubcommands::List(cmd) => cmd.execute().await,
            ComponentSubcommands::Create(cmd) => cmd.execute().await,
            ComponentSubcommands::Update(cmd) => cmd.execute().await,
            ComponentSubcommands::Delete(cmd) => cmd.execute().await,
        }
    }
}

/// Lists components for a JIRA project.
#[derive(Parser)]
pub struct ListCommand {
    /// Project key (e.g., "PROJ").
    #[arg(long)]
    pub project: String,

    /// Output format.
    #[arg(short = 'o', long, value_enum, default_value_t = OutputFormat::Table)]
    pub output: OutputFormat,
}

impl ListCommand {
    /// Fetches and displays components.
    pub async fn execute(self) -> Result<()> {
        let (client, _instance_url) = create_client()?;
        let components = client.get_project_components(&self.project).await?;
        if output_as(&components, &self.output)? {
            return Ok(());
        }
        print_components(&components);
        Ok(())
    }
}

/// Creates a component on a JIRA project.
#[derive(Parser)]
pub struct CreateCommand {
    /// Project key (e.g., "PROJ").
    #[arg(long)]
    pub project: String,

    /// Component name.
    #[arg(long)]
    pub name: String,

    /// Component description.
    #[arg(long)]
    pub description: Option<String>,
}

impl CreateCommand {
    /// Creates the component.
    pub async fn execute(self) -> Result<()> {
        let (client, _instance_url) = create_client()?;
        let component = client
            .create_component(&self.project, &self.name, self.description.as_deref())
            .await?;
        println!(
            "Created component {} (id: {}).",
            component.name, component.id
        );
        Ok(())
    }
}

/// Updates a JIRA component.
#[derive(Parser)]
pub struct UpdateCommand {
    /// Component ID (from `component list`).
    pub component_id: String,

    /// New component name.
    #[arg(long)]
    pub name: Option<String>,

    /// New component description.
    #[arg(long)]
    pub description: Option<String>,
}

impl UpdateCommand {
    /// Updates the component.
    pub async fn execute(self) -> Result<()> {
        if self.name.is_none() && self.description.is_none() {
            anyhow::bail!("Nothing to update: pass --name and/or --description.");
        }
        let (client, _instance_url) = create_client()?;
        client
            .update_component(
                &self.component_id,
                self.name.as_deref(),
                self.description.as_deref(),
            )
            .await?;
        println!("Updated component {}.", self.component_id);
        Ok(())
    }
}

/// Deletes a JIRA component.
#[derive(Parser)]
pub struct DeleteCommand {
    /// Component ID (from `component list`).
    pub component_id: String,

    /// Reassign issues referencing this component to this component id before
    /// deleting (otherwise the references are dropped).
    #[arg(long)]
    pub move_issues_to: Option<String>,

    /// Skips the confirmation prompt.
    #[arg(long)]
    pub force: bool,

    /// Prints what would be deleted without making any API calls.
    #[arg(long)]
    pub dry_run: bool,
}

impl DeleteCommand {
    /// Executes the delete command.
    pub async fn execute(self) -> Result<()> {
        let (client, _instance_url) = create_client()?;
        let mut reader = std::io::BufReader::new(std::io::stdin());
        let mut writer = std::io::stdout();
        self.execute_with_io(&client, &mut reader, &mut writer)
            .await
    }

    /// Inner form taking explicit client and IO handles, for unit tests.
    async fn execute_with_io(
        self,
        client: &AtlassianClient,
        reader: &mut (dyn std::io::BufRead + Send),
        writer: &mut (dyn std::io::Write + Send),
    ) -> Result<()> {
        if !self.force || self.dry_run {
            let prompt = format!("Delete component {}? [y/N] ", self.component_id);
            let dry_run_message = format!("Would delete component {}.", self.component_id);
            let outcome = guard_destructive_with_io(
                &GuardOptions {
                    prompt: &prompt,
                    dry_run_message: &dry_run_message,
                    force: self.force,
                    dry_run: self.dry_run,
                },
                reader,
                writer,
            )?;
            match outcome {
                GuardOutcome::Cancelled | GuardOutcome::DryRun => return Ok(()),
                GuardOutcome::Proceed => {}
            }
        }

        client
            .delete_component(&self.component_id, self.move_issues_to.as_deref())
            .await?;
        writeln!(writer, "Deleted component {}.", self.component_id)?;
        Ok(())
    }
}

/// Prints components as a simple table.
fn print_components(components: &[JiraComponent]) {
    if components.is_empty() {
        println!("No components.");
        return;
    }
    println!("{:<12} {:<30} DESCRIPTION", "ID", "NAME");
    for c in components {
        println!(
            "{:<12} {:<30} {}",
            c.id,
            c.name,
            c.description.as_deref().unwrap_or("")
        );
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;

    fn mock_client(base_url: &str) -> AtlassianClient {
        AtlassianClient::new(base_url, "user@test.com", "token").unwrap()
    }

    #[test]
    fn component_command_list_variant() {
        let cmd = ComponentCommand {
            command: ComponentSubcommands::List(ListCommand {
                project: "PROJ".to_string(),
                output: OutputFormat::Table,
            }),
        };
        assert!(matches!(cmd.command, ComponentSubcommands::List(_)));
    }

    #[test]
    fn component_command_delete_variant() {
        let cmd = ComponentCommand {
            command: ComponentSubcommands::Delete(DeleteCommand {
                component_id: "10000".to_string(),
                move_issues_to: None,
                force: false,
                dry_run: false,
            }),
        };
        assert!(matches!(cmd.command, ComponentSubcommands::Delete(_)));
    }

    #[test]
    fn print_components_empty_and_populated() {
        print_components(&[]);
        print_components(&[JiraComponent {
            id: "1".to_string(),
            name: "Backend".to_string(),
            description: Some("Server".to_string()),
        }]);
    }

    #[tokio::test]
    async fn update_with_no_fields_errors_before_client() {
        let cmd = UpdateCommand {
            component_id: "10000".to_string(),
            name: None,
            description: None,
        };
        let err = cmd.execute().await.unwrap_err();
        assert!(err.to_string().contains("Nothing to update"));
    }

    #[tokio::test]
    async fn delete_component_force_calls_delete() {
        let server = wiremock::MockServer::start().await;
        wiremock::Mock::given(wiremock::matchers::method("DELETE"))
            .and(wiremock::matchers::path("/rest/api/3/component/10000"))
            .respond_with(wiremock::ResponseTemplate::new(204))
            .expect(1)
            .mount(&server)
            .await;

        let client = mock_client(&server.uri());
        let cmd = DeleteCommand {
            component_id: "10000".to_string(),
            move_issues_to: None,
            force: true,
            dry_run: false,
        };
        let mut input = std::io::Cursor::new(Vec::<u8>::new());
        let mut output = Vec::<u8>::new();
        cmd.execute_with_io(&client, &mut input, &mut output)
            .await
            .unwrap();
        assert!(String::from_utf8(output)
            .unwrap()
            .contains("Deleted component 10000."));
    }

    #[tokio::test]
    async fn delete_component_dry_run_makes_no_api_call() {
        let client = mock_client("http://127.0.0.1:1");
        let cmd = DeleteCommand {
            component_id: "10000".to_string(),
            move_issues_to: None,
            force: false,
            dry_run: true,
        };
        let mut input = std::io::Cursor::new(Vec::<u8>::new());
        let mut output = Vec::<u8>::new();
        cmd.execute_with_io(&client, &mut input, &mut output)
            .await
            .unwrap();
        let out = String::from_utf8(output).unwrap();
        assert!(out.contains("Would delete component 10000."));
        assert!(!out.contains("Deleted component"));
    }

    // ── execute() end-to-end (drives create_client + the API call) ──

    #[tokio::test]
    async fn list_execute_drives_create_client_and_gets() {
        use crate::test_support::atlassian_env::AtlassianEnvGuard;
        let server = wiremock::MockServer::start().await;
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path(
                "/rest/api/3/project/PROJ/components",
            ))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!([
                    {"id": "10000", "name": "Backend", "description": "Server"}
                ])),
            )
            .mount(&server)
            .await;
        let _env = AtlassianEnvGuard::new(&server.uri(), "u@t.com", "tok");
        ListCommand {
            project: "PROJ".to_string(),
            output: OutputFormat::Table,
        }
        .execute()
        .await
        .unwrap();
    }

    #[tokio::test]
    async fn create_execute_drives_create_client_and_posts() {
        use crate::test_support::atlassian_env::AtlassianEnvGuard;
        let server = wiremock::MockServer::start().await;
        wiremock::Mock::given(wiremock::matchers::method("POST"))
            .and(wiremock::matchers::path("/rest/api/3/component"))
            .respond_with(
                wiremock::ResponseTemplate::new(201).set_body_json(serde_json::json!({
                    "id": "10000", "name": "Backend"
                })),
            )
            .mount(&server)
            .await;
        let _env = AtlassianEnvGuard::new(&server.uri(), "u@t.com", "tok");
        // Routed through the parent so the `Create` dispatch arm is covered too.
        ComponentCommand {
            command: ComponentSubcommands::Create(CreateCommand {
                project: "PROJ".to_string(),
                name: "Backend".to_string(),
                description: Some("Server side".to_string()),
            }),
        }
        .execute()
        .await
        .unwrap();
    }

    #[tokio::test]
    async fn update_execute_drives_create_client_and_puts() {
        use crate::test_support::atlassian_env::AtlassianEnvGuard;
        let server = wiremock::MockServer::start().await;
        wiremock::Mock::given(wiremock::matchers::method("PUT"))
            .and(wiremock::matchers::path("/rest/api/3/component/10000"))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "id": "10000", "name": "Renamed"
                })),
            )
            .mount(&server)
            .await;
        let _env = AtlassianEnvGuard::new(&server.uri(), "u@t.com", "tok");
        // Routed through the parent so the `Update` dispatch arm is covered too.
        ComponentCommand {
            command: ComponentSubcommands::Update(UpdateCommand {
                component_id: "10000".to_string(),
                name: Some("Renamed".to_string()),
                description: Some("New desc".to_string()),
            }),
        }
        .execute()
        .await
        .unwrap();
    }

    #[tokio::test]
    async fn delete_execute_wrapper_drives_create_client() {
        use crate::test_support::atlassian_env::AtlassianEnvGuard;
        let server = wiremock::MockServer::start().await;
        wiremock::Mock::given(wiremock::matchers::method("DELETE"))
            .and(wiremock::matchers::path("/rest/api/3/component/10000"))
            .respond_with(wiremock::ResponseTemplate::new(204))
            .mount(&server)
            .await;
        let _env = AtlassianEnvGuard::new(&server.uri(), "u@t.com", "tok");
        // `--force` skips the prompt, so the public execute() wrapper (which
        // wires up real stdin/stdout) does not read stdin. Routed through the
        // parent so the `Delete` dispatch arm is covered too.
        ComponentCommand {
            command: ComponentSubcommands::Delete(DeleteCommand {
                component_id: "10000".to_string(),
                move_issues_to: None,
                force: true,
                dry_run: false,
            }),
        }
        .execute()
        .await
        .unwrap();
    }

    /// Answering the prompt "y" takes the `GuardOutcome::Proceed` arm — the
    /// `--force` tests skip the guard block entirely.
    #[tokio::test]
    async fn delete_component_prompt_yes_calls_delete() {
        let server = wiremock::MockServer::start().await;
        wiremock::Mock::given(wiremock::matchers::method("DELETE"))
            .and(wiremock::matchers::path("/rest/api/3/component/10000"))
            .respond_with(wiremock::ResponseTemplate::new(204))
            .expect(1)
            .mount(&server)
            .await;

        let client = mock_client(&server.uri());
        let cmd = DeleteCommand {
            component_id: "10000".to_string(),
            move_issues_to: None,
            force: false,
            dry_run: false,
        };
        let mut input = std::io::Cursor::new(b"y\n".to_vec());
        let mut output = Vec::<u8>::new();
        cmd.execute_with_io(&client, &mut input, &mut output)
            .await
            .unwrap();
        let out = String::from_utf8(output).unwrap();
        assert!(out.contains("Delete component 10000?"));
        assert!(out.contains("Deleted component 10000."));
    }

    /// A failing writer makes the guard's own `writeln` fail, covering the `?`
    /// propagation on `guard_destructive_with_io`.
    #[tokio::test]
    async fn delete_component_dry_run_propagates_guard_error() {
        use crate::test_support::failing_io::FailingWriter;
        let client = mock_client("http://127.0.0.1:1");
        let cmd = DeleteCommand {
            component_id: "10000".to_string(),
            move_issues_to: None,
            force: false,
            dry_run: true,
        };
        let mut input = std::io::Cursor::new(Vec::<u8>::new());
        let mut writer = FailingWriter;
        let err = cmd
            .execute_with_io(&client, &mut input, &mut writer)
            .await
            .unwrap_err();
        assert!(err.to_string().contains("simulated write failure"));
    }
}