bitbucket-cli 0.3.11

A powerful command-line interface for Bitbucket Cloud - manage repos, PRs, issues, and pipelines from your terminal with OAuth 2.0
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
use anyhow::Result;
use crossterm::{
    event::{DisableMouseCapture, EnableMouseCapture},
    execute,
    terminal::{EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode},
};
use ratatui::{Terminal, backend::CrosstermBackend};
use std::io;

use super::event::{Event, EventHandler};
use super::ui;
use super::views::{View, ViewState};
use crate::api::BitbucketClient;
use crate::models::{Issue, Pipeline, PullRequest, Repository};

/// Application state
pub struct App {
    /// Is the application running
    pub running: bool,
    /// Current view
    pub current_view: View,
    /// View-specific state
    pub view_state: ViewState,
    /// API client
    pub client: Option<BitbucketClient>,
    /// Current workspace
    pub workspace: Option<String>,
    /// Status message
    pub status: Option<String>,
    /// Is loading data
    pub loading: bool,
    /// Error message
    pub error: Option<String>,

    // Data
    pub repositories: Vec<Repository>,
    pub pull_requests: Vec<PullRequest>,
    pub issues: Vec<Issue>,
    pub pipelines: Vec<Pipeline>,
}

impl App {
    pub fn new() -> Self {
        Self {
            running: true,
            current_view: View::Dashboard,
            view_state: ViewState::default(),
            client: None,
            workspace: None,
            status: None,
            loading: false,
            error: None,
            repositories: Vec::new(),
            pull_requests: Vec::new(),
            issues: Vec::new(),
            pipelines: Vec::new(),
        }
    }

    /// Initialize the application with API client
    pub fn with_client(mut self, client: BitbucketClient) -> Self {
        self.client = Some(client);
        self
    }

    /// Set the workspace
    pub fn with_workspace(mut self, workspace: String) -> Self {
        self.workspace = Some(workspace);
        self
    }

    /// Set status message
    pub fn set_status(&mut self, message: &str) {
        self.status = Some(message.to_string());
    }

    /// Clear status message
    pub fn clear_status(&mut self) {
        self.status = None;
    }

    /// Set error message
    pub fn set_error(&mut self, message: &str) {
        self.error = Some(message.to_string());
    }

    /// Clear error
    pub fn clear_error(&mut self) {
        self.error = None;
    }

    /// Switch to a different view
    pub fn switch_view(&mut self, view: View) {
        self.current_view = view;
        self.view_state.selected_index = 0;
        self.clear_error();
    }

    /// Handle keyboard input
    pub fn handle_key(&mut self, key: crossterm::event::KeyEvent) {
        use crossterm::event::KeyCode;

        // Global keys
        match key.code {
            KeyCode::Char('q') => {
                self.running = false;
                return;
            }
            KeyCode::Char('1') => {
                self.switch_view(View::Dashboard);
                return;
            }
            KeyCode::Char('2') => {
                self.switch_view(View::Repositories);
                return;
            }
            KeyCode::Char('3') => {
                self.switch_view(View::PullRequests);
                return;
            }
            KeyCode::Char('4') => {
                self.switch_view(View::Issues);
                return;
            }
            KeyCode::Char('5') => {
                self.switch_view(View::Pipelines);
                return;
            }
            KeyCode::Esc => {
                self.clear_error();
                return;
            }
            _ => {}
        }

        // View-specific keys
        match key.code {
            KeyCode::Up | KeyCode::Char('k') => {
                self.view_state.previous();
            }
            KeyCode::Down | KeyCode::Char('j') => {
                let max = match self.current_view {
                    View::Dashboard => 4,
                    View::Repositories => self.repositories.len(),
                    View::PullRequests => self.pull_requests.len(),
                    View::Issues => self.issues.len(),
                    View::Pipelines => self.pipelines.len(),
                };
                self.view_state.next(max);
            }
            KeyCode::Enter => {
                self.handle_select();
            }
            KeyCode::Char('r') => {
                // Refresh will be handled in main loop
            }
            _ => {}
        }
    }

    /// Handle selection
    fn handle_select(&mut self) {
        match self.current_view {
            View::Dashboard => {
                // Navigate to selected section
                match self.view_state.selected_index {
                    0 => self.switch_view(View::Repositories),
                    1 => self.switch_view(View::PullRequests),
                    2 => self.switch_view(View::Issues),
                    3 => self.switch_view(View::Pipelines),
                    _ => {}
                }
            }
            View::Repositories => {
                if let Some(repo) = self.repositories.get(self.view_state.selected_index) {
                    self.set_status(&format!("Selected: {}", repo.full_name));
                }
            }
            View::PullRequests => {
                if let Some(pr) = self.pull_requests.get(self.view_state.selected_index) {
                    self.set_status(&format!("Selected PR #{}: {}", pr.id, pr.title));
                }
            }
            View::Issues => {
                if let Some(issue) = self.issues.get(self.view_state.selected_index) {
                    self.set_status(&format!("Selected Issue #{}: {}", issue.id, issue.title));
                }
            }
            View::Pipelines => {
                if let Some(pipeline) = self.pipelines.get(self.view_state.selected_index) {
                    self.set_status(&format!("Selected Pipeline #{}", pipeline.build_number));
                }
            }
        }
    }

    /// Quit the application
    pub fn quit(&mut self) {
        self.running = false;
    }

    /// Load repositories
    pub async fn load_repositories(&mut self) -> Result<()> {
        if let (Some(client), Some(workspace)) = (&self.client, &self.workspace) {
            self.loading = true;
            match client.list_repositories(workspace, None, Some(50)).await {
                Ok(result) => {
                    self.repositories = result.values;
                    self.clear_error();
                }
                Err(e) => {
                    self.set_error(&format!("Failed to load repositories: {}", e));
                }
            }
            self.loading = false;
        } else {
            self.set_error("No workspace configured");
        }
        Ok(())
    }

