diff --git a/bots-common/log.go b/bots-common/log.go index 8ba4af1..f4f9ab6 100644 --- a/bots-common/log.go +++ b/bots-common/log.go @@ -33,3 +33,9 @@ func CreateStdoutLogger(stdout, stderr io.Writer) (*log.Logger, *log.Logger) { errLogger := log.New(stderr, idStr, log.Lmsgprefix) return stdLogger, errLogger } + +func PanicOnError(err error) { + if err != nil { + panic(err) + } +} diff --git a/prjgit-updater/main.go b/prjgit-updater/main.go index 9909c2e..c6c79c2 100644 --- a/prjgit-updater/main.go +++ b/prjgit-updater/main.go @@ -78,12 +78,11 @@ func processRepositoryAction(h *common.RequestHandler) error { return err } + func processConfiguredRepositoryAction(h *common.RequestHandler, action *common.RepositoryWebhookEvent, config *common.AutogitConfig) error { prjgit := config.GitProjectName git, err := common.CreateGitHandler(GitAuthor, GitEmail, AppName) - if err != nil { - return err - } + common.PanicOnError(err) // defer git.Close() prjGitRepo, err := gitea.CreateRepositoryIfNotExist(git, *action.Organization, prjgit) @@ -91,21 +90,13 @@ func processConfiguredRepositoryAction(h *common.RequestHandler, action *common. return fmt.Errorf("Error accessing/creating prjgit: %s err: %w", prjgit, err) } - if err := git.GitExec("", "clone", "--depth", "1", prjGitRepo.SSHURL, common.DefaultGitPrj); err != nil { - return err - } + common.PanicOnError(git.GitExec("", "clone", "--depth", "1", prjGitRepo.SSHURL, common.DefaultGitPrj)) switch action.Action { case "created": - if err := git.GitExec(common.DefaultGitPrj, "submodule", "--quiet", "add", "--depth", "1", action.Repository.Clone_Url); err != nil { - return err - } - if err := git.GitExec(common.DefaultGitPrj, "commit", "-m", "Automatic package inclusion via Direct Workflow"); err != nil { - return err - } - if err := git.GitExec(common.DefaultGitPrj, "push"); err != nil { - return err - } + common.PanicOnError(git.GitExec(common.DefaultGitPrj, "submodule", "--quiet", "add", "--depth", "1", action.Repository.Clone_Url)) + common.PanicOnError(git.GitExec(common.DefaultGitPrj, "commit", "-m", "Automatic package inclusion via Direct Workflow")) + common.PanicOnError(git.GitExec(common.DefaultGitPrj, "push")) case "deleted": if stat, err := os.Stat(filepath.Join(git.GitPath, common.DefaultGitPrj, action.Repository.Name)); err != nil || !stat.IsDir() { @@ -114,15 +105,9 @@ func processConfiguredRepositoryAction(h *common.RequestHandler, action *common. } return nil } - if err := git.GitExec(common.DefaultGitPrj, "rm", action.Repository.Name); err != nil { - return err - } - if err := git.GitExec(common.DefaultGitPrj, "commit", "-m", "Automatic package removal via Direct Workflow"); err != nil { - return err - } - if err := git.GitExec(common.DefaultGitPrj, "push"); err != nil { - return err - } + common.PanicOnError(git.GitExec(common.DefaultGitPrj, "rm", action.Repository.Name)) + common.PanicOnError(git.GitExec(common.DefaultGitPrj, "commit", "-m", "Automatic package removal via Direct Workflow")) + common.PanicOnError(git.GitExec(common.DefaultGitPrj, "push")) default: return fmt.Errorf("%s: %s", "Unknown action type", action.Action) @@ -157,9 +142,7 @@ func processPushAction(h *common.RequestHandler) error { func processConfiguredPushAction(h *common.RequestHandler, action *common.PushWebhookEvent, config *common.AutogitConfig) error { prjgit := config.GitProjectName git, err := common.CreateGitHandler(GitAuthor, GitEmail, AppName) - if err != nil { - return err - } + common.PanicOnError(err) defer git.Close() prjGitRepo, err := gitea.CreateRepositoryIfNotExist(git, *action.Repository.Owner, prjgit) @@ -167,36 +150,22 @@ func processConfiguredPushAction(h *common.RequestHandler, action *common.PushWe return fmt.Errorf("Error accessing/creating prjgit: %s err: %w", prjgit, err) } - if err := git.GitExec("", "clone", "--depth", "1", prjGitRepo.SSHURL, common.DefaultGitPrj); err != nil { - return err - } + common.PanicOnError(git.GitExec("", "clone", "--depth", "1", prjGitRepo.SSHURL, common.DefaultGitPrj)) if stat, err := os.Stat(filepath.Join(git.GitPath, common.DefaultGitPrj, action.Repository.Name)); err != nil || !stat.IsDir() { if git.DebugLogger { h.StdLogger.Printf("Pushed to package that is not part of the project. Ignoring: %v\n", err) } return nil } - if err := git.GitExec(common.DefaultGitPrj, "submodule", "update", "--init", "--depth", "1", "--checkout", action.Repository.Name); err != nil { - return err - } + common.PanicOnError(git.GitExec(common.DefaultGitPrj, "submodule", "update", "--init", "--depth", "1", "--checkout", action.Repository.Name)) id, err := git.GitBranchHead(filepath.Join(common.DefaultGitPrj, action.Repository.Name), action.Repository.Default_Branch) - if err != nil { - return err - } + common.PanicOnError(err) for _, commitId := range action.Commits { if commitId.Id == id { - if err := git.GitExec(filepath.Join(common.DefaultGitPrj, action.Repository.Name), "fetch", "--depth", "1", "origin", id); err != nil { - return err - } - if err := git.GitExec(filepath.Join(common.DefaultGitPrj, action.Repository.Name), "checkout", id); err != nil { - return err - } - if err := git.GitExec(common.DefaultGitPrj, "commit", "-a", "-m", "Automatic update via push via Direct Workflow"); err != nil { - return err - } - if err := git.GitExec(common.DefaultGitPrj, "push"); err != nil { - return err - } + common.PanicOnError(git.GitExec(filepath.Join(common.DefaultGitPrj, action.Repository.Name), "fetch", "--depth", "1", "origin", id)) + common.PanicOnError(git.GitExec(filepath.Join(common.DefaultGitPrj, action.Repository.Name), "checkout", id)) + common.PanicOnError(git.GitExec(common.DefaultGitPrj, "commit", "-a", "-m", "Automatic update via push via Direct Workflow")) + common.PanicOnError(git.GitExec(common.DefaultGitPrj, "push")) return nil } } @@ -205,7 +174,17 @@ func processConfiguredPushAction(h *common.RequestHandler, action *common.PushWe return nil } -func verifyProjectState(git *common.GitHandler, orgName string, config *common.AutogitConfig, configs []*common.AutogitConfig) error { +func verifyProjectState(git *common.GitHandler, orgName string, config *common.AutogitConfig, configs []*common.AutogitConfig) (err error) { + defer func() { + e := recover() + if e != nil { + errCast, ok := e.(error) + if ok { + err = errCast + } + } + }() + org := common.Organization{ Username: orgName, } @@ -214,15 +193,10 @@ func verifyProjectState(git *common.GitHandler, orgName string, config *common.A 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) - } - + common.PanicOnError(git.GitExec("", "clone", "--depth", "1", repo.SSHURL, config.GitProjectName)) log.Println("getting submodule list") sub, err := git.GitSubmoduleList(config.GitProjectName, "HEAD") - if err != nil { - return fmt.Errorf("Failed to fetch submodule list... Err: %w", err) - } + common.PanicOnError(err) isGitUpdated := false next_package: @@ -262,15 +236,9 @@ next_package: // up-to-date continue } else if idx < len(commits) { // update - if err := git.GitExec(config.GitProjectName, "submodule", "update", "--init", "--depth", "1", "--checkout", filename); err != nil { - return err - } - if err := git.GitExec(filepath.Join(config.GitProjectName, filename), "fetch", "--depth", "1", "origin", commits[0].SHA); err != nil { - return err - } - if err := git.GitExec(filepath.Join(config.GitProjectName, filename), "checkout", commits[0].SHA); err != nil { - return err - } + common.PanicOnError(git.GitExec(config.GitProjectName, "submodule", "update", "--init", "--depth", "1", "--checkout", filename)) + common.PanicOnError(git.GitExec(filepath.Join(config.GitProjectName, filename), "fetch", "--depth", "1", "origin", commits[0].SHA)) + common.PanicOnError(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 @@ -321,29 +289,19 @@ next_repo: } // add repository to git project - if err := git.GitExec(config.GitProjectName, "submodule", "--quiet", "add", "--depth", "1", r.SSHURL); err != nil { - return fmt.Errorf("Cannot add submodule '%s' to project '%s'. Err: %w", r.Name, config.GitProjectName, err) - } + common.PanicOnError(git.GitExec(config.GitProjectName, "submodule", "--quiet", "add", "--depth", "1", r.SSHURL)) if len(config.Branch) > 0 { - if err := git.GitExec(path.Join(config.GitProjectName, r.Name), "fetch", "--depth", "1", "origin", config.Branch); err != nil { - return fmt.Errorf("Failed to fetch branch '%s' from '%s'/'%s'. Err: %w", config.Branch, orgName, r.Name, err) - } - if err := git.GitExec(path.Join(config.GitProjectName, r.Name), "checkout", config.Branch); err != nil { - return fmt.Errorf("Failed to checkout fetched branch '%s' from '%s'/'%s'. Err: %w", config.Branch, orgName, r.Name, err) - } + common.PanicOnError(git.GitExec(path.Join(config.GitProjectName, r.Name), "fetch", "--depth", "1", "origin", config.Branch)) + common.PanicOnError(git.GitExec(path.Join(config.GitProjectName, r.Name), "checkout", config.Branch)) } isGitUpdated = true } if isGitUpdated { - if err := git.GitExec(config.GitProjectName, "commit", "-a", "-m", "Automatic update via push via Direct Workflow -- SYNC"); err != nil { - return err - } - if err := git.GitExec(config.GitProjectName, "push"); err != nil { - return err - } + common.PanicOnError(git.GitExec(config.GitProjectName, "commit", "-a", "-m", "Automatic update via push via Direct Workflow -- SYNC")) + common.PanicOnError(git.GitExec(config.GitProjectName, "push")) } if debugMode { @@ -356,33 +314,16 @@ next_repo: var checkOnStart bool var checkInterval time.Duration -func consistencyCheckProcess() { - if checkOnStart { - log.Println("== Startup consistency check begin...") - for org, configs := range configuredRepos { - for _, config := range configs { - log.Println(" - org: ", org, " - config: ", config.GitProjectName) - git, err := common.CreateGitHandler(GitAuthor, GitEmail, AppName) - if err != nil { - log.Println("Failed to allocate GitHandler:", err) - return - } - if err := verifyProjectState(git, org, config, configs); err != nil { - log.Println("Failed to verify state of org:", org, err) - return - } - } - } - log.Println("== Startup consistency check done...") - } - +func checkRepos() { for org, configs := range configuredRepos { for _, config := range configs { - sleepInterval := checkInterval - checkInterval/2 + time.Duration(rand.Int63n(int64(checkInterval))) - log.Println(" - sleep interval", sleepInterval) - time.Sleep(sleepInterval) + if checkInterval > 0 { + sleepInterval := checkInterval - checkInterval/2 + time.Duration(rand.Int63n(int64(checkInterval))) + log.Println(" - sleep interval", sleepInterval, "until next check") + time.Sleep(sleepInterval) + } - log.Printf(" ++ starting verification, org: `%s`\n", org) + log.Printf(" ++ starting verification, org: `%s` config: `%s`\n", org, config.GitProjectName) git, err := common.CreateGitHandler(GitAuthor, GitEmail, AppName) if err != nil { log.Println("Faield to allocate GitHandler:", err) @@ -391,11 +332,26 @@ func consistencyCheckProcess() { if err := verifyProjectState(git, org, config, configs); err != nil { log.Printf(" *** verification failed, org: `%s`, err: %#v\n", org, err) } - log.Printf(" ++ verification complete, org: `%s`\n", org) + log.Printf(" ++ verification complete, org: `%s` config: `%s`\n", org, config.GitProjectName) } } } +func consistencyCheckProcess() { + if checkOnStart { + savedCheckInterval := checkInterval + checkInterval = 0 + log.Println("== Startup consistency check begin...") + checkRepos() + log.Println("== Startup consistency check done...") + checkInterval = savedCheckInterval + } + + for { + checkRepos() + } +} + var debugMode bool func main() {