feat: decide which stage to run based on commit msg
Some checks failed
checks / build (push) Failing after 0s
checks / build (pull_request) Failing after 0s

This commit is contained in:
zzjc1234 2024-09-23 12:27:56 +08:00
parent 8d0000016a
commit 2d08a435a8

View File

@ -11,12 +11,15 @@ import (
"github.com/koding/multiconfig" "github.com/koding/multiconfig"
) )
type Conf struct { type JobType int
SandboxExecServer string `default:"localhost:5051"`
SandboxToken string `default:""` const (
LogLevel int `default:"0"` HC JobType = iota
OutputPath string `default:"joj3_result.json"` CQ
Stages []struct { OJ
)
type Stage struct {
Name string Name string
Executor struct { Executor struct {
Name string Name string
@ -30,6 +33,13 @@ type Conf struct {
With interface{} With interface{}
} }
} }
type Conf struct {
SandboxExecServer string `default:"localhost:5051"`
SandboxToken string `default:""`
LogLevel int `default:"0"`
OutputPath string `default:"joj3_result.json"`
Stages []Stage
} }
type OptionalCmd struct { type OptionalCmd struct {
@ -63,24 +73,51 @@ type OptionalCmd struct {
AddressSpaceLimit *bool AddressSpaceLimit *bool
} }
func parseConfFile(path string) (conf Conf, err error) { func parseConfFile(path string, jobtype JobType) (conf Conf, err error) {
d := &multiconfig.DefaultLoader{} d := &multiconfig.DefaultLoader{}
d.Loader = multiconfig.MultiLoader( d.Loader = multiconfig.MultiLoader(
&multiconfig.TagLoader{}, &multiconfig.TagLoader{},
&multiconfig.JSONLoader{Path: path}, &multiconfig.JSONLoader{Path: path},
) )
d.Validator = multiconfig.MultiValidator(&multiconfig.RequiredValidator{}) d.Validator = multiconfig.MultiValidator(&multiconfig.RequiredValidator{})
if err = d.Load(&conf); err != nil { if err = d.Load(&conf); err != nil {
slog.Error("parse stages conf", "error", err) slog.Error("parse stages conf", "error", err)
return return
} }
if err = d.Validate(&conf); err != nil { if err = d.Validate(&conf); err != nil {
slog.Error("validate stages conf", "error", err) slog.Error("validate stages conf", "error", err)
return return
} }
filteredStages := []Stage{}
for _, stage := range conf.Stages {
if filterStage(stage, jobtype) {
filteredStages = append(filteredStages, stage)
}
}
conf.Stages = filteredStages
return return
} }
func filterStage(stage Stage, jobtype JobType,
) bool {
switch jobtype {
case HC:
return stage.Name == "healthcheck"
case CQ:
return stage.Name == "compile" || stage.Name == "healthcheck"
case OJ:
return true
default:
return false
}
}
func validateHw(hw string) error { func validateHw(hw string) error {
matched, err := regexp.MatchString(`^hw[0-9]+$`, hw) matched, err := regexp.MatchString(`^hw[0-9]+$`, hw)
if err != nil { if err != nil {
@ -109,11 +146,12 @@ func commitMsgToConf() (conf Conf, err error) {
} }
file := "conf.json" file := "conf.json"
jobtype := HC
msg := commit.Message msg := commit.Message
slog.Debug("commit msg to conf", "msg", msg) slog.Debug("commit msg to conf", "msg", msg)
if msg == "" { if msg == "" {
conf, err = parseConfFile(file) conf, err = parseConfFile(file, jobtype)
return return
} }
@ -128,31 +166,22 @@ func commitMsgToConf() (conf Conf, err error) {
head = head[:len(head)-1] head = head[:len(head)-1]
} }
if len(words) == 3 {
hw = words[1]
if err = validateHw(hw); err != nil {
return
}
switch head { switch head {
case "feat", "fix", "refactor", "perf", "test", "build", "revert": case "feat", "fix", "refactor", "perf", "test", "build", "revert":
// TODO: Decide strategy to give students error file = strings.Replace(file, "conf", "conf-"+hw, 1)
// if len(words) < 2 { jobtype = CQ
// return Conf{}, fmt.Errorf("error: hw not assigned")
// }
if len(words) >= 2 {
hw = words[1]
if err = validateHw(hw); err == nil {
file = strings.Replace(file, "conf", "conf-"+hw+"-cq", 1)
}
}
case "joj", "grading": case "joj", "grading":
// TODO: Decide strategy to give students error file = strings.Replace(file, "conf", "conf-"+hw, 1)
// if len(words) < 2 { jobtype = OJ
// return Conf{}, fmt.Errorf("error: hw not assigned")
// }
if len(words) >= 2 {
hw = words[1]
if err = validateHw(hw); err == nil {
file = strings.Replace(file, "conf", "conf-"+hw+"-oj", 1)
}
} }
} }
conf, err = parseConfFile(file) conf, err = parseConfFile(file, jobtype)
return return
} }