-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
validate.go
185 lines (164 loc) · 5.54 KB
/
validate.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
package main
import (
"bytes"
"fmt"
"regexp"
"strings"
"text/template"
"github.com/openshift/origin/tools/rebasehelpers/util"
)
var CommitSummaryErrorTemplate = `
The following UPSTREAM commits have invalid summaries:
{{ range .Commits }} [{{ .Sha }}] {{ .Summary }}
{{ end }}
UPSTREAM commit summaries should look like:
UPSTREAM: [non-kube-repo/name: ]<PR number|carry|drop>: description
UPSTREAM commits which revert previous UPSTREAM commits should look like:
UPSTREAM: revert: <sha>: <normal upstream format>
UPSTREAM commits are validated against the following regular expression:
{{ .Pattern }}
Examples of valid summaries:
UPSTREAM: 12345: A kube fix
UPSTREAM: coreos/etcd: 12345: An etcd fix
UPSTREAM: <carry>: A carried kube change
UPSTREAM: <drop>: A dropped kube change
UPSTREAM: revert: abcd123: coreos/etcd: 12345: An etcd fix
UPSTREAM: k8s.io/heapster: 12345: A heapster fix
`
var AllValidators = []func([]util.Commit) error{
ValidateUpstreamCommitSummaries,
ValidateUpstreamCommitsWithoutGodepsChanges,
ValidateUpstreamCommitModifiesSingleGodepsRepo,
ValidateUpstreamCommitModifiesOnlyGodeps,
ValidateUpstreamCommitModifiesOnlyDeclaredGodepRepo,
}
// ValidateUpstreamCommitsWithoutGodepsChanges returns an error if any
// upstream commits have no Godeps changes.
func ValidateUpstreamCommitsWithoutGodepsChanges(commits []util.Commit) error {
problemCommits := []util.Commit{}
for _, commit := range commits {
if commit.HasGodepsChanges() && !commit.DeclaresUpstreamChange() {
problemCommits = append(problemCommits, commit)
}
}
if len(problemCommits) > 0 {
label := "The following commits contain Godeps changes but aren't declared as UPSTREAM"
msg := renderGodepFilesError(label, problemCommits, RenderOnlyGodepsFiles)
return fmt.Errorf(msg)
}
return nil
}
// ValidateUpstreamCommitModifiesSingleGodepsRepo returns an error if any
// upstream commits have changes that span more than one Godeps repo.
func ValidateUpstreamCommitModifiesSingleGodepsRepo(commits []util.Commit) error {
problemCommits := []util.Commit{}
for _, commit := range commits {
godepsChanges, err := commit.GodepsReposChanged()
if err != nil {
return err
}
if len(godepsChanges) > 1 {
problemCommits = append(problemCommits, commit)
}
}
if len(problemCommits) > 0 {
label := "The following UPSTREAM commits modify more than one repo in their changelist"
msg := renderGodepFilesError(label, problemCommits, RenderOnlyGodepsFiles)
return fmt.Errorf(msg)
}
return nil
}
// ValidateUpstreamCommitSummaries ensures that any commits which declare to
// be upstream match the regular expressions for UPSTREAM summaries.
func ValidateUpstreamCommitSummaries(commits []util.Commit) error {
problemCommits := []util.Commit{}
for _, commit := range commits {
if commit.DeclaresUpstreamChange() && !commit.MatchesUpstreamSummaryPattern() {
problemCommits = append(problemCommits, commit)
}
}
if len(problemCommits) > 0 {
tmpl, _ := template.New("problems").Parse(CommitSummaryErrorTemplate)
data := struct {
Pattern *regexp.Regexp
Commits []util.Commit
}{
Pattern: util.UpstreamSummaryPattern,
Commits: problemCommits,
}
buffer := &bytes.Buffer{}
tmpl.Execute(buffer, data)
return fmt.Errorf(buffer.String())
}
return nil
}
// ValidateUpstreamCommitModifiesOnlyGodeps ensures that any Godeps commits
// modify ONLY Godeps files.
func ValidateUpstreamCommitModifiesOnlyGodeps(commits []util.Commit) error {
problemCommits := []util.Commit{}
for _, commit := range commits {
if commit.HasGodepsChanges() && commit.HasNonGodepsChanges() {
problemCommits = append(problemCommits, commit)
}
}
if len(problemCommits) > 0 {
label := "The following UPSTREAM commits modify files outside Godeps"
msg := renderGodepFilesError(label, problemCommits, RenderAllFiles)
return fmt.Errorf(msg)
}
return nil
}
// ValidateUpstreamCommitModifiesOnlyDeclaredGodepRepo ensures that an
// upstream commit only modifies the Godep repo the summary declares.
func ValidateUpstreamCommitModifiesOnlyDeclaredGodepRepo(commits []util.Commit) error {
problemCommits := []util.Commit{}
for _, commit := range commits {
if commit.DeclaresUpstreamChange() {
declaredRepo, err := commit.DeclaredUpstreamRepo()
if err != nil {
return err
}
reposChanged, err := commit.GodepsReposChanged()
if err != nil {
return err
}
for _, changedRepo := range reposChanged {
if !strings.Contains(changedRepo, declaredRepo) {
problemCommits = append(problemCommits, commit)
}
}
}
}
if len(problemCommits) > 0 {
label := "The following UPSTREAM commits modify Godeps repos other than the repo the commit declares"
msg := renderGodepFilesError(label, problemCommits, RenderAllFiles)
return fmt.Errorf(msg)
}
return nil
}
type CommitFilesRenderOption int
const (
RenderNoFiles CommitFilesRenderOption = iota
RenderOnlyGodepsFiles
RenderOnlyNonGodepsFiles
RenderAllFiles
)
// renderGodepFilesError formats commits and their file lists into readable
// output prefixed with label.
func renderGodepFilesError(label string, commits []util.Commit, opt CommitFilesRenderOption) string {
msg := fmt.Sprintf("%s:\n\n", label)
for _, commit := range commits {
msg += fmt.Sprintf("[%s] %s\n", commit.Sha, commit.Summary)
if opt == RenderNoFiles {
continue
}
for _, file := range commit.Files {
if opt == RenderAllFiles ||
(opt == RenderOnlyGodepsFiles && file.HasGodepsChanges()) ||
(opt == RenderOnlyNonGodepsFiles && !file.HasGodepsChanges()) {
msg += fmt.Sprintf(" - %s\n", file)
}
}
}
return msg
}