autogits/workflow-pr/pr.go

66 lines
1.6 KiB
Go

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
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)
}