blob: d4f70b2c4ff21c52374b190e34b6408d58b18438 (
plain)
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
|
package domain
import (
"errors"
"regexp"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/pls"
)
type Sparkle struct {
ID ID `json:"id" jsonapi:"primary,sparkles"`
Sparklee string `json:"sparklee" jsonapi:"attr,sparklee"`
Author *User `json:"author" jsonapi:"attr,author"`
Reason string `json:"reason" jsonapi:"attr,reason"`
}
var SparkleRegex = regexp.MustCompile(`\A\s*(?P<sparklee>@\w+)\s+(?P<reason>.+)\z`)
var SparkleeIndex = SparkleRegex.SubexpIndex("sparklee")
var ReasonIndex = SparkleRegex.SubexpIndex("reason")
var ReasonIsRequired = errors.New("Reason is required")
var SparkleIsEmpty = errors.New("Sparkle is empty")
var SparkleIsInvalid = errors.New("Sparkle is invalid")
var SparkleeIsRequired = errors.New("Sparklee is required")
func NewSparkle(text string) (*Sparkle, error) {
if len(text) == 0 {
return nil, SparkleIsEmpty
}
matches := SparkleRegex.FindStringSubmatch(text)
if len(matches) == 0 {
return nil, SparkleIsInvalid
}
return &Sparkle{
ID: ID(pls.GenerateULID()),
Sparklee: matches[SparkleeIndex],
Reason: matches[ReasonIndex],
}, nil
}
func (s *Sparkle) GetID() ID {
return s.ID
}
func (s *Sparkle) SetID(id ID) error {
s.ID = id
return nil
}
func (s *Sparkle) ToGID() string {
return "gid://sparkle/Sparkle/" + s.ID.String()
}
func (s *Sparkle) Validate() error {
if s.Sparklee == "" {
return SparkleeIsRequired
}
if s.Reason == "" {
return ReasonIsRequired
}
return nil
}
|