atlassian-cli 0.4.2

Unified CLI for Atlassian Cloud products
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use url::form_urlencoded;

use super::utils::BitbucketContext;
use crate::commands::common::{render_success, MutationResult};

/// Information about a pull request for pipeline operations
#[derive(Debug, Clone)]
pub struct PullRequestInfo {
    pub source_branch: String,
    pub source_workspace: String,
    pub source_repo: String,
    pub state: String,
}

#[derive(Deserialize)]
struct PullRequestList {
    values: Vec<PullRequest>,
}

#[derive(Deserialize)]
struct PullRequest {
    id: i64,
    title: String,
    state: String,
    author: User,
    source: PullRequestBranch,
    destination: PullRequestBranch,
    #[serde(default)]
    description: Option<String>,
    #[serde(default)]
    created_on: Option<String>,
    #[serde(default)]
    updated_on: Option<String>,
    #[serde(default)]
    comment_count: Option<i32>,
    #[serde(default)]
    task_count: Option<i32>,
    #[serde(default)]
    participants: Option<Vec<Participant>>,
}

#[derive(Deserialize)]
struct User {
    display_name: String,
    #[allow(dead_code)]
    #[serde(default)]
    uuid: Option<String>,
}

#[derive(Deserialize)]
struct PullRequestBranch {
    branch: BranchRef,
    #[allow(dead_code)]
    #[serde(default)]
    repository: Option<Repository>,
}

#[derive(Deserialize)]
struct BranchRef {
    name: String,
}

#[derive(Deserialize, Clone)]
struct Repository {
    #[allow(dead_code)]
    #[serde(default)]
    full_name: Option<String>,
    #[serde(default)]
    workspace: Option<RepositoryWorkspace>,
    name: String,
}

#[derive(Deserialize, Clone)]
struct RepositoryWorkspace {
    slug: String,
}

#[derive(Deserialize)]
struct Participant {
    #[allow(dead_code)]
    #[serde(default)]
    approved: bool,
    #[allow(dead_code)]
    user: User,
    #[allow(dead_code)]
    role: String,
}

#[derive(Deserialize)]
struct Comment {
    id: i64,
    content: CommentContent,
    user: User,
    #[serde(default)]
    created_on: Option<String>,
}

#[derive(Deserialize)]
struct CommentContent {
    raw: String,
}

pub async fn list_pull_requests(
    ctx: &BitbucketContext<'_>,
    workspace: &str,
    slug: &str,
    state: &str,
    limit: usize,
) -> Result<()> {
    let query = form_urlencoded::Serializer::new(String::new())
        .append_pair("state", state)
        .append_pair("pagelen", &limit.min(100).to_string())
        .finish();
    let path = format!("/2.0/repositories/{workspace}/{slug}/pullrequests?{query}");

    let response: PullRequestList = ctx
        .client
        .get(&path)
        .await
        .with_context(|| format!("Failed to list pull requests for {workspace}/{slug}"))?;

    #[derive(Serialize)]
    struct Row<'a> {
        id: i64,
        title: &'a str,
        state: &'a str,
        author: &'a str,
        source: &'a str,
        destination: &'a str,
    }

    let rows: Vec<Row<'_>> = response
        .values
        .iter()
        .map(|pr| Row {
            id: pr.id,
            title: pr.title.as_str(),
            state: pr.state.as_str(),
            author: pr.author.display_name.as_str(),
            source: pr.source.branch.name.as_str(),
            destination: pr.destination.branch.name.as_str(),
        })
        .collect();

    if rows.is_empty() {
        ctx.verify_auth().await?;
        tracing::info!(workspace, slug, "No pull requests found");
        println!("No pull requests found");
        return Ok(());
    }

    ctx.renderer.render(&rows)
}

