This commit is contained in:
2025-05-05 18:57:05 +02:00
parent eb997e1ae9
commit da1df24666
20 changed files with 274 additions and 119 deletions

View File

@@ -22,7 +22,9 @@ import (
"flag"
"net/url"
"os"
"regexp"
"slices"
"strconv"
"time"
"src.opensuse.org/autogits/common"
@@ -39,6 +41,7 @@ var ListPROnly bool
var PRID int64
var CurrentUser *models.User
var GitHandler common.GitHandlerGenerator
var Gitea common.Gitea
func main() {
workflowConfig := flag.String("config", "", "Repository and workflow definition file")
@@ -50,6 +53,7 @@ func main() {
flag.BoolVar(&ListPROnly, "list-prs-only", false, "Only lists PRs without acting on them")
flag.Int64Var(&PRID, "id", -1, "Process only the specific ID and ignore the rest. Use for debugging")
basePath := flag.String("repo-path", "", "Repository path. Default is temporary directory")
pr := flag.String("only-pr", "", "Only specific PR to process. For debugging")
flag.Parse()
common.SetLoggingLevel(common.LogLevelInfo)
@@ -72,19 +76,23 @@ func main() {
return
}
gitea := common.AllocateGiteaTransport(*giteaUrl)
Gitea = common.AllocateGiteaTransport(*giteaUrl)
config, err := common.ReadConfigFile(*workflowConfig)
if err != nil {
common.LogError("Cannot read config files:", err)
return
}
configs, err := common.ResolveWorkflowConfigs(gitea, config)
configs, err := common.ResolveWorkflowConfigs(Gitea, config)
if err != nil {
common.LogError("Cannot resolve config files:", err)
return
}
for _, c := range configs {
common.LogDebug(*c)
}
req := new(RequestProcessor)
req.configuredRepos = make(map[string][]*common.AutogitConfig)
@@ -111,26 +119,67 @@ func main() {
}
}
if CurrentUser, err = gitea.GetCurrentUser(); err != nil {
if CurrentUser, err = Gitea.GetCurrentUser(); err != nil {
common.LogError("Failed to fetch current gitea user:", err)
return
}
common.LogInfo("Running with token from", CurrentUser.UserName)
req.Synced = &PullRequestSynced{
gitea: gitea,
}
req.Opened = &PullRequestOpened{
gitea: gitea,
}
req.Closed = &PullRequestClosed{
gitea: gitea,
}
req.Review = &PullRequestReviewed{
gitea: gitea,
req.Synced = &PullRequestSynced{}
req.Opened = &PullRequestOpened{}
req.Closed = &PullRequestClosed{}
req.Review = &PullRequestReviewed{}
if *pr != "" {
rx := regexp.MustCompile("^([a-zA-Z0-9_\\.-]+)/([a-zA-Z0-9_\\.-]+)#([0-9]+)$")
if data := rx.FindStringSubmatch(*pr); data == nil {
common.LogError("Cannot parse PR line to process", *pr)
} else {
org := data[1]
repo := data[2]
num, err := strconv.ParseInt(data[3], 10, 64)
common.LogInfo("Processing:", org, "/", repo, "#", num)
common.PanicOnError(err)
pr, err := Gitea.GetPullRequest(org, repo, num)
if err != nil {
common.LogError("Cannot fetch PR", err)
return
}
if pr.State != "open" {
common.LogError("Can only deal with open PRs. This one is", pr.State)
return
}
c := configs.GetPrjGitConfig(org, repo, pr.Base.Ref)
if c == nil {
if pr.Base.Repo.DefaultBranch == pr.Base.Ref {
c = configs.GetPrjGitConfig(org, repo, "")
}
}
if c == nil {
common.LogError("Cannot find config for PR.")
return
}
git, err := GitHandler.CreateGitHandler(org)
common.PanicOnError(err)
defer git.Close()
p := common.PullRequestWebhookEvent{
Action: "opened",
Number: num,
Pull_Request: common.PullRequestFromModel(pr),
}
if err = req.Opened.Process(&p, git, c); err != nil {
common.LogError("processor returned error", err)
}
}
return
}
checker := CreateDefaultStateChecker(*checkOnStart, req, gitea, time.Duration(*checkIntervalHours)*time.Hour)
checker := CreateDefaultStateChecker(*checkOnStart, req, Gitea, time.Duration(*checkIntervalHours)*time.Hour)
go checker.ConsistencyCheckProcess()
listenDefs := common.ListenDefinitions{