2024-11-08 15:05:09 +01:00
|
|
|
package main
|
|
|
|
|
2024-12-02 10:26:51 +01:00
|
|
|
//go:generate mockgen -source=pr_processor.go -destination=mock/pr_processor.go -typed
|
2024-11-08 15:05:09 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|