ghpending 0.5.2

CLI to watch GitHub repos for open issues and pull requests at a glance
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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
use std::cmp::Ordering;
use std::collections::{HashMap, HashSet};
use std::time::Duration;

use anyhow::{Context, Result};
use chrono::{DateTime, Utc};
use futures::stream::{FuturesUnordered, StreamExt};
use octocrab::Octocrab;
use serde::Deserialize;
use thiserror::Error;
use tokio::time::{self, timeout};

const FETCH_TIMEOUT: Duration = Duration::from_secs(30);
const MAX_CONCURRENT_FETCHES: usize = 4;

#[derive(Debug, Clone)]
pub struct RepoItem {
    pub kind: ItemKind,
    pub number: u64,
    pub title: String,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
    pub author: String,
    pub pr_draft: Option<bool>,
}

#[derive(Debug, Clone, PartialEq)]
pub enum ItemKind {
    PullRequest,
    Issue,
}

pub type SubscribedItems = HashMap<String, HashSet<u64>>;

#[derive(Debug, Clone)]
pub struct RepoResult {
    pub repo: String,
    pub status: RepoStatus,
}

#[derive(Debug, Clone)]
pub enum RepoStatus {
    Items(Vec<RepoItem>),
    NotFound,
    Error(RepoError),
}

#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum RepoError {
    #[error("timeout after 30s")]
    Timeout,
    #[error("{0}")]
    Api(String),
}

#[derive(Debug, Error)]
pub enum GithubError {
    #[error("repo not found: {0}")]
    NotFound(String),
    #[error("api error: {0}")]
    Api(#[from] octocrab::Error),
}

/// Maps an octocrab result to `GithubError`, treating HTTP 404 as `NotFound`.
fn map_github_err<T>(
    res: std::result::Result<T, octocrab::Error>,
    repo_label: &str,
) -> std::result::Result<T, GithubError> {
    match res {
        Ok(v) => Ok(v),
        Err(octocrab::Error::GitHub { source, .. }) if source.status_code.as_u16() == 404 => {
            Err(GithubError::NotFound(repo_label.to_owned()))
        }
        Err(e) => Err(GithubError::Api(e)),
    }
}

/// Turns an octocrab error into a message with real detail instead of
/// octocrab's `Display`, which collapses `Error::GitHub` down to just
/// "GitHub".
pub(crate) fn describe_api_error(e: &octocrab::Error) -> String {
    match e {
        octocrab::Error::GitHub { source, .. } => {
            let mut message = format!("HTTP {} {}", source.status_code, source.message);
            if source.status_code.as_u16() == 401 {
                message.push_str(" — set GITHUB_TOKEN");
            }
            message
        }
        other => other.to_string(),
    }
}

pub fn split_repo(s: &str) -> Option<(&str, &str)> {
    let (owner, name) = s.split_once('/')?;

    if owner.is_empty() || name.is_empty() {
        return None;
    }
    Some((owner, name))
}

/// Whether a GitHub account is a personal user or an organization.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AccountKind {
    User,
    Organization,
}

/// Where `add` should pull the candidate repo list from.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ListSource {
    /// Everything the token can reach (owned + collaborator + org member),
    /// private included. Used for `--all` and for listing your own account.
    Authenticated,
    /// A specific org's repos (private included when the token is a member).
    Org(String),
    /// A third-party user's public repos — all we can see for someone else.
    PublicUser(String),
}

/// Maps the `type` field of a GitHub account profile to an `AccountKind`.
/// Anything that is not exactly "Organization" is treated as a user.
pub fn account_kind_from_type(profile_type: &str) -> AccountKind {
    if profile_type == "Organization" {
        AccountKind::Organization
    } else {
        AccountKind::User
    }
}

