allowlist.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package config
  2. import "regexp"
  3. // Allowlist allows a rule to be ignored for specific
  4. // regexes, paths, and/or commits
  5. type Allowlist struct {
  6. // Short human readable description of the allowlist.
  7. Description string
  8. // Regexes is slice of content regular expressions that are allowed to be ignored.
  9. Regexes []*regexp.Regexp
  10. // Paths is a slice of path regular expressions that are allowed to be ignored.
  11. Paths []*regexp.Regexp
  12. // Commits is a slice of commit SHAs that are allowed to be ignored.
  13. Commits []string
  14. }
  15. // CommitAllowed returns true if the commit is allowed to be ignored.
  16. func (a *Allowlist) CommitAllowed(c string) bool {
  17. if c == "" {
  18. return false
  19. }
  20. for _, commit := range a.Commits {
  21. if commit == c {
  22. return true
  23. }
  24. }
  25. return false
  26. }
  27. // PathAllowed returns true if the path is allowed to be ignored.
  28. func (a *Allowlist) PathAllowed(path string) bool {
  29. return anyRegexMatch(path, a.Paths)
  30. }
  31. // RegexAllowed returns true if the regex is allowed to be ignored.
  32. func (a *Allowlist) RegexAllowed(s string) bool {
  33. return anyRegexMatch(s, a.Regexes)
  34. }