pub async fn get_pull_request(
    ctx: &BitbucketContext<'_>,
    workspace: &str,
    repo_slug: &str,
    pr_id: i64,
) -> Result<()> {
    let path = format!("/2.0/repositories/{workspace}/{repo_slug}/pullrequests/{pr_id}");
    let pr: PullRequest = ctx.client.get(&path).await.with_context(|| {
        format!("Failed to fetch pull request {pr_id} from {workspace}/{repo_slug}")
    })?;

    #[derive(Serialize)]
    struct View<'a> {
        id: i64,
        title: &'a str,
        state: &'a str,
        author: &'a str,
        source: &'a str,
        destination: &'a str,
        description: &'a str,
        created: &'a str,
        updated: &'a str,
        comments: String,
        tasks: String,
        approvals: String,
    }

    let approvals = pr
        .participants
        .as_ref()
        .map(|p| p.iter().filter(|part| part.approved).count())
        .unwrap_or(0);

    let view = View {
        id: pr.id,
        title: pr.title.as_str(),
        state: pr.state.as_str(),
        author: pr.author.display_name.as_str(),
        source: pr.source.branch.name.as_str(),
        destination: pr.destination.branch.name.as_str(),
        description: pr.description.as_deref().unwrap_or(""),
        created: pr.created_on.as_deref().unwrap_or(""),
        updated: pr.updated_on.as_deref().unwrap_or(""),
        comments: pr.comment_count.map(|c| c.to_string()).unwrap_or_default(),
        tasks: pr.task_count.map(|t| t.to_string()).unwrap_or_default(),
        approvals: approvals.to_string(),
    };

    ctx.renderer.render(&view)
}

#[allow(clippy::too_many_arguments)]
pub async fn create_pull_request(
    ctx: &BitbucketContext<'_>,
    workspace: &str,
    repo_slug: &str,
    title: &str,
    source_branch: &str,
    dest_branch: &str,
    description: Option<&str>,
    reviewers: Vec<String>,
) -> Result<()> {
    let mut payload = serde_json::json!({
        "title": title,
        "source": {
            "branch": {
                "name": source_branch
            }
        },
        "destination": {
            "branch": {
                "name": dest_branch
            }
        }
    });

    if let Some(desc) = description {
        payload["description"] = serde_json::json!(desc);
    }

    if !reviewers.is_empty() {
        let reviewer_objs: Vec<_> = reviewers
            .iter()
            .map(|uuid| serde_json::json!({"uuid": uuid}))
            .collect();
        payload["reviewers"] = serde_json::json!(reviewer_objs);
    }

    let path = format!("/2.0/repositories/{workspace}/{repo_slug}/pullrequests");
    let pr: PullRequest = ctx
        .client
        .post(&path, &payload)
        .await
        .with_context(|| format!("Failed to create pull request in {workspace}/{repo_slug}"))?;

    tracing::info!(
        pr_id = pr.id,
        workspace,
        repo_slug,
        "Pull request created successfully"
    );

    #[derive(Serialize)]
    struct Created {
        id: i64,
        title: String,
        source: String,
        destination: String,
        state: String,
    }

    let created = Created {
        id: pr.id,
        title: pr.title.clone(),
        source: pr.source.branch.name.clone(),
        destination: pr.destination.branch.name.clone(),
        state: pr.state.clone(),
    };

    ctx.renderer.render(&created)
}

pub async fn update_pull_request(
    ctx: &BitbucketContext<'_>,
    workspace: &str,
    repo_slug: &str,
    pr_id: i64,
    title: Option<&str>,
    description: Option<&str>,
) -> Result<()> {
    let mut payload = serde_json::json!({});

    if let Some(t) = title {
        payload["title"] = serde_json::json!(t);
    }

    if let Some(d) = description {
        payload["description"] = serde_json::json!(d);
    }

    let path = format!("/2.0/repositories/{workspace}/{repo_slug}/pullrequests/{pr_id}");
    let pr: PullRequest = ctx.client.put(&path, &payload).await.with_context(|| {
        format!("Failed to update pull request {pr_id} in {workspace}/{repo_slug}")
    })?;

    tracing::info!(
        pr_id = pr.id,
        workspace,
        repo_slug,
        "Pull request updated successfully"
    );

    #[derive(Serialize)]
    struct Updated {
        id: i64,
        title: String,
        description: String,
        state: String,
    }

    let updated = Updated {
        id: pr.id,
        title: pr.title.clone(),
        description: pr.description.unwrap_or_default(),
        state: pr.state.clone(),
    };

    ctx.renderer.render(&updated)
}