/// Decides which `ListSource` `add` should use.
///
/// - `all`: the `--all` flag was passed.
/// - `username`: the resolved target (`None` when `--all`).
/// - `auth_login`: the login the token authenticates as.
/// - `kind`: whether `username` is a user or org (`None` when `--all`).
pub fn resolve_list_source(
    all: bool,
    username: Option<&str>,
    auth_login: &str,
    kind: Option<AccountKind>,
) -> ListSource {
    if all {
        return ListSource::Authenticated;
    }
    let Some(username) = username else {
        return ListSource::Authenticated;
    };
    if username.eq_ignore_ascii_case(auth_login) {
        return ListSource::Authenticated;
    }
    match kind {
        Some(AccountKind::Organization) => ListSource::Org(username.to_owned()),
        _ => ListSource::PublicUser(username.to_owned()),
    }
}

pub fn item_cmp(a: &RepoItem, b: &RepoItem) -> Ordering {
    match (&a.kind, &b.kind) {
        (ItemKind::PullRequest, ItemKind::Issue) => Ordering::Less,
        (ItemKind::Issue, ItemKind::PullRequest) => Ordering::Greater,
        _ => b.number.cmp(&a.number),
    }
}

pub async fn list_user_repos(crab: &Octocrab, username: &str) -> Result<Vec<String>> {
    let first_page = crab
        .users(username)
        .repos()
        .r#type(octocrab::params::users::repos::Type::Owner)
        .per_page(100)
        .send()
        .await
        .context("listing user repositories")?;

    let all_pages = crab
        .all_pages(first_page)
        .await
        .context("paginating user repositories")?;

    let mut names: Vec<String> = all_pages.into_iter().filter_map(|r| r.full_name).collect();
    names.sort();
    Ok(names)
}

/// Lists every repo the token can reach — owned, collaborator and
/// organization-member, private included. Backs `add --all` and listing
/// your own account.
pub async fn list_authenticated_repos(crab: &Octocrab) -> Result<Vec<String>> {
    let first_page = crab
        .current()
        .list_repos_for_authenticated_user()
        .visibility("all")
        .affiliation("owner,collaborator,organization_member")
        .per_page(100)
        .send()
        .await
        .context("listing repositories for the authenticated user")?;

    let all_pages = crab
        .all_pages(first_page)
        .await
        .context("paginating authenticated repositories")?;

    let mut names: Vec<String> = all_pages.into_iter().filter_map(|r| r.full_name).collect();
    names.sort();
    names.dedup();
    Ok(names)
}

/// Lists an org's repos, private included when the token is a member.
pub async fn list_org_repos(crab: &Octocrab, org: &str) -> Result<Vec<String>> {
    let first_page = crab
        .orgs(org)
        .list_repos()
        .repo_type(octocrab::params::repos::Type::All)
        .per_page(100)
        .send()
        .await
        .context("listing organization repositories")?;

    let all_pages = crab
        .all_pages(first_page)
        .await
        .context("paginating organization repositories")?;

    let mut names: Vec<String> = all_pages.into_iter().filter_map(|r| r.full_name).collect();
    names.sort();
    Ok(names)
}

/// The login the token authenticates as, or `None` when unauthenticated
/// (no token / 401) so callers can fall back to public listing.
pub async fn authenticated_login(crab: &Octocrab) -> Result<Option<String>> {
    match crab.current().user().await {
        Ok(user) => Ok(Some(user.login)),
        Err(octocrab::Error::GitHub { source, .. }) if source.status_code.as_u16() == 401 => {
            Ok(None)
        }
        Err(e) => Err(e).context("identifying the authenticated user"),
    }
}

