feat: repo health check (#16) #17

Merged
张泊明518370910136 merged 37 commits from file_check into master 2024-09-11 20:09:27 +08:00
4 changed files with 18 additions and 35 deletions
Showing only changes of commit 07dd356e05 - Show all commits

View File

@ -59,7 +59,7 @@ func NonAsciiMsg(root string) error {
}
}
if len(nonAsciiMsgs) > 0 {
return fmt.Errorf("Non-ASCII characters in commit messages:\n" + strings.Join(nonAsciiMsgs, "\n"))
return fmt.Errorf("Non-ASCII characters in commit messages:\n%s", strings.Join(nonAsciiMsgs, "\n"))
}
return nil
}

View File

@ -77,16 +77,14 @@ func ForbiddenCheck(rootDir string, regexList []string, localList string, repo s
return fmt.Errorf("error getting forbiddens: %w", err)
}
var message string
if len(forbids) > 0 {
message = "The following forbidden files were found: " +
strings.Join(forbids, ", ") +
"\n\nTo fix it, first make a backup of your repository and then run the following commands:\nfor i in " +
strings.Join(forbids, " ") +
fmt.Sprint("; do git filter-repo --force --invert-paths --path \"$i\"; done\ngit remote add origin ",
repo, "\ngit push --set-upstream origin ", droneBranch, " --force")
return fmt.Errorf(message)
return fmt.Errorf("The following forbidden files were found: %s\n\nTo fix it, first make a backup of your repository and then run the following commands:\nfor i in %s%s",
strings.Join(forbids, ", "),
strings.Join(forbids, " "),
fmt.Sprint(
"; do git filter-repo --force --invert-paths --path \"$i\"; done\ngit remote add origin ",
repo, "\ngit push --set-upstream origin ",
droneBranch, " --force"))
}
return nil
}

View File

@ -86,7 +86,8 @@ func NonAsciiFiles(root string, localList string) error {
return fmt.Errorf("error getting non-ascii: %w", err)
}
if len(nonAscii) > 0 {
return fmt.Errorf("Non-ASCII characters found in the following files:\n" + strings.Join(nonAscii, "\n"))
return fmt.Errorf("Non-ASCII characters found in the following files:\n%s",
strings.Join(nonAscii, "\n"))
}
return nil
}

View File

@ -29,47 +29,31 @@ func getChecksum(filePath string) (string, error) {
}
// checkFileChecksum checks if a single file's checksum matches the expected value
func checkFileChecksum(rootDir, fileName, expectedChecksum string) (bool, string) {
func checkFileChecksum(rootDir, fileName, expectedChecksum string) error {
filePath := filepath.Join(rootDir, strings.TrimSpace(fileName))
actualChecksum, err := getChecksum(filePath)
if err != nil {
return false, fmt.Sprintf("Error reading file %s: %v", filePath, err)
return fmt.Errorf("Error reading file %s: %v", filePath, err)
}
if actualChecksum == expectedChecksum {
return true, ""
} else {
return false, fmt.Sprintf("Checksum for %s failed. Expected %s, but got %s. Please revert your changes or contact the teaching team if you have a valid reason for adjusting them.", filePath, expectedChecksum, actualChecksum)
if actualChecksum != expectedChecksum {
return fmt.Errorf("Checksum for %s failed. Expected %s, but got %s. Please revert your changes or contact the teaching team if you have a valid reason for adjusting them.", filePath, expectedChecksum, actualChecksum)
}
bomingzh marked this conversation as resolved Outdated

remove the comment

remove the comment
return nil
}
func VerifyFiles(rootDir string, checkFileNameList string, checkFileSumList string) error {
if len(checkFileNameList) == 0 {
return nil
os.Exit(1)
}
bomingzh marked this conversation as resolved Outdated

ditto

ditto
fileNames := strings.Split(checkFileNameList, ",")
checkSums := strings.Split(checkFileSumList, ",")
allPassed := true
var errorMessages []string
// Check each file's checksum
for i, fileName := range fileNames {
expectedChecksum := strings.TrimSpace(checkSums[i])
passed, message := checkFileChecksum(rootDir, fileName, expectedChecksum)
if message != "" {
return fmt.Errorf(message)
err := checkFileChecksum(rootDir, fileName, expectedChecksum)
if err != nil {
return err
bomingzh marked this conversation as resolved Outdated

remove it

remove it
}
if !passed {
allPassed = false
errorMessages = append(errorMessages, message)
}
}
if allPassed {
return nil
} else {
return fmt.Errorf("Some checksums failed. Please review the errors below:")
}
return nil
}