feat: repo health check (#16) #17
|  | @ -59,7 +59,7 @@ func NonAsciiMsg(root string) error { | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	if len(nonAsciiMsgs) > 0 { | 	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 | 	return nil | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -77,16 +77,14 @@ func ForbiddenCheck(rootDir string, regexList []string, localList string, repo s | ||||||
| 		return fmt.Errorf("error getting forbiddens: %w", err) | 		return fmt.Errorf("error getting forbiddens: %w", err) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	var message string |  | ||||||
| 
 |  | ||||||
| 	if len(forbids) > 0 { | 	if len(forbids) > 0 { | ||||||
| 		message = "The following forbidden files were found: " + | 		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, ", "), | ||||||
| 			"\n\nTo fix it, first make a backup of your repository and then run the following commands:\nfor i in " + | 			strings.Join(forbids, " "), | ||||||
| 			strings.Join(forbids, " ") + | 			fmt.Sprint( | ||||||
| 			fmt.Sprint("; do git filter-repo --force --invert-paths --path \"$i\"; done\ngit remote add origin ", | 				"; do git filter-repo --force --invert-paths --path \"$i\"; done\ngit remote add origin ", | ||||||
| 				repo, "\ngit push --set-upstream origin ", droneBranch, " --force") | 				repo, "\ngit push --set-upstream origin ", | ||||||
| 		return fmt.Errorf(message) | 				droneBranch, " --force")) | ||||||
| 	} | 	} | ||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -86,7 +86,8 @@ func NonAsciiFiles(root string, localList string) error { | ||||||
| 		return fmt.Errorf("error getting non-ascii: %w", err) | 		return fmt.Errorf("error getting non-ascii: %w", err) | ||||||
| 	} | 	} | ||||||
| 	if len(nonAscii) > 0 { | 	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 | 	return nil | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -29,47 +29,31 @@ func getChecksum(filePath string) (string, error) { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // checkFileChecksum checks if a single file's checksum matches the expected value
 | // 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)) | 	filePath := filepath.Join(rootDir, strings.TrimSpace(fileName)) | ||||||
| 	actualChecksum, err := getChecksum(filePath) | 	actualChecksum, err := getChecksum(filePath) | ||||||
| 	if err != nil { | 	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 { | ||||||
| 	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 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) |  | ||||||
| 	} | 	} | ||||||
| 
					
					bomingzh marked this conversation as resolved
					
						
						
							Outdated
						
					
				 | |||||||
|  | 	return nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func VerifyFiles(rootDir string, checkFileNameList string, checkFileSumList string) error { | func VerifyFiles(rootDir string, checkFileNameList string, checkFileSumList string) error { | ||||||
| 	if len(checkFileNameList) == 0 { | 	if len(checkFileNameList) == 0 { | ||||||
| 		return nil | 		return nil | ||||||
| 		os.Exit(1) |  | ||||||
| 	} | 	} | ||||||
| 
					
					bomingzh marked this conversation as resolved
					
						
						
							Outdated
						
					
				 
				
					
						张泊明518370910136
						commented  ditto ditto | |||||||
| 	fileNames := strings.Split(checkFileNameList, ",") | 	fileNames := strings.Split(checkFileNameList, ",") | ||||||
| 	checkSums := strings.Split(checkFileSumList, ",") | 	checkSums := strings.Split(checkFileSumList, ",") | ||||||
| 
 |  | ||||||
| 	allPassed := true |  | ||||||
| 	var errorMessages []string |  | ||||||
| 
 |  | ||||||
| 	// Check each file's checksum
 | 	// Check each file's checksum
 | ||||||
| 	for i, fileName := range fileNames { | 	for i, fileName := range fileNames { | ||||||
| 		expectedChecksum := strings.TrimSpace(checkSums[i]) | 		expectedChecksum := strings.TrimSpace(checkSums[i]) | ||||||
| 		passed, message := checkFileChecksum(rootDir, fileName, expectedChecksum) | 		err := checkFileChecksum(rootDir, fileName, expectedChecksum) | ||||||
| 		if message != "" { | 		if err != nil { | ||||||
| 			return fmt.Errorf(message) | 			return err | ||||||
| 
					
					bomingzh marked this conversation as resolved
					
						
						
							Outdated
						
					
				 
				
					
						张泊明518370910136
						commented  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 | 	return nil | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	
remove the comment