prctrl 1.0.0

Terminal-native GitHub PR management. Stay on top of code reviews without leaving your terminal.
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
use anyhow::Result;
use futures::future::join_all;
use std::collections::{HashMap, HashSet};

#[derive(Clone, Debug, serde::Serialize)]
pub struct StackedPR {
    pub number: u64,
    pub title: String,
    pub repo: String,
    pub head_branch: String,
    pub base_branch: String,
    pub position: usize,
    pub url: String,
    pub author: String,
    pub draft: bool,
}

#[derive(Clone, Debug, serde::Serialize)]
pub struct Stack {
    pub base_branch: String,
    pub repo: String,
    pub prs: Vec<StackedPR>,
    pub kind: StackKind,
}

#[derive(Clone, Debug, PartialEq, serde::Serialize)]
pub enum StackKind {
    /// PRs chained by branch: PR B targets PR A's head branch
    BranchChain,
    /// PRs grouped by ticket prefix + [N/M] markers in title
    Convention,
}

/// Detect stacked PRs using both branch-chaining and convention-based grouping
pub async fn detect_stacks(
    token: &str,
    org: &str,
    repos: &[String],
    author: Option<&str>,
    repo_filter: Option<&str>,
    include_drafts: bool,
    limit: Option<u32>,
) -> Result<Vec<Stack>> {
    let all_prs = fetch_all_prs(token, org, repos, author, repo_filter, include_drafts).await?;

    if all_prs.is_empty() {
        return Ok(Vec::new());
    }

    let mut stacks = Vec::new();
    let mut used_prs: HashSet<(String, u64)> = HashSet::new();

    // 1. Detect branch-chaining stacks
    let chain_stacks = detect_branch_chain_stacks(&all_prs);
    for stack in &chain_stacks {
        for pr in &stack.prs {
            used_prs.insert((pr.repo.clone(), pr.number));
        }
    }
    stacks.extend(chain_stacks);

    // 2. Detect convention-based stacks (ticket prefix + [N/M] markers)
    let convention_stacks = detect_convention_stacks(&all_prs, &used_prs);
    for stack in &convention_stacks {
        for pr in &stack.prs {
            used_prs.insert((pr.repo.clone(), pr.number));
        }
    }
    stacks.extend(convention_stacks);

    if let Some(l) = limit {
        stacks.truncate(l as usize);
    }

    Ok(stacks)
}

/// Fetch all open PRs across configured repos using octocrab
async fn fetch_all_prs(
    token: &str,
    org: &str,
    repos: &[String],
    author: Option<&str>,
    repo_filter: Option<&str>,
    include_drafts: bool,
) -> Result<Vec<StackedPR>> {
    let repo_futures = repos.iter().map(|repo_name| {
        let client = crate::github::new_client(token);
        let repo_name = repo_name.clone();
        async move {
            let mut all_prs = Vec::new();
            let first_page = client
                .pulls(org, &repo_name)
                .list()
                .state(octocrab::params::State::Open)
                .per_page(100)
                .send()
                .await?;

            all_prs.extend(first_page.items);

            let mut next_page = first_page.next;
            while next_page.is_some() {
                match client.get_page(&next_page).await {
                    Ok(Some(page)) => {
                        next_page = page.next.clone();
                        all_prs.extend(page.items);
                    }
                    Ok(None) => break,
                    Err(e) => {
                        eprintln!("Warning: Failed to fetch next page: {}", e);
                        break;
                    }
                }
            }

            Ok::<(String, Vec<_>), octocrab::Error>((repo_name, all_prs))
        }
    });

    let repo_results: Vec<(String, Vec<_>)> = join_all(repo_futures)
        .await
        .into_iter()
        .filter_map(|result| match result {
            Ok((repo, items)) => Some((repo, items)),
            Err(e) => {
                eprintln!("Warning: Failed to fetch PRs from a repo: {}", e);
                None
            }
        })
        .collect();

    let mut all_prs: Vec<StackedPR> = Vec::new();

    for (repo_name, prs) in repo_results {
        // Apply repo filter
        if let Some(filter) = repo_filter {
            if !repo_name.to_lowercase().contains(&filter.to_lowercase()) {
                continue;
            }
        }

        for pr in prs {
            let pr_author = pr.user.as_ref().map(|u| u.login.as_str()).unwrap_or("");

            // Apply author filter
            if let Some(author_filter) = author {
                if pr_author.to_lowercase() != author_filter.to_lowercase() {
                    continue;
                }
            }

            // Skip drafts unless included
            if !include_drafts && pr.draft.unwrap_or(false) {
                continue;
            }

            let head_branch = pr.head.ref_field.clone();
            let base_branch = pr.base.ref_field.clone();
            let title = pr.title.clone().unwrap_or_default();
            let url = pr.html_url.map(|u| u.to_string()).unwrap_or_default();

            all_prs.push(StackedPR {
                number: pr.number,
                title,
                repo: repo_name.clone(),
                head_branch,
                base_branch,
                position: 0,
                url,
                author: pr_author.to_string(),
                draft: pr.draft.unwrap_or(false),
            });
        }
    }

    // Sort by repo then PR number for consistency
    all_prs.sort_by(|a, b| {
        a.repo
            .cmp(&b.repo)
            .then_with(|| a.number.cmp(&b.number))
    });

    Ok(all_prs)
}

