simplify error handling

This commit is contained in:
Adam Majer 2024-09-11 18:50:49 +02:00
parent c61d648294
commit 4c8eae5e7c
2 changed files with 67 additions and 105 deletions

View File

@ -33,3 +33,9 @@ func CreateStdoutLogger(stdout, stderr io.Writer) (*log.Logger, *log.Logger) {
errLogger := log.New(stderr, idStr, log.Lmsgprefix) errLogger := log.New(stderr, idStr, log.Lmsgprefix)
return stdLogger, errLogger return stdLogger, errLogger
} }
func PanicOnError(err error) {
if err != nil {
panic(err)
}
}

View File

@ -78,12 +78,11 @@ func processRepositoryAction(h *common.RequestHandler) error {
return err return err
} }
func processConfiguredRepositoryAction(h *common.RequestHandler, action *common.RepositoryWebhookEvent, config *common.AutogitConfig) error { func processConfiguredRepositoryAction(h *common.RequestHandler, action *common.RepositoryWebhookEvent, config *common.AutogitConfig) error {
prjgit := config.GitProjectName prjgit := config.GitProjectName
git, err := common.CreateGitHandler(GitAuthor, GitEmail, AppName) git, err := common.CreateGitHandler(GitAuthor, GitEmail, AppName)
if err != nil { common.PanicOnError(err)
return err
}
// defer git.Close() // defer git.Close()
prjGitRepo, err := gitea.CreateRepositoryIfNotExist(git, *action.Organization, prjgit) 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) 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 { common.PanicOnError(git.GitExec("", "clone", "--depth", "1", prjGitRepo.SSHURL, common.DefaultGitPrj))
return err
}
switch action.Action { switch action.Action {
case "created": case "created":
if err := git.GitExec(common.DefaultGitPrj, "submodule", "--quiet", "add", "--depth", "1", action.Repository.Clone_Url); err != nil { common.PanicOnError(git.GitExec(common.DefaultGitPrj, "submodule", "--quiet", "add", "--depth", "1", action.Repository.Clone_Url))
return err common.PanicOnError(git.GitExec(common.DefaultGitPrj, "commit", "-m", "Automatic package inclusion via Direct Workflow"))
} common.PanicOnError(git.GitExec(common.DefaultGitPrj, "push"))
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
}
case "deleted": case "deleted":
if stat, err := os.Stat(filepath.Join(git.GitPath, common.DefaultGitPrj, action.Repository.Name)); err != nil || !stat.IsDir() { 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 return nil
} }
if err := git.GitExec(common.DefaultGitPrj, "rm", action.Repository.Name); err != nil { common.PanicOnError(git.GitExec(common.DefaultGitPrj, "rm", action.Repository.Name))
return err common.PanicOnError(git.GitExec(common.DefaultGitPrj, "commit", "-m", "Automatic package removal via Direct Workflow"))
} common.PanicOnError(git.GitExec(common.DefaultGitPrj, "push"))
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
}
default: default:
return fmt.Errorf("%s: %s", "Unknown action type", action.Action) 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 { func processConfiguredPushAction(h *common.RequestHandler, action *common.PushWebhookEvent, config *common.AutogitConfig) error {
prjgit := config.GitProjectName prjgit := config.GitProjectName
git, err := common.CreateGitHandler(GitAuthor, GitEmail, AppName) git, err := common.CreateGitHandler(GitAuthor, GitEmail, AppName)
if err != nil { common.PanicOnError(err)
return err
}
defer git.Close() defer git.Close()
prjGitRepo, err := gitea.CreateRepositoryIfNotExist(git, *action.Repository.Owner, prjgit) 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) 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 { common.PanicOnError(git.GitExec("", "clone", "--depth", "1", prjGitRepo.SSHURL, common.DefaultGitPrj))
return err
}
if stat, err := os.Stat(filepath.Join(git.GitPath, common.DefaultGitPrj, action.Repository.Name)); err != nil || !stat.IsDir() { if stat, err := os.Stat(filepath.Join(git.GitPath, common.DefaultGitPrj, action.Repository.Name)); err != nil || !stat.IsDir() {
if git.DebugLogger { if git.DebugLogger {
h.StdLogger.Printf("Pushed to package that is not part of the project. Ignoring: %v\n", err) h.StdLogger.Printf("Pushed to package that is not part of the project. Ignoring: %v\n", err)
} }
return nil return nil
} }
if err := git.GitExec(common.DefaultGitPrj, "submodule", "update", "--init", "--depth", "1", "--checkout", action.Repository.Name); err != nil { common.PanicOnError(git.GitExec(common.DefaultGitPrj, "submodule", "update", "--init", "--depth", "1", "--checkout", action.Repository.Name))
return err
}
id, err := git.GitBranchHead(filepath.Join(common.DefaultGitPrj, action.Repository.Name), action.Repository.Default_Branch) id, err := git.GitBranchHead(filepath.Join(common.DefaultGitPrj, action.Repository.Name), action.Repository.Default_Branch)
if err != nil { common.PanicOnError(err)
return err
}
for _, commitId := range action.Commits { for _, commitId := range action.Commits {
if commitId.Id == id { if commitId.Id == id {
if err := git.GitExec(filepath.Join(common.DefaultGitPrj, action.Repository.Name), "fetch", "--depth", "1", "origin", id); err != nil { common.PanicOnError(git.GitExec(filepath.Join(common.DefaultGitPrj, action.Repository.Name), "fetch", "--depth", "1", "origin", id))
return err 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"))
if err := git.GitExec(filepath.Join(common.DefaultGitPrj, action.Repository.Name), "checkout", id); err != nil { common.PanicOnError(git.GitExec(common.DefaultGitPrj, "push"))
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
}
return nil return nil
} }
} }
@ -205,7 +174,17 @@ func processConfiguredPushAction(h *common.RequestHandler, action *common.PushWe
return nil 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{ org := common.Organization{
Username: orgName, 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) 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 { common.PanicOnError(git.GitExec("", "clone", "--depth", "1", repo.SSHURL, config.GitProjectName))
return fmt.Errorf("Error closing projectgit for %s, Err: %w", config.GitProjectName, err)
}
log.Println("getting submodule list") log.Println("getting submodule list")
sub, err := git.GitSubmoduleList(config.GitProjectName, "HEAD") sub, err := git.GitSubmoduleList(config.GitProjectName, "HEAD")
if err != nil { common.PanicOnError(err)
return fmt.Errorf("Failed to fetch submodule list... Err: %w", err)
}
isGitUpdated := false isGitUpdated := false
next_package: next_package:
@ -262,15 +236,9 @@ next_package:
// up-to-date // up-to-date
continue continue
} else if idx < len(commits) { // update } else if idx < len(commits) { // update
if err := git.GitExec(config.GitProjectName, "submodule", "update", "--init", "--depth", "1", "--checkout", filename); err != nil { common.PanicOnError(git.GitExec(config.GitProjectName, "submodule", "update", "--init", "--depth", "1", "--checkout", filename))
return err 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))
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
}
isGitUpdated = true isGitUpdated = true
} else { } else {
// probably need `merge-base` or `rev-list` here instead, or the project updated already // 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 // add repository to git project
if err := git.GitExec(config.GitProjectName, "submodule", "--quiet", "add", "--depth", "1", r.SSHURL); err != nil { common.PanicOnError(git.GitExec(config.GitProjectName, "submodule", "--quiet", "add", "--depth", "1", r.SSHURL))
return fmt.Errorf("Cannot add submodule '%s' to project '%s'. Err: %w", r.Name, config.GitProjectName, err)
}
if len(config.Branch) > 0 { if len(config.Branch) > 0 {
if err := git.GitExec(path.Join(config.GitProjectName, r.Name), "fetch", "--depth", "1", "origin", config.Branch); err != nil { common.PanicOnError(git.GitExec(path.Join(config.GitProjectName, r.Name), "fetch", "--depth", "1", "origin", config.Branch))
return fmt.Errorf("Failed to fetch branch '%s' from '%s'/'%s'. Err: %w", config.Branch, orgName, r.Name, err) common.PanicOnError(git.GitExec(path.Join(config.GitProjectName, r.Name), "checkout", config.Branch))
}
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)
}
} }
isGitUpdated = true isGitUpdated = true
} }
if isGitUpdated { if isGitUpdated {
if err := git.GitExec(config.GitProjectName, "commit", "-a", "-m", "Automatic update via push via Direct Workflow -- SYNC"); err != nil { common.PanicOnError(git.GitExec(config.GitProjectName, "commit", "-a", "-m", "Automatic update via push via Direct Workflow -- SYNC"))
return err common.PanicOnError(git.GitExec(config.GitProjectName, "push"))
}
if err := git.GitExec(config.GitProjectName, "push"); err != nil {
return err
}
} }
if debugMode { if debugMode {
@ -356,33 +314,16 @@ next_repo:
var checkOnStart bool var checkOnStart bool
var checkInterval time.Duration var checkInterval time.Duration
func consistencyCheckProcess() { func checkRepos() {
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...")
}
for org, configs := range configuredRepos { for org, configs := range configuredRepos {
for _, config := range configs { for _, config := range configs {
if checkInterval > 0 {
sleepInterval := checkInterval - checkInterval/2 + time.Duration(rand.Int63n(int64(checkInterval))) sleepInterval := checkInterval - checkInterval/2 + time.Duration(rand.Int63n(int64(checkInterval)))
log.Println(" - sleep interval", sleepInterval) log.Println(" - sleep interval", sleepInterval, "until next check")
time.Sleep(sleepInterval) 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) git, err := common.CreateGitHandler(GitAuthor, GitEmail, AppName)
if err != nil { if err != nil {
log.Println("Faield to allocate GitHandler:", err) log.Println("Faield to allocate GitHandler:", err)
@ -391,11 +332,26 @@ func consistencyCheckProcess() {
if err := verifyProjectState(git, org, config, configs); err != nil { if err := verifyProjectState(git, org, config, configs); err != nil {
log.Printf(" *** verification failed, org: `%s`, err: %#v\n", org, err) 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 var debugMode bool
func main() { func main() {