feat: repo health check (#16) #17
|  | @ -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 | ||||
| } | ||||
|  |  | |||
|  | @ -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 | ||||
| } | ||||
|  |  | |||
|  | @ -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 | ||||
| } | ||||
|  |  | |||
|  | @ -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) | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
| 
 | ||||
| func VerifyFiles(rootDir string, checkFileNameList string, checkFileSumList string) error { | ||||
| 	if len(checkFileNameList) == 0 { | ||||
| 		return nil | ||||
| 		os.Exit(1) | ||||
| 	} | ||||
| 	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 | ||||
| 		} | ||||
| 		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 | ||||
| } | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	Block a user