Files
autogits/reparent-bot/main.go
2026-02-03 17:18:46 +01:00

143 lines
3.7 KiB
Go

package main
import (
"flag"
"net/url"
"os"
"slices"
"time"
"src.opensuse.org/autogits/common"
)
func (bot *ReparentBot) PeriodCheck() {
common.LogDebug("--- starting periodic check ---")
for _, c := range bot.configs {
org, repo, _ := c.GetPrjGit()
issues, err := bot.gitea.GetOpenIssues(org, repo, common.Label_NewRepository, common.IssueType_Issue, "[ADD]")
if err != nil {
common.LogError("Error fetching issues for processing:", err)
return
}
common.LogDebug("Processing potential new issues for", org+"/"+repo, len(issues))
for _, issue := range issues {
err := bot.ProcessIssue(org, repo, issue)
if err != nil {
common.LogError("issue processing error:", err)
}
}
}
common.LogDebug("--- ending periodic check ---")
}
func main() {
giteaUrl := flag.String("gitea-url", "https://src.opensuse.org", "Gitea instance used")
rabbitMqHost := flag.String("rabbit-url", "amqps://rabbit.opensuse.org", "RabbitMQ instance where Gitea webhook notifications are sent")
interval := flag.Int64("interval", 10, "Notification polling interval in minutes (min 1 min)")
configFile := flag.String("config", "", "PrjGit listing config file")
logging := flag.String("logging", "info", "Logging level: [none, error, info, debug]")
flag.BoolVar(&common.IsDryRun, "dry", false, "Dry run, no effect. For debugging")
testMain := flag.Bool("test.main", false, "Internal use for testing main")
flag.Parse()
if *testMain {
return
}
if err := common.SetLoggingLevelFromString(*logging); err != nil {
common.LogError(err.Error())
return
}
if cf := os.Getenv("AUTOGITS_CONFIG"); len(cf) > 0 {
*configFile = cf
}
if url := os.Getenv("AUTOGITS_URL"); len(url) > 0 {
*giteaUrl = url
}
if url := os.Getenv("AUTOGITS_RABBITURL"); len(url) > 0 {
*rabbitMqHost = url
}
if *configFile == "" {
common.LogError("Missing config file")
return
}
configData, err := common.ReadConfigFile(*configFile)
if err != nil {
common.LogError("Failed to read config file", err)
return
}
if err := common.RequireGiteaSecretToken(); err != nil {
common.LogError(err)
return
}
if err := common.RequireRabbitSecrets(); err != nil {
common.LogError(err)
return
}
giteaTransport := common.AllocateGiteaTransport(*giteaUrl)
configs, err := common.ResolveWorkflowConfigs(giteaTransport, configData)
if err != nil {
common.LogError("Cannot parse workflow configs:", err)
return
}
if *interval < 1 {
*interval = 1
}
bot := &ReparentBot{
gitea: giteaTransport,
configs: configs,
giteaUrl: *giteaUrl,
maintainershipFetcher: &RealMaintainershipFetcher{},
}
common.LogInfo(" ** reparent-bot starting")
common.LogInfo(" ** polling interval:", *interval, "min")
common.LogInfo(" ** connecting to RabbitMQ:", *rabbitMqHost)
u, err := url.Parse(*rabbitMqHost)
if err != nil {
common.LogError("Cannot parse RabbitMQ host:", err)
return
}
botUser, err := bot.gitea.GetCurrentUser()
if err != nil {
common.LogError("Failed to fetch current user:", err)
return
}
bot.botUser = botUser.UserName
process_issue := IssueProcessor{
bot: bot,
}
eventsProcessor := &common.RabbitMQGiteaEventsProcessor{
Orgs: []string{},
Handlers: map[string]common.RequestProcessor{
common.RequestType_Issue: &process_issue,
common.RequestType_IssueComment: &process_issue,
},
}
eventsProcessor.Connection().RabbitURL = u
for _, c := range bot.configs {
if org, _, _ := c.GetPrjGit(); !slices.Contains(eventsProcessor.Orgs, org) {
eventsProcessor.Orgs = append(eventsProcessor.Orgs, org)
}
}
go common.ProcessRabbitMQEvents(eventsProcessor)
for {
bot.PeriodCheck()
time.Sleep(time.Duration(*interval * int64(time.Minute)))
}
}