Go · 1036 bytes Raw Blame History
1 // SPDX-License-Identifier: AGPL-3.0-or-later
2
3 package pulls
4
5 import (
6 "regexp"
7 "strconv"
8 )
9
10 // reLinkedIssue matches the GitHub-style auto-close keywords:
11 //
12 // close|closes|closed | fix|fixes|fixed | resolve|resolves|resolved
13 //
14 // followed by `#N`. Case-insensitive. The keyword + `#N` must be a
15 // connected token (one space allowed) so plain "...closes the door
16 // on #5..." doesn't accidentally trigger an auto-close.
17 var reLinkedIssue = regexp.MustCompile(`(?i)\b(?:close[sd]?|fix(?:e[sd])?|resolve[sd]?)\s+#([0-9]{1,9})\b`)
18
19 // parseLinkedIssues returns the deduplicated set of issue numbers a
20 // PR body or commit message intends to auto-close.
21 func parseLinkedIssues(body string) []int64 {
22 if body == "" {
23 return nil
24 }
25 seen := map[int64]struct{}{}
26 out := []int64{}
27 for _, m := range reLinkedIssue.FindAllStringSubmatch(body, -1) {
28 n, err := strconv.ParseInt(m[1], 10, 64)
29 if err != nil {
30 continue
31 }
32 if _, dup := seen[n]; dup {
33 continue
34 }
35 seen[n] = struct{}{}
36 out = append(out, n)
37 }
38 return out
39 }
40