fix(healthcheck/forbidden): relative path
Some checks failed
build / build (push) Failing after 1m5s
build / trigger-build-image (push) Has been skipped
build / build (pull_request) Failing after 1m5s
build / trigger-build-image (pull_request) Has been skipped

This commit is contained in:
zzjc1234 2024-10-17 15:26:47 +08:00
parent 83b6cb6b2c
commit 64cbeef111
2 changed files with 26 additions and 18 deletions

View File

@ -59,7 +59,7 @@ func main() {
if err != nil {
fmt.Printf("### Repo Size Check Failed:\n%s\n", err.Error())
}
err = healthcheck.ForbiddenCheck(*rootDir, gitWhitelist)
err = healthcheck.ForbiddenCheck(*rootDir)
if err != nil {
fmt.Printf("### Forbidden File Check Failed:\n%s\n", err.Error())
}

View File

@ -11,11 +11,12 @@ import (
)
// getForbiddens retrieves a list of forbidden files in the specified root directory.
// It searches for files that do not match the specified regex patterns in the given file list.
func getForbiddens(root string, fileList []string) ([]string, error) {
// It searches for files that match the specified ignore patterns in the .gitignore file.
func getForbiddens(root string) ([]string, error) {
var matches []string
ignore, err := gitignore.NewFromFile("./.gitignore")
// Create a gitignore instance from the .gitignore file
ignore, err := gitignore.NewFromFile(filepath.Join(root, ".gitignore"))
if err != nil {
return nil, err
}
@ -25,29 +26,36 @@ func getForbiddens(root string, fileList []string) ([]string, error) {
return err
}
if info.IsDir() && (info.Name() == ".") {
if info.IsDir() {
if info.Name() == ".git" {
return filepath.SkipDir
} else if info.Name() == root {
return nil
}
if info.IsDir() && (info.Name() == ".git") {
return filepath.SkipDir
} else {
match := ignore.Relative(info.Name(), true)
if match != nil {
if match.Ignore() {
}
// Get the relative path to the git repo root
relPath, err := filepath.Rel(root, path)
if err != nil {
return err
}
match := ignore.Relative(relPath, true)
// Check if the relative file path should be ignored based on the .gitignore rules
if match != nil && match.Ignore() {
matches = append(matches, path)
}
}
}
return nil
})
return matches, err
}
// forbiddenCheck checks for forbidden files in the specified root directory.
// ForbiddenCheck checks for forbidden files in the specified root directory.
// It prints the list of forbidden files found, along with instructions on how to fix them.
func ForbiddenCheck(rootDir string, regexList []string) error {
forbids, err := getForbiddens(rootDir, regexList)
func ForbiddenCheck(rootDir string) error {
forbids, err := getForbiddens(rootDir)
if err != nil {
slog.Error("getting forbiddens", "error", err)
return fmt.Errorf("error getting forbiddens: %w", err)