BlueGit

timfa bluegit

Files

bluegit / internal / server / quick_apply.go

quick_apply.go

feat: lees .bluegit-sjablonen en klikbare takenlijsten
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
79722e46 History
quick_apply.go 4.1 KiB Download Raw
1package server
2
3import (
4 "context"
5 "strings"
6
7 "gitlab.com/Timfa/bluegit/internal/quick"
8 "gitlab.com/Timfa/bluegit/internal/store"
9)
10
11type quickBase struct {
12 Labels []string
13 Assignees []string
14 Milestone string
15 Due string
16 Confidential bool
17}
18
19type quickResult struct {
20 Body string
21 LabelIDs []int64
22 AssigneeIDs []int64
23 MilestoneID int64
24 Due string
25 Confidential bool
26 Labels bool
27 Assignees bool
28 Meta bool
29}
30
31func (q quickResult) touched() bool {
32 return q.Labels || q.Assignees || q.Meta
33}
34
35// foldQuick haalt quick actions uit de tekst als de gebruiker ze mag uitvoeren.
36// Een regel zonder recht blijft staan. Een onbekend label of een onbekende
37// gebruiker is een fout en er wordt nog niets opgeslagen.
38func (s *Server) foldQuick(ctx context.Context, repo store.Repo, body string, canTriage, creating bool, base quickBase) (quickResult, error) {
39 labels := append([]string{}, base.Labels...)
40 assignees := append([]string{}, base.Assignees...)
41 mile := base.Milestone
42 due := base.Due
43 conf := base.Confidential
44 var kept []string
45 var touchL, touchA, touchM bool
46 for _, p := range quick.Scan(body) {
47 if p.Cmd == nil || !allowQuick(p.Cmd.Kind, canTriage, creating) {
48 kept = append(kept, p.Text)
49 continue
50 }
51 switch p.Cmd.Kind {
52 case "label":
53 touchL = true
54 for _, name := range p.Cmd.Names {
55 if !hasName(labels, name) {
56 labels = append(labels, name)
57 }
58 }
59 case "unlabel":
60 touchL = true
61 labels = dropNames(labels, p.Cmd.Names)
62 case "assign":
63 touchA = true
64 for _, name := range p.Cmd.Names {
65 if !hasFold(assignees, name) {
66 assignees = append(assignees, name)
67 }
68 }
69 case "unassign":
70 touchA = true
71 assignees = dropFold(assignees, p.Cmd.Names)
72 case "milestone":
73 touchM = true
74 mile = p.Cmd.Names[0]
75 case "due":
76 touchM = true
77 due = p.Cmd.Due
78 case "confidential":
79 touchM = true
80 conf = true
81 }
82 }
83 res := quickResult{Body: strings.Join(kept, "\n"), Due: due, Confidential: conf, Labels: touchL, Assignees: touchA, Meta: touchM}
84 if touchL {
85 ids, err := s.resolveLabels(ctx, repo.ID, labels)
86 if err != nil {
87 return quickResult{}, err
88 }
89 res.LabelIDs = ids
90 }
91 if touchA {
92 ids, err := s.resolveAssignees(ctx, repo, assignees)
93 if err != nil {
94 return quickResult{}, err
95 }
96 res.AssigneeIDs = ids
97 }
98 if touchM {
99 id, err := s.resolveMilestone(ctx, repo.ID, mile)
100 if err != nil {
101 return quickResult{}, err
102 }
103 if err := store.ValidDue(due); err != nil {
104 return quickResult{}, err
105 }
106 res.MilestoneID = id
107 }
108 return res, nil
109}
110
111func (s *Server) persistQuick(ctx context.Context, repo store.Repo, issueID int64, res quickResult) error {
112 if res.Labels {
113 if err := s.Store.SetIssueLabels(ctx, repo.ID, issueID, res.LabelIDs); err != nil {
114 return err
115 }
116 }
117 if res.Assignees {
118 if err := s.Store.SetIssueAssignees(ctx, repo.ID, issueID, res.AssigneeIDs); err != nil {
119 return err
120 }
121 }
122 if res.Meta {
123 if err := s.Store.SetIssueMeta(ctx, repo.ID, issueID, res.MilestoneID, res.Confidential, res.Due); err != nil {
124 return err
125 }
126 }
127 return nil
128}
129
130func allowQuick(kind string, canTriage, creating bool) bool {
131 if canTriage {
132 return true
133 }
134 return creating && kind == "confidential"
135}
136
137func hasName(list []string, name string) bool {
138 for _, item := range list {
139 if item == name {
140 return true
141 }
142 }
143 return false
144}
145
146func hasFold(list []string, name string) bool {
147 for _, item := range list {
148 if strings.EqualFold(item, name) {
149 return true
150 }
151 }
152 return false
153}
154
155func dropNames(list, gone []string) []string {
156 dead := map[string]bool{}
157 for _, name := range gone {
158 dead[name] = true
159 }
160 var out []string
161 for _, name := range list {
162 if !dead[name] {
163 out = append(out, name)
164 }
165 }
166 return out
167}
168
169func dropFold(list, gone []string) []string {
170 var out []string
171 for _, name := range list {
172 if !hasFold(gone, name) {
173 out = append(out, name)
174 }
175 }
176 return out
177}
178
179func quickFromIssue(iss store.Issue) quickBase {
180 return quickBase{
181 Labels: labelNames(iss.Labels), Assignees: append([]string{}, iss.Assignees...),
182 Milestone: iss.Milestone, Due: iss.DueOn, Confidential: iss.Confidential,
183 }
184}