refactor
This commit is contained in:
parent
8bedcc5195
commit
463e3e198b
@ -31,8 +31,6 @@ import (
|
||||
"src.opensuse.org/autogits/common"
|
||||
)
|
||||
|
||||
//go:generate mockgen -source=main.go -destination=mock/main.go -typed
|
||||
|
||||
const (
|
||||
AppName = "workflow-pr"
|
||||
GitAuthor = "AutoGits - pr-review"
|
||||
@ -58,12 +56,6 @@ func fetchPrGit(h *common.RequestHandler, pr *models.PullRequest) error {
|
||||
return h.Error
|
||||
}*/
|
||||
|
||||
func processPrjGitPullRequestSync(req *common.PullRequestWebhookEvent) error {
|
||||
// req := h.Data.(*common.PullRequestAction)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func prGitBranchNameForPR(req *common.PullRequestWebhookEvent) string {
|
||||
return fmt.Sprintf("PR_%s#%d", req.Repository.Name, req.Pull_Request.Number)
|
||||
}
|
||||
@ -77,59 +69,6 @@ func updateOrCreatePRBranch(req *common.PullRequestWebhookEvent, git *common.Git
|
||||
return nil
|
||||
}
|
||||
|
||||
type PullRequestProcessor interface {
|
||||
Process(req *common.PullRequestWebhookEvent, git *common.GitHandler, config *common.AutogitConfig) error
|
||||
}
|
||||
|
||||
type RequestProcessor struct {
|
||||
Opened, Synced, Closed PullRequestProcessor
|
||||
}
|
||||
|
||||
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 := 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 := common.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)
|
||||
}
|
||||
|
||||
var DebugMode bool
|
||||
var checkOnStart bool
|
||||
var checkInterval time.Duration
|
||||
|
64
workflow-pr/pr.go
Normal file
64
workflow-pr/pr.go
Normal file
@ -0,0 +1,64 @@
|
||||
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
|
||||
}
|
||||
|
||||
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 := 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 := common.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)
|
||||
}
|
||||
|
||||
|
@ -14,6 +14,12 @@ func processPrjGitPullRequestSync(req *common.PullRequestWebhookEvent) error {
|
||||
}
|
||||
*/
|
||||
|
||||
func processPrjGitPullRequestSync(req *common.PullRequestWebhookEvent) error {
|
||||
// req := h.Data.(*common.PullRequestAction)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type PullRequestSynced struct {
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user