feat(healthcheck/nonascii): ignore content in gitattributes
Some checks failed
build / build (push) Failing after 47s
build / trigger-build-image (push) Has been skipped

This commit is contained in:
zzjc1234 2024-10-21 13:01:49 +08:00
parent 03d072bfb6
commit 55e2b38583
3 changed files with 23 additions and 23 deletions

View File

@ -43,6 +43,7 @@ func main() {
showVersion := flag.Bool("version", false, "print current version") showVersion := flag.Bool("version", false, "print current version")
rootDir := flag.String("root", ".", "root dir for forbidden files check") rootDir := flag.String("root", ".", "root dir for forbidden files check")
repoSize := flag.Float64("repoSize", 2, "maximum size of the repo in MiB") repoSize := flag.Float64("repoSize", 2, "maximum size of the repo in MiB")
// TODO: remove gitWhitelist, it is only for backward compatibility now
localList := flag.String("localList", "", "local file list for non-ascii file check") localList := flag.String("localList", "", "local file list for non-ascii file check")
checkFileNameList := flag.String("checkFileNameList", "", "comma-separated list of files to check") checkFileNameList := flag.String("checkFileNameList", "", "comma-separated list of files to check")
checkFileSumList := flag.String("checkFileSumList", "", "comma-separated list of expected checksums") checkFileSumList := flag.String("checkFileSumList", "", "comma-separated list of expected checksums")
@ -77,7 +78,7 @@ func main() {
if err != nil { if err != nil {
fmt.Printf("### Forbidden File Check Failed:\n%s\n", err.Error()) fmt.Printf("### Forbidden File Check Failed:\n%s\n", err.Error())
} }
err = healthcheck.NonAsciiFiles(*rootDir, *localList) err = healthcheck.NonAsciiFiles(*rootDir)
if err != nil { if err != nil {
fmt.Printf("### Non-ASCII Characters File Check Failed:\n%s\n", err.Error()) fmt.Printf("### Non-ASCII Characters File Check Failed:\n%s\n", err.Error())
} }

2
go.mod
View File

@ -5,6 +5,7 @@ go 1.23.1
require ( require (
github.com/criyle/go-judge v1.8.5 github.com/criyle/go-judge v1.8.5
github.com/denormal/go-gitignore v0.0.0-20180930084346-ae8ad1d07817 github.com/denormal/go-gitignore v0.0.0-20180930084346-ae8ad1d07817
github.com/go-git/go-billy/v5 v5.5.0
github.com/go-git/go-git/v5 v5.12.0 github.com/go-git/go-git/v5 v5.12.0
github.com/jinzhu/copier v0.4.0 github.com/jinzhu/copier v0.4.0
github.com/koding/multiconfig v0.0.0-20171124222453-69c27309b2d7 github.com/koding/multiconfig v0.0.0-20171124222453-69c27309b2d7
@ -27,7 +28,6 @@ require (
github.com/fatih/camelcase v1.0.0 // indirect github.com/fatih/camelcase v1.0.0 // indirect
github.com/fatih/structs v1.1.0 // indirect github.com/fatih/structs v1.1.0 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect github.com/kevinburke/ssh_config v1.2.0 // indirect

View File

@ -8,38 +8,33 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"unicode" "unicode"
"github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-git/v5/plumbing/format/gitattributes"
) )
// getNonAscii retrieves a list of files in the specified root directory that contain non-ASCII characters. // getNonAscii retrieves a list of files in the specified root directory that contain non-ASCII characters.
// It searches for non-ASCII characters in each file's content and returns a list of paths to files containing non-ASCII characters. // It searches for non-ASCII characters in each file's content and returns a list of paths to files containing non-ASCII characters.
func getNonAscii(root string, localList string) ([]string, error) { func getNonAscii(root string) ([]string, error) {
var nonAscii []string var nonAscii []string
fs := memfs.New()
rootPath := []string{"./"}
var emptyStr []string
var dirs []string attribute, err := gitattributes.ReadPatterns(fs, rootPath)
if err != nil {
if localList != "" { return nil, err
file, err := os.Open(localList)
if err != nil {
return nil, fmt.Errorf("Failed to open file %s: %v\n", localList, err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
dirs = append(dirs, scanner.Text())
}
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("Error reading file %s: %v\n", localList, err)
}
} }
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error { matcher := gitattributes.NewMatcher(attribute)
err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
return err return err
} }
if info.IsDir() { if info.IsDir() {
if info.Name() == ".git" || info.Name() == ".gitea" || info.Name() == "ci" || (localList != "" && inString(info.Name(), dirs)) { if info.Name() == ".git" || info.Name() == ".gitea" {
return filepath.SkipDir return filepath.SkipDir
} else { } else {
return nil return nil
@ -50,6 +45,10 @@ func getNonAscii(root string, localList string) ([]string, error) {
return nil return nil
} }
if _, ret := matcher.Match(rootPath, append(emptyStr, info.Name())); ret {
return nil
}
file, err := os.Open(path) file, err := os.Open(path)
if err != nil { if err != nil {
return err return err
@ -79,8 +78,8 @@ func getNonAscii(root string, localList string) ([]string, error) {
// nonAsciiFiles checks for non-ASCII characters in files within the specified root directory. // nonAsciiFiles checks for non-ASCII characters in files within the specified root directory.
// It prints a message with the paths to files containing non-ASCII characters, if any. // It prints a message with the paths to files containing non-ASCII characters, if any.
func NonAsciiFiles(root string, localList string) error { func NonAsciiFiles(root string) error {
nonAscii, err := getNonAscii(root, localList) nonAscii, err := getNonAscii(root)
if err != nil { if err != nil {
slog.Error("getting non-ascii", "err", err) slog.Error("getting non-ascii", "err", err)
return fmt.Errorf("error getting non-ascii: %w", err) return fmt.Errorf("error getting non-ascii: %w", err)