-
Notifications
You must be signed in to change notification settings - Fork 2
/
query.go
217 lines (187 loc) · 4.89 KB
/
query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package gosnap
import (
"errors"
"fmt"
"image"
"image/color"
"github.com/ecwid/gosnap/registry"
)
type Published struct {
Key string
}
func (p Published) Error() string {
return fmt.Sprint("snapshot published: ", defaultRegistry.Resolve(p.Key))
}
type subImage interface {
SubImage(r image.Rectangle) image.Image
}
type mask struct {
Rect image.Rectangle
Color color.Color
}
type Query struct {
matcher Matcher
key string
data map[string]string
masks []mask
}
func (f Matcher) New(snapshot string) Query {
return Query{
key: snapshot,
matcher: f,
data: f.data,
}
}
func (q Query) Mask(rectangle image.Rectangle, color color.Color) Query {
q.masks = append(q.masks, mask{Rect: rectangle, Color: color})
return q
}
func (q Query) Metadata(key string, value any) Query {
q.data[key] = fmt.Sprint(value)
return q
}
func (q Query) baselineKey() string {
return q.matcher.prependPathString() + q.key
}
func (q Query) makeTargetHash(target image.Image, x, y int) Hash {
if q.matcher.normalize {
croppedTarget := target.(subImage).SubImage(image.Rect(0, 0, x, y))
return MakeHash(croppedTarget, q.matcher.hashSize)
}
return MakeHash(target, q.matcher.hashSize)
}
func (q Query) Match(target image.Image) error {
if target == nil {
return errors.New("no target (actual) image set")
}
if q.key == "" {
return errors.New("baseline key is required")
}
if q.matcher.approvalEnabled && q.matcher.approvalKey == "" {
return errors.New("approvalEnabled but approvalKey not defined")
}
for _, mask := range q.masks {
target = Masked{
Image: target,
Rect: mask.Rect,
Color: mask.Color,
}
}
var (
err error
baselineKey = q.baselineKey()
baseline = new(Snapshot)
)
// get baseline hash
err = baseline.Head(baselineKey)
// force update baseline without matching and exit
if errors.Is(err, registry.ErrNoSuchKey) || q.matcher.forceUpdate {
hash := MakeHash(target, q.matcher.hashSize)
return q.uploadBaseline(baselineKey, hash, target)
}
if err != nil {
return err
}
// Comparing the baseline with target
x, y := baseline.GetSize()
targetHash := q.makeTargetHash(target, x, y)
xorHash, equal := baseline.Hash.equal(targetHash, q.matcher.distance)
if equal {
return nil
}
// update baseline and exit
if q.matcher.update {
return q.uploadBaseline(baselineKey, targetHash, target)
}
// check if approved
if q.matcher.approvalEnabled {
approvals := Approvals{}
err = approvals.Pull(q.matcher.approvalKey)
if err != nil && !errors.Is(err, registry.ErrNoSuchKey) {
return errors.Join(errors.New("can't pull approvals"), err)
}
if len(ApprovalsContains(approvals.Value, xorHash, q.matcher.distance)) > 0 {
return nil
}
}
return Change{
Key: baselineKey,
XorHash: xorHash,
TargetHash: targetHash,
Data: q.data,
target: target,
}
}
func (q Query) UploadChange(value error) error {
var err error
if change, ok := value.(Change); ok {
// upload target image
change.Target, err = q.uploadSnapshot(change.TargetHash, change.target)
if err != nil {
return errors.Join(err, change)
}
// no hash matches so we need download the baseline image to make diff between them
baseline := new(Snapshot)
err = baseline.Pull(change.Key)
if err != nil {
return errors.Join(err, change)
}
// upload diff overlay image
change.Overlay, err = q.uploadSnapshot(change.XorHash, overlay(baseline.Value, change.target))
if err != nil {
return errors.Join(err, change)
}
//
return q.matcher.addChangeForApproval(change)
}
return value
}
func ApprovalsContains(approvals []Approval, hash Hash, distance int) []Approval {
for _, tar := range approvals {
if hash.Equal(tar.Hash, distance) {
return []Approval{tar}
}
}
/**/
for _, tar1 := range approvals {
for _, tar2 := range approvals {
if hash.Equal(tar1.Hash.Or(tar2.Hash), distance) {
return []Approval{tar1, tar2}
}
}
}
return []Approval{}
}
func (q Query) uploadSnapshot(hash Hash, image image.Image) (key string, err error) {
key = q.matcher.generateKey()
err = q.pushSnapshot(key, hash, image)
return key, err
}
func (q Query) uploadBaseline(key string, newHash Hash, newImage image.Image) error {
if key == "" {
return errors.New("can't update baseline snapshot due key is empty")
}
err := q.pushSnapshot(key, newHash, newImage)
if err != nil {
return err
}
return Published{Key: key}
}
func (q Query) pushSnapshot(key string, hash Hash, image image.Image) (err error) {
upload := Snapshot{
Hash: hash,
Value: image,
Metadata: map[string]string{},
}
for k, v := range q.data {
upload.Metadata[k] = v
}
if err = upload.Push(key); err != nil {
err = errors.Join(errors.New("can't upload snapshot image"), err)
}
return err
}
// Compare match with baseline and upload target, overlay and approval report
func (q Query) Compare(actual image.Image) error {
return q.UploadChange(q.Match(actual))
}