-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathextractors.test.ts
More file actions
852 lines (732 loc) · 28.1 KB
/
Copy pathextractors.test.ts
File metadata and controls
852 lines (732 loc) · 28.1 KB
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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
import { describe, expect, it } from "vitest";
import {
ExtractedIdentifier,
extractLinearIssueIdentifiersForCommit,
extractPullRequestNumbersForCommit,
extractRevertedIssueIdentifiersForCommit,
getRevertBranchDepth,
getRevertMessageDepth,
} from "./extractors";
import { CommitContext } from "./types";
function ids(result: ExtractedIdentifier[]): string[] {
return result.map((r) => r.identifier);
}
describe("extractLinearIssueIdentifiersForCommit", () => {
it("extracts identifiers from branch name", () => {
const commit: CommitContext = {
sha: "abc123",
branchName: "feature/ENG-123-awesome-change",
message: "Some message",
};
const result = extractLinearIssueIdentifiersForCommit(commit);
expect(ids(result)).toEqual(["ENG-123"]);
});
it("extracts identifiers from commit message with magic words", () => {
const commit: CommitContext = {
sha: "abc123",
branchName: "feature/no-key-here",
message: "Fixes PLAT-42 and ENG-7 in one go",
};
const result = extractLinearIssueIdentifiersForCommit(commit);
expect(ids(result).sort()).toEqual(["ENG-7", "PLAT-42"].sort());
});
it("deduplicates identifiers across branch and message (case-insensitive)", () => {
const commit: CommitContext = {
sha: "abc123",
branchName: "feature/eng-123-awesome-change",
message: "Fixed ENG-123, see ENG-123",
};
const result = extractLinearIssueIdentifiersForCommit(commit);
expect(ids(result)).toEqual(["ENG-123"]);
});
it("returns empty array when no identifiers are present", () => {
const commit: CommitContext = {
sha: "abc123",
branchName: "chore/update-deps",
message: "update dependencies",
};
const result = extractLinearIssueIdentifiersForCommit(commit);
expect(ids(result)).toEqual([]);
});
it("matches team keys with 1-7 alphanumeric characters", () => {
const commit: CommitContext = {
sha: "abc123",
branchName: "feature/A-1-single-char",
message: "Fixes ABCDEFG-999 and X1Y2Z3A-100",
};
const result = extractLinearIssueIdentifiersForCommit(commit);
expect(ids(result).sort()).toEqual(["A-1", "ABCDEFG-999", "X1Y2Z3A-100"].sort());
});
it("does not extract identifiers from commit message without magic words", () => {
const commit: CommitContext = {
sha: "abc123",
branchName: "feature/no-key-here",
message: "See LIN-123 for details",
};
const result = extractLinearIssueIdentifiersForCommit(commit);
expect(ids(result)).toEqual([]);
});
it("does not match team keys longer than 7 characters", () => {
const commit: CommitContext = {
sha: "abc123",
branchName: "feature/ABCDEFGH-123-too-long",
message: "Some message",
};
const result = extractLinearIssueIdentifiersForCommit(commit);
expect(ids(result)).toEqual([]);
});
});
describe("version suffix handling", () => {
// Version strings should NOT match
it.each([
["release/ios-1.57.1", []],
["release/ios-1.57.0", []],
["ruby/setup-ruby-1.269.0", []],
["dependabot/swift/dd-sdk-ios-2.9.0", []],
["bump-terraform-1.13", []],
["release/ver-1.57.01", []],
])("branch %s should yield %j", (branch, expected) => {
const result = extractLinearIssueIdentifiersForCommit({
sha: "abc",
branchName: branch,
message: null,
});
expect(ids(result)).toEqual(expected);
});
// Legitimate identifiers should match
it.each([
["john/ios-1641-introduce-a-proper-blockview", ["IOS-1641"]],
["jane/lin-47025-fix-branch-name-matching", ["LIN-47025"]],
["john/fix-lin-56696", ["LIN-56696"]],
["jane/asks/all/LIN-56081", ["LIN-56081"]],
["john/ios-1633-use-shimmer-animation", ["IOS-1633"]],
["jane/INF-530", ["INF-530"]],
])("branch %s should yield %j", (branch, expected) => {
const result = extractLinearIssueIdentifiersForCommit({
sha: "abc",
branchName: branch,
message: null,
});
expect(ids(result)).toEqual(expected);
});
});
describe("leading zero rejection", () => {
it("rejects LIN-0004 style identifiers", () => {
const result = extractLinearIssueIdentifiersForCommit({
sha: "abc",
branchName: "feature/LIN-0004-test",
message: null,
});
expect(ids(result)).toEqual([]);
});
});
describe("underscore handling", () => {
it.each([
["story/LIN-123_LIN-321_hello_world", ["LIN-123", "LIN-321"]],
["username/lin-123_branch_name", ["LIN-123"]],
])("branch %s should yield %j", (branch, expected) => {
const result = extractLinearIssueIdentifiersForCommit({
sha: "abc",
branchName: branch,
message: null,
});
expect(ids(result)).toEqual(expected);
});
});
describe("multiple identifiers", () => {
it.each([
["Fixes LIN-123 and LIN-321", ["LIN-123", "LIN-321"]],
["Closes LIN-123, LIN-321", ["LIN-123", "LIN-321"]],
])("message %s should yield %j", (message, expected) => {
const result = extractLinearIssueIdentifiersForCommit({
sha: "abc",
branchName: null,
message,
});
expect(ids(result).sort()).toEqual(expected.sort());
});
});
describe("commit message magic word behavior", () => {
it("extracts with closing keyword", () => {
const result = extractLinearIssueIdentifiersForCommit({
sha: "abc",
branchName: null,
message: "Fixes LIN-123",
});
expect(ids(result)).toEqual(["LIN-123"]);
});
it("extracts with contributing phrase", () => {
const result = extractLinearIssueIdentifiersForCommit({
sha: "abc",
branchName: null,
message: "Related to LIN-123",
});
expect(ids(result)).toEqual(["LIN-123"]);
});
it("does not extract without magic words", () => {
const result = extractLinearIssueIdentifiersForCommit({
sha: "abc",
branchName: null,
message: "See LIN-123 for details",
});
expect(ids(result)).toEqual([]);
});
it("extracts multiple keys after keyword", () => {
const result = extractLinearIssueIdentifiersForCommit({
sha: "abc",
branchName: null,
message: "Fixes LIN-123, LIN-456 and ENG-789",
});
expect(ids(result).sort()).toEqual(["ENG-789", "LIN-123", "LIN-456"]);
});
it("extracts magic word in title line", () => {
const result = extractLinearIssueIdentifiersForCommit({
sha: "abc",
branchName: null,
message: "Fix LIN-123: something",
});
expect(ids(result)).toEqual(["LIN-123"]);
});
it("does not extract key in title without keyword", () => {
const result = extractLinearIssueIdentifiersForCommit({
sha: "abc",
branchName: null,
message: "LIN-123: Fix something",
});
expect(ids(result)).toEqual([]);
});
it.each([
"close",
"closes",
"closed",
"closing",
"fix",
"fixes",
"fixed",
"fixing",
"resolve",
"resolves",
"resolved",
"resolving",
"complete",
"completes",
"completed",
"completing",
"implement",
"implements",
"implemented",
"implementing",
])("closing keyword '%s' extracts issue", (keyword) => {
const result = extractLinearIssueIdentifiersForCommit({
sha: "abc",
branchName: null,
message: `${keyword} LIN-100`,
});
expect(ids(result)).toEqual(["LIN-100"]);
});
it.each(["ref", "refs", "references", "part of", "related to", "relates to", "contributes to", "towards", "toward"])(
"contributing phrase '%s' extracts issue",
(phrase) => {
const result = extractLinearIssueIdentifiersForCommit({
sha: "abc",
branchName: null,
message: `${phrase} LIN-200`,
});
expect(ids(result)).toEqual(["LIN-200"]);
},
);
it("supports keyword with colon separator", () => {
const result = extractLinearIssueIdentifiersForCommit({
sha: "abc",
branchName: null,
message: "Closes: LIN-123",
});
expect(ids(result)).toEqual(["LIN-123"]);
});
it("only extracts keys preceded by magic word on same line", () => {
const result = extractLinearIssueIdentifiersForCommit({
sha: "abc",
branchName: null,
message: "See LIN-111, fixes LIN-222",
});
expect(ids(result)).toEqual(["LIN-222"]);
});
it("does not extract from the original bug scenario", () => {
const result = extractLinearIssueIdentifiersForCommit({
sha: "abc",
branchName: null,
message: "Title\nSeparate issue to follow up on that here LIN-60064",
});
expect(ids(result)).toEqual([]);
});
it("branch provides keys independently of message magic words", () => {
const result = extractLinearIssueIdentifiersForCommit({
sha: "abc",
branchName: "feature/LIN-100-something",
message: "See LIN-200 for details",
});
expect(ids(result)).toEqual(["LIN-100"]);
});
it("is case insensitive for magic words", () => {
const result = extractLinearIssueIdentifiersForCommit({
sha: "abc",
branchName: null,
message: "fIXES LIN-123",
});
expect(ids(result)).toEqual(["LIN-123"]);
});
it("extracts with multi-word phrase 'Part of'", () => {
const result = extractLinearIssueIdentifiersForCommit({
sha: "abc",
branchName: null,
message: "Part of LIN-123",
});
expect(ids(result)).toEqual(["LIN-123"]);
});
it("extracts with multi-word phrase 'Related to'", () => {
const result = extractLinearIssueIdentifiersForCommit({
sha: "abc",
branchName: null,
message: "Related to LIN-456",
});
expect(ids(result)).toEqual(["LIN-456"]);
});
it("extracts issue from Linear URL with slug after magic word", () => {
const result = extractLinearIssueIdentifiersForCommit({
sha: "abc",
branchName: null,
message: "Fixes https://linear.app/myorg/issue/LIN-123/fix-auth",
});
expect(ids(result)).toEqual(["LIN-123"]);
});
it("extracts issue from Linear URL without slug after magic word", () => {
const result = extractLinearIssueIdentifiersForCommit({
sha: "abc",
branchName: null,
message: "Fixes https://linear.app/myorg/issue/LIN-123",
});
expect(ids(result)).toEqual(["LIN-123"]);
});
it("does not extract Linear URL without magic word", () => {
const result = extractLinearIssueIdentifiersForCommit({
sha: "abc",
branchName: null,
message: "See https://linear.app/myorg/issue/LIN-123/fix",
});
expect(ids(result)).toEqual([]);
});
it("extracts mixed Linear URLs and raw IDs after magic word", () => {
const result = extractLinearIssueIdentifiersForCommit({
sha: "abc",
branchName: null,
message: "Fixes https://linear.app/myorg/issue/LIN-123/slug, ENG-456 and LIN-789",
});
expect(ids(result).sort()).toEqual(["ENG-456", "LIN-123", "LIN-789"]);
});
it("extracts issue from http Linear URL after magic word", () => {
const result = extractLinearIssueIdentifiersForCommit({
sha: "abc",
branchName: null,
message: "Fixes http://linear.app/myorg/issue/LIN-123",
});
expect(ids(result)).toEqual(["LIN-123"]);
});
it("extracts issue from Linear URL with trailing slash", () => {
const result = extractLinearIssueIdentifiersForCommit({
sha: "abc",
branchName: null,
message: "Fixes https://linear.app/my-org/issue/LIN-213/",
});
expect(ids(result)).toEqual(["LIN-213"]);
});
it("extracts issue from Linear URL with contributing phrase", () => {
const result = extractLinearIssueIdentifiersForCommit({
sha: "abc",
branchName: null,
message: "Part of https://linear.app/myorg/issue/LIN-213/some-slug",
});
expect(ids(result)).toEqual(["LIN-213"]);
});
});
describe("bracketed identifier in commit subject", () => {
it("extracts identifier from a [KEY-N] prefix on the subject line", () => {
const result = extractLinearIssueIdentifiersForCommit({
sha: "abc",
branchName: null,
message: "[ENG-123] My change",
});
expect(ids(result)).toEqual(["ENG-123"]);
});
it("extracts identifier from a lowercase [key-n] prefix", () => {
const result = extractLinearIssueIdentifiersForCommit({
sha: "abc",
branchName: null,
message: "[eng-123] adjust thing",
});
expect(ids(result)).toEqual(["ENG-123"]);
});
it("does not extract bracketed identifier from elsewhere in the message", () => {
const result = extractLinearIssueIdentifiersForCommit({
sha: "abc",
branchName: null,
message: "Title\n\nSee [ENG-123] in the docs",
});
expect(ids(result)).toEqual([]);
});
});
describe("revert branch handling", () => {
it("blocks extraction from merge commit with revert branch name", () => {
const result = extractLinearIssueIdentifiersForCommit({
sha: "abc",
branchName: "revert-571-romain/bac-39",
message: "Merge pull request #572 from org/revert-571-romain/bac-39",
});
expect(ids(result)).toEqual([]);
});
it("blocks extraction when revert branch has multiple identifiers", () => {
const result = extractLinearIssueIdentifiersForCommit({
sha: "abc",
branchName: "revert-571-romain/drive-320-and-drive-321",
message: null,
});
expect(ids(result)).toEqual([]);
});
it("blocks PR number extraction from revert branch", () => {
const result = extractPullRequestNumbersForCommit({
sha: "abc",
branchName: "revert-571-romain/bac-39",
message: "Merge pull request #572 from org/revert-571-romain/bac-39",
});
expect(result).toEqual([]);
});
it("blocks add-extraction from message-only revert with magic word", () => {
const result = extractLinearIssueIdentifiersForCommit({
sha: "abc",
branchName: null,
message: 'Revert "Fixes ENG-100"',
});
expect(ids(result)).toEqual([]);
});
it("reverted extractor picks up message-only revert with magic word", () => {
const result = extractRevertedIssueIdentifiersForCommit({
sha: "abc",
branchName: null,
message: 'Revert "Fixes ENG-100"',
});
expect(ids(result)).toEqual(["ENG-100"]);
});
it("extracts magic-word references from a revert commit body as added (issue closed by the revert)", () => {
const message = `Revert "Migration ActionMenu to StyleX" (#73629)
Fixes ENG-123`;
const result = extractLinearIssueIdentifiersForCommit({
sha: "abc",
branchName: null,
message,
});
expect(ids(result)).toEqual(["ENG-123"]);
});
it("scans the revert body even when branch is the GitHub auto-revert form", () => {
// GitHub's "Revert" button creates `revert-<N>-<original-branch>`. The
// branch contribution is suppressed (inner names the reverted work), but
// the revert author's body note must still flow to added.
const result = extractLinearIssueIdentifiersForCommit({
sha: "abc",
branchName: "revert-456-axel/eng-100-original-feature",
message: `Revert "Original feature" (#456)
Fixes ENG-123`,
});
expect(ids(result)).toEqual(["ENG-123"]);
});
it("stray quote in revert body does not leak the body into the reverted bucket", () => {
const message = `Revert "Original title" (#73629)
Fixes ENG-123 said "ship it"`;
const result = extractRevertedIssueIdentifiersForCommit({
sha: "abc",
branchName: null,
message,
});
expect(ids(result)).toEqual([]);
});
it("revert with magic word in both inner subject and body splits added vs reverted", () => {
const message = `Revert "Fixes ENG-100"
Fixes ENG-123`;
expect(ids(extractLinearIssueIdentifiersForCommit({ sha: "abc", message }))).toEqual(["ENG-123"]);
expect(ids(extractRevertedIssueIdentifiersForCommit({ sha: "abc", message }))).toEqual(["ENG-100"]);
});
it("allows extraction from revert-of-revert branch (even depth)", () => {
const result = extractLinearIssueIdentifiersForCommit({
sha: "abc",
branchName: "revert-572-revert-571-romain/bac-39",
message: null,
});
expect(ids(result)).toEqual(["BAC-39"]);
});
});
describe("extractRevertedIssueIdentifiersForCommit", () => {
it("extracts identifier from unwrapped revert message with magic word", () => {
const result = extractRevertedIssueIdentifiersForCommit({
sha: "abc",
message: 'Revert "Fixes DRIVE-320: memory leak in background location service"',
});
expect(ids(result)).toEqual(["DRIVE-320"]);
});
it("ignores identifier in unwrapped message without magic word", () => {
const result = extractRevertedIssueIdentifiersForCommit({
sha: "abc",
message: 'Revert "DRIVE-320: Fix memory leak in background location service"',
});
expect(ids(result)).toEqual([]);
});
it("extracts identifier from revert branch name", () => {
const result = extractRevertedIssueIdentifiersForCommit({
sha: "abc",
branchName: "revert-571-romain/bac-39",
message: 'Revert "Add TEST variable to .env.example"',
});
expect(ids(result)).toEqual(["BAC-39"]);
});
it("extracts from both message and branch, deduplicates", () => {
const result = extractRevertedIssueIdentifiersForCommit({
sha: "abc",
branchName: "revert-566-romain/drive-320",
message: 'Revert "Fixes DRIVE-320: memory leak"',
});
expect(ids(result)).toEqual(["DRIVE-320"]);
});
it("extracts multiple identifiers from unwrapped message", () => {
const result = extractRevertedIssueIdentifiersForCommit({
sha: "abc",
message: 'Revert "Fixes DRIVE-320 and DRIVE-321"',
});
expect(ids(result).sort()).toEqual(["DRIVE-320", "DRIVE-321"]);
});
it("returns empty for non-revert commit", () => {
const result = extractRevertedIssueIdentifiersForCommit({
sha: "abc",
branchName: "romain/drive-320",
message: "DRIVE-320: Fix memory leak",
});
expect(ids(result)).toEqual([]);
});
it("returns empty when revert message has no identifiers", () => {
const result = extractRevertedIssueIdentifiersForCommit({
sha: "abc",
message: 'Revert "Fix bug"',
});
expect(ids(result)).toEqual([]);
});
it("returns empty for null/undefined inputs", () => {
expect(ids(extractRevertedIssueIdentifiersForCommit({ sha: "abc", message: null }))).toEqual([]);
expect(ids(extractRevertedIssueIdentifiersForCommit({ sha: "abc" }))).toEqual([]);
});
});
describe("getRevertBranchDepth", () => {
it.each([
[null, 0],
["romain/bac-39", 0],
["revert-571-romain/bac-39", 1],
["revert-572-revert-571-romain/bac-39", 2],
["revert-574-revert-573-revert-572-romain/bac-39", 3],
["org/revert-572-revert-571-romain/bac-39", 2],
])("branch %j → depth %d", (branch, expected) => {
expect(getRevertBranchDepth(branch)).toBe(expected);
});
});
describe("getRevertMessageDepth", () => {
it.each([
[null, 0],
["Fix memory leak", 0],
['Revert "DRIVE-320: Fix"', 1],
['Revert "Revert "DRIVE-320: Fix""', 2],
['Revert "Revert "Revert "DRIVE-320: Fix"""', 3],
['Reapply "DRIVE-320: Fix"', 0],
])("message %j → depth %d", (message, expected) => {
expect(getRevertMessageDepth(message)).toBe(expected);
});
});
describe("squash sub-commit blocks", () => {
// `git merge --squash` followed by `git commit` writes a body that begins
// with a "Squashed commit of the following:" header and dumps every commit
// pulled in via the squash, including upstream commits merged into the
// feature branch. Those references describe branch history, not the change
// landing here, so they must not be re-attributed to this release.
it("ignores PR refs inside a squash sub-commit dump (no real title)", () => {
const message = `Squashed commit of the following:
commit aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Older shipped PR title (#85)
Fixes LIN-50`;
expect(extractPullRequestNumbersForCommit({ sha: "abc", message })).toEqual([]);
expect(ids(extractLinearIssueIdentifiersForCommit({ sha: "abc", message }))).toEqual([]);
});
it("ignores PR refs inside a squash dump but keeps a real title prepended", () => {
const message = `New dashboard widget (#100)
Squashed commit of the following:
commit aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Older shipped PR title (#85)
Fixes LIN-50
commit bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
Bug fix for graph render
Fixes LIN-100`;
// Title's PR # is the legitimate one; body's #85 must not leak.
expect(extractPullRequestNumbersForCommit({ sha: "abc", message })).toEqual([100]);
// LIN-100 is in a sub-commit body — also nested history. With the squash
// dump stripped, neither LIN-50 (already shipped) nor LIN-100 (whose
// attribution belongs to a different commit) are re-extracted from here.
expect(ids(extractLinearIssueIdentifiersForCommit({ sha: "abc", message }))).toEqual([]);
});
it("keeps magic-word refs from the PR description body above the squash dump", () => {
const message = `New dashboard widget (#100)
Closes LIN-100
Squashed commit of the following:
commit aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Fixes LIN-50`;
expect(extractPullRequestNumbersForCommit({ sha: "abc", message })).toEqual([100]);
expect(ids(extractLinearIssueIdentifiersForCommit({ sha: "abc", message }))).toEqual(["LIN-100"]);
});
it("does not extract PR # from body cross-reference like 'builds on #85'", () => {
const message = `Add settings page (#100)
This builds on #85 and #87. Closes LIN-200.`;
expect(extractPullRequestNumbersForCommit({ sha: "abc", message })).toEqual([100]);
expect(ids(extractLinearIssueIdentifiersForCommit({ sha: "abc", message }))).toEqual(["LIN-200"]);
});
it("preserves a user-authored footer appended after the squash dump", () => {
// Default `git merge --squash` puts the dump at the top, so any footer
// (e.g., trailers like `Closes LIN-X`, `Co-authored-by: ...`) typically
// ends up below the dump after the developer edits the message.
const message = `Squashed commit of the following:
commit aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Author: Dev <dev@example.com>
Date: Wed May 6 16:45:34 2026 +0000
Edge cases
commit bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
Author: Dev <dev@example.com>
Date: Wed May 6 16:45:34 2026 +0000
Implement search filter
Closes LIN-200`;
expect(ids(extractLinearIssueIdentifiersForCommit({ sha: "abc", message }))).toEqual(["LIN-200"]);
});
it("preserves both a prepended title and an appended footer around the dump", () => {
const message = `Search filter (#100)
Squashed commit of the following:
commit aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Older shipped PR title (#85)
Fixes LIN-50
Closes LIN-200`;
expect(extractPullRequestNumbersForCommit({ sha: "abc", message })).toEqual([100]);
expect(ids(extractLinearIssueIdentifiersForCommit({ sha: "abc", message })).sort()).toEqual(["LIN-200"]);
});
it("handles the marker without recognizable commit blocks below", () => {
const message = `Squashed commit of the following:
Closes LIN-200`;
expect(ids(extractLinearIssueIdentifiersForCommit({ sha: "abc", message }))).toEqual(["LIN-200"]);
});
it("strips squash blocks from a revert commit body before scanning for added references", () => {
const message = `Revert "Some PR"
Squashed commit of the following:
Fixes LIN-50`;
expect(ids(extractLinearIssueIdentifiersForCommit({ sha: "abc", message }))).toEqual([]);
});
});
describe("extractPullRequestNumbersForCommit", () => {
// Messages that should extract PR numbers
it.each([
["For issue #111, fix (#123)", [123], "squash merge skips fallback"],
["Fix #124 with better handling", [124], "hash in middle of title"],
["Merge pull request #431 from org/branch", [431], "GitHub merge format"],
["Merge pull request #42 from owner/branch\n\nDescription", [42], "merge with multiline description"],
])("message %j should yield %j (%s)", (message, expected) => {
const result = extractPullRequestNumbersForCommit({ sha: "abc", message });
expect(result).toEqual(expected);
});
// Messages that should NOT extract PR numbers
it.each([
["Some fix", "no PR reference"],
[null, "null message"],
[undefined, "undefined message"],
['Revert "Fix bug (#123)"', "revert commit"],
['Revert "Merge pull request #456 from owner/branch"', "revert of merge"],
])("message %j should yield [] (%s)", (message, _description) => {
const result = extractPullRequestNumbersForCommit({ sha: "abc", message });
expect(result).toEqual([]);
});
// Numbers above the GraphQL Int (32-bit) bound cannot be GitHub PR numbers
// and must be filtered so they don't poison the release sync mutation.
it.each([
[
"FLEX-2816: fix something\n\nTwo issues from cursor[bot] review #4211934690.",
[],
"title has no PR number and body scan is no longer attempted",
],
[
"Fix something (#51876)\n\nTwo issues from cursor[bot] review #4211934690.",
[51876],
"squash match keeps valid PR; body cross-references are ignored",
],
["Fix bug\n\nRelated to (#4211934690)", [], "body fallback removed: cross-reference in body is ignored"],
["Fix bug\n\nSee #123 and sentry #9999999999", [], "body fallback removed: numbers in body don't leak"],
["Title (#4211934690)", [], "squash format with oversized number is dropped"],
["Merge pull request #4211934690 from x/y", [], "merge format with oversized number is dropped"],
[`Title (#${2_147_483_647})`, [2_147_483_647], "Int32 max is allowed"],
[`Title (#${2_147_483_648})`, [], "one above Int32 max is dropped"],
])("message %j should yield %j (%s)", (message, expected) => {
const result = extractPullRequestNumbersForCommit({ sha: "abc", message });
expect(result).toEqual(expected);
});
});
describe("extractPullRequestNumbersForCommit — GitLab merge request trailer", () => {
// GitLab emits this trailer whenever it creates a merge commit.
it.each([
[
"default merge",
"Merge branch 'eng-123-add-feature' into 'main'\n\nAdd feature\n\nSee merge request acme/web!1",
[1],
],
[
"squash-before-merge merge commit",
"Merge branch 'eng-456-fix-bug' into 'main'\n\nFix bug\n\nSee merge request acme/web!4",
[4],
],
])("%s → %j", (_name, message, expected) => {
const result = extractPullRequestNumbersForCommit({ sha: "abc", message });
expect(result).toEqual(expected);
});
// Path-shape variants the regex must accept.
it.each([
["subgroup path", "Merge\n\nSee merge request acme/platform/web!42", [42]],
["hyphens in path", "Merge\n\nSee merge request linear-app/axel-test!3", [3]],
["dots in path", "Merge\n\nSee merge request my.org/my.repo!9", [9]],
["trailing text after number", "Merge\n\nSee merge request acme/web!42 (auto-merged)", [42]],
[
"multiple trailers on consecutive lines (collect + dedupe)",
"Merge\n\nSee merge request acme/web!42\nSee merge request acme/web!43",
[42, 43],
],
])("%s → %j", (_name, message, expected) => {
const result = extractPullRequestNumbersForCommit({ sha: "abc", message });
expect(result).toEqual(expected);
});
// Body content that must NOT trigger extraction.
it.each([
["inline reference (not line-anchored)", "Something See merge request acme/web!42 inline"],
["leading whitespace before the trailer", " See merge request acme/web!42"],
["bare !N without the trailer phrase", "Closes !42 maybe"],
["empty path before !N", "See merge request !42"],
["reference with no number", "See merge request acme/web!"],
["number above Int32 max is dropped", `Merge\n\nSee merge request foo/bar!${2_147_483_648}`],
[
'revert short-circuits before reaching the trailer (Revert "..." at start)',
"Revert \"Merge branch 'x' into 'main'\n\nSee merge request acme/web!42\"",
],
])("%s yields no PR numbers", (_name, message) => {
const result = extractPullRequestNumbersForCommit({ sha: "abc", message });
expect(result).toEqual([]);
});
it("revert merge branch (revert-N-...) returns [] even with a trailer in the body", () => {
const result = extractPullRequestNumbersForCommit({
sha: "abc",
branchName: "revert-1234-feature-branch",
message: "Merge\n\nSee merge request acme/web!42",
});
expect(result).toEqual([]);
});
});