From 463e3e198bf187ba434e00dc4713ad33e312e084da33b0e398d15a1be541e9f4 Mon Sep 17 00:00:00 2001 From: Adam Majer Date: Fri, 8 Nov 2024 15:05:09 +0100 Subject: [PATCH] refactor --- workflow-pr/main.go | 61 ---------------------------------------- workflow-pr/pr.go | 64 ++++++++++++++++++++++++++++++++++++++++++ workflow-pr/pr_sync.go | 6 ++++ 3 files changed, 70 insertions(+), 61 deletions(-) create mode 100644 workflow-pr/pr.go diff --git a/workflow-pr/main.go b/workflow-pr/main.go index 55eb1b6..5ca90c4 100644 --- a/workflow-pr/main.go +++ b/workflow-pr/main.go @@ -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 diff --git a/workflow-pr/pr.go b/workflow-pr/pr.go new file mode 100644 index 0000000..a211a13 --- /dev/null +++ b/workflow-pr/pr.go @@ -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) +} + + diff --git a/workflow-pr/pr_sync.go b/workflow-pr/pr_sync.go index 6da0458..64b9eab 100644 --- a/workflow-pr/pr_sync.go +++ b/workflow-pr/pr_sync.go @@ -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 { }