/// Detect stacks where a PR's base branch is another PR's head branch.
/// Uses a root-first approach: finds chain roots (PRs whose base is NOT another PR's head),
/// then walks down each chain to build the full stack without overlaps.
fn detect_branch_chain_stacks(all_prs: &[StackedPR]) -> Vec<Stack> {
    // Build maps for quick lookup
    // base_branch -> PRs that have this as their base (children)
    let mut base_to_children: HashMap<(String, String), Vec<&StackedPR>> = HashMap::new();
    // head_branch -> PRs that have this as their head (parents)
    let mut head_to_prs: HashMap<(String, String), Vec<&StackedPR>> = HashMap::new();

    for pr in all_prs {
        head_to_prs
            .entry((pr.repo.clone(), pr.head_branch.clone()))
            .or_default()
            .push(pr);
        base_to_children
            .entry((pr.repo.clone(), pr.base_branch.clone()))
            .or_default()
            .push(pr);
    }

    // Find chain roots: PRs whose base branch is NOT any other PR's head branch
    // (these are the starting points of chains)
    let roots: Vec<&StackedPR> = all_prs.iter().filter(|pr| {
        let key = (pr.repo.clone(), pr.base_branch.clone());
        // A root is a PR whose base branch isn't another PR's head branch
        !head_to_prs.contains_key(&key)
            || head_to_prs.get(&key).map_or(true, |prs| prs.iter().all(|p| p.number == pr.number))
    }).collect();

    let mut stacks = Vec::new();
    let mut used_prs: HashSet<(String, u64)> = HashSet::new();

    // Walk down from each root to build chains
    for root in roots {
        if used_prs.contains(&(root.repo.clone(), root.number)) {
            continue;
        }

        let mut chain = vec![root.clone()];
        used_prs.insert((root.repo.clone(), root.number));

        // Walk down: find PRs whose base_branch == last PR's head_branch
        loop {
            let last = chain.last().unwrap();
            let child_key = (last.repo.clone(), last.head_branch.clone());
            if let Some(children) = base_to_children.get(&child_key) {
                // Find a child that's not already used
                if let Some(child) = children.iter().find(|c| !used_prs.contains(&(c.repo.clone(), c.number)) && c.number != last.number) {
                    used_prs.insert((child.repo.clone(), child.number));
                    chain.push((*child).clone());
                    continue;
                }
            }
            break;
        }

        // Only create a stack if the chain has 2+ PRs
        if chain.len() >= 2 {
            let base_branch = chain.first().unwrap().base_branch.clone();
            let repo = chain.first().unwrap().repo.clone();
            stacks.push(build_stack(&repo, &base_branch, chain, StackKind::BranchChain));
        }
    }

    stacks
}

/// Extract a ticket key from a branch name or title.
/// Matches patterns like TAHC-1666, PROJ-123, ABC-1, etc.
fn extract_ticket_key(s: &str) -> Option<String> {
    let re = regex::Regex::new(r"(?i)\b([A-Z][A-Z0-9]*-\d+)\b").ok()?;
    let caps = re.captures(s)?;
    let key = caps.get(1)?.as_str().to_uppercase();
    Some(key)
}

/// Extract the position index from title markers like [1/3], (2/5), [3/3], etc.
fn extract_position_index(title: &str) -> Option<usize> {
    let re = regex::Regex::new(r"[\[\(](\d+)/(\d+)[\]\)]").ok()?;
    let caps = re.captures(title)?;
    caps.get(1)?.as_str().parse::<usize>().ok()
}

