feat(cmd/teapot-checker): only block used groups
This commit is contained in:
parent
4bf38c21d5
commit
648fe7d0a4
|
@ -22,9 +22,8 @@ func generateStages(conf *conf.Conf, groups []string) ([]stage.Stage, error) {
|
|||
for _, s := range conf.Stage.Stages {
|
||||
if s.Group != "" {
|
||||
var ok bool
|
||||
loweredStageGroup := strings.ToLower(s.Group)
|
||||
for _, group := range groups {
|
||||
if group == loweredStageGroup {
|
||||
if strings.EqualFold(group, s.Group) {
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
|
@ -134,6 +133,7 @@ func newErrorStageResults(err error) ([]stage.StageResult, string) {
|
|||
|
||||
func newTeapotCheckStageResults(
|
||||
checkResults []teapot.CheckResult,
|
||||
groups []string,
|
||||
) (stageResults []stage.StageResult, forceQuitStageName string, err error) {
|
||||
if len(checkResults) == 0 {
|
||||
return
|
||||
|
@ -141,8 +141,17 @@ func newTeapotCheckStageResults(
|
|||
comment := ""
|
||||
forceQuit := false
|
||||
for _, checkResult := range checkResults {
|
||||
useGroup := false
|
||||
if checkResult.Name != "" {
|
||||
comment += fmt.Sprintf("keyword `%s` ", checkResult.Name)
|
||||
} else {
|
||||
useGroup = true
|
||||
}
|
||||
for _, group := range groups {
|
||||
if strings.EqualFold(group, checkResult.Name) {
|
||||
useGroup = true
|
||||
break
|
||||
}
|
||||
}
|
||||
comment += fmt.Sprintf(
|
||||
"in last %d hour(s): submit count %d, max count %d\n",
|
||||
|
@ -150,7 +159,7 @@ func newTeapotCheckStageResults(
|
|||
checkResult.SubmitCount,
|
||||
checkResult.MaxCount,
|
||||
)
|
||||
if checkResult.SubmitCount+1 > checkResult.MaxCount {
|
||||
if useGroup && checkResult.SubmitCount+1 > checkResult.MaxCount {
|
||||
forceQuit = true
|
||||
err = fmt.Errorf("submit count exceeded")
|
||||
}
|
||||
|
@ -176,6 +185,7 @@ func Run(
|
|||
) {
|
||||
stageResults, forceQuitStageName, err = newTeapotCheckStageResults(
|
||||
checkResults,
|
||||
groups,
|
||||
)
|
||||
if err != nil {
|
||||
slog.Error("teapot check", "error", err)
|
||||
|
|
|
@ -65,13 +65,25 @@ func check(conf *conf.Conf) (checkResults []CheckResult, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
func generateOutput(checkResults []CheckResult) (comment string, err error) {
|
||||
func generateOutput(
|
||||
checkResults []CheckResult,
|
||||
groups []string,
|
||||
) (comment string, err error) {
|
||||
if len(checkResults) == 0 {
|
||||
return
|
||||
}
|
||||
for _, checkResult := range checkResults {
|
||||
useGroup := false
|
||||
if checkResult.Name != "" {
|
||||
comment += fmt.Sprintf("keyword `%s` ", checkResult.Name)
|
||||
} else {
|
||||
useGroup = true
|
||||
}
|
||||
for _, group := range groups {
|
||||
if strings.EqualFold(group, checkResult.Name) {
|
||||
useGroup = true
|
||||
break
|
||||
}
|
||||
}
|
||||
comment += fmt.Sprintf(
|
||||
"in last %d hour(s): submit count %d, max count %d\n",
|
||||
|
@ -79,7 +91,7 @@ func generateOutput(checkResults []CheckResult) (comment string, err error) {
|
|||
checkResult.SubmitCount,
|
||||
checkResult.MaxCount,
|
||||
)
|
||||
if checkResult.SubmitCount+1 > checkResult.MaxCount {
|
||||
if useGroup && checkResult.SubmitCount+1 > checkResult.MaxCount {
|
||||
err = fmt.Errorf("submit count exceeded")
|
||||
}
|
||||
}
|
||||
|
@ -98,7 +110,7 @@ var (
|
|||
Version string = "debug"
|
||||
)
|
||||
|
||||
func main() {
|
||||
func mainImpl() (err error) {
|
||||
showVersion := flag.Bool("version", false, "print current version")
|
||||
flag.StringVar(&confPath, "conf-path", "./conf.json", "path for config file")
|
||||
flag.Parse()
|
||||
|
@ -108,21 +120,39 @@ func main() {
|
|||
}
|
||||
setupSlog()
|
||||
slog.Info("start teapot-checker", "version", Version)
|
||||
commitMsg, err := conf.GetCommitMsg()
|
||||
if err != nil {
|
||||
slog.Error("get commit msg", "error", err)
|
||||
return
|
||||
}
|
||||
conventionalCommit, err := conf.ParseConventionalCommit(commitMsg)
|
||||
if err != nil {
|
||||
slog.Error("parse commit msg", "error", err)
|
||||
return
|
||||
}
|
||||
confObj, _, err := conf.ParseConfFile(confPath)
|
||||
if err != nil {
|
||||
slog.Error("parse conf", "error", err)
|
||||
return
|
||||
}
|
||||
groups := conf.MatchGroups(confObj, conventionalCommit)
|
||||
checkResults, err := check(confObj)
|
||||
if err != nil {
|
||||
slog.Error("teapot check", "error", err)
|
||||
return
|
||||
}
|
||||
exitCode := 0
|
||||
output, err := generateOutput(checkResults)
|
||||
output, err := generateOutput(checkResults, groups)
|
||||
if err != nil {
|
||||
exitCode = 1
|
||||
slog.Error("generate output", "error", err)
|
||||
return
|
||||
}
|
||||
fmt.Println(output)
|
||||
os.Exit(exitCode)
|
||||
return
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := mainImpl(); err != nil {
|
||||
slog.Error("main exit", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -147,7 +147,7 @@ func GetCommitMsg() (msg string, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
func parseConventionalCommit(commit string) (*ConventionalCommit, error) {
|
||||
func ParseConventionalCommit(commit string) (*ConventionalCommit, error) {
|
||||
re := regexp.MustCompile(`(?s)^(\w+)(\(([^)]+)\))?!?: (.+?(\[([^\]]+)\])?)(\n\n(.+?))?(\n\n(.+))?$`)
|
||||
matches := re.FindStringSubmatch(strings.TrimSpace(commit))
|
||||
if matches == nil {
|
||||
|
@ -237,7 +237,7 @@ func parseMsg(confRoot, confName, msg, tag string) (
|
|||
confPath string, conventionalCommit *ConventionalCommit, err error,
|
||||
) {
|
||||
slog.Info("parse msg", "msg", msg)
|
||||
conventionalCommit, err = parseConventionalCommit(msg)
|
||||
conventionalCommit, err = ParseConventionalCommit(msg)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
|
|
@ -119,7 +119,7 @@ func TestParseConventionalCommit(t *testing.T) {
|
|||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := parseConventionalCommit(tt.commit)
|
||||
got, err := ParseConventionalCommit(tt.commit)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("ParseConventionalCommit() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
|
|
Loading…
Reference in New Issue
Block a user