Commit ee971df409
Verified · cmc
Layout: unified · split
internal/control/commitrefs.go +79 −28
| @@ -8,17 +8,26 @@ import ( | |||
| 8 | "strings" | 8 | "strings" |
| 9 | 9 | ||
| 10 | "gitbay.org/gitbay/internal/gitutil" | 10 | "gitbay.org/gitbay/internal/gitutil" |
| 11 | "gitbay.org/gitbay/internal/policy" | ||
| 11 | "gitbay.org/gitbay/internal/store" | 12 | "gitbay.org/gitbay/internal/store" |
| 12 | ) | 13 | ) |
| 13 | 14 | ||
| 14 | // closePat matches closing keywords; refPat matches any same-repo issue | 15 | // closePat matches closing keywords, with an optional owner/name before |
| 15 | // reference. Cross-repo references stay display-only (autolink) — acting | 16 | // the number for an issue in another repository; refPat matches any bare |
| 16 | // across repositories would need its own authorization story. | 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 | var ( | 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 | refPat = regexp.MustCompile(`(^|[\s([{:])#(\d+)\b`) | 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 | const maxMessageCommits = 100 | 31 | const maxMessageCommits = 100 |
| 23 | 32 | ||
| 24 | // ProcessCommitMessages acts on issue references in commits that just | 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 | return | 43 | return |
| 35 | } | 44 | } |
| 36 | for _, m := range msgs { | 45 | for _, m := range msgs { |
| 37 | closes := map[int64]bool{} | 46 | closes := closingRefs(m.Message) |
| 38 | for _, n := range closingRefs(m.Message) { | 47 | local := map[int64]bool{} |
| 39 | closes[n] = true | 48 | for _, ref := range closes { |
| 49 | if ref.Path == "" { | ||
| 50 | local[ref.N] = true | ||
| 51 | } | ||
| 40 | } | 52 | } |
| 41 | refs := map[int64]bool{} | 53 | refs := map[int64]bool{} |
| 42 | for _, g := range refPat.FindAllStringSubmatch(m.Message, -1) { | 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 | refs[n] = true | 56 | refs[n] = true |
| 45 | } | 57 | } |
| 46 | } | 58 | } |
| 47 | subject, _, _ := strings.Cut(m.Message, "\n") | 59 | subject, _, _ := strings.Cut(m.Message, "\n") |
| 48 | author := authorLink(st, m.AuthorName, m.AuthorEmail) | 60 | author := authorLink(st, m.AuthorName, m.AuthorEmail) |
| 49 | for n := range closes { | 61 | for _, ref := range closes { |
| 50 | actOnIssue(st, repo, actorID, m.SHA, n, true, subject, author) | 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 | for n := range refs { | 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 | // the sha let a bare "#N" in a commit message claim it first and silently | 82 | // the sha let a bare "#N" in a commit message claim it first and silently |
| 67 | // suppress the close. | 83 | // suppress the close. |
| 68 | func ProcessMRDescription(st *store.Store, repo store.Repo, mr store.MR, actorID int64) { | 84 | func ProcessMRDescription(st *store.Store, repo store.Repo, mr store.MR, actorID int64) { |
| 69 | for _, n := range closingRefs(mr.Title + "\n" + mr.Body) { | 85 | for _, ref := range closingRefs(mr.Title + "\n" + mr.Body) { |
| 70 | issue, err := st.IssueByNumber(repo.ID, n) | 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 | if err != nil || issue.State != "open" { | 91 | if err != nil || issue.State != "open" { |
| 72 | continue // no such issue, or a commit already closed it | 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 | continue // this merge request already acted on this issue | 96 | continue // this merge request already acted on this issue |
| 77 | } | 97 | } |
| 78 | if err := st.SetIssueState(issue.ID, "closed"); err != nil { | 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 | continue | 100 | continue |
| 81 | } | 101 | } |
| 82 | link := fmt.Sprintf("[!%d](/%s/mrs/%d)", mr.Number, repo.Path(), mr.Number) | 102 | link := fmt.Sprintf("[!%d](/%s/mrs/%d)", mr.Number, repo.Path(), mr.Number) |
| 83 | st.AddIssueSystemComment(issue.ID, actorID, | 103 | st.AddIssueSystemComment(issue.ID, actorID, |
| 84 | fmt.Sprintf("closed by merge request %s: %s", link, mr.Title)) | 104 | fmt.Sprintf("closed by merge request %s: %s", link, mr.Title)) |
| 85 | st.RecordEvent(repo.ID, actorID, "issue.closed", | 105 | st.RecordEvent(target.ID, actorID, "issue.closed", |
| 86 | fmt.Sprintf(`{"number":%d,"mr":%d}`, n, mr.Number)) | 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 | return fmt.Sprintf("mr-%d", number) | 113 | return fmt.Sprintf("mr-%d", number) |
| 94 | } | 114 | } |
| 95 | 115 | ||
| 96 | // closingRefs returns the issue numbers a text closes, in no order. | 116 | // closingRefs returns the references a text closes, in no order. |
| 97 | func closingRefs(text string) []int64 { | 117 | func closingRefs(text string) []closeRef { |
| 98 | seen := map[int64]bool{} | 118 | seen := map[closeRef]bool{} |
| 99 | var out []int64 | 119 | var out []closeRef |
| 100 | for _, g := range closePat.FindAllStringSubmatch(text, -1) { | 120 | for _, g := range closePat.FindAllStringSubmatch(text, -1) { |
| 101 | n, err := strconv.ParseInt(g[1], 10, 64) | 121 | n, err := strconv.ParseInt(g[2], 10, 64) |
| 102 | if err != nil || seen[n] { | 122 | if err != nil { |
| 123 | continue | ||
| 124 | } | ||
| 125 | ref := closeRef{Path: strings.ToLower(g[1]), N: n} | ||
| 126 | if seen[ref] { | ||
| 103 | continue | 127 | continue |
| 104 | } | 128 | } |
| 105 | seen[n] = true | 129 | seen[ref] = true |
| 106 | out = append(out, n) | 130 | out = append(out, ref) |
| 107 | } | 131 | } |
| 108 | return out | 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 | // RecordLandedCommits attributes commits that just landed on the default | 162 | // RecordLandedCommits attributes commits that just landed on the default |
| 112 | // branch to accounts by verified author email, for the activity graph. | 163 | // branch to accounts by verified author email, for the activity graph. |
| 113 | // Dedup by (repo, sha) makes rebases and re-runs harmless; unresolvable | 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 | return name | 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) { | 191 | func actOnIssue(st *store.Store, source, target store.Repo, actorID int64, sha string, number int64, close bool, subject, author string) { |
| 141 | issue, err := st.IssueByNumber(repo.ID, number) | 192 | issue, err := st.IssueByNumber(target.ID, number) |
| 142 | if err != nil { | 193 | if err != nil { |
| 143 | return // no such issue: the reference is just text | 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 | // Informational system entries, not comments from the pusher; the | 204 | // Informational system entries, not comments from the pusher; the |
| 154 | // linked sha renders clickable on the web. | 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 | if close && issue.State == "open" { | 207 | if close && issue.State == "open" { |
| 157 | if err := st.SetIssueState(issue.ID, "closed"); err != nil { | 208 | if err := st.SetIssueState(issue.ID, "closed"); err != nil { |
| 158 | slog.Error("commit refs: closing issue", "issue", number, "err", err) | 209 | slog.Error("commit refs: closing issue", "issue", number, "err", err) |
| 159 | return | 210 | return |
| 160 | } | 211 | } |
| 161 | st.AddIssueSystemComment(issue.ID, actorID, fmt.Sprintf("closed by commit %s by %s: %s", link, author, subject)) | 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 | return | 214 | return |
| 164 | } | 215 | } |
| 165 | st.AddIssueSystemComment(issue.ID, actorID, fmt.Sprintf("referenced in commit %s by %s: %s", link, author, subject)) | 216 | st.AddIssueSystemComment(issue.ID, actorID, fmt.Sprintf("referenced in commit %s by %s: %s", link, author, subject)) |
internal/control/commitrefs_test.go +52 −8
| @@ -2,7 +2,10 @@ package control | |||
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "slices" | 4 | "slices" |
| 5 | "strings" | ||
| 5 | "testing" | 6 | "testing" |
| 7 | |||
| 8 | "gitbay.org/gitbay/internal/store" | ||
| 6 | ) | 9 | ) |
| 7 | 10 | ||
| 8 | // The same keyword set has to work wherever the intent is written: a | 11 | // The same keyword set has to work wherever the intent is written: a |
| @@ -11,23 +14,64 @@ func TestClosingRefs(t *testing.T) { | |||
| 11 | for _, tc := range []struct { | 14 | for _, tc := range []struct { |
| 12 | name string | 15 | name string |
| 13 | text string | 16 | text string |
| 14 | want []int64 | 17 | want []closeRef |
| 15 | }{ | 18 | }{ |
| 16 | {"closes", "Closes #50", []int64{50}}, | 19 | {"closes", "Closes #50", []closeRef{{"", 50}}}, |
| 17 | {"lowercase and fix", "fixes #7", []int64{7}}, | 20 | {"lowercase and fix", "fixes #7", []closeRef{{"", 7}}}, |
| 18 | {"resolved", "resolved: #12", []int64{12}}, | 21 | {"resolved", "resolved: #12", []closeRef{{"", 12}}}, |
| 19 | {"several", "Closes #1\n\nAlso fixes #2 and resolves #3", []int64{1, 2, 3}}, | 22 | {"several", "Closes #1\n\nAlso fixes #2 and resolves #3", []closeRef{{"", 1}, {"", 2}, {"", 3}}}, |
| 20 | {"repeats collapse", "closes #4, closes #4", []int64{4}}, | 23 | {"repeats collapse", "closes #4, closes #4", []closeRef{{"", 4}}}, |
| 21 | {"bare references do not close", "see #9 for context", nil}, | 24 | {"bare references do not close", "see #9 for context", nil}, |
| 22 | {"cross-repo stays display-only", "closes krz/other#3", nil}, | 25 | {"cross-repo carries the path", "closes krz/other#3", []closeRef{{"krz/other", 3}}}, |
| 26 | {"same number in two repos", "closes #3, closes krz/other#3", []closeRef{{"", 3}, {"krz/other", 3}}}, | ||
| 23 | {"keyword must be its own word", "unclosed #5", nil}, | 27 | {"keyword must be its own word", "unclosed #5", nil}, |
| 24 | } { | 28 | } { |
| 25 | t.Run(tc.name, func(t *testing.T) { | 29 | t.Run(tc.name, func(t *testing.T) { |
| 26 | got := closingRefs(tc.text) | 30 | got := closingRefs(tc.text) |
| 27 | slices.Sort(got) | 31 | slices.SortFunc(got, func(a, b closeRef) int { |
| 32 | if a.Path != b.Path { | ||
| 33 | return strings.Compare(a.Path, b.Path) | ||
| 34 | } | ||
| 35 | return int(a.N - b.N) | ||
| 36 | }) | ||
| 28 | if !slices.Equal(got, tc.want) { | 37 | if !slices.Equal(got, tc.want) { |
| 29 | t.Errorf("closingRefs(%q) = %v, want %v", tc.text, got, tc.want) | 38 | t.Errorf("closingRefs(%q) = %v, want %v", tc.text, got, tc.want) |
| 30 | } | 39 | } |
| 31 | }) | 40 | }) |
| 32 | } | 41 | } |
| 33 | } | 42 | } |
| 43 | |||
| 44 | // A merged merge request's description closes an issue in another | ||
| 45 | // repository only when the merger holds write there. This drives the | ||
| 46 | // same target resolution the commit path uses, without needing git. | ||
| 47 | func TestMRDescriptionClosesAcrossRepos(t *testing.T) { | ||
| 48 | f := newOrgFixture(t) | ||
| 49 | libIssue, _ := f.st.CreateIssue(f.priv.ID, f.alice, "in priv", "", "md") | ||
| 50 | appIssue, _ := f.st.CreateIssue(f.app.ID, f.alice, "in app", "", "md") | ||
| 51 | _ = libIssue | ||
| 52 | _ = appIssue | ||
| 53 | mr := func(n int64, title string) store.MR { | ||
| 54 | return store.MR{Number: n, Title: title, Body: ""} | ||
| 55 | } | ||
| 56 | // carol cannot write acme/priv: the issue stays open and no comment | ||
| 57 | // lands. | ||
| 58 | ProcessMRDescription(f.st, f.app, mr(1, "Closes acme/priv#1"), f.carol) | ||
| 59 | if iss, _ := f.st.IssueByNumber(f.priv.ID, 1); iss.State != "open" { | ||
| 60 | t.Fatal("outsider closed a private repo's issue") | ||
| 61 | } | ||
| 62 | // alice can: it closes with a comment naming the source repository. | ||
| 63 | ProcessMRDescription(f.st, f.app, mr(2, "Closes acme/priv#1"), f.alice) | ||
| 64 | iss, _ := f.st.IssueByNumber(f.priv.ID, 1) | ||
| 65 | if iss.State != "closed" { | ||
| 66 | t.Fatal("writer did not close across repos") | ||
| 67 | } | ||
| 68 | comments, _ := f.st.ListIssueComments(iss.ID) | ||
| 69 | if len(comments) != 1 || !strings.Contains(comments[0].Body, "(/alice/app/mrs/2)") { | ||
| 70 | t.Fatalf("close comment = %+v", comments) | ||
| 71 | } | ||
| 72 | // An unknown path is text; a bare #N still acts in the source repo. | ||
| 73 | ProcessMRDescription(f.st, f.app, mr(3, "Closes nobody/nothing#1 and closes #1"), f.alice) | ||
| 74 | if iss, _ := f.st.IssueByNumber(f.app.ID, 1); iss.State != "closed" { | ||
| 75 | t.Fatal("bare #N stopped working") | ||
| 76 | } | ||
| 77 | } | ||