internal/botissue/botissue.go
121 lines · 3955 bytes
1// Package botissue keeps the issues gitbay-bot maintains for a repository:
2// dependency updates, dependency advisories, code scanning findings. Each
3// is one issue per repository, opened when something is first reported,
4// rewritten when the set changes, and closed when nothing is left.
5package botissue
6
7import (
8 "fmt"
9 "strings"
10
11 "gitbay.org/gitbay/internal/config"
12 "gitbay.org/gitbay/internal/store"
13)
14
15// Tracked is one issue and the set it reports.
16type Tracked struct {
17 Title string
18 Number int64 // the issue last opened, 0 for none
19 Record func(number int64) error
20 Empty bool // nothing to report
21 Changed bool // the set differs from the last report
22 Save func() error
23 Body func() string
24}
25
26// Maintain brings the issue in line with the set.
27func Maintain(st *store.Store, cfg config.Config, repo store.Repo, t Tracked) error {
28 issue, hasIssue := openIssue(st, repo, t.Number)
29 if t.Empty {
30 if hasIssue {
31 st.SetIssueState(issue.ID, "closed")
32 t.Record(0)
33 }
34 if t.Changed {
35 return t.Save()
36 }
37 return nil
38 }
39 // Nothing changed since the last report: leave it be, whether the issue
40 // is still open or the maintainer has closed it. Reopening on an
41 // unchanged set would make closing the issue pointless.
42 if !t.Changed {
43 return nil
44 }
45 if err := t.Save(); err != nil {
46 return err
47 }
48 body := t.Body()
49 if hasIssue {
50 if err := st.UpdateIssueText(issue.ID, nil, &body, nil); err != nil {
51 return err
52 }
53 notify(st, cfg, repo, t.Title, issue.Number, fmt.Sprintf("updated issue #%d", issue.Number), body)
54 return nil
55 }
56 author, err := st.UserByUsername(store.BotUsername)
57 if err != nil {
58 // Migration 0028 leaves the account uncreated when the name was
59 // already taken, which is the one case worth spelling out: the
60 // feature is stuck until an operator frees the name.
61 return fmt.Errorf("no %s account to author the issue (the name was taken when this instance upgraded): %w",
62 store.BotUsername, err)
63 }
64 number, err := st.CreateIssue(repo.ID, author.ID, t.Title, body, "md")
65 if err != nil {
66 return err
67 }
68 if err := t.Record(number); err != nil {
69 return err
70 }
71 st.RecordEvent(repo.ID, author.ID, "issue.created", fmt.Sprintf(`{"number":%d}`, number))
72 notify(st, cfg, repo, t.Title, number, fmt.Sprintf("opened issue #%d", number), body)
73 return nil
74}
75
76// openIssue loads the issue being maintained, if there still is one. A
77// closed issue counts as gone: reopening one the maintainer closed would be
78// arguing with them, so the next change opens a fresh issue.
79func openIssue(st *store.Store, repo store.Repo, number int64) (store.Issue, bool) {
80 if number == 0 {
81 return store.Issue{}, false
82 }
83 issue, err := st.IssueByNumber(repo.ID, number)
84 if err != nil || issue.State != "open" {
85 return store.Issue{}, false
86 }
87 return issue, true
88}
89
90// notify files an inbox row for the repo's owners and watchers and mails
91// them, the same targets and shape as an issue opened over SSH.
92// Best-effort, like every other notification.
93func notify(st *store.Store, cfg config.Config, repo store.Repo, title string, number int64, action, body string) {
94 targets, err := st.RepoNotifyTargets(repo)
95 if err != nil {
96 return
97 }
98 author, err := st.UserByUsername(store.BotUsername)
99 if err != nil {
100 return
101 }
102 recipients, err := st.NotifyRecipients(repo.ID, author.ID, targets, true)
103 if err != nil {
104 return
105 }
106 subject := fmt.Sprintf("[%s] #%d: %s", repo.Path(), number, title)
107 text := fmt.Sprintf("%s %s\n\n%s\n%s/%s/issues/%d\n", store.BotUsername, action, body,
108 strings.TrimSuffix(cfg.Server.SiteURL, "/"), repo.Path(), number)
109 path := fmt.Sprintf("%s/issues/%d", repo.Path(), number)
110 for _, id := range recipients {
111 st.AddNotice(id, repo.ID, "issue", store.BotUsername, action, path)
112 if cfg.Mail.SMTPHost == "" {
113 continue
114 }
115 email, err := st.ActivityMailAddress(id)
116 if err != nil || email == "" {
117 continue
118 }
119 st.EnqueueMail(email, subject, text)
120 }
121}