autogits/workflow-pr/pr_processor.go

66 lines
1.6 KiB
Go
Raw Normal View History

2024-11-08 15:05:09 +01:00
package main
//go:generate mockgen -source=pr.go -destination=mock/pr.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
2024-11-08 16:08:53 +01:00
configuredRepos map[string][]*common.AutogitConfig
2024-11-11 15:52:34 +01:00
git common.GitHandlerGenerator
2024-11-08 15:05:09 +01:00
}
2024-11-08 16:08:53 +01:00
2024-11-08 15:05:09 +01:00
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.")
}
2024-11-08 16:08:53 +01:00
configs := w.configuredRepos[req.Repository.Owner.Username]
2024-11-08 15:05:09 +01:00
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)
}
2024-11-11 15:52:34 +01:00
git, err := w.git.CreateGitHandler(GitAuthor, GitEmail, AppName)
2024-11-08 15:05:09 +01:00
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)
}