From 9cf640dee5fc3f7cb1fb549be3584bc8b8660dc9 Mon Sep 17 00:00:00 2001 From: Boming Zhang Date: Wed, 4 Jun 2025 06:05:11 -0400 Subject: [PATCH] feat(healthcheck): check commit author email --- cmd/repo-health-checker/main.go | 9 ++++++++- pkg/healthcheck/all.go | 10 +++++++++- pkg/healthcheck/commit.go | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/cmd/repo-health-checker/main.go b/cmd/repo-health-checker/main.go index c4a16cd..e170c13 100644 --- a/cmd/repo-health-checker/main.go +++ b/cmd/repo-health-checker/main.go @@ -45,6 +45,7 @@ var ( checkFileNameList string checkFileSumList string metaFile []string + allowedDomainList string showVersion *bool Version string ) @@ -55,6 +56,7 @@ func init() { flag.Float64Var(&repoSize, "repoSize", 2, "maximum size of the repo in MiB") flag.StringVar(&checkFileNameList, "checkFileNameList", "", "comma-separated list of files to check") flag.StringVar(&checkFileSumList, "checkFileSumList", "", "comma-separated list of expected checksums") + flag.StringVar(&allowedDomainList, "allowedDomainList", "sjtu.edu.cn", "comma-separated list of allowed domains for commit author email") parseMultiValueFlag(&metaFile, "meta", "meta files to check") } @@ -73,7 +75,12 @@ func main() { "meta", metaFile, ) res := healthcheck.All( - rootDir, checkFileNameList, checkFileSumList, metaFile, repoSize, + rootDir, + checkFileNameList, + checkFileSumList, + allowedDomainList, + metaFile, + repoSize, ) jsonRes, err := json.Marshal(res) if err != nil { diff --git a/pkg/healthcheck/all.go b/pkg/healthcheck/all.go index 68ce56f..14d3fb7 100644 --- a/pkg/healthcheck/all.go +++ b/pkg/healthcheck/all.go @@ -3,6 +3,7 @@ package healthcheck import ( "fmt" + "strings" ) type Result struct { @@ -11,7 +12,7 @@ type Result struct { } func All( - rootDir, checkFileNameList, checkFileSumList string, + rootDir, checkFileNameList, checkFileSumList, allowedDomainList string, metaFile []string, repoSize float64, ) (res Result) { var err error @@ -57,5 +58,12 @@ func All( } else { res.Msg += "### Repo File Check Passed\n" } + err = AuthorEmailCheck(rootDir, strings.Split(allowedDomainList, ",")) + if err != nil { + res.Msg += fmt.Sprintf("### Author Email Check Failed:\n%s\n", err.Error()) + res.Failed = true + } else { + res.Msg += "### Author Email Check Passed\n" + } return res } diff --git a/pkg/healthcheck/commit.go b/pkg/healthcheck/commit.go index d27b6e4..0a96890 100644 --- a/pkg/healthcheck/commit.go +++ b/pkg/healthcheck/commit.go @@ -74,3 +74,35 @@ func NonASCIIMsg(root string) error { } return nil } + +func AuthorEmailCheck(root string, allowedDomains []string) error { + repo, err := git.PlainOpen(root) + if err != nil { + slog.Error("opening git repo", "err", err) + return fmt.Errorf("error opening git repo: %v", err) + } + + ref, err := repo.Head() + if err != nil { + slog.Error("getting reference", "err", err) + return fmt.Errorf("error getting reference: %v", err) + } + + commit, err := repo.CommitObject(ref.Hash()) + if err != nil { + slog.Error("getting latest commit", "err", err) + return fmt.Errorf("error getting latest commit: %v", err) + } + + email := commit.Author.Email + for _, domain := range allowedDomains { + if strings.HasSuffix(email, "@"+domain) { + return nil + } + } + return fmt.Errorf( + "Author email %s is not in the allowed domains: %v", + email, + allowedDomains, + ) +}