Files
autogits/workflow-pr/pr_processor.go
2025-05-06 18:06:04 +02:00

80 lines
2.0 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.Git, config *common.AutogitConfig) error
}
type RequestProcessor struct {
Opened, Synced, Closed, Review PullRequestProcessor
configuredRepos map[string][]*common.AutogitConfig
}
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
}
org := req.Pull_Request.Base.Repo.Owner.Username
pkg := req.Pull_Request.Base.Repo.Name
branch := req.Pull_Request.Base.Ref
assumed_git_project_name := org + "/" + pkg + "#" + branch
var config *common.AutogitConfig
for _, c := range configs {
if c.GitProjectName == assumed_git_project_name {
config = c
break
}
if c.Organization == org {
// default branch *or* match branch
if (c.Branch == "" && branch == req.Pull_Request.Base.Repo.Default_Branch) ||
(c.Branch != "" && c.Branch == branch) {
config = c
break
}
}
}
if config == nil {
return fmt.Errorf("Cannot find config for branch '%s'", req.Pull_Request.Base.Ref)
}
git, err := GitHandler.CreateGitHandler(config.Organization)
if err != nil {
return fmt.Errorf("Error allocating GitHandler. Err: %w", err)
}
defer git.Close()
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)
case "reviewed":
return w.Review.Process(req, git, config)
}
return fmt.Errorf("Unhandled pull request action: %s", req.Action)
}