autogits/workflow-pr/pr_processor.go
2024-12-02 10:26:51 +01:00

66 lines
1.7 KiB
Go

package main
//go:generate mockgen -source=pr_processor.go -destination=mock/pr_processor.go -typed
import (
"fmt"
"src.opensuse.org/autogits/common"
)
type PullRequestProcessor interface {
Process(req *common.PullRequestWebhookEvent, git *common.GitHandler, config *common.AutogitConfig) error
}
type RequestProcessor struct {
Opened, Synced, Closed PullRequestProcessor
configuredRepos map[string][]*common.AutogitConfig
git common.GitHandlerGenerator
}
func (w *RequestProcessor) ProcessFunc(request *common.Request) error {
req, ok := request.Data.(*common.PullRequestWebhookEvent)
if !ok {
return fmt.Errorf("*** Invalid data format for PR processing.")
}
configs := w.configuredRepos[req.Repository.Owner.Username]
if len(configs) < 1 {
// ignoring pull request against unconfigured project (could be just regular sources?)
return nil
}
var config *common.AutogitConfig
for _, c := range configs {
if c.GitProjectName == req.Pull_Request.Base.Repo.Name ||
c.Branch == req.Pull_Request.Base.Ref {
config = c
break
}
}
if config == nil {
return fmt.Errorf("Cannot find config for branch '%s'", req.Pull_Request.Base.Ref)
}
git, err := w.git.CreateGitHandler(GitAuthor, GitEmail, AppName)
if err != nil {
return fmt.Errorf("Error allocating GitHandler. Err: %w", err)
}
switch req.Action {
case "opened", "reopened":
return w.Opened.Process(req, git, config)
case "synchronized":
return w.Synced.Process(req, git, config)
case "edited":
// not need to be handled??
return nil
case "closed":
return w.Closed.Process(req, git, config)
}
return fmt.Errorf("Unhandled pull request action: %s", req.Action)
}