package common /* * This file is part of Autogits. * * Copyright © 2024 SUSE LLC * * Autogits is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 2 of the License, or (at your option) any later * version. * * Autogits is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * Foobar. If not, see . */ import ( "encoding/json" "fmt" "io" "log" "os" "slices" "strings" ) type AutogitConfig struct { Workflows []string // [pr, direct, test] Organization string GitProjectName string // Organization/GitProjectName.git is PrjGit Branch string // branch name of PkgGit that aligns with PrjGit submodules Reviewers []string // only used by `pr` workflow } 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", "direct", "test"} for _, workflow := range config { pr := false for _, w := range workflow.Workflows { if w == "pr" { pr = true } 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 } if !pr && len(workflow.Reviewers) > 0 { log.Println(" Warning: non-PR-only workflows contains Reviewers. These are ignored in Org:", workflow.Organization) } if pr && !slices.Contains(workflow.Reviewers, Bot_BuildReview) { workflow.Reviewers = append(workflow.Reviewers, Bot_BuildReview) } } 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) }