gitlab-tracker 0.2.10

A fast terminal TUI dashboard for tracking GitLab Merge Requests across branches
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
use crate::config::AppConfig;
use crate::models::{GitlabMrState, MergeabilityStatus, Pipeline, PipelineState, TrackedMr};
use crate::ui::table::badge_label;
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span, Text};

/// Renders the pipeline list view for the Inspector panel.
///
/// Shows the last fetched pipelines for the selected MR with their jobs,
/// grouped by pipeline run. Displayed when the user presses [P].
pub fn render_pipelines_text(mr: &TrackedMr) -> Text<'static> {
    let mut lines = vec![
        Line::from(vec![Span::styled(
            format!("Pipelines — MR !{}", mr.id),
            Style::default()
                .fg(Color::Cyan)
                .add_modifier(Modifier::BOLD | Modifier::UNDERLINED),
        )]),
        Line::from(vec![Span::raw("")]),
    ];

    if mr.pipelines.is_empty() {
        lines.push(Line::from(vec![Span::styled(
            "No pipelines found for this MR.",
            Style::default().fg(Color::DarkGray),
        )]));
    } else {
        for pipeline in &mr.pipelines {
            lines.extend(render_pipeline_block(pipeline));
            lines.push(Line::from(vec![Span::raw("")]));
        }
    }

    Text::from(lines)
}

/// Renders a single pipeline block with its status header and job list.
fn render_pipeline_block(pipeline: &Pipeline) -> Vec<Line<'static>> {
    let (status_icon, status_color) = pipeline_status_style(&pipeline.status);

    // Format the creation timestamp to a compact readable form (drop sub-seconds and timezone).
    let date_display = pipeline
        .created_at
        .as_deref()
        .map(|s| s.get(..19).unwrap_or(s).replace('T', " "))
        .unwrap_or_else(|| "unknown date".to_string());

    let mut lines = vec![Line::from(vec![
        Span::styled(
            format!("#{} ", pipeline.id),
            Style::default()
                .fg(Color::White)
                .add_modifier(Modifier::BOLD),
        ),
        Span::styled(
            status_icon,
            Style::default()
                .fg(status_color)
                .add_modifier(Modifier::BOLD),
        ),
        Span::styled(
            format!("  {}", date_display),
            Style::default().fg(Color::DarkGray),
        ),
    ])];

    if pipeline.jobs.is_empty() {
        lines.push(Line::from(vec![Span::styled(
            "  No jobs.",
            Style::default().fg(Color::DarkGray),
        )]));
        return lines;
    }

    // Group jobs by stage for readability.
    let mut current_stage = String::new();
    for job in &pipeline.jobs {
        if job.stage != current_stage {
            current_stage = job.stage.clone();
            lines.push(Line::from(vec![Span::styled(
                format!("{}", current_stage),
                Style::default()
                    .fg(Color::Yellow)
                    .add_modifier(Modifier::BOLD),
            )]));
        }

        let (job_icon, job_color) = job_status_style(&job.status);
        let duration = job
            .duration
            .map(|d| format!(" ({:.0}s)", d))
            .unwrap_or_default();

        lines.push(Line::from(vec![
            Span::raw("    "),
            Span::styled(job_icon, Style::default().fg(job_color)),
            Span::raw(format!(" {}{}", job.name, duration)),
        ]));
    }

    lines
}

/// Maps a `PipelineState` to a display icon and its colour.
fn pipeline_status_style(state: &PipelineState) -> (&'static str, Color) {
    match state {
        PipelineState::Success => ("✔ passed", Color::Green),
        PipelineState::Failed => ("✘ failed", Color::Red),
        PipelineState::Running => ("⟳ running", Color::Cyan),
        PipelineState::Pending => ("◔ pending", Color::Yellow),
        PipelineState::Canceled => ("⊘ canceled", Color::DarkGray),
        PipelineState::Skipped => ("⊝ skipped", Color::DarkGray),
        PipelineState::Created => ("○ created", Color::White),
        PipelineState::Unknown => ("? unknown", Color::DarkGray),
    }
}

/// Maps a job status string (as returned by the GitLab API) to icon + colour.
fn job_status_style(status: &str) -> (&'static str, Color) {
    match status {
        "success" => ("", Color::Green),
        "failed" => ("", Color::Red),
        "running" => ("", Color::Cyan),
        "pending" => ("", Color::Yellow),
        "canceled" => ("", Color::DarkGray),
        "skipped" => ("", Color::DarkGray),
        "created" => ("", Color::White),
        _ => ("?", Color::DarkGray),
    }
}

