This commit is contained in:
Adam Majer 2024-09-12 16:40:43 +02:00
parent b7ec9a9ffb
commit a552f751f0
3 changed files with 22 additions and 25 deletions

View File

@ -53,7 +53,7 @@ const RequestType_PRReviewRequest = "pull_request_review_request"
const RequestType_PRReviewComment = "pull_request_review_comment" const RequestType_PRReviewComment = "pull_request_review_comment"
const RequestType_Wiki = "wiki" const RequestType_Wiki = "wiki"
type RequestProcessor func(*RequestHandler) error type RequestProcessor func(*Request) error
type ListenDefinitions struct { type ListenDefinitions struct {
RabbitURL string // amqps://user:password@host/queue 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 return ch
} }
func ProcessEvent(f RequestProcessor, h *RequestHandler) { func ProcessEvent(f RequestProcessor, request *Request) {
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
log.Println(r) log.Println(r)
} }
}() }()
if err := f(h); err != nil { if err := f(request); err != nil {
log.Println(err) log.Println(err)
} }
@ -233,19 +233,20 @@ func ProcessRabbitMQEvents(listenDefs ListenDefinitions, orgs []string) error {
log.Println("org:", org, "type:", reqType) log.Println("org:", org, "type:", reqType)
if handler, found := listenDefs.Handlers[reqType]; found { if handler, found := listenDefs.Handlers[reqType]; found {
h, err := CreateRequestHandler() /* h, err := CreateRequestHandler()
if err != nil { if err != nil {
log.Println("Cannot create request handler", err) log.Println("Cannot create request handler", err)
continue continue
} }
*/
req, err := ParseRequestJSON(reqType, msg.Body) req, err := ParseRequestJSON(reqType, msg.Body)
if err != nil { if err != nil {
log.Println("Error parsing request JSON:", err) log.Println("Error parsing request JSON:", err)
continue continue
} else { } else {
log.Println("processing req", req.Type) log.Println("processing req", req.Type)
h.Request = req // h.Request = req
ProcessEvent(handler, h) ProcessEvent(handler, req)
} }
} }

View File

@ -87,12 +87,8 @@ func ParseRequestJSON(reqType string, data []byte) (req *Request, err error) {
} }
type RequestHandler struct { type RequestHandler struct {
Branch string
PrjGit string
StdLogger, ErrLogger *log.Logger StdLogger, ErrLogger *log.Logger
Request *Request Request *Request
// Git *GitHandler
} }
func (r *RequestHandler) WriteError() { func (r *RequestHandler) WriteError() {

View File

@ -56,31 +56,31 @@ func concatenateErrors(err1, err2 error) error {
return fmt.Errorf("%w\n%w", err1, err2) return fmt.Errorf("%w\n%w", err1, err2)
} }
func processRepositoryAction(h *common.RequestHandler) error { func processRepositoryAction(request *common.Request) error {
action := h.Request.Data.(*common.RepositoryWebhookEvent) action := request.Data.(*common.RepositoryWebhookEvent)
configs, configFound := configuredRepos[action.Organization.Username] configs, configFound := configuredRepos[action.Organization.Username]
if !configFound { 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 return nil
} }
for _, config := range configs { for _, config := range configs {
if config.GitProjectName == action.Repository.Name { 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 return nil
} }
} }
var err error var err error
for _, config := range configs { for _, config := range configs {
err = concatenateErrors(err, processConfiguredRepositoryAction(h, action, config)) err = concatenateErrors(err, processConfiguredRepositoryAction(action, config))
} }
return err 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 prjgit := config.GitProjectName
git, err := common.CreateGitHandler(GitAuthor, GitEmail, AppName) git, err := common.CreateGitHandler(GitAuthor, GitEmail, AppName)
common.PanicOnError(err) common.PanicOnError(err)
@ -114,7 +114,7 @@ func processConfiguredRepositoryAction(h *common.RequestHandler, action *common.
case "deleted": case "deleted":
if stat, err := os.Stat(filepath.Join(git.GitPath, prjgit, action.Repository.Name)); err != nil || !stat.IsDir() { if stat, err := os.Stat(filepath.Join(git.GitPath, prjgit, action.Repository.Name)); err != nil || !stat.IsDir() {
if git.DebugLogger { 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 return nil
} }
@ -129,31 +129,31 @@ func processConfiguredRepositoryAction(h *common.RequestHandler, action *common.
return nil return nil
} }
func processPushAction(h *common.RequestHandler) error { func processPushAction(request *common.Request) error {
action := h.Request.Data.(*common.PushWebhookEvent) action := request.Data.(*common.PushWebhookEvent)
configs, configFound := configuredRepos[action.Repository.Owner.Username] configs, configFound := configuredRepos[action.Repository.Owner.Username]
if !configFound { 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 return nil
} }
for _, config := range configs { for _, config := range configs {
if config.GitProjectName == action.Repository.Name { 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 return nil
} }
} }
var err error var err error
for _, config := range configs { for _, config := range configs {
err = concatenateErrors(err, processConfiguredPushAction(h, action, config)) err = concatenateErrors(err, processConfiguredPushAction(action, config))
} }
return err 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 prjgit := config.GitProjectName
git, err := common.CreateGitHandler(GitAuthor, GitEmail, AppName) git, err := common.CreateGitHandler(GitAuthor, GitEmail, AppName)
common.PanicOnError(err) 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)) 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 stat, err := os.Stat(filepath.Join(git.GitPath, prjgit, action.Repository.Name)); err != nil || !stat.IsDir() {
if git.DebugLogger { 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 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 return nil
} }