internal/control/commitrefs.go
115 lines · 4100 bytes
1package control
2
3import (
4 "fmt"
5 "log/slog"
6 "regexp"
7 "strconv"
8 "strings"
9
10 "gitbay.org/gitbay/internal/gitutil"
11 "gitbay.org/gitbay/internal/store"
12)
13
14// closePat matches closing keywords; refPat matches any same-repo issue
15// reference. Cross-repo references stay display-only (autolink) — acting
16// across repositories would need its own authorization story.
17var (
18 closePat = regexp.MustCompile(`(?i)\b(?:close[sd]?|fix(?:e[sd])?|resolve[sd]?)[ :]+#(\d+)\b`)
19 refPat = regexp.MustCompile(`(^|[\s([{:])#(\d+)\b`)
20)
21
22const maxMessageCommits = 100
23
24// ProcessCommitMessages acts on issue references in commits that just
25// landed on the default branch (old..new): closing keywords close the
26// issue, bare #N leaves a reference comment. Each (issue, sha) pair acts
27// at most once, ever. actorID — the pusher or merger — authorizes and
28// signs the resulting comments; failures are logged, never fatal, because
29// this runs after the push or merge already succeeded.
30func ProcessCommitMessages(st *store.Store, dir string, repo store.Repo, actorID int64, old, new string) {
31 msgs, err := gitutil.RevListMessages(dir, old, new, maxMessageCommits)
32 if err != nil {
33 slog.Error("commit refs: listing messages", "repo", repo.Path(), "err", err)
34 return
35 }
36 for _, m := range msgs {
37 closes := map[int64]bool{}
38 for _, g := range closePat.FindAllStringSubmatch(m.Message, -1) {
39 if n, err := strconv.ParseInt(g[1], 10, 64); err == nil {
40 closes[n] = true
41 }
42 }
43 refs := map[int64]bool{}
44 for _, g := range refPat.FindAllStringSubmatch(m.Message, -1) {
45 if n, err := strconv.ParseInt(g[2], 10, 64); err == nil && !closes[n] {
46 refs[n] = true
47 }
48 }
49 subject, _, _ := strings.Cut(m.Message, "\n")
50 author := authorLink(st, m.AuthorName, m.AuthorEmail)
51 for n := range closes {
52 actOnIssue(st, repo, actorID, m.SHA, n, true, subject, author)
53 }
54 for n := range refs {
55 actOnIssue(st, repo, actorID, m.SHA, n, false, subject, author)
56 }
57 }
58}
59
60// RecordLandedCommits attributes commits that just landed on the default
61// branch to accounts by verified author email, for the activity graph.
62// Dedup by (repo, sha) makes rebases and re-runs harmless; unresolvable
63// authors are simply not activity.
64func RecordLandedCommits(st *store.Store, dir string, repo store.Repo, old, new string) {
65 authors, err := gitutil.RevListAuthors(dir, old, new, maxMessageCommits)
66 if err != nil {
67 return
68 }
69 for _, a := range authors {
70 if uid, ok := st.UserIDByVerifiedEmail(a.Email); ok {
71 st.RecordCommitActivity(repo.ID, a.SHA, uid, a.Day)
72 }
73 }
74}
75
76// authorLink renders the commit's author for a system comment: a link to
77// their profile when the author email is verified on an account here, and
78// the name git recorded otherwise.
79func authorLink(st *store.Store, name, email string) string {
80 if user, ok := st.UsernameByVerifiedEmail(email); ok {
81 return fmt.Sprintf("[%s](/%s)", user, user)
82 }
83 if name == "" {
84 return email
85 }
86 return name
87}
88
89func actOnIssue(st *store.Store, repo store.Repo, actorID int64, sha string, number int64, close bool, subject, author string) {
90 issue, err := st.IssueByNumber(repo.ID, number)
91 if err != nil {
92 return // no such issue: the reference is just text
93 }
94 fresh, err := st.TryRecordCommitRef(issue.ID, sha)
95 if err != nil || !fresh {
96 return
97 }
98 short := sha
99 if len(short) > 10 {
100 short = short[:10]
101 }
102 // Informational system entries, not comments from the pusher; the
103 // linked sha renders clickable on the web.
104 link := fmt.Sprintf("[%s](/%s/commit/%s)", short, repo.Path(), sha)
105 if close && issue.State == "open" {
106 if err := st.SetIssueState(issue.ID, "closed"); err != nil {
107 slog.Error("commit refs: closing issue", "issue", number, "err", err)
108 return
109 }
110 st.AddIssueSystemComment(issue.ID, actorID, fmt.Sprintf("closed by commit %s by %s: %s", link, author, subject))
111 st.RecordEvent(repo.ID, actorID, "issue.closed", fmt.Sprintf(`{"number":%d,"sha":%q}`, number, sha))
112 return
113 }
114 st.AddIssueSystemComment(issue.ID, actorID, fmt.Sprintf("referenced in commit %s by %s: %s", link, author, subject))
115}