diff --git a/cmd/repo-health-checker/main.go b/cmd/repo-health-checker/main.go index 494a5a2..f16a93c 100644 --- a/cmd/repo-health-checker/main.go +++ b/cmd/repo-health-checker/main.go @@ -9,7 +9,6 @@ import ( "strings" "github.com/joint-online-judge/JOJ3/cmd/joj3/env" - "github.com/joint-online-judge/JOJ3/internal/conf" "github.com/joint-online-judge/JOJ3/pkg/healthcheck" ) @@ -57,19 +56,7 @@ func init() { flag.StringVar(&checkFileNameList, "checkFileNameList", "", "comma-separated list of files to check") flag.StringVar(&checkFileSumList, "checkFileSumList", "", "comma-separated list of expected checksums") parseMultiValueFlag(&metaFile, "meta", "meta files to check") - flag.StringVar(&confPath, "confPath", "", "path to conf file for teapot check") -} - -func prepareTeapotCheck() ( - confObj *conf.Conf, groups []string, err error, -) { - confObj, err = conf.ParseConfFile(confPath) - if err != nil { - slog.Error("parse conf", "error", err) - return - } - groups = strings.Split(os.Getenv(env.Groups), ",") - return + flag.StringVar(&confPath, "confPath", "", "path to conf file for teapot check") // TODO: remove me } func main() { @@ -86,14 +73,9 @@ func main() { "checkFileSumList", checkFileSumList, "meta", metaFile, ) - var err error - confObj, groups, err := prepareTeapotCheck() - if err != nil { - slog.Error("prepare teapot check", "error", err) - confObj = nil - } + groups := strings.Split(os.Getenv(env.Groups), ",") res := healthcheck.All( - confObj, rootDir, checkFileNameList, checkFileSumList, + rootDir, checkFileNameList, checkFileSumList, groups, metaFile, repoSize, ) jsonRes, err := json.Marshal(res) diff --git a/pkg/healthcheck/all.go b/pkg/healthcheck/all.go index aa3eb2d..2293701 100644 --- a/pkg/healthcheck/all.go +++ b/pkg/healthcheck/all.go @@ -2,8 +2,6 @@ package healthcheck import ( "fmt" - - "github.com/joint-online-judge/JOJ3/internal/conf" ) type Result struct { @@ -12,21 +10,11 @@ type Result struct { } func All( - confObj *conf.Conf, rootDir, checkFileNameList, checkFileSumList string, groups, metaFile []string, repoSize float64, ) (res Result) { var err error - if confObj != nil { - output, err := TeapotCheck(confObj, groups) - if err != nil { - res.Msg += fmt.Sprintf("### Teapot Check Failed:\n%s\n", output) - res.Failed = true - } else { - res.Msg += fmt.Sprintf("### Teapot Check Result:\n%s\n", output) - } - } err = RepoSize(repoSize) if err != nil { res.Msg += fmt.Sprintf("### Repo Size Check Failed:\n%s\n", err.Error()) diff --git a/pkg/healthcheck/teapot.go b/pkg/healthcheck/teapot.go deleted file mode 100644 index c1b4b21..0000000 --- a/pkg/healthcheck/teapot.go +++ /dev/null @@ -1,107 +0,0 @@ -package healthcheck - -import ( - "bytes" - "encoding/json" - "fmt" - "log/slog" - "os" - "os/exec" - "strings" - - "github.com/joint-online-judge/JOJ3/internal/conf" -) - -type CheckResult struct { - Name string `json:"name"` - SubmitCount int `json:"submit_count"` - MaxCount int `json:"max_count"` - TimePeriod int `json:"time_period"` -} - -func runTeapot(conf *conf.Conf) (checkResults []CheckResult, err error) { - os.Setenv("LOG_FILE_PATH", conf.Teapot.LogPath) - var formattedGroups []string - for _, group := range conf.Teapot.Groups { - groupConfig := fmt.Sprintf("%s=%d:%d", - group.Name, group.MaxCount, group.TimePeriodHour) - formattedGroups = append(formattedGroups, groupConfig) - } - args := []string{ - "joj3-check", conf.Teapot.EnvFilePath, - conf.Teapot.GradingRepoName, - conf.Teapot.ScoreboardPath, - "--group-config", strings.Join(formattedGroups, ","), - } - var stdoutBuf, stderrBuf bytes.Buffer - cmd := exec.Command("joint-teapot", args...) // #nosec G204 - cmd.Stdout = &stdoutBuf - cmd.Stderr = &stderrBuf - err = cmd.Run() - if err != nil { - slog.Error("teapot check args", "args", args) - slog.Error("teapot check exec", - "stdout", stdoutBuf.String(), "stderr", stderrBuf.String()) - slog.Error("teapot check exec", "error", err) - return - } - if err = json.Unmarshal(stdoutBuf.Bytes(), &checkResults); err != nil { - slog.Error("unmarshal teapot check result", "error", err, - "stdout", stdoutBuf.String()) - return - } - slog.Info("teapot check result", "result", checkResults) - return -} - -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", - checkResult.TimePeriod, - checkResult.SubmitCount, - checkResult.MaxCount, - ) - if useGroup && checkResult.SubmitCount+1 > checkResult.MaxCount { - err = fmt.Errorf( - "keyword `%s` submit count exceeded", - checkResult.Name, - ) - comment += ", exceeded" - } - comment += "\n" - } - return -} - -func TeapotCheck(conf *conf.Conf, groups []string) (output string, err error) { - checkResults, err := runTeapot(conf) - if err != nil { - slog.Error("teapot check", "error", err) - return - } - output, err = generateOutput(checkResults, groups) - if err != nil { - slog.Error("generate output", "error", err) - return - } - return -}