feat(healthcheck): check commit author email
This commit is contained in:
parent
a5cd85be35
commit
9cf640dee5
|
@ -45,6 +45,7 @@ var (
|
||||||
checkFileNameList string
|
checkFileNameList string
|
||||||
checkFileSumList string
|
checkFileSumList string
|
||||||
metaFile []string
|
metaFile []string
|
||||||
|
allowedDomainList string
|
||||||
showVersion *bool
|
showVersion *bool
|
||||||
Version string
|
Version string
|
||||||
)
|
)
|
||||||
|
@ -55,6 +56,7 @@ func init() {
|
||||||
flag.Float64Var(&repoSize, "repoSize", 2, "maximum size of the repo in MiB")
|
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(&checkFileNameList, "checkFileNameList", "", "comma-separated list of files to check")
|
||||||
flag.StringVar(&checkFileSumList, "checkFileSumList", "", "comma-separated list of expected checksums")
|
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")
|
parseMultiValueFlag(&metaFile, "meta", "meta files to check")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,7 +75,12 @@ func main() {
|
||||||
"meta", metaFile,
|
"meta", metaFile,
|
||||||
)
|
)
|
||||||
res := healthcheck.All(
|
res := healthcheck.All(
|
||||||
rootDir, checkFileNameList, checkFileSumList, metaFile, repoSize,
|
rootDir,
|
||||||
|
checkFileNameList,
|
||||||
|
checkFileSumList,
|
||||||
|
allowedDomainList,
|
||||||
|
metaFile,
|
||||||
|
repoSize,
|
||||||
)
|
)
|
||||||
jsonRes, err := json.Marshal(res)
|
jsonRes, err := json.Marshal(res)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -3,6 +3,7 @@ package healthcheck
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Result struct {
|
type Result struct {
|
||||||
|
@ -11,7 +12,7 @@ type Result struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func All(
|
func All(
|
||||||
rootDir, checkFileNameList, checkFileSumList string,
|
rootDir, checkFileNameList, checkFileSumList, allowedDomainList string,
|
||||||
metaFile []string, repoSize float64,
|
metaFile []string, repoSize float64,
|
||||||
) (res Result) {
|
) (res Result) {
|
||||||
var err error
|
var err error
|
||||||
|
@ -57,5 +58,12 @@ func All(
|
||||||
} else {
|
} else {
|
||||||
res.Msg += "### Repo File Check Passed\n"
|
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
|
return res
|
||||||
}
|
}
|
||||||
|
|
|
@ -74,3 +74,35 @@ func NonASCIIMsg(root string) error {
|
||||||
}
|
}
|
||||||
return nil
|
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,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user