pub async fn merge_pull_request(
    ctx: &BitbucketContext<'_>,
    workspace: &str,
    repo_slug: &str,
    pr_id: i64,
    merge_strategy: Option<&str>,
    message: Option<&str>,
) -> Result<()> {
    let mut payload = serde_json::json!({});

    if let Some(strategy) = merge_strategy {
        payload["merge_strategy"] = serde_json::json!(strategy);
    }

    if let Some(msg) = message {
        payload["message"] = serde_json::json!(msg);
    }

    let path = format!("/2.0/repositories/{workspace}/{repo_slug}/pullrequests/{pr_id}/merge");
    let pr: PullRequest = ctx.client.post(&path, &payload).await.with_context(|| {
        format!("Failed to merge pull request {pr_id} in {workspace}/{repo_slug}")
    })?;

    tracing::info!(
        pr_id = pr.id,
        workspace,
        repo_slug,
        "Pull request merged successfully"
    );

    #[derive(Serialize)]
    struct Merged {
        id: i64,
        title: String,
        state: String,
        source: String,
        destination: String,
    }

    let merged = Merged {
        id: pr.id,
        title: pr.title.clone(),
        state: pr.state.clone(),
        source: pr.source.branch.name.clone(),
        destination: pr.destination.branch.name.clone(),
    };

    ctx.renderer.render(&merged)
}

pub async fn decline_pull_request(
    ctx: &BitbucketContext<'_>,
    workspace: &str,
    repo_slug: &str,
    pr_id: i64,
) -> Result<()> {
    let path = format!("/2.0/repositories/{workspace}/{repo_slug}/pullrequests/{pr_id}/decline");
    let pr: PullRequest = ctx
        .client
        .post(&path, &serde_json::json!({}))
        .await
        .with_context(|| {
            format!("Failed to decline pull request {pr_id} in {workspace}/{repo_slug}")
        })?;

    tracing::info!(
        pr_id = pr.id,
        workspace,
        repo_slug,
        "Pull request declined successfully"
    );

    render_success(
        ctx.renderer,
        &format!("✅ Pull request #{pr_id} declined: {}", pr.title),
        &MutationResult::with_id(format!("Pull request #{pr_id} declined"), pr_id.to_string()),
    )
}

pub async fn approve_pull_request(
    ctx: &BitbucketContext<'_>,
    workspace: &str,
    repo_slug: &str,
    pr_id: i64,
) -> Result<()> {
    #[derive(Deserialize)]
    struct Approval {
        #[allow(dead_code)]
        #[serde(default)]
        approved: bool,
        #[allow(dead_code)]
        user: User,
    }

    let path = format!("/2.0/repositories/{workspace}/{repo_slug}/pullrequests/{pr_id}/approve");
    let approval: Approval = ctx
        .client
        .post(&path, &serde_json::json!({}))
        .await
        .with_context(|| {
            format!("Failed to approve pull request {pr_id} in {workspace}/{repo_slug}")
        })?;

    tracing::info!(
        pr_id,
        workspace,
        repo_slug,
        "Pull request approved successfully"
    );

    println!(
        "✅ Pull request #{pr_id} approved by {}",
        approval.user.display_name
    );
    Ok(())
}

pub async fn unapprove_pull_request(
    ctx: &BitbucketContext<'_>,
    workspace: &str,
    repo_slug: &str,
    pr_id: i64,
) -> Result<()> {
    let path = format!("/2.0/repositories/{workspace}/{repo_slug}/pullrequests/{pr_id}/approve");
    let _: serde_json::Value = ctx.client.delete(&path).await.with_context(|| {
        format!("Failed to unapprove pull request {pr_id} in {workspace}/{repo_slug}")
    })?;

    tracing::info!(
        pr_id,
        workspace,
        repo_slug,
        "Pull request approval removed successfully"
    );

    render_success(
        ctx.renderer,
        &format!("✅ Approval removed from pull request #{pr_id}"),
        &MutationResult::with_id(
            format!("Approval removed from pull request #{pr_id}"),
            pr_id.to_string(),
        ),
    )
}

pub async fn list_pr_comments(
    ctx: &BitbucketContext<'_>,
    workspace: &str,
    repo_slug: &str,
    pr_id: i64,
) -> Result<()> {
    #[derive(Deserialize)]
    struct CommentList {
        values: Vec<Comment>,
    }

    let path = format!("/2.0/repositories/{workspace}/{repo_slug}/pullrequests/{pr_id}/comments");
    let response: CommentList = ctx.client.get(&path).await.with_context(|| {
        format!("Failed to list comments for pull request {pr_id} in {workspace}/{repo_slug}")
    })?;

    #[derive(Serialize)]
    struct Row<'a> {
        id: i64,
        author: &'a str,
        content: &'a str,
        created: &'a str,
    }

    let rows: Vec<Row<'_>> = response
        .values
        .iter()
        .map(|comment| Row {
            id: comment.id,
            author: comment.user.display_name.as_str(),
            content: comment.content.raw.lines().next().unwrap_or(""),
            created: comment.created_on.as_deref().unwrap_or(""),
        })
        .collect();

    if rows.is_empty() {
        tracing::info!(pr_id, workspace, repo_slug, "No comments found");
        println!("No comments found");
        return Ok(());
    }

    ctx.renderer.render(&rows)
}

