feat: skip gitlocallist

This commit is contained in:
zzjc1234 2024-06-20 20:24:39 +08:00 committed by Boming Zhang
parent 1c72d00437
commit 8ba39d521a
GPG Key ID: D47306D7062CDA9D
2 changed files with 37 additions and 4 deletions

View File

@ -40,6 +40,7 @@ func main() {
var gitWhitelist, metaFile, releaseTags []string var gitWhitelist, metaFile, releaseTags []string
rootDir := flag.String("root", "", "") rootDir := flag.String("root", "", "")
repo := flag.String("repo", "", "") repo := flag.String("repo", "", "")
localList := flag.String("localList", "", "")
droneBranch := flag.String("droneBranch", "", "") droneBranch := flag.String("droneBranch", "", "")
releaseCategories := flag.String("releaseCategories", "", "") releaseCategories := flag.String("releaseCategories", "", "")
releaseNumber := flag.Int("releaseNumber", 0, "") releaseNumber := flag.Int("releaseNumber", 0, "")
@ -55,7 +56,7 @@ func main() {
if err != nil { if err != nil {
fmt.Printf("## Repo Size Check Failed:\n%s\n", err.Error()) fmt.Printf("## Repo Size Check Failed:\n%s\n", err.Error())
} }
err = healthcheck.ForbiddenCheck(*rootDir, gitWhitelist, *repo, *droneBranch) err = healthcheck.ForbiddenCheck(*rootDir, gitWhitelist, localList, *repo, *droneBranch)
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())
} }

View File

@ -1,6 +1,7 @@
package healthcheck package healthcheck
import ( import (
"bufio"
"fmt" "fmt"
"log/slog" "log/slog"
"os" "os"
@ -9,9 +10,18 @@ import (
"strings" "strings"
) )
func inString(str1 string, strList []string) bool {
for _, str := range strList {
if str1 == str {
return true
}
}
return false
}
// getForbiddens retrieves a list of forbidden files in the specified root directory. // 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. // 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) { func getForbiddens(root string, fileList []string, localList string) ([]string, error) {
var matches []string var matches []string
var regexList []*regexp.Regexp var regexList []*regexp.Regexp
@ -20,6 +30,24 @@ func getForbiddens(root string, fileList []string) ([]string, error) {
return nil, err return nil, err
} }
var dirs []string
if localList != "" {
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 { err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
return err return err
@ -28,6 +56,10 @@ func getForbiddens(root string, fileList []string) ([]string, error) {
if info.IsDir() { if info.IsDir() {
if info.Name() == ".git" || info.Name() == ".gitea" || info.Name() == "ci" { if info.Name() == ".git" || info.Name() == ".gitea" || info.Name() == "ci" {
return filepath.SkipDir return filepath.SkipDir
} else if localList != "" {
if inString(info.Name(), dirs) {
return filepath.SkipDir
}
} else { } else {
return nil return nil
} }
@ -52,8 +84,8 @@ func getForbiddens(root string, fileList []string) ([]string, error) {
// 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. // It prints the list of forbidden files found, along with instructions on how to fix them.
func ForbiddenCheck(rootDir string, regexList []string, repo string, droneBranch string) error { func ForbiddenCheck(rootDir string, regexList []string, localList string, repo string, droneBranch string) error {
forbids, err := getForbiddens(rootDir, regexList) forbids, err := getForbiddens(rootDir, regexList, localList)
if err != nil { if err != nil {
slog.Error("getting forbiddens", "error", err) slog.Error("getting forbiddens", "error", err)
return fmt.Errorf("error getting forbiddens: %w", err) return fmt.Errorf("error getting forbiddens: %w", err)