Skip to main content

gitpoulpi_hosting/
refs.rs

1//! References in commit messages and branch names (plan D7).
2//!
3//! Pure: what a token means depends on the repository's linked ISSUES and
4//! PULLS families, handed in as a [`RefContext`]. Unlinked means nothing is
5//! a link. The TUI and the CLI both scan through here, so they cannot
6//! disagree about what `ABC-12` or `#7` refers to.
7//!
8//! Offsets are char offsets into the scanned text, `[start, end)`.
9
10use gitpoulpi_config::ApiFamily;
11
12use crate::spec::spec_for;
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15pub enum RefKind {
16    Issue,
17    PullRequest,
18}
19
20/// One reference found in a message or branch name.
21#[derive(Debug, Clone, PartialEq, Eq)]
22pub struct FoundRef {
23    pub start: usize,
24    pub end: usize,
25    /// The text as written (`AB#7`, `#7`, `ABC-12`).
26    pub text: String,
27    pub kind: RefKind,
28    /// The canonical spelling a human would type (`#7`, `AB#7`, `!4`, `ABC-12`).
29    pub reference: String,
30    pub url: Option<String>,
31}
32
33/// Trackers whose items are `KEY-N` (key of a project the user linked).
34trait KeyTracker {
35    fn is_key_tracker(&self) -> bool;
36}
37
38impl KeyTracker for ApiFamily {
39    fn is_key_tracker(&self) -> bool {
40        matches!(
41            self,
42            ApiFamily::Jira | ApiFamily::Linear | ApiFamily::YouTrack
43        )
44    }
45}
46
47/// A Trello card known from the cached snapshot.
48#[derive(Debug, Clone, PartialEq, Eq)]
49pub struct KnownCard {
50    pub id_short: u64,
51    pub url: String,
52}
53
54/// The linked issue tracker.
55#[derive(Debug, Clone, PartialEq, Eq)]
56pub struct IssueLinker {
57    pub family: ApiFamily,
58    pub web_base: String,
59    /// The linked project (Jira, Linear, YouTrack: its key).
60    pub project: String,
61    /// Jira, Linear, YouTrack: further project keys seen in the profile's cached project list.
62    pub project_keys: Vec<String>,
63    /// Trello: the cached cards (for `#N` and `N-slug`).
64    pub cards: Vec<KnownCard>,
65}
66
67/// The linked pull-request source.
68#[derive(Debug, Clone, PartialEq, Eq)]
69pub struct PullLinker {
70    pub family: ApiFamily,
71    pub web_base: String,
72    pub project: String,
73}
74
75/// From the repository's two links; both `None` = nothing is a link.
76#[derive(Debug, Clone, Default, PartialEq, Eq)]
77pub struct RefContext {
78    pub issues: Option<IssueLinker>,
79    pub pulls: Option<PullLinker>,
80}
81
82/// Digits of a number token: 1 to 9 of them.
83const MAX_DIGITS: usize = 9;
84
85fn is_word(c: char) -> bool {
86    c.is_alphanumeric() || c == '_'
87}
88
89/// The run of ASCII digits at `from`, as `(end, value)`.
90fn digits_at(chars: &[char], from: usize) -> Option<(usize, u64)> {
91    let mut j = from;
92    while j < chars.len() && chars[j].is_ascii_digit() {
93        j += 1;
94    }
95    if j == from || j - from > MAX_DIGITS {
96        return None;
97    }
98    let n = chars[from..j].iter().collect::<String>().parse().ok()?;
99    Some((j, n))
100}
101
102fn slice(chars: &[char], a: usize, b: usize) -> String {
103    chars[a..b].iter().collect()
104}
105
106impl IssueLinker {
107    fn jira_key_known(&self, key: &str) -> bool {
108        key == self.project || self.project_keys.iter().any(|k| k == key)
109    }
110
111    fn card_url(&self, id_short: u64) -> Option<&str> {
112        self.cards
113            .iter()
114            .find(|c| c.id_short == id_short)
115            .map(|c| c.url.as_str())
116    }
117
118    /// Does this tracker read a bare `#N` as one of its items?
119    fn takes_hash(&self, n: u64) -> bool {
120        match self.family {
121            ApiFamily::Github
122            | ApiFamily::Gitlab
123            | ApiFamily::AzureDevops
124            | ApiFamily::Gitea
125            | ApiFamily::Redmine
126            | ApiFamily::LocalMarkdown => true,
127            ApiFamily::Trello => self.card_url(n).is_some(),
128            _ => false,
129        }
130    }
131}
132
133struct Matcher<'a> {
134    ctx: &'a RefContext,
135}
136
137impl Matcher<'_> {
138    fn azure_issues(&self) -> Option<&IssueLinker> {
139        self.ctx
140            .issues
141            .as_ref()
142            .filter(|i| i.family == ApiFamily::AzureDevops)
143    }
144
145    fn issue_number_ref(&self, issues: &IssueLinker, n: u64) -> (String, Option<String>) {
146        match issues.family {
147            ApiFamily::AzureDevops => {
148                // `#N` is ambiguous when the pull requests also use `#N`.
149                let pulls_azure = self
150                    .ctx
151                    .pulls
152                    .as_ref()
153                    .is_some_and(|p| p.family == ApiFamily::AzureDevops);
154                let reference = if pulls_azure {
155                    format!("#{n}")
156                } else {
157                    format!("AB#{n}")
158                };
159                (reference, self.issue_url(issues, n))
160            }
161            ApiFamily::Trello => {
162                let url = issues.card_url(n).map(str::to_string);
163                (url.clone().unwrap_or_default(), url)
164            }
165            _ => (format!("#{n}"), self.issue_url(issues, n)),
166        }
167    }
168
169    fn issue_url(&self, issues: &IssueLinker, n: u64) -> Option<String> {
170        (spec_for(issues.family).issue_web_url)(&issues.web_base, &issues.project, &n.to_string())
171    }
172
173    /// A reference starting at `i`, as `(end, found)`.
174    fn at(&self, chars: &[char], i: usize) -> Option<(usize, FoundRef)> {
175        let c = chars[i];
176        let prev = i.checked_sub(1).map(|p| chars[p]);
177        let boundary = prev.is_none_or(|p| !is_word(p) && p != '&');
178        let issues = self.ctx.issues.as_ref();
179        let make = |end: usize, kind, reference: String, url: Option<String>| FoundRef {
180            start: i,
181            end,
182            text: slice(chars, i, end),
183            kind,
184            reference,
185            url,
186        };
187
188        // Trello card URL.
189        if c == 'h' && boundary && issues.is_some_and(|t| t.family == ApiFamily::Trello) {
190            {
191                let rest = slice(chars, i, chars.len().min(i + 25));
192                let prefix = "https://trello.com/c/";
193                if rest.starts_with(prefix) {
194                    let mut j = i + prefix.len();
195                    let key_start = j;
196                    while j < chars.len() && chars[j].is_ascii_alphanumeric() {
197                        j += 1;
198                    }
199                    if j > key_start {
200                        let short = slice(chars, key_start, j);
201                        // Optional `/12-slug` tail.
202                        let mut end = j;
203                        if chars.get(j) == Some(&'/') {
204                            let mut k = j + 1;
205                            while k < chars.len()
206                                && (chars[k].is_ascii_alphanumeric() || chars[k] == '-')
207                            {
208                                k += 1;
209                            }
210                            if k > j + 1 {
211                                end = k;
212                            }
213                        }
214                        let url = format!("https://trello.com/c/{short}");
215                        return Some((end, make(end, RefKind::Issue, url.clone(), Some(url))));
216                    }
217                }
218            }
219        }
220
221        // Jira, Linear and YouTrack `KEY-N`.
222        if c.is_ascii_uppercase() && prev.is_none_or(|p| !is_word(p) && p != '-') {
223            if let Some(j) = issues.filter(|j| j.family.is_key_tracker()) {
224                let mut k = i;
225                while k < chars.len()
226                    && (chars[k].is_ascii_uppercase()
227                        || chars[k].is_ascii_digit()
228                        || chars[k] == '_')
229                {
230                    k += 1;
231                }
232                if chars.get(k) == Some(&'-') {
233                    let key = slice(chars, i, k);
234                    if let Some((end, _)) = digits_at(chars, k + 1) {
235                        if j.jira_key_known(&key) && chars.get(end).is_none_or(|n| !is_word(*n)) {
236                            let reference = slice(chars, i, end);
237                            let url = (spec_for(j.family).issue_web_url)(
238                                &j.web_base,
239                                &j.project,
240                                &reference,
241                            );
242                            return Some((end, make(end, RefKind::Issue, reference, url)));
243                        }
244                    }
245                }
246            }
247        }
248
249        // Azure `AB#N`.
250        if c == 'A' && boundary && chars.get(i + 1) == Some(&'B') && chars.get(i + 2) == Some(&'#')
251        {
252            if let Some(az) = self.azure_issues() {
253                if let Some((end, n)) = digits_at(chars, i + 3) {
254                    if chars.get(end).is_none_or(|n| !is_word(*n)) {
255                        let (reference, url) = self.issue_number_ref(az, n);
256                        return Some((end, make(end, RefKind::Issue, reference, url)));
257                    }
258                }
259            }
260        }
261
262        // `#N` and `!N`.
263        if (c == '#' || c == '!') && boundary {
264            let (end, n) = digits_at(chars, i + 1)?;
265            if chars.get(end).is_some_and(|n| is_word(*n)) {
266                return None;
267            }
268            if c == '#' {
269                if let Some(is) = issues.filter(|is| is.takes_hash(n)) {
270                    let (reference, url) = self.issue_number_ref(is, n);
271                    return Some((end, make(end, RefKind::Issue, reference, url)));
272                }
273            }
274            if let Some(p) = self.ctx.pulls.as_ref() {
275                if spec_for(p.family).pr_sigil == c {
276                    let url = (spec_for(p.family).pr_web_url)(&p.web_base, &p.project, n);
277                    return Some((end, make(end, RefKind::PullRequest, format!("{c}{n}"), url)));
278                }
279            }
280        }
281        None
282    }
283}
284
285/// Every reference in a commit message, in order, that the linked sources
286/// give a meaning to.
287pub fn scan_message(text: &str, ctx: &RefContext) -> Vec<FoundRef> {
288    if ctx.issues.is_none() && ctx.pulls.is_none() {
289        return Vec::new();
290    }
291    let chars: Vec<char> = text.chars().collect();
292    let matcher = Matcher { ctx };
293    let mut out = Vec::new();
294    let mut i = 0;
295    while i < chars.len() {
296        match matcher.at(&chars, i) {
297            Some((end, found)) => {
298                out.push(found);
299                i = end;
300            }
301            None => i += 1,
302        }
303    }
304    out
305}
306
307/// The issue a branch name names: `ABC-123-x` / `feature/ABC-123` (Jira),
308/// `123-slug` (the other trackers; Trello through its cached cards).
309pub fn from_branch(branch: &str, ctx: &RefContext) -> Option<FoundRef> {
310    let issues = ctx.issues.as_ref()?;
311    let chars: Vec<char> = branch.chars().collect();
312    let matcher = Matcher { ctx };
313    if issues.family.is_key_tracker() {
314        let mut i = 0;
315        while i < chars.len() {
316            if let Some((_, found)) = matcher.at(&chars, i) {
317                return Some(found);
318            }
319            i += 1;
320        }
321        return None;
322    }
323    let (end, n) = digits_at(&chars, 0)?;
324    if chars.get(end) != Some(&'-') || chars.len() == end + 1 {
325        return None;
326    }
327    if issues.family == ApiFamily::Trello && issues.card_url(n).is_none() {
328        return None;
329    }
330    if !matches!(
331        issues.family,
332        ApiFamily::Github
333            | ApiFamily::Gitlab
334            | ApiFamily::AzureDevops
335            | ApiFamily::Trello
336            | ApiFamily::Gitea
337            | ApiFamily::Redmine
338            | ApiFamily::LocalMarkdown
339    ) {
340        return None;
341    }
342    let (reference, url) = matcher.issue_number_ref(issues, n);
343    Some(FoundRef {
344        start: 0,
345        end,
346        text: slice(&chars, 0, end),
347        kind: RefKind::Issue,
348        reference,
349        url,
350    })
351}
352
353#[cfg(test)]
354mod tests {
355    use super::*;
356
357    fn issues(family: ApiFamily, base: &str, project: &str) -> IssueLinker {
358        IssueLinker {
359            family,
360            web_base: base.into(),
361            project: project.into(),
362            project_keys: Vec::new(),
363            cards: Vec::new(),
364        }
365    }
366
367    fn pulls(family: ApiFamily, base: &str, project: &str) -> PullLinker {
368        PullLinker {
369            family,
370            web_base: base.into(),
371            project: project.into(),
372        }
373    }
374
375    fn gl() -> RefContext {
376        RefContext {
377            issues: Some(issues(ApiFamily::Gitlab, "https://gl", "g/p")),
378            pulls: Some(pulls(ApiFamily::Gitlab, "https://gl", "g/p")),
379        }
380    }
381
382    fn jira() -> RefContext {
383        let mut i = issues(ApiFamily::Jira, "https://acme.atlassian.net", "ABC");
384        i.project_keys = vec!["ABC".into(), "OPS".into()];
385        RefContext {
386            issues: Some(i),
387            pulls: None,
388        }
389    }
390
391    fn azure(pull: Option<PullLinker>) -> RefContext {
392        RefContext {
393            issues: Some(issues(
394                ApiFamily::AzureDevops,
395                "https://dev.azure.com/o",
396                "P",
397            )),
398            pulls: pull,
399        }
400    }
401
402    fn trello() -> RefContext {
403        let mut i = issues(ApiFamily::Trello, "https://trello.com", "board");
404        i.cards = vec![KnownCard {
405            id_short: 7,
406            url: "https://trello.com/c/Ab12CdEf".into(),
407        }];
408        RefContext {
409            issues: Some(i),
410            pulls: None,
411        }
412    }
413
414    /// `(text, reference, url)` of every found ref.
415    fn found(text: &str, ctx: &RefContext) -> Vec<(String, String, Option<String>)> {
416        scan_message(text, ctx)
417            .into_iter()
418            .map(|f| (f.text, f.reference, f.url))
419            .collect()
420    }
421
422    fn texts(text: &str, ctx: &RefContext) -> Vec<String> {
423        scan_message(text, ctx)
424            .into_iter()
425            .map(|f| f.text)
426            .collect()
427    }
428
429    #[test]
430    fn github_and_gitlab_hash_numbers() {
431        let ctx = RefContext {
432            issues: Some(issues(ApiFamily::Github, "https://github.com", "o/r")),
433            pulls: None,
434        };
435        let f = scan_message("fix (#12), see #7.", &ctx);
436        assert_eq!(f.len(), 2);
437        assert_eq!(f[0].text, "#12");
438        assert_eq!(f[0].kind, RefKind::Issue);
439        assert_eq!(
440            f[0].url.as_deref(),
441            Some("https://github.com/o/r/issues/12")
442        );
443        assert_eq!((f[0].start, f[0].end), (5, 8));
444    }
445
446    #[test]
447    fn gitlab_issue_and_merge_request() {
448        let f = found("Closes #4242, see also !4343.", &gl());
449        assert_eq!(
450            f,
451            vec![
452                (
453                    "#4242".to_string(),
454                    "#4242".to_string(),
455                    Some("https://gl/g/p/-/issues/4242".to_string())
456                ),
457                (
458                    "!4343".to_string(),
459                    "!4343".to_string(),
460                    Some("https://gl/g/p/-/merge_requests/4343".to_string())
461                ),
462            ]
463        );
464        assert_eq!(scan_message("!1", &gl())[0].kind, RefKind::PullRequest);
465    }
466
467    #[test]
468    fn github_pulls_alone_claim_hash() {
469        let ctx = RefContext {
470            issues: None,
471            pulls: Some(pulls(ApiFamily::Github, "https://github.com", "o/r")),
472        };
473        let f = scan_message("merge #5 not !5", &ctx);
474        assert_eq!(f.len(), 1);
475        assert_eq!(f[0].kind, RefKind::PullRequest);
476        assert_eq!(f[0].url.as_deref(), Some("https://github.com/o/r/pull/5"));
477    }
478
479    #[test]
480    fn issues_win_over_pulls_on_hash() {
481        let ctx = RefContext {
482            issues: Some(issues(ApiFamily::Github, "https://github.com", "o/r")),
483            pulls: Some(pulls(ApiFamily::Github, "https://github.com", "o/r")),
484        };
485        let f = scan_message("#5", &ctx);
486        assert_eq!(f[0].kind, RefKind::Issue);
487        assert_eq!(f[0].url.as_deref(), Some("https://github.com/o/r/issues/5"));
488    }
489
490    #[cfg(feature = "jira")]
491    #[test]
492    fn jira_keys_only_for_known_projects() {
493        let f = found("Fixes ABC-12, see UTF-8, OPS-3.", &jira());
494        assert_eq!(
495            f,
496            vec![
497                (
498                    "ABC-12".to_string(),
499                    "ABC-12".to_string(),
500                    Some("https://acme.atlassian.net/browse/ABC-12".to_string())
501                ),
502                (
503                    "OPS-3".to_string(),
504                    "OPS-3".to_string(),
505                    Some("https://acme.atlassian.net/browse/OPS-3".to_string())
506                ),
507            ]
508        );
509    }
510
511    #[test]
512    fn jira_negatives() {
513        for text in [
514            "UTF-8", "SHA-256", "ISO-8601", "abc-12", "XABC-12", "ABC-12x", "ABC-", "ABC_12", "#12",
515        ] {
516            assert!(texts(text, &jira()).is_empty(), "{text}");
517        }
518        // The linked key counts even without a cached project list.
519        let mut ctx = jira();
520        ctx.issues.as_mut().unwrap().project_keys.clear();
521        assert_eq!(texts("ABC-1 OPS-2", &ctx), vec!["ABC-1"]);
522    }
523
524    #[cfg(feature = "gitea")]
525    #[test]
526    fn gitea_hash_numbers_link_to_issues_and_pulls_and_branches_start_with_the_number() {
527        let ctx = RefContext {
528            issues: Some(issues(ApiFamily::Gitea, "https://codeberg.org", "o/r")),
529            pulls: Some(pulls(ApiFamily::Gitea, "https://codeberg.org", "o/r")),
530        };
531        let f = scan_message("fix #12, see #7.", &ctx);
532        assert_eq!(f.len(), 2);
533        // The issue tracker wins a bare `#N`, as on GitHub (the same URL
534        // works for a pull request, Gitea redirects).
535        assert_eq!(
536            f[0].url.as_deref(),
537            Some("https://codeberg.org/o/r/issues/12")
538        );
539        let branch = from_branch("12-fix-login", &ctx).expect("a branch reference");
540        assert_eq!(
541            branch.url.as_deref(),
542            Some("https://codeberg.org/o/r/issues/12")
543        );
544    }
545
546    #[test]
547    fn hash_negatives_for_every_hash_family() {
548        for text in [
549            "abc#1",
550            "&#39;",
551            "#1a2b3c",
552            "color #fff",
553            "# 12",
554            "#",
555            "#12abc",
556        ] {
557            assert!(texts(text, &gl()).is_empty(), "{text}");
558        }
559        assert!(texts("wow! !important", &gl()).is_empty());
560    }
561
562    #[cfg(feature = "azure-devops")]
563    #[test]
564    fn azure_boards_hash_and_ab_hash() {
565        let ctx = azure(None);
566        let f = scan_message("Fixes AB#7 and #8", &ctx);
567        assert_eq!(f.len(), 2);
568        assert_eq!(f[0].text, "AB#7");
569        assert_eq!((f[0].start, f[0].end), (6, 10));
570        assert_eq!(
571            f[0].url.as_deref(),
572            Some("https://dev.azure.com/o/P/_workitems/edit/7")
573        );
574        assert_eq!(f[1].text, "#8");
575        // Not glued to a word.
576        assert!(texts("XAB#7 aAB#7", &ctx).is_empty());
577    }
578
579    #[test]
580    fn azure_reference_spelling_depends_on_the_pulls_source() {
581        // Azure Repos pulls use `!N`, so `#N` is unambiguous.
582        let own = azure(Some(pulls(
583            ApiFamily::AzureDevops,
584            "https://dev.azure.com/o",
585            "P/R",
586        )));
587        assert_eq!(scan_message("AB#7", &own)[0].reference, "#7");
588        // A pulls source elsewhere (GitHub `#N`) makes `AB#N` the safe spelling.
589        let other = azure(Some(pulls(ApiFamily::Github, "https://github.com", "o/r")));
590        let f = scan_message("#7", &other);
591        assert_eq!(f[0].reference, "AB#7");
592        assert_eq!(f[0].kind, RefKind::Issue);
593        assert_eq!(scan_message("#7", &azure(None))[0].reference, "AB#7");
594    }
595
596    #[test]
597    fn ab_hash_is_for_azure_issues_only() {
598        assert!(texts("AB#7", &gl()).is_empty());
599        assert!(texts("AB#7", &jira()).is_empty());
600        assert!(texts("AB#7", &trello()).is_empty());
601        let github = RefContext {
602            issues: Some(issues(ApiFamily::Github, "https://github.com", "o/r")),
603            pulls: None,
604        };
605        assert!(texts("AB#7", &github).is_empty());
606    }
607
608    #[cfg(feature = "azure-devops")]
609    #[test]
610    fn azure_pulls_use_bang() {
611        let ctx = RefContext {
612            issues: None,
613            pulls: Some(pulls(
614                ApiFamily::AzureDevops,
615                "https://dev.azure.com/o",
616                "P/R",
617            )),
618        };
619        let f = scan_message("see !12 and #3", &ctx);
620        assert_eq!(f.len(), 1);
621        assert_eq!(
622            f[0].url.as_deref(),
623            Some("https://dev.azure.com/o/P/_git/R/pullrequest/12")
624        );
625    }
626
627    #[cfg(feature = "bitbucket")]
628    #[test]
629    fn bitbucket_pulls_use_hash() {
630        let ctx = RefContext {
631            issues: None,
632            pulls: Some(pulls(ApiFamily::Bitbucket, "https://bitbucket.org", "w/r")),
633        };
634        let f = scan_message("PR #4 not !4", &ctx);
635        assert_eq!(f.len(), 1);
636        assert_eq!(
637            f[0].url.as_deref(),
638            Some("https://bitbucket.org/w/r/pull-requests/4")
639        );
640    }
641
642    #[test]
643    fn jira_issues_leave_hash_to_pulls() {
644        let mut ctx = jira();
645        ctx.pulls = Some(pulls(ApiFamily::Github, "https://github.com", "o/r"));
646        let f = scan_message("ABC-1 via #9", &ctx);
647        assert_eq!(f.len(), 2);
648        assert_eq!(f[1].kind, RefKind::PullRequest);
649    }
650
651    #[test]
652    fn trello_urls_and_known_hash_numbers() {
653        let ctx = trello();
654        let f = scan_message(
655            "see https://trello.com/c/Zz9Yy8Xx/12-some-slug. and #7, not #8",
656            &ctx,
657        );
658        assert_eq!(f.len(), 2);
659        assert_eq!(f[0].text, "https://trello.com/c/Zz9Yy8Xx/12-some-slug");
660        assert_eq!(f[0].url.as_deref(), Some("https://trello.com/c/Zz9Yy8Xx"));
661        assert_eq!(f[1].text, "#7");
662        assert_eq!(f[1].url.as_deref(), Some("https://trello.com/c/Ab12CdEf"));
663    }
664
665    #[test]
666    fn unlinked_is_plain_text() {
667        let ctx = RefContext::default();
668        assert!(scan_message("#1 !2 ABC-3 AB#4", &ctx).is_empty());
669        assert_eq!(from_branch("123-x", &ctx), None);
670    }
671
672    #[test]
673    fn offsets_are_char_offsets() {
674        let f = scan_message("é #5", &gl());
675        assert_eq!((f[0].start, f[0].end), (2, 4));
676    }
677
678    #[test]
679    fn branch_forms() {
680        let b = |name: &str, ctx: &RefContext| from_branch(name, ctx).map(|f| f.reference);
681        assert_eq!(b("ABC-123-x", &jira()).as_deref(), Some("ABC-123"));
682        assert_eq!(b("feature/ABC-123", &jira()).as_deref(), Some("ABC-123"));
683        assert_eq!(
684            b("feature/ABC-123-fix-login", &jira()).as_deref(),
685            Some("ABC-123")
686        );
687        assert_eq!(b("main", &jira()), None);
688        assert_eq!(b("utf-8-fix", &jira()), None);
689        assert_eq!(b("123-slug", &gl()).as_deref(), Some("#123"));
690        let f = from_branch("123-slug", &gl()).unwrap();
691        assert_eq!(f.url.as_deref(), Some("https://gl/g/p/-/issues/123"));
692        assert_eq!((f.start, f.end), (0, 3));
693        assert_eq!(b("123", &gl()), None);
694        assert_eq!(b("fix-123", &gl()), None);
695        // No `AB#` in a branch name; Azure uses N-slug.
696        assert_eq!(b("123-slug", &azure(None)).as_deref(), Some("AB#123"));
697        assert_eq!(b("AB#123", &azure(None)), None);
698        // Trello: idShort through the cached cards.
699        assert_eq!(
700            b("7-fix-login", &trello()).as_deref(),
701            Some("https://trello.com/c/Ab12CdEf")
702        );
703        assert_eq!(b("8-fix-login", &trello()), None);
704    }
705}