pub fn create_chip_span(label: &str, config: &AppConfig) -> Span<'static> {
    let (bg, fg) = config.get_label_style(label);
    Span::styled(
        format!(" {} ", label),
        Style::default().bg(bg).fg(fg).add_modifier(Modifier::BOLD),
    )
}

/// Renders a section header separator with a coloured title.
fn section_header(title: &'static str) -> Line<'static> {
    Line::from(vec![
        Span::styled("── ", Style::default().fg(Color::DarkGray)),
        Span::styled(
            title,
            Style::default()
                .fg(Color::Yellow)
                .add_modifier(Modifier::BOLD),
        ),
        Span::styled(
            " ──────────────────────────────",
            Style::default().fg(Color::DarkGray),
        ),
    ])
}

pub fn render_safe_inspector_text(mr: &TrackedMr, config: &AppConfig) -> Text<'static> {
    // Format ISO 8601 timestamp to a more readable form (date + time, drop sub-seconds).
    let updated_at_display = mr
        .updated_at
        .as_deref()
        .map(|s| s.get(..19).unwrap_or(s).replace('T', " "))
        .unwrap_or_else(|| "Unknown".to_string());

    // Compute the activity badge (icon + color) based on configurable thresholds.
    let (badge_icon, badge_color) = config.activity_badge(mr.updated_at.as_deref());

    // Build the state badge using the same colour coding as the table column.
    // badge_label() centers the text to BADGE_WIDTH chars — no manual padding needed.
    let (state_text, state_fg, state_bg) = match mr.state {
        GitlabMrState::Opened => ("OPEN", Color::Black, Color::Green),
        GitlabMrState::Merged => ("MERGED", Color::Black, Color::Magenta),
        GitlabMrState::Closed => ("CLOSED", Color::Black, Color::Red),
    };
    let state_label = badge_label(state_text);

    // Build the git clone command for the source branch — used in the [Y]ank hint.
    // Derives the SSH clone URL from the web URL: replaces the HTTPS scheme and host
    // with the git@ SSH equivalent (standard GitLab convention).
    let git_clone_cmd = {
        // e.g. "https://gitlab.com/org/project/-/merge_requests/42"
        //   -> "git clone -b feat/my-branch git@gitlab.com:org/project.git"
        let ssh_url = mr
            .web_url
            .split("/-/")
            .next()
            .unwrap_or("")
            .replacen("https://", "git@", 1)
            .replacen('/', ":", 1);
        format!("git clone -b {} {}.git", mr.source_branch, ssh_url)
    };

    // ── SECTION 1: Identity ──────────────────────────────────────────────────
    let mut lines = vec![
        section_header("Identity"),
        Line::from(vec![Span::styled(
            format!("MR ID    : !{}", mr.id),
            Style::default()
                .fg(Color::Cyan)
                .add_modifier(Modifier::BOLD | Modifier::UNDERLINED),
        )]),
        Line::from(vec![
            Span::raw("State    : "),
            Span::styled(
                state_label,
                Style::default()
                    .fg(state_fg)
                    .bg(state_bg)
                    .add_modifier(Modifier::BOLD),
            ),
        ]),
        Line::from(vec![
            Span::raw("Branch   : "),
            Span::styled(
                mr.source_branch.clone(),
                Style::default()
                    .fg(Color::LightBlue)
                    .add_modifier(Modifier::BOLD),
            ),
            Span::raw(""),
            Span::styled(
                mr.target_branch.clone(),
                Style::default().fg(Color::DarkGray),
            ),
        ]),
        Line::from(vec![
            Span::raw("Clone    : "),
            Span::styled(git_clone_cmd.clone(), Style::default().fg(Color::DarkGray)),
            Span::raw("  "),
            Span::styled(
                "[Y] copy",
                Style::default()
                    .fg(Color::Yellow)
                    .add_modifier(Modifier::BOLD),
            ),
        ]),
        Line::from(vec![
            Span::raw("URL      : "),
            Span::styled(mr.web_url.clone(), Style::default().fg(Color::DarkGray)),
        ]),
    ];

    // ── SECTION 2: People ────────────────────────────────────────────────────
    lines.push(Line::from(vec![Span::raw("")]));
    lines.push(section_header("People"));
    lines.push(Line::from(vec![
        Span::raw("Author   : "),
        Span::styled(
            mr.author.clone(),
            Style::default().add_modifier(Modifier::BOLD),
        ),
    ]));
    lines.push(Line::from(vec![
        Span::raw("Assignee : "),
        Span::styled(
            mr.assignee.clone(),
            Style::default().add_modifier(Modifier::BOLD),
        ),
    ]));

    // Reviewers: listed inline, or dimmed "None" if empty.
    if mr.reviewers.is_empty() {
        lines.push(Line::from(vec![
            Span::raw("Reviewers: "),
            Span::styled("None", Style::default().fg(Color::DarkGray)),
        ]));
    } else {
        for (i, reviewer) in mr.reviewers.iter().enumerate() {
            let label = if i == 0 { "Reviewers: " } else { "           " };
            lines.push(Line::from(vec![
                Span::raw(label),
                Span::styled(
                    reviewer.clone(),
                    Style::default()
                        .fg(Color::LightBlue)
                        .add_modifier(Modifier::BOLD),
                ),
            ]));
        }
    }

    // Notes / comments indicator — always shown, highlights when non-zero.
    let (notes_text, notes_fg, notes_bg) = if mr.user_notes_count == 0 {
        (
            "  ✔ No comments  ".to_string(),
            Color::DarkGray,
            Color::Black,
        )
    } else {
        (
            format!("  💬 {} comment(s) — review pending  ", mr.user_notes_count),
            Color::Black,
            Color::Yellow,
        )
    };
    lines.push(Line::from(vec![
        Span::raw("Notes    : "),
        Span::styled(
            notes_text,
            Style::default()
                .fg(notes_fg)
                .bg(notes_bg)
                .add_modifier(Modifier::BOLD),
        ),
    ]));

    // ── SECTION 3: Planning ──────────────────────────────────────────────────
    lines.push(Line::from(vec![Span::raw("")]));
    lines.push(section_header("Planning"));
    lines.push(Line::from(vec![
        Span::raw("Milestone: "),
        Span::styled(mr.milestone.clone(), Style::default().fg(Color::Cyan)),
    ]));

    // Milestone due date — show with urgency colouring when set.
    let (due_text, due_color) = match mr.milestone_due_date.as_deref() {
        None | Some("") => ("Not set".to_string(), Color::DarkGray),
        Some(date) => {
            // Colour the date based on proximity: red if past, yellow if within 7 days,
            // green otherwise. We do a simple lexicographic comparison against today's date
            // (YYYY-MM-DD format sorts correctly without parsing).
            let today = chrono::Utc::now().format("%Y-%m-%d").to_string();
            let in_7_days = (chrono::Utc::now() + chrono::Duration::days(7))
                .format("%Y-%m-%d")
                .to_string();
            let color = if date < today.as_str() {
                Color::Red
            } else if date <= in_7_days.as_str() {
                Color::Yellow
            } else {
                Color::Green
            };
            (date.to_string(), color)
        }
    };
    lines.push(Line::from(vec![
        Span::raw("Due date : "),
        Span::styled(
            due_text,
            Style::default().fg(due_color).add_modifier(Modifier::BOLD),
        ),
    ]));

    // ── SECTION 4: Status ────────────────────────────────────────────────────
    lines.push(Line::from(vec![Span::raw("")]));
    lines.push(section_header("Status"));

    // Mergeability — only meaningful for open MRs.
    // badge_label() centers the text to BADGE_WIDTH, matching the State badge width.
    if mr.state == GitlabMrState::Opened {
        let (merge_text, merge_fg, merge_bg) = match mr.mergeability {
            MergeabilityStatus::Mergeable => ("MERGEABLE", Color::Black, Color::LightGreen),
            MergeabilityStatus::Conflict => ("CONFLICT", Color::White, Color::Red),
            MergeabilityStatus::NeedsRebase => ("REBASE", Color::Black, Color::Yellow),
            MergeabilityStatus::NotOpen => ("CLOSED", Color::Black, Color::Red),
            MergeabilityStatus::Draft => ("DRAFT", Color::White, Color::Rgb(80, 80, 80)),
            MergeabilityStatus::DiscussionsNotResolved => {
                ("DISCUSSIONS", Color::Black, Color::LightMagenta)
            }
            MergeabilityStatus::CiMustPass => ("CI MUST PASS", Color::Black, Color::LightYellow),
            MergeabilityStatus::CiStillRunning => ("CI STILL RUNNING", Color::Black, Color::Yellow),
            MergeabilityStatus::NotApproved => ("NOT APPROVED", Color::Black, Color::LightRed),
            MergeabilityStatus::RequestedChanges => ("REQUESTED CHANGES", Color::White, Color::Red),
            MergeabilityStatus::Unknown => ("UNKNOWN", Color::DarkGray, Color::Black),
        };
        lines.push(Line::from(vec![
            Span::raw("Merge    : "),
            Span::styled(
                badge_label(merge_text),
                Style::default()
                    .fg(merge_fg)
                    .bg(merge_bg)
                    .add_modifier(Modifier::BOLD),
            ),
        ]));
    }

    // merged_by / merged_at — only shown for merged MRs.
    if mr.state == GitlabMrState::Merged {
        let merged_by_display = mr.merged_by.as_deref().unwrap_or("Unknown");
        let merged_at_display = mr
            .merged_at
            .as_deref()
            .map(|s| s.get(..19).unwrap_or(s).replace('T', " "))
            .unwrap_or_else(|| "Unknown".to_string());
        lines.push(Line::from(vec![
            Span::raw("Merged by: "),
            Span::styled(
                merged_by_display.to_string(),
                Style::default()
                    .fg(Color::Magenta)
                    .add_modifier(Modifier::BOLD),
            ),
        ]));
        lines.push(Line::from(vec![
            Span::raw("Merged at: "),
            Span::styled(merged_at_display, Style::default().fg(Color::Magenta)),
        ]));
    }

    lines.push(Line::from(vec![
        Span::raw("Updated  : "),
        Span::styled(updated_at_display, Style::default().fg(Color::Yellow)),
        Span::raw("  "),
        Span::styled(
            badge_icon,
            Style::default()
                .fg(badge_color)
                .add_modifier(Modifier::BOLD),
        ),
    ]));

    // ── SECTION 5: Labels ────────────────────────────────────────────────────
    lines.push(Line::from(vec![Span::raw("")]));
    lines.push(section_header("Labels"));

    if !mr.labels.is_empty() {
        let mut label_spans: Vec<Span<'static>> = vec![];
        for label in &mr.labels {
            label_spans.push(create_chip_span(label, config));
            label_spans.push(Span::raw(" "));
        }
        lines.push(Line::from(label_spans));
    } else {
        lines.push(Line::from(vec![Span::styled(
            "None",
            Style::default().dark_gray(),
        )]));
    }

    // ── Description ──────────────────────────────────────────────────────────
    lines.push(Line::from(vec![Span::raw("")]));
    lines.push(section_header("Description"));
    lines.push(Line::from(vec![Span::raw("")]));

    if mr.description.trim().is_empty() {
        lines.push(Line::from(vec![Span::styled(
            "No description text provided.",
            Style::default().dark_gray(),
        )]));
        return Text::from(lines);
    }

    for raw_line in mr.description.lines() {
        let trimmed = raw_line.trim();

        if trimmed.starts_with("# ") || trimmed.starts_with("## ") {
            let text = trimmed.trim_start_matches('#').trim();
            lines.push(Line::from(vec![Span::styled(
                text.to_string(),
                Style::default()
                    .fg(ratatui::style::Color::Cyan)
                    .add_modifier(Modifier::BOLD),
            )]));
        } else if trimmed.starts_with("### ") {
            let text = trimmed.trim_start_matches("### ");
            lines.push(Line::from(vec![Span::styled(
                text.to_string(),
                Style::default()
                    .fg(ratatui::style::Color::Yellow)
                    .add_modifier(Modifier::BOLD),
            )]));
        } else if trimmed == "---" {
            lines.push(Line::from(vec![Span::styled(
                "───────────────────────────────────",
                Style::default().fg(ratatui::style::Color::DarkGray),
            )]));
        } else {
            let mut spans = Vec::new();
            let mut line_content = raw_line.to_string();
            if trimmed.starts_with("- ") {
                spans.push(Span::styled(
                    "",
                    Style::default().fg(ratatui::style::Color::Yellow),
                ));
                line_content = raw_line.replacen("- ", "", 1);
            } else if trimmed.starts_with("* ") {
                spans.push(Span::styled(
                    "",
                    Style::default().fg(ratatui::style::Color::Yellow),
                ));
                line_content = raw_line.replacen("* ", "", 1);
            }

            let bold_parts = line_content.split("**");
            let mut is_bold = false;

            for bold_part in bold_parts {
                let code_parts = bold_part.split('`');
                let mut is_code = false;

                for code_part in code_parts {
                    let mut style = Style::default();
                    if is_bold {
                        style = style.add_modifier(Modifier::BOLD);
                    }
                    if is_code {
                        style = style.fg(ratatui::style::Color::Magenta);
                    }

                    spans.push(Span::styled(code_part.to_string(), style));
                    is_code = !is_code;
                }
                is_bold = !is_bold;
            }
            lines.push(Line::from(spans));
        }
    }

    Text::from(lines)
}