autogits/prjgit-updater/main.go
2024-09-02 17:56:00 +02:00

259 lines
8.4 KiB
Go

package main
import (
"flag"
"fmt"
"log"
"math/rand"
"os"
"path/filepath"
"slices"
"sync"
"time"
"src.opensuse.org/autogits/common"
)
var configuredRepos map[string]*common.AutogitConfig
var gitea *common.GiteaTransport
func isConfiguredOrg(org *common.Organization) bool {
_, found := configuredRepos[org.Username]
return found
}
func processRepositoryAction(h *common.RequestHandler) error {
action := h.Request.Data.(*common.RepositoryWebhookEvent)
config, configFound := configuredRepos[action.Organization.Username]
if !configFound {
if h.Git.DebugLogger {
h.StdLogger.Printf("Repository event for %s. Not configured. Ignoring.\n", action.Organization.Username)
}
return nil
}
prjgit := config.GitProjectName
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
}
prjGitRepo, err := gitea.CreateRepositoryIfNotExist(h.Git, *action.Organization, prjgit)
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)
switch action.Action {
case "created":
h.Git.GitExec(common.DefaultGitPrj, "submodule", "--quiet", "add", "--depth", "1", action.Repository.Clone_Url)
h.Git.GitExec(common.DefaultGitPrj, "commit", "-m", "Automatic package inclusion via Direct Workflow")
h.Git.GitExec(common.DefaultGitPrj, "push")
case "deleted":
if stat, err := os.Stat(filepath.Join(h.Git.GitPath, common.DefaultGitPrj, action.Repository.Name)); err != nil || !stat.IsDir() {
if h.Git.DebugLogger {
h.StdLogger.Printf("delete event for %s -- not in project. Ignoring\n", action.Repository.Name)
}
return nil
}
h.Git.GitExec(common.DefaultGitPrj, "rm", action.Repository.Name)
h.Git.GitExec(common.DefaultGitPrj, "commit", "-m", "Automatic package removal via Direct Workflow")
h.Git.GitExec(common.DefaultGitPrj, "push")
default:
return fmt.Errorf("%s: %s", "Unknown action type", action.Action)
}
return nil
}
func processPushAction(h *common.RequestHandler) error {
action := h.Request.Data.(*common.PushWebhookEvent)
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
}
prjgit := config.GitProjectName
if action.Repository.Name == prjgit {
if h.Git.DebugLogger {
h.StdLogger.Printf("push to %s -- ignoring\n", prjgit)
}
return nil
}
prjGitRepo, err := gitea.CreateRepositoryIfNotExist(h.Git, *action.Repository.Owner, prjgit)
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)
if stat, err := os.Stat(filepath.Join(h.Git.GitPath, common.DefaultGitPrj, action.Repository.Name)); err != nil || !stat.IsDir() {
if h.Git.DebugLogger {
h.StdLogger.Printf("Pushed to package that is not part of the project. Ignoring: %v\n", err)
}
return nil
}
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)
for _, commitId := range action.Commits {
if commitId.Id == id {
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)
h.Git.GitExec(common.DefaultGitPrj, "commit", "-a", "-m", "Automatic update via push via Direct Workflow")
h.Git.GitExec(common.DefaultGitPrj, "push")
return nil
}
}
h.StdLogger.Println("push of refs not on the main branch. ignoring.")
return nil
}
func verifyProjectState(git *common.GitHandler, orgName string, config *common.AutogitConfig) error {
org := common.Organization{
Username: orgName,
}
repo, err := gitea.CreateRepositoryIfNotExist(git, org, config.GitProjectName)
if err != nil {
return fmt.Errorf("Error fetching or creating '%s/%s' -- aborting verifyProjectState(). Err: %w", orgName, config.GitProjectName, err)
}
if err := git.GitExec("", "clone", "--depth", "1", repo.SSHURL, config.GitProjectName); err != nil {
return fmt.Errorf("Error closing projectgit for %s, Err: %w", config.GitProjectName, err)
}
sub, err := git.GitSubmoduleList(config.GitProjectName, "HEAD")
if err != nil {
return fmt.Errorf("Failed to fetch submodule list... Err: %w", err)
}
isGitUpdated := false
for filename, commitId := range sub {
commits, err := gitea.GetRecentCommits(orgName, filename, config.Branch, 10)
if err != nil {
return fmt.Errorf("Failed to fetch recent commits for package: '%s'. Err: %w", filename, err)
}
idx := 1000
for i, c := range commits {
if c.SHA == commitId {
idx = i
break
}
}
if idx == 0 {
// up-to-date
continue
} else if idx < len(commits) { // update
git.GitExec(config.GitProjectName, "submodule", "update", "--init", "--depth", "1", "--checkout", filename)
git.GitExec(filepath.Join(config.GitProjectName, filename), "fetch", "--depth", "1", "origin", commits[0].SHA)
git.GitExec(filepath.Join(config.GitProjectName, filename), "checkout", commits[0].SHA)
isGitUpdated = true
} else {
// probably need `merge-base` or `rev-list` here instead, or the project updated already
return fmt.Errorf("Cannot find SHA of last matching update for package: '%s'. idx: %d", filename, idx)
}
}
if isGitUpdated {
git.GitExec(common.DefaultGitPrj, "commit", "-a", "-m", "Automatic update via push via Direct Workflow -- SYNC")
git.GitExec(common.DefaultGitPrj, "push")
// commit changes and push
}
return nil
}
var checkOnStart bool
var checkInterval time.Duration
func consistencyCheckProcess(git *common.GitHandler, repos map[string]*common.AutogitConfig) {
if checkOnStart {
var wg sync.WaitGroup
for org, conf := range repos {
wg.Add(1)
go func() {
defer wg.Done()
verifyProjectState(git, 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(git, org, conf); err != nil {
log.Printf(" *** verification failed, org: `%s`, err: %#v\n", org, err)
}
log.Printf(" ++ verification complete, org: `%s`\n", org)
}
}
var debugMode bool
func main() {
workflowConfig := flag.String("config", "", "Repository and workflow definition file")
giteaHost := flag.String("gitea", "src.opensuse.org", "Gitea instance")
rabbitUrl := flag.String("url", "amqps://rabbit.opensuse.org", "URL for RabbitMQ instance")
flag.BoolVar(&debugMode, "debug", false, "Extra debugging information")
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")
flag.Parse()
checkInterval = time.Duration(*checkIntervalHours) * time.Hour
if len(*workflowConfig) == 0 {
log.Fatalln("No configuratio file specified. Aborting")
}
configs, err := common.ReadWorkflowConfigsFile(*workflowConfig)
if err != nil {
log.Fatal(err)
}
configuredRepos = make(map[string]*common.AutogitConfig)
orgs := make([]string, 0, 10)
for _, c := range configs {
if slices.Contains(c.Workflows, "push") {
if debugMode {
log.Printf(" + adding org: '%s', branch: '%s', prjgit: '%s'\n", c.Organization, c.Branch, c.GitProjectName)
}
configuredRepos[c.Organization] = c
orgs = append(orgs, c.Organization)
}
}
gitea = common.AllocateGiteaTransport(*giteaHost)
var defs common.ListenDefinitions
defs.GitAuthor = "GiteaBot - AutoDevel"
defs.RabbitURL = *rabbitUrl
defs.Handlers = make(map[string]common.RequestProcessor)
defs.Handlers[common.RequestType_Push] = processPushAction
defs.Handlers[common.RequestType_Repository] = processRepositoryAction
if err := common.RequireGiteaSecretToken(); err != nil {
log.Fatal(err)
}
if err := common.RequireRabbitSecrets(); err != nil {
log.Fatal(err)
}
log.Fatal(common.ProcessRabbitMQEvents(defs, orgs))
}