/// Detect convention-based stacks: PRs sharing the same ticket key
/// with [N/M] position markers in the title
fn detect_convention_stacks(all_prs: &[StackedPR], already_used: &HashSet<(String, u64)>) -> Vec<Stack> {
    // Group PRs by (repo, ticket_key) that have [N/M] markers
    let mut ticket_groups: HashMap<(String, String), Vec<&StackedPR>> = HashMap::new();

    for pr in all_prs {
        if already_used.contains(&(pr.repo.clone(), pr.number)) {
            continue;
        }

        // Try to extract ticket key from branch name first, then title
        let ticket_key = extract_ticket_key(&pr.head_branch)
            .or_else(|| extract_ticket_key(&pr.title));

        if let Some(key) = ticket_key {
            // Only include PRs that have a position marker [N/M] or are in a group with them
            ticket_groups
                .entry((pr.repo.clone(), key))
                .or_default()
                .push(pr);
        }
    }

    let mut stacks = Vec::new();

    for ((repo, ticket_key), mut prs) in ticket_groups {
        // Only consider groups with 2+ PRs
        if prs.len() < 2 {
            continue;
        }

        // Check if at least one PR has a [N/M] marker, or all share the same base branch
        let has_position_marker = prs.iter().any(|p| extract_position_index(&p.title).is_some());
        let all_same_base = prs.windows(2).all(|w| w[0].base_branch == w[1].base_branch);

        if !has_position_marker && !all_same_base {
            continue;
        }

        // Sort by position marker if available, otherwise by PR number
        prs.sort_by(|a, b| {
            let pos_a = extract_position_index(&a.title).unwrap_or(0);
            let pos_b = extract_position_index(&b.title).unwrap_or(0);
            pos_a.cmp(&pos_b).then_with(|| a.number.cmp(&b.number))
        });

        let stacked_prs: Vec<StackedPR> = prs.into_iter().cloned().collect();
        let base_branch = stacked_prs
            .first()
            .map(|p| p.base_branch.clone())
            .unwrap_or_default();

        // Include ticket key in the stack for display
        stacks.push(build_stack_with_key(
            &repo,
            &base_branch,
            &ticket_key,
            stacked_prs,
            StackKind::Convention,
        ));
    }

    stacks
}

fn build_stack(repo: &str, base_branch: &str, prs: Vec<StackedPR>, kind: StackKind) -> Stack {
    let mut stack = Stack {
        base_branch: base_branch.to_string(),
        repo: repo.to_string(),
        prs,
        kind,
    };
    for (i, pr) in stack.prs.iter_mut().enumerate() {
        pr.position = i + 1;
    }
    stack
}

fn build_stack_with_key(
    repo: &str,
    base_branch: &str,
    _ticket_key: &str,
    prs: Vec<StackedPR>,
    kind: StackKind,
) -> Stack {
    // For now, same as build_stack — ticket_key can be used for display later
    build_stack(repo, base_branch, prs, kind)
}

pub fn render_stacks(stacks: &[Stack]) -> String {
    use colored::*;

    let mut output = String::new();

    if stacks.is_empty() {
        output.push_str("\n🔍 No stacked PRs detected.\n");
        output.push_str("Tip: Stacks are detected via:\n");
        output.push_str("  • Branch chaining: PR B targets PR A's head branch\n");
        output.push_str("  • Convention: same ticket key (e.g. TAHC-1666) with [N/M] markers\n");
        return output;
    }

    output.push_str(&format!("\n📦 Found {} stack(s)\n\n", stacks.len()));

    for stack in stacks {
        let kind_label = match stack.kind {
            StackKind::BranchChain => "branch-chain",
            StackKind::Convention => "convention",
        };
        output.push_str(&format!(
            "┌─ Stack on `{}` ({} PRs, {})\n",
            stack.base_branch, stack.prs.len(), kind_label
        ));

        for pr in &stack.prs {
            let prefix = if pr.position == 1 { "🔵" } else { "  " };
            let draft_tag = if pr.draft { " [DRAFT]" } else { "" };
            output.push_str(&format!(
                "{} [{}/{}] #{} - {}{}\n  └─ {} \n    {}\n",
                prefix,
                pr.position,
                stack.prs.len(),
                pr.number,
                pr.title.bold(),
                draft_tag,
                pr.head_branch.dimmed(),
                pr.url.blue().underline()
            ));
        }
        output.push('\n');
    }

    output
}