omni-dev 0.26.0

A powerful Git commit message analysis and amendment toolkit
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
//! CLI command for deleting Confluence pages.

use std::io::{self, BufRead, Write};

use anyhow::Result;
use clap::Parser;

use crate::atlassian::api::AtlassianApi;
use crate::atlassian::confluence_api::ConfluenceApi;
use crate::cli::atlassian::confirm::{guard_destructive_with_io, GuardOptions, GuardOutcome};
use crate::cli::atlassian::helpers::create_client;

/// Deletes a Confluence page.
#[derive(Parser)]
pub struct DeleteCommand {
    /// Confluence page ID (e.g., 12345678).
    pub id: 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,

    /// Permanently purges the page instead of moving to trash (requires space admin).
    #[arg(long)]
    pub purge: bool,
}

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

    /// Inner form taking explicit API, instance URL, and IO handles, for unit tests.
    async fn execute_with_io(
        self,
        api: &ConfluenceApi,
        instance_url: &str,
        reader: &mut (dyn BufRead + Send),
        writer: &mut (dyn Write + Send),
    ) -> Result<()> {
        if !self.force || self.dry_run {
            let item = api.get_content(&self.id).await?;
            let suffix = if self.purge { " (purge)" } else { "" };
            let prompt = format!("Delete page {} ({}){}? [y/N] ", self.id, item.title, suffix);
            let dry_run_message =
                format!("Would delete page {} ({}){}.", self.id, item.title, suffix);

            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 => {}
            }
        }

        api.delete_page(&self.id, self.purge).await?;
        writeln!(writer, "Deleted page {} from {}.", self.id, instance_url)?;

        Ok(())
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;
    use crate::atlassian::client::AtlassianClient;
    use std::io::Cursor;
    use wiremock::matchers::{method, path, query_param};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    async fn setup_mock() -> (MockServer, ConfluenceApi) {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/wiki/api/v2/pages/12345"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "id": "12345",
                "title": "Architecture Overview",
                "status": "current",
                "spaceId": "98765",
                "version": {"number": 1},
                "body": {"atlas_doc_format": {"value": "{\"version\":1,\"type\":\"doc\",\"content\":[]}"}},
                "parentId": null
            })))
            .mount(&server)
            .await;
        Mock::given(method("GET"))
            .and(path("/wiki/api/v2/spaces/98765"))
            .respond_with(
                ResponseTemplate::new(200).set_body_json(serde_json::json!({"key": "ENG"})),
            )
            .mount(&server)
            .await;
        let client = AtlassianClient::new(&server.uri(), "u@t.com", "tok").unwrap();
        let api = ConfluenceApi::new(client);
        (server, api)
    }

    #[test]
    fn delete_command_struct_fields() {
        let cmd = DeleteCommand {
            id: "12345".to_string(),
            force: false,
            dry_run: false,
            purge: false,
        };
        assert_eq!(cmd.id, "12345");
        assert!(!cmd.force);
        assert!(!cmd.dry_run);
        assert!(!cmd.purge);
    }

    #[test]
    fn delete_command_force_mode() {
        let cmd = DeleteCommand {
            id: "12345".to_string(),
            force: true,
            dry_run: false,
            purge: false,
        };
        assert!(cmd.force);
    }

    #[test]
    fn delete_command_dry_run_mode() {
        let cmd = DeleteCommand {
            id: "12345".to_string(),
            force: false,
            dry_run: true,
            purge: false,
        };
        assert!(cmd.dry_run);
    }

    #[test]
    fn delete_command_purge_mode() {
        let cmd = DeleteCommand {
            id: "12345".to_string(),
            force: true,
            dry_run: false,
            purge: true,
        };
        assert!(cmd.purge);
    }

    #[tokio::test]
    async fn execute_with_force_calls_delete() {
        let (server, api) = setup_mock().await;
        Mock::given(method("DELETE"))
            .and(path("/wiki/api/v2/pages/12345"))
            .respond_with(ResponseTemplate::new(204))
            .expect(1)
            .mount(&server)
            .await;

        let cmd = DeleteCommand {
            id: "12345".to_string(),
            force: true,
            dry_run: false,
            purge: false,
        };
        let mut input = Cursor::new(Vec::<u8>::new());
        let mut output = Vec::<u8>::new();
        cmd.execute_with_io(
            &api,
            "https://example.atlassian.net",
            &mut input,
            &mut output,
        )
        .await
        .unwrap();
        let out = String::from_utf8(output).unwrap();
        assert!(out.contains("Deleted page 12345"));
    }

    #[tokio::test]
    async fn execute_with_dry_run_does_not_call_delete() {
        let (_server, api) = setup_mock().await;

        let cmd = DeleteCommand {
            id: "12345".to_string(),
            force: false,
            dry_run: true,
            purge: false,
        };
        let mut input = Cursor::new(Vec::<u8>::new());
        let mut output = Vec::<u8>::new();
        cmd.execute_with_io(
            &api,
            "https://example.atlassian.net",
            &mut input,
            &mut output,
        )
        .await
        .unwrap();
        let out = String::from_utf8(output).unwrap();
        assert!(out.contains("Would delete page 12345 (Architecture Overview)."));
        assert!(!out.contains("Deleted page 12345"));
    }

    #[tokio::test]
    async fn execute_with_dry_run_and_purge_includes_suffix() {
        let (_server, api) = setup_mock().await;

        let cmd = DeleteCommand {
            id: "12345".to_string(),
            force: false,
            dry_run: true,
            purge: true,
        };
        let mut input = Cursor::new(Vec::<u8>::new());
        let mut output = Vec::<u8>::new();
        cmd.execute_with_io(
            &api,
            "https://example.atlassian.net",
            &mut input,
            &mut output,
        )
        .await
        .unwrap();
        let out = String::from_utf8(output).unwrap();
        assert!(out.contains("(purge)"));
    }

    #[tokio::test]
    async fn execute_with_prompt_yes_calls_delete() {
        let (server, api) = setup_mock().await;
        Mock::given(method("DELETE"))
            .and(path("/wiki/api/v2/pages/12345"))
            .respond_with(ResponseTemplate::new(204))
            .expect(1)
            .mount(&server)
            .await;

        let cmd = DeleteCommand {
            id: "12345".to_string(),
            force: false,
            dry_run: false,
            purge: false,
        };
        let mut input = Cursor::new(b"y\n".to_vec());
        let mut output = Vec::<u8>::new();
        cmd.execute_with_io(
            &api,
            "https://example.atlassian.net",
            &mut input,
            &mut output,
        )
        .await
        .unwrap();
        let out = String::from_utf8(output).unwrap();
        assert!(out.contains("Delete page 12345 (Architecture Overview)?"));
        assert!(out.contains("Deleted page 12345"));
    }

    #[tokio::test]
    async fn execute_with_prompt_no_does_not_call_delete() {
        let (_server, api) = setup_mock().await;

        let cmd = DeleteCommand {
            id: "12345".to_string(),
            force: false,
            dry_run: false,
            purge: false,
        };
        let mut input = Cursor::new(b"n\n".to_vec());
        let mut output = Vec::<u8>::new();
        cmd.execute_with_io(
            &api,
            "https://example.atlassian.net",
            &mut input,
            &mut output,
        )
        .await
        .unwrap();
        let out = String::from_utf8(output).unwrap();
        assert!(out.contains("Cancelled."));
        assert!(!out.contains("Deleted page 12345"));
    }

    #[tokio::test]
    async fn execute_with_force_and_purge_appends_query_param() {
        let (server, api) = setup_mock().await;
        Mock::given(method("DELETE"))
            .and(path("/wiki/api/v2/pages/12345"))
            .and(query_param("purge", "true"))
            .respond_with(ResponseTemplate::new(204))
            .expect(1)
            .mount(&server)
            .await;

        let cmd = DeleteCommand {
            id: "12345".to_string(),
            force: true,
            dry_run: false,
            purge: true,
        };
        let mut input = Cursor::new(Vec::<u8>::new());
        let mut output = Vec::<u8>::new();
        cmd.execute_with_io(
            &api,
            "https://example.atlassian.net",
            &mut input,
            &mut output,
        )
        .await
        .unwrap();
    }

    #[tokio::test]
    async fn execute_with_force_propagates_delete_api_error() {
        let (server, api) = setup_mock().await;
        Mock::given(method("DELETE"))
            .and(path("/wiki/api/v2/pages/12345"))
            .respond_with(ResponseTemplate::new(403).set_body_string("Forbidden"))
            .mount(&server)
            .await;

        let cmd = DeleteCommand {
            id: "12345".to_string(),
            force: true,
            dry_run: false,
            purge: false,
        };
        let mut input = Cursor::new(Vec::<u8>::new());
        let mut output = Vec::<u8>::new();
        let err = cmd
            .execute_with_io(
                &api,
                "https://example.atlassian.net",
                &mut input,
                &mut output,
            )
            .await
            .unwrap_err();
        assert!(err.to_string().contains("403"));
    }

    #[tokio::test]
    async fn execute_lookup_error_aborts_before_prompt() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/wiki/api/v2/pages/12345"))
            .respond_with(ResponseTemplate::new(404).set_body_string("Not Found"))
            .mount(&server)
            .await;
        let client = AtlassianClient::new(&server.uri(), "u@t.com", "tok").unwrap();
        let api = ConfluenceApi::new(client);

        let cmd = DeleteCommand {
            id: "12345".to_string(),
            force: false,
            dry_run: false,
            purge: false,
        };
        let mut input = Cursor::new(Vec::<u8>::new());
        let mut output = Vec::<u8>::new();
        let err = cmd
            .execute_with_io(
                &api,
                "https://example.atlassian.net",
                &mut input,
                &mut output,
            )
            .await
            .unwrap_err();
        assert!(err.to_string().contains("404"));
    }

    /// Force-mode + failing writer covers `?` on the post-API writeln.
    #[tokio::test]
    async fn execute_with_force_propagates_writeln_error() {
        use crate::test_support::failing_io::FailingWriter;
        let (server, api) = setup_mock().await;
        Mock::given(method("DELETE"))
            .and(path("/wiki/api/v2/pages/12345"))
            .respond_with(ResponseTemplate::new(204))
            .mount(&server)
            .await;
        let cmd = DeleteCommand {
            id: "12345".to_string(),
            force: true,
            dry_run: false,
            purge: false,
        };
        let mut input = Cursor::new(Vec::<u8>::new());
        let mut writer = FailingWriter;
        let err = cmd
            .execute_with_io(
                &api,
                "https://example.atlassian.net",
                &mut input,
                &mut writer,
            )
            .await
            .unwrap_err();
        assert!(err.to_string().contains("simulated write failure"));
    }

    /// Dry-run with a failing writer covers `?` on guard_destructive_with_io.
    #[tokio::test]
    async fn execute_dry_run_propagates_guard_error() {
        use crate::test_support::failing_io::FailingWriter;
        let (_server, api) = setup_mock().await;
        let cmd = DeleteCommand {
            id: "12345".to_string(),
            force: false,
            dry_run: true,
            purge: false,
        };
        let mut input = Cursor::new(Vec::<u8>::new());
        let mut writer = FailingWriter;
        let err = cmd
            .execute_with_io(
                &api,
                "https://example.atlassian.net",
                &mut input,
                &mut writer,
            )
            .await
            .unwrap_err();
        assert!(err.to_string().contains("simulated write failure"));
    }

    /// End-to-end exercise of the public `execute()` wrapper.
    #[tokio::test]
    async fn execute_with_force_drives_create_client_and_calls_delete() {
        use crate::test_support::atlassian_env::AtlassianEnvGuard;
        let server = MockServer::start().await;
        Mock::given(method("DELETE"))
            .and(path("/wiki/api/v2/pages/12345"))
            .respond_with(ResponseTemplate::new(204))
            .expect(1)
            .mount(&server)
            .await;

        let _env = AtlassianEnvGuard::new(&server.uri(), "u@t.com", "tok");
        let cmd = DeleteCommand {
            id: "12345".to_string(),
            force: true,
            dry_run: false,
            purge: false,
        };
        cmd.execute().await.unwrap();
    }
}