-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathfields.go
More file actions
257 lines (214 loc) · 8.51 KB
/
Copy pathfields.go
File metadata and controls
257 lines (214 loc) · 8.51 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package publiccode
import (
"fmt"
"net/url"
"strings"
"github.com/alranel/go-vcsurl/v2"
urlutil "github.com/italia/publiccode-parser-go/v5/internal"
publiccodeValidator "github.com/italia/publiccode-parser-go/v5/validators"
)
type validateFn func(publiccode PublicCode, parser Parser, network bool) error
// validateFields validates publiccode with additional rules not validatable
// with a simple YAML schema.
// It returns any error encountered as ValidationResults.
func validateFieldsV0(publiccode PublicCode, parser Parser, network bool) error { //nolint:maintidx
publiccodev0 := publiccode.(*PublicCodeV0)
var vr ValidationResults
if publiccodev0.URL != nil && network {
if reachable, err := parser.isReachable(*(*url.URL)(publiccodev0.URL), network); !reachable {
vr = append(vr, newValidationError("url", "'%s' not reachable: %s", publiccodev0.URL, err.Error()))
}
if !vcsurl.IsRepo((*url.URL)(publiccodev0.URL)) {
vr = append(vr, newValidationError("url", "is not a valid code repository"))
}
}
if publiccodev0.LandingURL != nil {
if reachable, err := parser.isReachable(*(*url.URL)(publiccodev0.LandingURL), network); !reachable {
vr = append(vr, newValidationError(
"landingURL",
"'%s' not reachable: %s", publiccodev0.LandingURL, err.Error(),
))
}
}
if publiccodev0.Roadmap != nil {
if reachable, err := parser.isReachable(*(*url.URL)(publiccodev0.Roadmap), network); !reachable {
vr = append(vr, newValidationError(
"roadmap",
"'%s' not reachable: %s", publiccodev0.Roadmap, err.Error(),
))
}
}
if publiccodev0.Logo != nil && *publiccodev0.Logo != "" {
if _, err := isRelativePathOrURL(*publiccodev0.Logo, "logo"); err != nil {
vr = append(vr, err)
} else if !parser.disableExternalChecks {
u, isGitRepo := toCodeHostingURL(*publiccodev0.Logo, parser.currentBaseURL, parser.allowLocalGitClone)
validLogo, err := parser.validLogo(u, network, isGitRepo)
if !validLogo {
vr = append(vr, newValidationError("logo", err.Error()))
}
}
}
if publiccodev0.MonochromeLogo != nil && *publiccodev0.MonochromeLogo != "" {
vr = append(vr, ValidationWarning{"monochromeLogo", "This key is DEPRECATED and will be removed in the future. Use 'logo' instead", 0, 0})
if _, err := isRelativePathOrURL(*publiccodev0.MonochromeLogo, "monochromeLogo"); err != nil {
vr = append(vr, err)
} else if !parser.disableExternalChecks {
u, isGitRepo := toCodeHostingURL(*publiccodev0.MonochromeLogo, parser.currentBaseURL, parser.allowLocalGitClone)
validLogo, err := parser.validLogo(u, network, isGitRepo)
if !validLogo {
vr = append(vr, newValidationError("monochromeLogo", err.Error()))
}
}
}
if publiccodev0.IntendedAudience != nil {
// This is not ideal, but we need to revalidate the countries
// here, because otherwise we could get a warning and the advice
// to use uppercase on an invalid country.
validate := publiccodeValidator.New()
if publiccodev0.IntendedAudience.Countries != nil {
for i, c := range *publiccodev0.IntendedAudience.Countries {
if validate.Var(c, "iso3166_1_alpha2_lower_or_upper") == nil && c == strings.ToLower(c) {
vr = append(vr, ValidationWarning{
fmt.Sprintf("intendedAudience.countries[%d]", i),
fmt.Sprintf("Lowercase country codes are DEPRECATED. Use uppercase instead ('%s')", strings.ToUpper(c)),
0, 0,
})
}
}
}
if publiccodev0.IntendedAudience.UnsupportedCountries != nil {
for i, c := range *publiccodev0.IntendedAudience.UnsupportedCountries {
if validate.Var(c, "iso3166_1_alpha2_lower_or_upper") == nil && c == strings.ToLower(c) {
vr = append(vr, ValidationWarning{
fmt.Sprintf("intendedAudience.unsupportedCountries[%d]", i),
fmt.Sprintf("Lowercase country codes are DEPRECATED. Use uppercase instead ('%s')", strings.ToUpper(c)),
0, 0,
})
}
}
}
}
if publiccodev0.Legal.AuthorsFile != nil {
vr = append(vr, ValidationWarning{"legal.authorsFile", "This key is DEPRECATED and will be removed in the future. It's safe to drop it", 0, 0})
if _, err := isRelativePathOrURL(*publiccodev0.Legal.AuthorsFile, "legal.authorsFile"); err != nil {
vr = append(vr, err)
} else if !parser.disableExternalChecks {
u, isGitRepo := toCodeHostingURL(*publiccodev0.Legal.AuthorsFile, parser.currentBaseURL, parser.allowLocalGitClone)
exists, err := parser.fileExists(u, network, isGitRepo)
if !exists {
vr = append(vr, newValidationError("legal.authorsFile", "'%s' does not exist: %s", urlutil.DisplayURL(&u), err.Error()))
}
}
}
if publiccodev0.Legal.RepoOwner != nil {
vr = append(vr, ValidationWarning{"legal.repoOwner", "This key is DEPRECATED and will be removed in the future. Use 'organisation.name' instead", 0, 0})
}
if publiccodev0.InputTypes != nil {
vr = append(vr, ValidationWarning{"inputTypes", "This key is DEPRECATED and will be removed in the future. It's safe to drop it", 0, 0})
}
if publiccodev0.OutputTypes != nil {
vr = append(vr, ValidationWarning{"outputTypes", "This key is DEPRECATED and will be removed in the future. It's safe to drop it", 0, 0})
}
for lang, desc := range publiccodev0.Description {
if publiccodev0.Description == nil {
publiccodev0.Description = make(map[string]DescV0)
}
if len(desc.GenericName) > 0 {
vr = append(vr, ValidationWarning{
fmt.Sprintf("description.%s.genericName", lang),
"This key is DEPRECATED and will be removed in the future. It's safe to drop it", 0, 0,
})
}
if !parser.disableExternalChecks && network && desc.Documentation != nil {
if reachable, err := parser.isReachable(*(*url.URL)(desc.Documentation), network); !reachable {
vr = append(vr, newValidationError(
fmt.Sprintf("description.%s.documentation", lang),
"'%s' not reachable: %s", desc.Documentation, err.Error(),
))
}
}
if !parser.disableExternalChecks && network && desc.APIDocumentation != nil {
if reachable, err := parser.isReachable(*(*url.URL)(desc.APIDocumentation), network); !reachable {
vr = append(vr, newValidationError(
fmt.Sprintf("description.%s.apiDocumentation", lang),
"'%s' not reachable: %s", desc.APIDocumentation, err.Error(),
))
}
}
for i, v := range desc.Screenshots {
keyName := fmt.Sprintf("description.%s.screenshots[%d]", lang, i)
if _, err := isRelativePathOrURL(v, keyName); err != nil {
vr = append(vr, err)
} else if !parser.disableExternalChecks {
u, isGitRepo := toCodeHostingURL(v, parser.currentBaseURL, parser.allowLocalGitClone)
isImage, err := parser.isImageFile(u, network, isGitRepo)
if !isImage {
vr = append(vr, newValidationError(
keyName,
"'%s' is not an image: %s", v, err.Error(),
))
}
}
}
for i, v := range desc.Videos {
_, err := parser.isOEmbedURL((*url.URL)(v))
if err != nil {
vr = append(vr, newValidationError(
fmt.Sprintf("description.%s.videos[%d]", lang, i),
"'%s' is not a valid video URL supporting oEmbed: %s", v, err.Error(),
))
}
}
}
it := publiccodev0.IT
if publiccodev0.It != nil {
vr = append(vr, ValidationWarning{
"it",
"Lowercase country codes are DEPRECATED and will be removed in the future. Use 'IT' instead", 0, 0,
})
it = publiccodev0.It
}
if publiccodev0.IT != nil && publiccodev0.It != nil {
vr = append(vr, newValidationError("it", "'IT' key already present. Remove this key"))
it = publiccodev0.IT
}
if it != nil {
if it.Conforme != nil {
vr = append(vr, ValidationWarning{
"IT.conforme",
"This key is DEPRECATED and will be removed in the future. It's safe to drop it", 0, 0,
})
}
if it.Riuso.CodiceIPA != "" {
validate := publiccodeValidator.New()
if validate.Var(it.Riuso.CodiceIPA, "is_italian_ipa_code") == nil {
vr = append(vr, ValidationWarning{
"IT.riuso.codiceIPA",
fmt.Sprintf(
"This key is DEPRECATED and will be removed in the future. Use 'organisation.uri' and set it to 'urn:x-italian-pa:%s' instead",
it.Riuso.CodiceIPA,
),
0, 0,
})
}
}
}
if len(vr) == 0 {
return nil
}
return vr
}
// isRelativePathOrURL checks whether the field contains either a relative filename
// or an HTTP URL
//
//nolint:unparam
func isRelativePathOrURL(content string, keyName string) (bool, error) {
if strings.HasPrefix(content, "/") {
return false, newValidationError(keyName, "is an absolute path. Only relative paths or HTTP(s) URLs allowed")
}
if strings.HasPrefix(content, "file:") {
return false, newValidationError(keyName, "is a file:// URL. Only relative paths or HTTP(s) URLs allowed")
}
return true, nil
}