From cde327406ce5720aca8b0b67c179dbdba785da2f1dc9add175d57b8ad1babe10 Mon Sep 17 00:00:00 2001 From: Adam Majer <amajer@suse.com> Date: Tue, 27 Aug 2024 11:18:46 +0200 Subject: [PATCH] . --- bots-common/config.go | 56 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 bots-common/config.go diff --git a/bots-common/config.go b/bots-common/config.go new file mode 100644 index 0000000..9cc5f19 --- /dev/null +++ b/bots-common/config.go @@ -0,0 +1,56 @@ +package common + +import ( + "encoding/json" + "fmt" + "io" + "os" + "slices" + "strings" +) + +type AutogitConfig struct { + Workflows []string + Organization string + GitProjectName string +} + +func ReadWorkflowConfigs(reader io.Reader) ([]*AutogitConfig, error) { + data, err := io.ReadAll(reader) + if err != nil { + return nil, fmt.Errorf("Error reading config file. err: %w", err) + } + + var config []*AutogitConfig + if err = json.Unmarshal(data, &config); err != nil { + return nil, fmt.Errorf("Error parsing config file. err: %w", err) + } + + availableWorkflows := []string{"pr", "push", "test"} + for _, workflow := range config { + for _, w := range workflow.Workflows { + if !slices.Contains(availableWorkflows, w) { + return nil, fmt.Errorf( + "Invalid Workflow '%s'. Only available workflows are: %s", + w, strings.Join(availableWorkflows, " "), + ) + } + } + if len(workflow.GitProjectName) == 0 { + workflow.GitProjectName = DefaultGitPrj + } + } + + return config, nil +} + +func ReadWorkflowConfigsFile(filename string) ([]*AutogitConfig, error) { + file, err := os.Open(filename) + if err != nil { + return nil, fmt.Errorf("Cannot open config file for reading. err: %w", err) + } + defer file.Close() + + return ReadWorkflowConfigs(file) +} +