autogits/prjgit-updater/main.go

215 lines
6.7 KiB
Go
Raw Normal View History

2024-07-07 21:08:41 +02:00
package main
import (
2024-08-26 17:07:52 +02:00
"flag"
2024-07-07 21:08:41 +02:00
"fmt"
2024-08-26 17:07:52 +02:00
"log"
2024-08-28 12:39:32 +02:00
"math/rand"
2024-07-07 21:08:41 +02:00
"os"
"path/filepath"
2024-08-28 00:45:47 +02:00
"slices"
2024-08-28 12:39:32 +02:00
"sync"
"time"
2024-07-07 21:08:41 +02:00
"src.opensuse.org/autogits/common"
)
2024-08-28 00:45:47 +02:00
type ConfigRepos struct {
prjgit string
branch string // "" == default branch
}
2024-08-28 17:53:15 +02:00
2024-08-28 00:45:47 +02:00
var configuredRepos map[string]ConfigRepos
2024-08-30 17:05:57 +02:00
var gitea *common.GiteaTransport
2024-08-28 00:45:47 +02:00
2024-08-30 16:37:51 +02:00
func isConfiguredOrg(org *common.Organization) bool {
_, found := configuredRepos[org.Username]
return found
}
2024-07-08 17:14:18 +02:00
func processRepositoryAction(h *common.RequestHandler) error {
2024-08-28 00:45:47 +02:00
action := h.Request.Data.(*common.RepositoryWebhookEvent)
2024-08-30 16:37:51 +02:00
config, configFound := configuredRepos[action.Organization.Username]
2024-07-07 21:08:41 +02:00
2024-08-30 16:37:51 +02:00
if !configFound {
if h.Git.DebugLogger {
h.StdLogger.Printf("Repository event for %s. Not configured. Ignoring.\n", action.Organization.Username)
}
2024-07-08 17:14:18 +02:00
return nil
2024-07-07 21:08:41 +02:00
}
2024-08-30 16:37:51 +02:00
prjgit := config.prjgit
if action.Repository.Name == prjgit {
if h.Git.DebugLogger {
h.StdLogger.Printf("repository event %s for PrjGit '%s'. Ignoring\n", common.DefaultGitPrj, action.Action)
}
return nil
}
2024-08-30 17:05:57 +02:00
prjGitRepo, err := gitea.CreateRepositoryIfNotExist(h.Git, *action.Organization, prjgit)
2024-08-30 16:37:51 +02:00
if err != nil {
return fmt.Errorf("Error accessing/creating prjgit: %s err: %w", prjgit, err)
}
h.Git.GitExec("", "clone", "--depth", "1", prjGitRepo.SSHURL, common.DefaultGitPrj)
2024-07-07 21:08:41 +02:00
switch action.Action {
case "created":
2024-08-28 17:20:09 +02:00
h.Git.GitExec(common.DefaultGitPrj, "submodule", "--quiet", "add", "--depth", "1", action.Repository.Clone_Url)
2024-08-30 16:37:51 +02:00
h.Git.GitExec(common.DefaultGitPrj, "commit", "-m", "Automatic package inclusion via Direct Workflow")
2024-08-28 17:20:09 +02:00
h.Git.GitExec(common.DefaultGitPrj, "push")
2024-08-30 16:37:51 +02:00
2024-07-07 21:08:41 +02:00
case "deleted":
2024-08-28 17:20:09 +02:00
if stat, err := os.Stat(filepath.Join(h.Git.GitPath, common.DefaultGitPrj, action.Repository.Name)); err != nil || !stat.IsDir() {
2024-08-30 16:37:51 +02:00
if h.Git.DebugLogger {
h.StdLogger.Printf("delete event for %s -- not in project. Ignoring\n", action.Repository.Name)
}
2024-07-08 17:14:18 +02:00
return nil
2024-07-07 21:08:41 +02:00
}
2024-08-28 17:20:09 +02:00
h.Git.GitExec(common.DefaultGitPrj, "rm", action.Repository.Name)
2024-08-30 16:37:51 +02:00
h.Git.GitExec(common.DefaultGitPrj, "commit", "-m", "Automatic package removal via Direct Workflow")
2024-08-28 17:20:09 +02:00
h.Git.GitExec(common.DefaultGitPrj, "push")
2024-08-30 16:37:51 +02:00
2024-07-07 21:08:41 +02:00
default:
2024-07-08 17:14:18 +02:00
return fmt.Errorf("%s: %s", "Unknown action type", action.Action)
2024-07-07 21:08:41 +02:00
}
2024-07-08 17:14:18 +02:00
return nil
}
2024-07-07 21:08:41 +02:00
2024-07-08 17:14:18 +02:00
func processPushAction(h *common.RequestHandler) error {
2024-08-28 00:45:47 +02:00
action := h.Request.Data.(*common.PushWebhookEvent)
2024-08-30 16:37:51 +02:00
config, configFound := configuredRepos[action.Repository.Owner.Username]
if !configFound {
if h.Git.DebugLogger {
h.StdLogger.Printf("Push event to Organizationr '%s'. Not configured. Ignoring.\n", action.Repository.Owner.Username)
}
return nil
}
2024-07-07 21:08:41 +02:00
2024-08-30 16:37:51 +02:00
prjgit := config.prjgit
if action.Repository.Name == prjgit {
if h.Git.DebugLogger {
h.StdLogger.Printf("push to %s -- ignoring\n", prjgit)
}
2024-07-08 17:14:18 +02:00
return nil
2024-07-07 21:08:41 +02:00
}
2024-08-30 17:05:57 +02:00
prjGitRepo, err := gitea.CreateRepositoryIfNotExist(h.Git, *action.Repository.Owner, prjgit)
2024-08-30 16:37:51 +02:00
if err != nil {
return fmt.Errorf("Error accessing/creating prjgit: %s err: %w", prjgit, err)
}
h.Git.GitExec("", "clone", "--depth", "1", prjGitRepo.SSHURL, common.DefaultGitPrj)
2024-08-28 17:20:09 +02:00
if stat, err := os.Stat(filepath.Join(h.Git.GitPath, common.DefaultGitPrj, action.Repository.Name)); err != nil || !stat.IsDir() {
2024-08-30 16:37:51 +02:00
if h.Git.DebugLogger {
h.StdLogger.Printf("Pushed to package that is not part of the project. Ignoring: %v\n", err)
}
2024-08-28 00:45:47 +02:00
return nil
2024-07-07 21:08:41 +02:00
}
2024-08-28 17:20:09 +02:00
h.Git.GitExec(common.DefaultGitPrj, "submodule", "update", "--init", "--depth", "1", "--checkout", action.Repository.Name)
id, _ := h.Git.GitBranchHead(filepath.Join(common.DefaultGitPrj, action.Repository.Name), action.Repository.Default_Branch)
2024-07-07 21:08:41 +02:00
for _, commitId := range action.Commits {
if commitId.Id == id {
2024-08-28 17:20:09 +02:00
h.Git.GitExec(filepath.Join(common.DefaultGitPrj, action.Repository.Name), "fetch", "--depth", "1", "origin", id)
h.Git.GitExec(filepath.Join(common.DefaultGitPrj, action.Repository.Name), "checkout", id)
2024-08-30 16:37:51 +02:00
h.Git.GitExec(common.DefaultGitPrj, "commit", "-a", "-m", "Automatic update via push via Direct Workflow")
2024-08-28 17:20:09 +02:00
h.Git.GitExec(common.DefaultGitPrj, "push")
2024-07-08 17:14:18 +02:00
return nil
2024-07-07 21:08:41 +02:00
}
}
2024-08-26 17:07:52 +02:00
h.StdLogger.Println("push of refs not on the main branch. ignoring.")
2024-07-07 21:08:41 +02:00
return nil
}
2024-08-28 12:39:32 +02:00
func verifyProjectState(org string, config ConfigRepos) error {
2024-08-28 17:53:15 +02:00
return nil
2024-08-28 12:39:32 +02:00
}
var checkOnStart bool
2024-08-28 17:53:15 +02:00
var checkInterval time.Duration
2024-08-28 12:39:32 +02:00
func consistencyCheckProcess(repos map[string]ConfigRepos) {
if checkOnStart {
2024-08-28 17:53:15 +02:00
var wg sync.WaitGroup
2024-08-28 12:39:32 +02:00
for org, conf := range repos {
wg.Add(1)
go func() {
defer wg.Done()
2024-08-28 17:53:15 +02:00
2024-08-28 12:39:32 +02:00
verifyProjectState(org, conf)
}()
}
wg.Wait()
log.Printf("== Startup consistency check done...")
}
for org, conf := range repos {
time.Sleep(checkInterval - checkInterval/2 + time.Duration(rand.Int63n(int64(checkInterval))))
log.Printf(" ++ starting verification, org: `%s`\n", org)
if err := verifyProjectState(org, conf); err != nil {
log.Printf(" *** verification failed, org: `%s`, err: %#v\n", org, err)
}
log.Printf(" ++ verification complete, org: `%s`\n", org)
}
}
2024-08-28 00:45:47 +02:00
var debugMode bool
2024-07-07 21:08:41 +02:00
func main() {
2024-08-26 17:07:52 +02:00
workflowConfig := flag.String("config", "", "Repository and workflow definition file")
2024-08-30 17:05:57 +02:00
giteaHost := flag.String("gitea", "src.opensuse.org", "Gitea instance")
2024-08-28 00:45:47 +02:00
rabbitUrl := flag.String("url", "amqps://rabbit.opensuse.org", "URL for RabbitMQ instance")
flag.BoolVar(&debugMode, "debug", false, "Extra debugging information")
2024-08-28 12:39:32 +02:00
flag.BoolVar(&checkOnStart, "check-on-start", false, "Check all repositories for consistency on start, without delays")
checkIntervalHours := flag.Float64("check-interval", 5, "Check interval (+-random delay) for repositories for consitency, in hours")
2024-08-26 17:07:52 +02:00
flag.Parse()
2024-08-28 12:39:32 +02:00
checkInterval = time.Duration(*checkIntervalHours) * time.Hour
2024-08-28 00:45:47 +02:00
if len(*workflowConfig) == 0 {
log.Fatalln("No configuratio file specified. Aborting")
}
2024-08-26 17:07:52 +02:00
configs, err := common.ReadWorkflowConfigsFile(*workflowConfig)
if err != nil {
log.Fatalf("Error reading config file. err: %v", err)
}
2024-08-28 00:45:47 +02:00
configuredRepos = make(map[string]ConfigRepos)
orgs := make([]string, 0, 10)
for _, c := range configs {
if slices.Contains(c.Workflows, "push") {
if debugMode {
2024-08-28 17:53:15 +02:00
log.Printf(" + adding org: '%s', branch: '%s', prjgit: '%s'\n", c.Organization, c.Branch, c.GitProjectName)
2024-08-28 00:45:47 +02:00
}
configuredRepos[c.Organization] = ConfigRepos{
prjgit: c.GitProjectName,
branch: c.Branch,
}
orgs = append(orgs, c.Organization)
}
}
2024-08-30 17:05:57 +02:00
gitea = common.AllocateGiteaTransport(*giteaHost)
2024-07-08 17:14:18 +02:00
var defs common.ListenDefinitions
defs.GitAuthor = "GiteaBot - AutoDevel"
2024-08-28 00:45:47 +02:00
defs.RabbitURL = *rabbitUrl
2024-07-09 15:05:49 +02:00
2024-08-28 00:45:47 +02:00
defs.Handlers = make(map[string]common.RequestProcessor)
2024-07-08 17:14:18 +02:00
defs.Handlers[common.RequestType_Push] = processPushAction
defs.Handlers[common.RequestType_Repository] = processRepositoryAction
2024-08-28 00:45:47 +02:00
if err := common.RequireGiteaSecretToken(); err != nil {
log.Fatal(err)
}
if err := common.RequireRabbitSecrets(); err != nil {
log.Fatal(err)
}
log.Fatal(common.ProcessRabbitMQEvents(defs, orgs))
2024-07-07 21:08:41 +02:00
}