pub async fn add_pr_comment(
    ctx: &BitbucketContext<'_>,
    workspace: &str,
    repo_slug: &str,
    pr_id: i64,
    content: &str,
) -> Result<()> {
    let payload = serde_json::json!({
        "content": {
            "raw": content
        }
    });

    let path = format!("/2.0/repositories/{workspace}/{repo_slug}/pullrequests/{pr_id}/comments");
    let comment: Comment = ctx.client.post(&path, &payload).await.with_context(|| {
        format!("Failed to add comment to pull request {pr_id} in {workspace}/{repo_slug}")
    })?;

    tracing::info!(comment_id = comment.id, pr_id, "Comment added successfully");
    render_success(
        ctx.renderer,
        &format!("✅ Comment added to pull request #{pr_id}"),
        &MutationResult::with_id(
            format!("Comment added to pull request #{pr_id}"),
            pr_id.to_string(),
        ),
    )
}

pub async fn add_pr_reviewers(
    ctx: &BitbucketContext<'_>,
    workspace: &str,
    repo_slug: &str,
    pr_id: i64,
    reviewers: Vec<String>,
) -> Result<()> {
    for uuid in reviewers {
        let path = format!(
            "/2.0/repositories/{workspace}/{repo_slug}/pullrequests/{pr_id}/default-reviewers/{uuid}"
        );
        let _: serde_json::Value = ctx
            .client
            .put(&path, &serde_json::json!({}))
            .await
            .with_context(|| format!("Failed to add reviewer {uuid} to pull request {pr_id}"))?;

        tracing::info!(uuid, pr_id, "Reviewer added successfully");
    }

    render_success(
        ctx.renderer,
        &format!("✅ Reviewers added to pull request #{pr_id}"),
        &MutationResult::with_id(
            format!("Reviewers added to pull request #{pr_id}"),
            pr_id.to_string(),
        ),
    )
}

pub async fn get_pr_diff(
    _ctx: &BitbucketContext<'_>,
    workspace: &str,
    repo_slug: &str,
    pr_id: i64,
) -> Result<()> {
    tracing::info!(
        pr_id,
        workspace,
        repo_slug,
        "Fetching diff for pull request"
    );

    println!("Diff for pull request #{pr_id}:");
    println!("View at: https://bitbucket.org/{workspace}/{repo_slug}/pull-requests/{pr_id}/diff");
    println!("\nNote: Use the web interface to view the full diff with syntax highlighting");

    Ok(())
}

/// Get pull request information for pipeline operations
/// Returns source branch, workspace, repo, and PR state
pub async fn get_pr_info(
    ctx: &BitbucketContext<'_>,
    workspace: &str,
    repo_slug: &str,
    pr_id: i64,
) -> Result<PullRequestInfo> {
    let path = format!("/2.0/repositories/{workspace}/{repo_slug}/pullrequests/{pr_id}");
    let pr: PullRequest = ctx
        .client
        .get(&path)
        .await
        .with_context(|| format!("Failed to fetch PR #{pr_id} from {workspace}/{repo_slug}"))?;

    let source_branch = pr.source.branch.name;

    // Extract source workspace and repo from the source repository
    let (source_workspace, source_repo) = if let Some(ref repo) = pr.source.repository {
        let ws = repo
            .workspace
            .as_ref()
            .map(|w| w.slug.clone())
            .unwrap_or_else(|| workspace.to_string());
        let repo_name = repo.name.clone();
        (ws, repo_name)
    } else {
        // If no source repository info, assume same workspace/repo (not a fork)
        (workspace.to_string(), repo_slug.to_string())
    };

    Ok(PullRequestInfo {
        source_branch,
        source_workspace,
        source_repo,
        state: pr.state,
    })
}

/// Check if a PR is from a fork
pub fn is_from_fork(pr_info: &PullRequestInfo, target_workspace: &str, target_repo: &str) -> bool {
    pr_info.source_workspace != target_workspace || pr_info.source_repo != target_repo
}