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 24 deletions
Showing only changes of commit dc6e564832 - Show all commits

View File

@ -77,8 +77,10 @@ func main() {
fmt.Printf("## Release Tag Check Failed:\n%s\n", err.Error()) fmt.Printf("## Release Tag Check Failed:\n%s\n", err.Error())
} }
// FIXME: for drone usage // FIXME: for drone usage
err = healthcheck.VerifyDirectory(*rootDir, *adminDir) if adminDir != nil && *adminDir != "" {
if err != nil { err = healthcheck.VerifyDirectory(*rootDir, *adminDir)
fmt.Printf("## Directory File Check Failed:\n%s\n", err.Error()) if err != nil {
fmt.Printf("## Directory File Check Failed:\n%s\n", err.Error())
}
} }
} }

View File

@ -6,6 +6,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings"
) )
// getForbiddens retrieves a list of forbidden files in the specified root directory. // getForbiddens retrieves a list of forbidden files in the specified root directory.
@ -61,15 +62,12 @@ func ForbiddenCheck(rootDir string, regexList []string, repo string, droneBranch
var message string var message string
if len(forbids) > 0 { if len(forbids) > 0 {
message += fmt.Sprint(103, "the following forbidden files were found: ") message = "The following forbidden files were found: " +
for _, file := range forbids { strings.Join(forbids, ", ") +
message += fmt.Sprint(file, ", ") "\n\nTo fix it, first make a backup of your repository and then run the following commands:\nfor i in " +
} strings.Join(forbids, " ") +
message += "\n\nTo fix it, first make a backup of your repository and then run the following commands:\nfor i in " fmt.Sprint("; do git filter-repo --force --invert-paths --path \\\"\\$i\\\"; done\ngit remote add origin ",
for _, file := range forbids { repo, "\ngit push --set-upstream origin ", droneBranch, " --force")
message += fmt.Sprint(file, " ")
}
message += 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(message)
} }
return nil return nil

View File

@ -2,19 +2,12 @@ package healthcheck
import ( import (
"fmt" "fmt"
"strings"
"github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing"
) )
func catTags(all []string) (out string) {
out = ""
for _, str := range all {
out += str + " "
}
return out
}
func getTagsFromRepo(repoPath string) ([]string, error) { func getTagsFromRepo(repoPath string) ([]string, error) {
repo, err := git.PlainOpen(repoPath) repo, err := git.PlainOpen(repoPath)
if err != nil { if err != nil {
@ -63,8 +56,7 @@ func CheckReleases(repoPath string, category string, n int) error {
} }
} }
if !found { if !found {
tagList := catTags(tags) return fmt.Errorf("Wrong release tag '%s'. Please use one of '%s'.", target, strings.Join(tags, "', '"))
return fmt.Errorf("Wrong release tag '%s'. Please use one of %s.", target, tagList)
} }
return nil return nil
} }

View File

@ -57,14 +57,16 @@ func VerifyDirectory(rootDir, compareDir string) error {
relPath, _ := filepath.Rel(rootDir, path) relPath, _ := filepath.Rel(rootDir, path)
file2 := filepath.Join(compareDir, relPath) file2 := filepath.Join(compareDir, relPath)
if !fileExists(file2) { if !fileExists(file2) {
return fmt.Errorf("File %s in %s is missing in %s. Please immediately revert your changes!\n", relPath, rootDir, compareDir) return fmt.Errorf("File %s is missing. Please immediately revert your changes!\n",
filepath.Join(rootDir, relPath))
} }
match, err := filesMatch(path, file2) match, err := filesMatch(path, file2)
if err != nil { if err != nil {
return fmt.Errorf("error matching files: %w", err) return fmt.Errorf("error matching files: %w", err)
} }
if !match { if !match {
return fmt.Errorf("File %s in %s is not identical to %s. Please revert your changes or contact the teaching team if you have a valid reason for adjusting them.\n", relPath, rootDir, compareDir) return fmt.Errorf("File %s is altered. Please revert your changes or contact the teaching team if you have a valid reason for adjusting them.\n",
filepath.Join(rootDir, relPath))
} }
} }
return nil return nil