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