    /// Load pull requests for the current workspace
    pub async fn load_pull_requests(&mut self) -> Result<()> {
        if let (Some(client), Some(workspace)) = (&self.client, &self.workspace) {
            self.loading = true;
            self.pull_requests.clear();

            // Load PRs from all repositories
            if let Ok(repos) = client.list_repositories(workspace, None, Some(50)).await {
                for repo in repos.values {
                    let repo_slug = repo.slug.as_deref().unwrap_or(&repo.name);
                    if let Ok(prs) = client
                        .list_pull_requests(workspace, repo_slug, None, None, Some(10))
                        .await
                    {
                        self.pull_requests.extend(prs.values);
                    }
                }
            }

            self.clear_error();
            self.loading = false;
        } else {
            self.set_error("No workspace configured");
        }
        Ok(())
    }

    /// Load issues for the current workspace
    pub async fn load_issues(&mut self) -> Result<()> {
        if let (Some(client), Some(workspace)) = (&self.client, &self.workspace) {
            self.loading = true;
            self.issues.clear();

            // Load issues from all repositories
            if let Ok(repos) = client.list_repositories(workspace, None, Some(50)).await {
                for repo in repos.values {
                    let repo_slug = repo.slug.as_deref().unwrap_or(&repo.name);
                    if let Ok(issues) = client
                        .list_issues(workspace, repo_slug, None, None, Some(10))
                        .await
                    {
                        self.issues.extend(issues.values);
                    }
                }
            }

            self.clear_error();
            self.loading = false;
        } else {
            self.set_error("No workspace configured");
        }
        Ok(())
    }

    /// Load pipelines for the current workspace
    pub async fn load_pipelines(&mut self) -> Result<()> {
        if let (Some(client), Some(workspace)) = (&self.client, &self.workspace) {
            self.loading = true;
            self.pipelines.clear();

            // Load pipelines from all repositories
            if let Ok(repos) = client.list_repositories(workspace, None, Some(50)).await {
                for repo in repos.values {
                    let repo_slug = repo.slug.as_deref().unwrap_or(&repo.name);
                    if let Ok(pipelines) = client
                        .list_pipelines(workspace, repo_slug, None, Some(10))
                        .await
                    {
                        self.pipelines.extend(pipelines.values);
                    }
                }
            }

            self.clear_error();
            self.loading = false;
        } else {
            self.set_error("No workspace configured");
        }
        Ok(())
    }

    /// Load all data
    pub async fn load_all_data(&mut self) -> Result<()> {
        self.load_repositories().await?;
        self.load_pull_requests().await?;
        self.load_issues().await?;
        self.load_pipelines().await?;
        Ok(())
    }
}

impl Default for App {
    fn default() -> Self {
        Self::new()
    }
}

/// Run the TUI application
pub async fn run_tui(workspace: Option<String>) -> Result<()> {
    // Setup terminal
    enable_raw_mode()?;
    let mut stdout = io::stdout();
    execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
    let backend = CrosstermBackend::new(stdout);
    let mut terminal = Terminal::new(backend)?;

    // Create app
    let mut app = App::new();

    // Try to get API client
    match BitbucketClient::from_stored().await {
        Ok(client) => {
            app = app.with_client(client);
            if let Some(ws) = workspace {
                app = app.with_workspace(ws);
            } else {
                app.set_error("No workspace specified. Use: bitbucket tui --workspace <workspace>");
            }
        }
        Err(e) => {
            app.set_error(&format!("Not authenticated: {}", e));
        }
    }

    // Load initial data if we have a workspace
    if app.workspace.is_some() && app.client.is_some() {
        app.set_status("Loading data...");
        terminal.draw(|f| ui::draw(f, &app))?;

        if let Err(e) = app.load_repositories().await {
            app.set_error(&format!("Failed to load data: {}", e));
        } else {
            app.set_status("Data loaded. Press 'r' to refresh.");
        }
    }

    // Create event handler
    let event_handler = EventHandler::new(250);
    let mut should_refresh = false;

    // Main loop
    while app.running {
        // Draw UI
        terminal.draw(|f| ui::draw(f, &app))?;

        // Handle refresh if requested
        if should_refresh && app.workspace.is_some() && app.client.is_some() {
            should_refresh = false;
            app.set_status("Refreshing...");
            terminal.draw(|f| ui::draw(f, &app))?;

            match app.current_view {
                View::Dashboard | View::Repositories => {
                    let _ = app.load_repositories().await;
                }
                View::PullRequests => {
                    let _ = app.load_pull_requests().await;
                }
                View::Issues => {
                    let _ = app.load_issues().await;
                }
                View::Pipelines => {
                    let _ = app.load_pipelines().await;
                }
            }

            app.set_status("Refreshed");
        }

        // Handle events
        match event_handler.next()? {
            Event::Key(key) => {
                // Check if refresh was requested
                if let crossterm::event::KeyCode::Char('r') = key.code {
                    should_refresh = true;
                }
                app.handle_key(key);
            }
            Event::Tick => {
                // Periodic tick for animations, etc.
            }
            Event::Resize(_, _) => {
                // Terminal will redraw automatically
            }
            _ => {}
        }
    }

    // Restore terminal
    disable_raw_mode()?;
    execute!(
        terminal.backend_mut(),
        LeaveAlternateScreen,
        DisableMouseCapture
    )?;
    terminal.show_cursor()?;

    Ok(())
}