/// Whether `username` is a personal user or an organization.
pub async fn account_kind(crab: &Octocrab, username: &str) -> Result<AccountKind> {
    let profile = crab
        .users(username)
        .profile()
        .await
        .with_context(|| format!("fetching profile for {username}"))?;
    Ok(account_kind_from_type(&profile.r#type))
}

/// Resolves which `ListSource` to use for a concrete target, querying GitHub
/// for the authenticated login and the target's account kind as needed.
pub async fn resolve_source_for(crab: &Octocrab, username: &str) -> Result<ListSource> {
    let auth_login = authenticated_login(crab).await?;
    if let Some(login) = &auth_login
        && login.eq_ignore_ascii_case(username)
    {
        return Ok(ListSource::Authenticated);
    }
    let kind = account_kind(crab, username).await?;
    Ok(resolve_list_source(
        false,
        Some(username),
        auth_login.as_deref().unwrap_or(""),
        Some(kind),
    ))
}

#[derive(Debug, Deserialize)]
struct SubscribedIssue {
    number: u64,
    repository_url: String,
}

pub async fn fetch_subscribed_items(crab: &Octocrab) -> Result<SubscribedItems> {
    let first_page = crab
        .get::<octocrab::Page<SubscribedIssue>, _, _>(
            "/issues?filter=subscribed&state=open&per_page=100",
            None::<&()>,
        )
        .await
        .context("listing subscribed issues and pull requests (GITHUB_TOKEN is required)")?;
    let issues = crab
        .all_pages(first_page)
        .await
        .context("paginating subscribed issues and pull requests")?;

    Ok(index_subscribed_items(issues))
}

fn index_subscribed_items(issues: Vec<SubscribedIssue>) -> SubscribedItems {
    let mut subscribed = SubscribedItems::new();
    for issue in issues {
        let Some(repo) = repo_name_from_api_url(&issue.repository_url) else {
            continue;
        };
        subscribed.entry(repo).or_default().insert(issue.number);
    }
    subscribed
}

fn repo_name_from_api_url(url: &str) -> Option<String> {
    let (_, path) = url.split_once("/repos/")?;
    let mut segments = path.split('/');
    let owner = segments.next()?;
    let name = segments.next()?;
    if owner.is_empty() || name.is_empty() {
        return None;
    }
    Some(format!("{owner}/{name}").to_ascii_lowercase())
}

pub async fn fetch_repo_items(
    crab: &Octocrab,
    repo: &str,
    subscribed_numbers: Option<&HashSet<u64>>,
) -> RepoResult {
    let Some((owner, name)) = split_repo(repo) else {
        return RepoResult {
            repo: repo.to_owned(),
            status: RepoStatus::NotFound,
        };
    };

    match fetch_items_inner(crab, owner, name, subscribed_numbers).await {
        Ok(items) => RepoResult {
            repo: repo.to_owned(),
            status: RepoStatus::Items(items),
        },
        Err(GithubError::NotFound(_)) => RepoResult {
            repo: repo.to_owned(),
            status: RepoStatus::NotFound,
        },
        Err(GithubError::Api(e)) => RepoResult {
            repo: repo.to_owned(),
            status: RepoStatus::Error(RepoError::Api(describe_api_error(&e))),
        },
    }
}

/// Fetches every repo over REST, capped at `MAX_CONCURRENT_FETCHES`
/// in-flight requests and an overall `FETCH_TIMEOUT` deadline for the whole
/// batch (repos still in flight when the deadline hits are reported as
/// timeouts). Used when there's no `GITHUB_TOKEN` — GraphQL has no
/// anonymous mode — and as the overflow fallback for repos whose open
/// issues/PRs exceed a single GraphQL page.
pub(crate) async fn fetch_repos_rest(
    crab: &Octocrab,
    repos: &[String],
    subscribed: Option<&SubscribedItems>,
) -> Vec<RepoResult> {
    let empty_subscriptions = HashSet::new();
    let mut results = vec![None; repos.len()];
    let mut in_flight = FuturesUnordered::new();
    let mut next = 0;

    while next < repos.len() && in_flight.len() < MAX_CONCURRENT_FETCHES {
        let repo = repos[next].clone();
        let repo_key = repo.to_ascii_lowercase();
        let subscribed_numbers =
            subscribed.map(|items| items.get(&repo_key).unwrap_or(&empty_subscriptions));
        in_flight.push(fetch_repo_with_timeout(
            crab,
            next,
            repo,
            subscribed_numbers,
        ));
        next += 1;
    }

    let deadline = time::sleep(FETCH_TIMEOUT);
    tokio::pin!(deadline);

    while !in_flight.is_empty() {
        tokio::select! {
            _ = &mut deadline => break,
            Some((index, result)) = in_flight.next() => {
                results[index] = Some(result);

                if next < repos.len() {
                    let repo = repos[next].clone();
                    let repo_key = repo.to_ascii_lowercase();
                    let subscribed_numbers = subscribed
                        .map(|items| items.get(&repo_key).unwrap_or(&empty_subscriptions));
                    in_flight.push(fetch_repo_with_timeout(
                        crab,
                        next,
                        repo,
                        subscribed_numbers,
                    ));
                    next += 1;
                }
            }
        }
    }

    results
        .into_iter()
        .enumerate()
        .map(|(index, result)| result.unwrap_or_else(|| timeout_result(repos[index].clone())))
        .collect()
}

async fn fetch_repo_with_timeout(
    crab: &Octocrab,
    index: usize,
    repo: String,
    subscribed_numbers: Option<&HashSet<u64>>,
) -> (usize, RepoResult) {
    let result = match timeout(
        FETCH_TIMEOUT,
        fetch_repo_items(crab, &repo, subscribed_numbers),
    )
    .await
    {
        Ok(result) => result,
        Err(_) => timeout_result(repo),
    };
    (index, result)
}

fn timeout_result(repo: String) -> RepoResult {
    RepoResult {
        repo,
        status: RepoStatus::Error(RepoError::Timeout),
    }
}

async fn fetch_items_inner(
    crab: &Octocrab,
    owner: &str,
    name: &str,
    subscribed_numbers: Option<&HashSet<u64>>,
) -> std::result::Result<Vec<RepoItem>, GithubError> {
    let label = format!("{owner}/{name}");

    let issues_handler = crab.issues(owner, name);
    let issues_future = issues_handler
        .list()
        .state(octocrab::params::State::Open)
        .per_page(100)
        .send();

    let prs_handler = crab.pulls(owner, name);
    let prs_future = prs_handler
        .list()
        .state(octocrab::params::State::Open)
        .per_page(100)
        .send();

    let (issues_res, prs_res) = futures::future::join(issues_future, prs_future).await;

    let issues_page = map_github_err(issues_res, &label)?;
    let prs_page = map_github_err(prs_res, &label)?;

    let all_issues = crab
        .all_pages(issues_page)
        .await
        .map_err(GithubError::Api)?;
    let all_prs = crab.all_pages(prs_page).await.map_err(GithubError::Api)?;

    let mut items: Vec<RepoItem> = Vec::new();

    for issue in all_issues {
        // Skip PRs that appear in the issues endpoint
        if issue.pull_request.is_some() {
            continue;
        }
        let author = issue.user.login.clone();
        let created_at = issue.created_at;
        let updated_at = issue.updated_at;
        items.push(RepoItem {
            kind: ItemKind::Issue,
            number: issue.number,
            title: issue.title,
            created_at,
            updated_at,
            author,
            pr_draft: None,
        });
    }

    for pr in all_prs {
        let author = pr.user.login.clone();
        let created_at = pr.created_at;
        let updated_at = pr.updated_at;
        let pr_draft = pr.draft;
        items.push(RepoItem {
            kind: ItemKind::PullRequest,
            number: pr.number,
            title: pr.title,
            created_at,
            updated_at,
            author,
            pr_draft,
        });
    }

    retain_subscribed(&mut items, subscribed_numbers);

    // Sort: PRs first, then issues; within each group by number descending
    items.sort_by(item_cmp);

    Ok(items)
}

pub(crate) fn retain_subscribed(
    items: &mut Vec<RepoItem>,
    subscribed_numbers: Option<&HashSet<u64>>,
) {
    if let Some(numbers) = subscribed_numbers {
        items.retain(|item| numbers.contains(&item.number));
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn make_item(kind: ItemKind, number: u64) -> RepoItem {
        RepoItem {
            kind,
            number,
            title: format!("item {number}"),
            created_at: Utc::now(),
            updated_at: Utc::now(),
            author: "user".into(),
            pr_draft: None,
        }
    }

    #[test]
    fn account_kind_organization() {
        assert_eq!(
            account_kind_from_type("Organization"),
            AccountKind::Organization
        );
    }

    #[test]
    fn account_kind_user() {
        assert_eq!(account_kind_from_type("User"), AccountKind::User);
    }

    #[test]
    fn account_kind_unknown_defaults_to_user() {
        assert_eq!(account_kind_from_type("Bot"), AccountKind::User);
    }

    #[test]
    fn all_flag_lists_authenticated() {
        assert_eq!(
            resolve_list_source(true, None, "me", None),
            ListSource::Authenticated
        );
    }

    #[test]
    fn own_username_lists_authenticated() {
        assert_eq!(
            resolve_list_source(false, Some("me"), "me", Some(AccountKind::User)),
            ListSource::Authenticated
        );
    }

    #[test]
    fn own_username_is_case_insensitive() {
        assert_eq!(
            resolve_list_source(false, Some("ME"), "me", Some(AccountKind::User)),
            ListSource::Authenticated
        );
    }

    #[test]
    fn org_target_lists_org_repos() {
        assert_eq!(
            resolve_list_source(false, Some("acme"), "me", Some(AccountKind::Organization)),
            ListSource::Org("acme".to_owned())
        );
    }

    #[test]
    fn third_party_user_lists_public_only() {
        assert_eq!(
            resolve_list_source(false, Some("octocat"), "me", Some(AccountKind::User)),
            ListSource::PublicUser("octocat".to_owned())
        );
    }

    #[test]
    fn split_repo_valid() {
        assert_eq!(split_repo("a/b"), Some(("a", "b")));
    }

    #[test]
    fn split_repo_no_slash() {
        assert_eq!(split_repo("abc"), None);
    }

    #[test]
    fn split_repo_trailing_slash() {
        assert_eq!(split_repo("a/"), None);
    }

    #[test]
    fn split_repo_leading_slash() {
        assert_eq!(split_repo("/b"), None);
    }

    #[test]
    fn split_repo_many_slashes() {
        // splitn(2) gives ("a", "b/c") — name contains a slash, which is fine
        assert_eq!(split_repo("a/b/c"), Some(("a", "b/c")));
    }

    #[test]
    fn subscribed_index_and_filter_keep_only_matching_repo_items() {
        let subscribed = index_subscribed_items(vec![
            SubscribedIssue {
                number: 7,
                repository_url: "https://api.github.com/repos/Acme/Widget".into(),
            },
            SubscribedIssue {
                number: 99,
                repository_url: "https://api.github.com/users/octocat".into(),
            },
        ]);
        let mut items = vec![
            make_item(ItemKind::Issue, 7),
            make_item(ItemKind::PullRequest, 9),
        ];

        retain_subscribed(&mut items, subscribed.get("acme/widget"));

        assert_eq!(items.len(), 1);
        assert_eq!(items[0].number, 7);
    }

    #[test]
    fn missing_subscription_filter_keeps_all_repo_items() {
        let mut items = vec![
            make_item(ItemKind::Issue, 7),
            make_item(ItemKind::PullRequest, 9),
        ];

        retain_subscribed(&mut items, None);

        assert_eq!(items.len(), 2);
    }

    #[test]
    fn item_cmp_sorts_prs_before_issues_then_number_desc() {
        let mut items = [
            make_item(ItemKind::Issue, 5),
            make_item(ItemKind::PullRequest, 2),
            make_item(ItemKind::Issue, 10),
            make_item(ItemKind::PullRequest, 8),
        ];
        items.sort_by(item_cmp);
        let numbers: Vec<u64> = items.iter().map(|i| i.number).collect();
        assert_eq!(numbers, vec![8, 2, 10, 5]);
    }
}