Merge branch 'main' of c3:gitea_test/autogits
This commit is contained in:
@@ -24,7 +24,6 @@ import (
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"slices"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -32,6 +31,16 @@ type ConfigFile struct {
|
||||
GitProjectName []string
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
type AutogitConfigs []*AutogitConfig
|
||||
|
||||
func ReadConfig(reader io.Reader) (*ConfigFile, error) {
|
||||
data, err := io.ReadAll(reader)
|
||||
if err != nil {
|
||||
@@ -94,7 +103,7 @@ func ReadWorkflowConfig(gitea Gitea, git_project string) (*AutogitConfig, error)
|
||||
return &config, nil
|
||||
}
|
||||
|
||||
func ResolveWorkflowConfigs(gitea Gitea, config *ConfigFile) ([]*AutogitConfig, error) {
|
||||
func ResolveWorkflowConfigs(gitea Gitea, config *ConfigFile) (AutogitConfigs, error) {
|
||||
configs := make([]*AutogitConfig, 0, len(config.GitProjectName))
|
||||
for _, git_project := range config.GitProjectName {
|
||||
c, err := ReadWorkflowConfig(gitea, git_project)
|
||||
@@ -109,16 +118,6 @@ func ResolveWorkflowConfigs(gitea Gitea, config *ConfigFile) ([]*AutogitConfig,
|
||||
return configs, nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
type AutogitConfigs []*AutogitConfig
|
||||
|
||||
func (configs AutogitConfigs) GetPrjGitConfig(org, repo, branch string) *AutogitConfig {
|
||||
for _, c := range configs {
|
||||
if c.Organization == org && c.Branch == branch {
|
||||
@@ -129,48 +128,3 @@ func (configs AutogitConfigs) GetPrjGitConfig(org, repo, branch string) *Autogit
|
||||
return nil
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
@@ -21,6 +21,7 @@ package main
|
||||
import (
|
||||
"flag"
|
||||
"log"
|
||||
"net/url"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
@@ -78,7 +79,13 @@ func main() {
|
||||
log.Fatalln("No configuratio file specified. Aborting")
|
||||
}
|
||||
|
||||
configs, err := common.ReadWorkflowConfigsFile(*workflowConfig)
|
||||
gitea := common.AllocateGiteaTransport(*giteaHost)
|
||||
config, err := common.ReadConfigFile(*workflowConfig)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
configs, err := common.ResolveWorkflowConfigs(gitea, config)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
@@ -104,8 +111,6 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
gitea := common.AllocateGiteaTransport(*giteaHost)
|
||||
|
||||
if CurrentUser, err = gitea.GetCurrentUser(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
@@ -130,7 +135,7 @@ func main() {
|
||||
var defs common.ListenDefinitions
|
||||
|
||||
defs.GitAuthor = GitAuthor
|
||||
defs.RabbitURL = *rabbitUrl
|
||||
defs.RabbitURL, _ = url.Parse(*rabbitUrl)
|
||||
|
||||
defs.Handlers = make(map[string]common.RequestProcessor)
|
||||
defs.Handlers[common.RequestType_PR] = req
|
||||
@@ -138,5 +143,5 @@ func main() {
|
||||
defs.Handlers[common.RequestType_PRReviewAccepted] = req
|
||||
defs.Handlers[common.RequestType_PRReviewRejected] = req
|
||||
|
||||
log.Fatal(common.ProcessRabbitMQEvents(defs, orgs))
|
||||
log.Fatal(defs.ProcessRabbitMQEvents())
|
||||
}
|
||||
|
Reference in New Issue
Block a user