From a552f751f0c36abe77b8885f0ce723632b51492247ce1f211b23f75e1f7aa390 Mon Sep 17 00:00:00 2001 From: Adam Majer Date: Thu, 12 Sep 2024 16:40:43 +0200 Subject: [PATCH] refactor --- bots-common/listen.go | 13 +++++++------ bots-common/request_handler.go | 4 ---- prjgit-updater/main.go | 30 +++++++++++++++--------------- 3 files changed, 22 insertions(+), 25 deletions(-) diff --git a/bots-common/listen.go b/bots-common/listen.go index 1db2537..58afe74 100644 --- a/bots-common/listen.go +++ b/bots-common/listen.go @@ -53,7 +53,7 @@ const RequestType_PRReviewRequest = "pull_request_review_request" const RequestType_PRReviewComment = "pull_request_review_comment" const RequestType_Wiki = "wiki" -type RequestProcessor func(*RequestHandler) error +type RequestProcessor func(*Request) error type ListenDefinitions struct { RabbitURL string // amqps://user:password@host/queue @@ -172,14 +172,14 @@ func connectToRabbitMQ(log *log.Logger, server url.URL, topics []string) chan Ra return ch } -func ProcessEvent(f RequestProcessor, h *RequestHandler) { +func ProcessEvent(f RequestProcessor, request *Request) { defer func() { if r := recover(); r != nil { log.Println(r) } }() - if err := f(h); err != nil { + if err := f(request); err != nil { log.Println(err) } @@ -233,19 +233,20 @@ func ProcessRabbitMQEvents(listenDefs ListenDefinitions, orgs []string) error { log.Println("org:", org, "type:", reqType) if handler, found := listenDefs.Handlers[reqType]; found { - h, err := CreateRequestHandler() +/* h, err := CreateRequestHandler() if err != nil { log.Println("Cannot create request handler", err) continue } + */ req, err := ParseRequestJSON(reqType, msg.Body) if err != nil { log.Println("Error parsing request JSON:", err) continue } else { log.Println("processing req", req.Type) - h.Request = req - ProcessEvent(handler, h) +// h.Request = req + ProcessEvent(handler, req) } } diff --git a/bots-common/request_handler.go b/bots-common/request_handler.go index 6736b81..7b78a69 100644 --- a/bots-common/request_handler.go +++ b/bots-common/request_handler.go @@ -87,12 +87,8 @@ func ParseRequestJSON(reqType string, data []byte) (req *Request, err error) { } type RequestHandler struct { - Branch string - PrjGit string - StdLogger, ErrLogger *log.Logger Request *Request - // Git *GitHandler } func (r *RequestHandler) WriteError() { diff --git a/prjgit-updater/main.go b/prjgit-updater/main.go index 4ecc7f2..114d29a 100644 --- a/prjgit-updater/main.go +++ b/prjgit-updater/main.go @@ -56,31 +56,31 @@ func concatenateErrors(err1, err2 error) error { return fmt.Errorf("%w\n%w", err1, err2) } -func processRepositoryAction(h *common.RequestHandler) error { - action := h.Request.Data.(*common.RepositoryWebhookEvent) +func processRepositoryAction(request *common.Request) error { + action := request.Data.(*common.RepositoryWebhookEvent) configs, configFound := configuredRepos[action.Organization.Username] if !configFound { - h.StdLogger.Printf("Repository event for %s. Not configured. Ignoring.\n", action.Organization.Username) + log.Printf("Repository event for %s. Not configured. Ignoring.\n", action.Organization.Username) return nil } for _, config := range configs { if config.GitProjectName == action.Repository.Name { - h.StdLogger.Println("+ ignoring repo event for PrjGit repository", config.GitProjectName) + log.Println("+ ignoring repo event for PrjGit repository", config.GitProjectName) return nil } } var err error for _, config := range configs { - err = concatenateErrors(err, processConfiguredRepositoryAction(h, action, config)) + err = concatenateErrors(err, processConfiguredRepositoryAction(action, config)) } return err } -func processConfiguredRepositoryAction(h *common.RequestHandler, action *common.RepositoryWebhookEvent, config *common.AutogitConfig) error { +func processConfiguredRepositoryAction(action *common.RepositoryWebhookEvent, config *common.AutogitConfig) error { prjgit := config.GitProjectName git, err := common.CreateGitHandler(GitAuthor, GitEmail, AppName) common.PanicOnError(err) @@ -114,7 +114,7 @@ func processConfiguredRepositoryAction(h *common.RequestHandler, action *common. case "deleted": if stat, err := os.Stat(filepath.Join(git.GitPath, prjgit, action.Repository.Name)); err != nil || !stat.IsDir() { if git.DebugLogger { - h.StdLogger.Printf("delete event for %s -- not in project. Ignoring\n", action.Repository.Name) + log.Println("delete event for", action.Repository.Name, "-- not in project. Ignoring") } return nil } @@ -129,31 +129,31 @@ func processConfiguredRepositoryAction(h *common.RequestHandler, action *common. return nil } -func processPushAction(h *common.RequestHandler) error { - action := h.Request.Data.(*common.PushWebhookEvent) +func processPushAction(request *common.Request) error { + action := request.Data.(*common.PushWebhookEvent) configs, configFound := configuredRepos[action.Repository.Owner.Username] if !configFound { - h.StdLogger.Printf("Repository event for %s. Not configured. Ignoring.\n", action.Repository.Owner.Username) + log.Printf("Repository event for %s. Not configured. Ignoring.\n", action.Repository.Owner.Username) return nil } for _, config := range configs { if config.GitProjectName == action.Repository.Name { - h.StdLogger.Println("+ ignoring push to PrjGit repository", config.GitProjectName) + log.Println("+ ignoring push to PrjGit repository", config.GitProjectName) return nil } } var err error for _, config := range configs { - err = concatenateErrors(err, processConfiguredPushAction(h, action, config)) + err = concatenateErrors(err, processConfiguredPushAction(action, config)) } return err } -func processConfiguredPushAction(h *common.RequestHandler, action *common.PushWebhookEvent, config *common.AutogitConfig) error { +func processConfiguredPushAction(action *common.PushWebhookEvent, config *common.AutogitConfig) error { prjgit := config.GitProjectName git, err := common.CreateGitHandler(GitAuthor, GitEmail, AppName) common.PanicOnError(err) @@ -173,7 +173,7 @@ func processConfiguredPushAction(h *common.RequestHandler, action *common.PushWe common.PanicOnError(git.GitExec("", "clone", "--depth", "1", prjGitRepo.SSHURL, prjgit)) if stat, err := os.Stat(filepath.Join(git.GitPath, prjgit, action.Repository.Name)); err != nil || !stat.IsDir() { if git.DebugLogger { - h.StdLogger.Println("Pushed to package that is not part of the project. Ignoring:", err) + log.Println("Pushed to package that is not part of the project. Ignoring:", err) } return nil } @@ -193,7 +193,7 @@ func processConfiguredPushAction(h *common.RequestHandler, action *common.PushWe } } - h.StdLogger.Println("push of refs the configured branch", config.Branch, ". ignoring.") + log.Println("push of refs the configured branch", config.Branch, ". ignoring.") return nil }