| @@ -8,17 +8,26 @@ import ( |
| 8 | 8 | "strings" |
| 9 | 9 | |
| 10 | 10 | "gitbay.org/gitbay/internal/gitutil" |
| 11 | "gitbay.org/gitbay/internal/policy" |
| 11 | 12 | "gitbay.org/gitbay/internal/store" |
| 12 | 13 | ) |
| 13 | 14 | |
| 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. |
| 15 | // closePat matches closing keywords, with an optional owner/name before |
| 16 | // the number for an issue in another repository; refPat matches any bare |
| 17 | // same-repo reference. A cross-repo close acts only when the actor holds |
| 18 | // write on the target (closeTarget); a bare cross-repo reference stays |
| 19 | // display-only. |
| 17 | 20 | var ( |
| 18 | | closePat = regexp.MustCompile(`(?i)\b(?:close[sd]?|fix(?:e[sd])?|resolve[sd]?)[ :]+#(\d+)\b`) |
| 21 | closePat = regexp.MustCompile(`(?i)\b(?:close[sd]?|fix(?:e[sd])?|resolve[sd]?)[ :]+(?:([a-z0-9][a-z0-9._-]*/[a-z0-9][a-z0-9._-]*))?#(\d+)\b`) |
| 19 | 22 | refPat = regexp.MustCompile(`(^|[\s([{:])#(\d+)\b`) |
| 20 | 23 | ) |
| 21 | 24 | |
| 25 | // closeRef is one closing reference: Path is "" for the same repository. |
| 26 | type closeRef struct { |
| 27 | Path string |
| 28 | N int64 |
| 29 | } |
| 30 | |
| 22 | 31 | const maxMessageCommits = 100 |
| 23 | 32 | |
| 24 | 33 | // ProcessCommitMessages acts on issue references in commits that just |
| @@ -34,23 +43,30 @@ func ProcessCommitMessages(st *store.Store, dir string, repo store.Repo, actorID |
| 34 | 43 | return |
| 35 | 44 | } |
| 36 | 45 | for _, m := range msgs { |
| 37 | | closes := map[int64]bool{} |
| 38 | | for _, n := range closingRefs(m.Message) { |
| 39 | | closes[n] = true |
| 46 | closes := closingRefs(m.Message) |
| 47 | local := map[int64]bool{} |
| 48 | for _, ref := range closes { |
| 49 | if ref.Path == "" { |
| 50 | local[ref.N] = true |
| 51 | } |
| 40 | 52 | } |
| 41 | 53 | refs := map[int64]bool{} |
| 42 | 54 | for _, g := range refPat.FindAllStringSubmatch(m.Message, -1) { |
| 43 | | if n, err := strconv.ParseInt(g[2], 10, 64); err == nil && !closes[n] { |
| 55 | if n, err := strconv.ParseInt(g[2], 10, 64); err == nil && !local[n] { |
| 44 | 56 | refs[n] = true |
| 45 | 57 | } |
| 46 | 58 | } |
| 47 | 59 | subject, _, _ := strings.Cut(m.Message, "\n") |
| 48 | 60 | author := authorLink(st, m.AuthorName, m.AuthorEmail) |
| 49 | | for n := range closes { |
| 50 | | actOnIssue(st, repo, actorID, m.SHA, n, true, subject, author) |
| 61 | for _, ref := range closes { |
| 62 | target, ok := closeTarget(st, repo, actorID, ref.Path) |
| 63 | if !ok { |
| 64 | continue |
| 65 | } |
| 66 | actOnIssue(st, repo, target, actorID, m.SHA, ref.N, true, subject, author) |
| 51 | 67 | } |
| 52 | 68 | for n := range refs { |
| 53 | | actOnIssue(st, repo, actorID, m.SHA, n, false, subject, author) |
| 69 | actOnIssue(st, repo, repo, actorID, m.SHA, n, false, subject, author) |
| 54 | 70 | } |
| 55 | 71 | } |
| 56 | 72 | } |
| @@ -66,8 +82,12 @@ func ProcessCommitMessages(st *store.Store, dir string, repo store.Repo, actorID |
| 66 | 82 | // the sha let a bare "#N" in a commit message claim it first and silently |
| 67 | 83 | // suppress the close. |
| 68 | 84 | func ProcessMRDescription(st *store.Store, repo store.Repo, mr store.MR, actorID int64) { |
| 69 | | for _, n := range closingRefs(mr.Title + "\n" + mr.Body) { |
| 70 | | issue, err := st.IssueByNumber(repo.ID, n) |
| 85 | for _, ref := range closingRefs(mr.Title + "\n" + mr.Body) { |
| 86 | target, ok := closeTarget(st, repo, actorID, ref.Path) |
| 87 | if !ok { |
| 88 | continue |
| 89 | } |
| 90 | issue, err := st.IssueByNumber(target.ID, ref.N) |
| 71 | 91 | if err != nil || issue.State != "open" { |
| 72 | 92 | continue // no such issue, or a commit already closed it |
| 73 | 93 | } |
| @@ -76,14 +96,14 @@ func ProcessMRDescription(st *store.Store, repo store.Repo, mr store.MR, actorID |
| 76 | 96 | continue // this merge request already acted on this issue |
| 77 | 97 | } |
| 78 | 98 | if err := st.SetIssueState(issue.ID, "closed"); err != nil { |
| 79 | | slog.Error("mr refs: closing issue", "issue", n, "err", err) |
| 99 | slog.Error("mr refs: closing issue", "issue", ref.N, "err", err) |
| 80 | 100 | continue |
| 81 | 101 | } |
| 82 | 102 | link := fmt.Sprintf("[!%d](/%s/mrs/%d)", mr.Number, repo.Path(), mr.Number) |
| 83 | 103 | st.AddIssueSystemComment(issue.ID, actorID, |
| 84 | 104 | fmt.Sprintf("closed by merge request %s: %s", link, mr.Title)) |
| 85 | | st.RecordEvent(repo.ID, actorID, "issue.closed", |
| 86 | | fmt.Sprintf(`{"number":%d,"mr":%d}`, n, mr.Number)) |
| 105 | st.RecordEvent(target.ID, actorID, "issue.closed", |
| 106 | fmt.Sprintf(`{"number":%d,"mr":%d}`, ref.N, mr.Number)) |
| 87 | 107 | } |
| 88 | 108 | } |
| 89 | 109 | |
| @@ -93,21 +113,52 @@ func mrRefKey(number int64) string { |
| 93 | 113 | return fmt.Sprintf("mr-%d", number) |
| 94 | 114 | } |
| 95 | 115 | |
| 96 | | // closingRefs returns the issue numbers a text closes, in no order. |
| 97 | | func closingRefs(text string) []int64 { |
| 98 | | seen := map[int64]bool{} |
| 99 | | var out []int64 |
| 116 | // closingRefs returns the references a text closes, in no order. |
| 117 | func closingRefs(text string) []closeRef { |
| 118 | seen := map[closeRef]bool{} |
| 119 | var out []closeRef |
| 100 | 120 | for _, g := range closePat.FindAllStringSubmatch(text, -1) { |
| 101 | | n, err := strconv.ParseInt(g[1], 10, 64) |
| 102 | | if err != nil || seen[n] { |
| 121 | n, err := strconv.ParseInt(g[2], 10, 64) |
| 122 | if err != nil { |
| 123 | continue |
| 124 | } |
| 125 | ref := closeRef{Path: strings.ToLower(g[1]), N: n} |
| 126 | if seen[ref] { |
| 103 | 127 | continue |
| 104 | 128 | } |
| 105 | | seen[n] = true |
| 106 | | out = append(out, n) |
| 129 | seen[ref] = true |
| 130 | out = append(out, ref) |
| 107 | 131 | } |
| 108 | 132 | return out |
| 109 | 133 | } |
| 110 | 134 | |
| 135 | // closeTarget resolves where a closing reference acts: the source |
| 136 | // repository for a bare #N, or the named repository when the actor holds |
| 137 | // write there. false means the reference stays text; nothing is logged |
| 138 | // above debug, since a refusal must not confirm the target exists. |
| 139 | func closeTarget(st *store.Store, source store.Repo, actorID int64, path string) (store.Repo, bool) { |
| 140 | if path == "" { |
| 141 | return source, true |
| 142 | } |
| 143 | target, err := st.RepoByPath(path) |
| 144 | if err != nil { |
| 145 | return store.Repo{}, false |
| 146 | } |
| 147 | actor, err := st.UserByID(actorID) |
| 148 | if err != nil { |
| 149 | return store.Repo{}, false |
| 150 | } |
| 151 | grant, err := st.AccessRole(target.ID, actorID) |
| 152 | if err != nil { |
| 153 | return store.Repo{}, false |
| 154 | } |
| 155 | if !policy.CanWrite(actor, target, grant) { |
| 156 | slog.Debug("commit refs: cross-repo close refused", "source", source.Path(), "target", path) |
| 157 | return store.Repo{}, false |
| 158 | } |
| 159 | return target, true |
| 160 | } |
| 161 | |
| 111 | 162 | // RecordLandedCommits attributes commits that just landed on the default |
| 112 | 163 | // branch to accounts by verified author email, for the activity graph. |
| 113 | 164 | // Dedup by (repo, sha) makes rebases and re-runs harmless; unresolvable |
| @@ -137,8 +188,8 @@ func authorLink(st *store.Store, name, email string) string { |
| 137 | 188 | return name |
| 138 | 189 | } |
| 139 | 190 | |
| 140 | | func actOnIssue(st *store.Store, repo store.Repo, actorID int64, sha string, number int64, close bool, subject, author string) { |
| 141 | | issue, err := st.IssueByNumber(repo.ID, number) |
| 191 | func actOnIssue(st *store.Store, source, target store.Repo, actorID int64, sha string, number int64, close bool, subject, author string) { |
| 192 | issue, err := st.IssueByNumber(target.ID, number) |
| 142 | 193 | if err != nil { |
| 143 | 194 | return // no such issue: the reference is just text |
| 144 | 195 | } |
| @@ -152,14 +203,14 @@ func actOnIssue(st *store.Store, repo store.Repo, actorID int64, sha string, num |
| 152 | 203 | } |
| 153 | 204 | // Informational system entries, not comments from the pusher; the |
| 154 | 205 | // linked sha renders clickable on the web. |
| 155 | | link := fmt.Sprintf("[%s](/%s/commit/%s)", short, repo.Path(), sha) |
| 206 | link := fmt.Sprintf("[%s](/%s/commit/%s)", short, source.Path(), sha) |
| 156 | 207 | if close && issue.State == "open" { |
| 157 | 208 | if err := st.SetIssueState(issue.ID, "closed"); err != nil { |
| 158 | 209 | slog.Error("commit refs: closing issue", "issue", number, "err", err) |
| 159 | 210 | return |
| 160 | 211 | } |
| 161 | 212 | st.AddIssueSystemComment(issue.ID, actorID, fmt.Sprintf("closed by commit %s by %s: %s", link, author, subject)) |
| 162 | | st.RecordEvent(repo.ID, actorID, "issue.closed", fmt.Sprintf(`{"number":%d,"sha":%q}`, number, sha)) |
| 213 | st.RecordEvent(target.ID, actorID, "issue.closed", fmt.Sprintf(`{"number":%d,"sha":%q}`, number, sha)) |
| 163 | 214 | return |
| 164 | 215 | } |
| 165 | 216 | st.AddIssueSystemComment(issue.ID, actorID, fmt.Sprintf("referenced in commit %s by %s: %s", link, author, subject)) |