69 lines
1.7 KiB
Go
69 lines
1.7 KiB
Go
package validation
|
|
|
|
import (
|
|
"path"
|
|
"strings"
|
|
)
|
|
|
|
const defaultFileSizeLimit int64 = 1_000_000
|
|
|
|
func checkFileSize(tree *CandidateTree, settings *FileSizeSettings) error {
|
|
limit := defaultFileSizeLimit
|
|
if settings.LimitBytes != nil {
|
|
limit = *settings.LimitBytes
|
|
}
|
|
for _, entry := range tree.inputEntries {
|
|
if entry.Type != "blob" || !entry.HasSize || excludedByPath(entry.Path, settings.Exclusions) {
|
|
continue
|
|
}
|
|
if entry.Size > limit {
|
|
return &Failure{
|
|
Check: FileSize,
|
|
Findings: []Finding{{
|
|
Path: entry.Path,
|
|
Message: "committed file exceeds the selected size limit",
|
|
}},
|
|
Measurement: &FileSizeMeasurement{Bytes: entry.Size, LimitBytes: limit},
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func fileSizeSettingsForModule(settings *FileSizeSettings, modulePath string) *FileSizeSettings {
|
|
if settings == nil {
|
|
return nil
|
|
}
|
|
selected := *settings
|
|
moduleRelative := make([]string, len(settings.Exclusions))
|
|
for index, exclusion := range settings.Exclusions {
|
|
if modulePath == "." || path.IsAbs(exclusion) || exclusion == "" {
|
|
moduleRelative[index] = exclusion
|
|
continue
|
|
}
|
|
moduleRelative[index] = modulePath + "/" + exclusion
|
|
}
|
|
selected.Exclusions = normalizedExclusions(moduleRelative)
|
|
return &selected
|
|
}
|
|
|
|
func normalizedExclusions(exclusions []string) []string {
|
|
result := make([]string, 0, len(exclusions))
|
|
for _, exclusion := range exclusions {
|
|
if exclusion == "" {
|
|
continue
|
|
}
|
|
result = append(result, path.Clean(exclusion))
|
|
}
|
|
return result
|
|
}
|
|
|
|
func excludedByPath(filePath string, exclusions []string) bool {
|
|
for _, exclusion := range exclusions {
|
|
if exclusion == "." || filePath == exclusion || strings.HasPrefix(filePath, exclusion+"/") {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|