2024-08-27 11:18:46 +02:00
|
|
|
package common
|
|
|
|
|
2024-09-10 18:24:41 +02:00
|
|
|
/*
|
|
|
|
* 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 <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2024-08-27 11:18:46 +02:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2024-09-30 16:19:40 +02:00
|
|
|
"log"
|
2024-08-27 11:18:46 +02:00
|
|
|
"os"
|
|
|
|
"slices"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AutogitConfig struct {
|
2024-09-10 18:24:41 +02:00
|
|
|
Workflows []string // [pr, direct, test]
|
2024-08-27 11:18:46 +02:00
|
|
|
Organization string
|
2024-09-10 18:24:41 +02:00
|
|
|
GitProjectName string // Organization/GitProjectName.git is PrjGit
|
|
|
|
Branch string // branch name of PkgGit that aligns with PrjGit submodules
|
2024-09-30 16:19:40 +02:00
|
|
|
Reviewers []string // only used by `pr` workflow
|
2024-08-27 11:18:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-08-30 16:37:51 +02:00
|
|
|
availableWorkflows := []string{"pr", "direct", "test"}
|
2024-08-27 11:18:46 +02:00
|
|
|
for _, workflow := range config {
|
2024-09-30 16:19:40 +02:00
|
|
|
pr := false
|
2024-08-27 11:18:46 +02:00
|
|
|
for _, w := range workflow.Workflows {
|
2024-09-30 16:19:40 +02:00
|
|
|
if w == "pr" {
|
|
|
|
pr = true
|
|
|
|
}
|
2024-08-27 11:18:46 +02:00
|
|
|
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
|
|
|
|
}
|
2024-09-30 16:19:40 +02:00
|
|
|
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)
|
|
|
|
}
|
2024-08-27 11:18:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|