forked from akuity/kargo-render
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidation.go
66 lines (57 loc) · 1.86 KB
/
validation.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
package bookkeeper
import (
"regexp"
"strings"
"github.com/pkg/errors"
)
func validateAndCanonicalizeRequest(req RenderRequest) (RenderRequest, error) {
req.RepoURL = strings.TrimSpace(req.RepoURL)
if req.RepoURL == "" {
return req, errors.New("validation failed: RepoURL is a required field")
}
repoURLRegex :=
regexp.MustCompile(`^(?:(?:(?:https?://)|(?:git@))[\w:/\-\.\?=@&%]+)$`)
if !repoURLRegex.MatchString(req.RepoURL) {
return req, errors.Errorf(
"validation failed: RepoURL %q does not appear to be a valid git "+
"repository URL",
req.RepoURL,
)
}
// TODO: Should this be required? I think some git providers don't require
// this if the password is a bearer token -- e.g. such as in the case of a
// GitHub personal access token.
req.RepoCreds.Username = strings.TrimSpace(req.RepoCreds.Username)
req.RepoCreds.Password = strings.TrimSpace(req.RepoCreds.Password)
if req.RepoCreds.Password == "" {
return req, errors.New(
"validation failed: RepoCreds.Password is a required field",
)
}
req.Ref = strings.TrimSpace(req.Ref)
req.TargetBranch = strings.TrimSpace(req.TargetBranch)
if req.TargetBranch == "" {
return req,
errors.New("validation failed: TargetBranch is a required field")
}
targetBranchRegex := regexp.MustCompile(`^(?:[\w\.-]+\/?)*\w$`)
if !targetBranchRegex.MatchString(req.TargetBranch) {
return req, errors.Errorf(
"validation failed: TargetBranch %q is an invalid branch name",
req.TargetBranch,
)
}
req.TargetBranch = strings.TrimPrefix(req.TargetBranch, "refs/heads/")
if len(req.Images) > 0 {
for i := range req.Images {
req.Images[i] = strings.TrimSpace(req.Images[i])
if req.Images[i] == "" {
return req, errors.New(
"validation failed: Images must not contain any empty strings",
)
}
}
}
req.CommitMessage = strings.TrimSpace(req.CommitMessage)
return req, nil
}