refactor
This commit is contained in:
parent
463e3e198b
commit
0a7978569e
@ -37,9 +37,6 @@ const (
|
||||
GitEmail = "adam+autogits-pr@zombino.com"
|
||||
)
|
||||
|
||||
var configuredRepos map[string][]*common.AutogitConfig
|
||||
var gitea common.Gitea
|
||||
|
||||
/*
|
||||
func fetchPrGit(h *common.RequestHandler, pr *models.PullRequest) error {
|
||||
// clone PR head and base and return path
|
||||
@ -65,7 +62,7 @@ func updateOrCreatePRBranch(req *common.PullRequestWebhookEvent, git *common.Git
|
||||
common.PanicOnError(git.GitExec(path.Join(common.DefaultGitPrj, req.Repository.Name), "fetch", "--depth", "1", "origin", req.Pull_Request.Head.Sha))
|
||||
common.PanicOnError(git.GitExec(path.Join(common.DefaultGitPrj, req.Repository.Name), "checkout", req.Pull_Request.Head.Sha))
|
||||
common.PanicOnError(git.GitExec(common.DefaultGitPrj, "commit", "-a", "-m", commitMsg))
|
||||
common.PanicOnError(git.GitExec(common.DefaultGitPrj, "push", "origin", "+HEAD:" + branchName))
|
||||
common.PanicOnError(git.GitExec(common.DefaultGitPrj, "push", "origin", "+HEAD:"+branchName))
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -77,7 +74,7 @@ func verifyProjectState(processor *RequestProcessor, git *common.GitHandler, org
|
||||
org := common.Organization{
|
||||
Username: orgName,
|
||||
}
|
||||
repo, err := gitea.CreateRepositoryIfNotExist(git, org, config.GitProjectName)
|
||||
repo, err := processor.gitea.CreateRepositoryIfNotExist(git, org, config.GitProjectName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error fetching or creating '%s/%s' -- aborting verifyProjectState(). Err: %w", orgName, config.GitProjectName, err)
|
||||
}
|
||||
@ -95,7 +92,7 @@ nextSubmodule:
|
||||
}
|
||||
|
||||
// check if open PR have PR against project
|
||||
prs, err := gitea.GetRecentPullRequests(config.Organization, submoduleName)
|
||||
prs, err := processor.gitea.GetRecentPullRequests(config.Organization, submoduleName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error fetching pull requests for %s/%s. Err: %w", config.Organization, submoduleName, err)
|
||||
}
|
||||
@ -132,7 +129,7 @@ nextSubmodule:
|
||||
}
|
||||
|
||||
// check if the commited changes are syned with branches
|
||||
commits, err := gitea.GetRecentCommits(config.Organization, submoduleName, config.Branch, 10)
|
||||
commits, err := processor.gitea.GetRecentCommits(config.Organization, submoduleName, config.Branch, 10)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error fetching recent commits for %s/%s. Err: %w", config.Organization, submoduleName, err)
|
||||
}
|
||||
@ -172,7 +169,7 @@ nextSubmodule:
|
||||
}
|
||||
|
||||
func checkRepos(processor *RequestProcessor) {
|
||||
for org, configs := range configuredRepos {
|
||||
for org, configs := range processor.configuredRepos {
|
||||
for _, config := range configs {
|
||||
if checkInterval > 0 {
|
||||
sleepInterval := checkInterval - checkInterval/2 + time.Duration(rand.Int63n(int64(checkInterval)))
|
||||
@ -239,31 +236,37 @@ func main() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
configuredRepos = make(map[string][]*common.AutogitConfig)
|
||||
req := new(RequestProcessor)
|
||||
|
||||
req.configuredRepos = make(map[string][]*common.AutogitConfig)
|
||||
orgs := make([]string, 0, 1)
|
||||
for _, c := range configs {
|
||||
if slices.Contains(c.Workflows, "pr") {
|
||||
if DebugMode {
|
||||
log.Printf(" + adding org: '%s', branch: '%s', prjgit: '%s'\n", c.Organization, c.Branch, c.GitProjectName)
|
||||
}
|
||||
configs := configuredRepos[c.Organization]
|
||||
configs := req.configuredRepos[c.Organization]
|
||||
if configs == nil {
|
||||
configs = make([]*common.AutogitConfig, 0, 1)
|
||||
}
|
||||
configs = append(configs, c)
|
||||
configuredRepos[c.Organization] = configs
|
||||
req.configuredRepos[c.Organization] = configs
|
||||
|
||||
orgs = append(orgs, c.Organization)
|
||||
}
|
||||
}
|
||||
|
||||
gitea = common.AllocateGiteaTransport(*giteaHost)
|
||||
req.Synced = &PullRequestSynced{
|
||||
gitea: req.gitea,
|
||||
}
|
||||
req.Opened = &PullRequestOpened{
|
||||
gitea: req.gitea,
|
||||
}
|
||||
req.Closed = &PullRequestClosed{
|
||||
gitea: req.gitea,
|
||||
}
|
||||
|
||||
req := new(RequestProcessor)
|
||||
|
||||
req.Synced = &PullRequestSynced{}
|
||||
req.Opened = &PullRequestOpened{}
|
||||
req.Closed = &PullRequestClosed{}
|
||||
req.gitea = common.AllocateGiteaTransport(*giteaHost)
|
||||
|
||||
go consistencyCheckProcess(req)
|
||||
|
||||
|
@ -10,10 +10,7 @@ import (
|
||||
"testing"
|
||||
|
||||
// "go.uber.org/mock/gomock"
|
||||
"go.uber.org/mock/gomock"
|
||||
"src.opensuse.org/autogits/common"
|
||||
mock_common "src.opensuse.org/autogits/common/mock"
|
||||
mock_main "src.opensuse.org/workflow-pr/mock"
|
||||
// "src.opensuse.org/autogits/common/mock"
|
||||
)
|
||||
|
||||
@ -198,52 +195,3 @@ func TestCreatePrBranch(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func TestPRProcessor(t *testing.T) {
|
||||
ctl := gomock.NewController(t)
|
||||
defer ctl.Finish()
|
||||
|
||||
var logBuf bytes.Buffer
|
||||
oldOut := log.Writer()
|
||||
log.SetOutput(&logBuf)
|
||||
defer log.SetOutput(oldOut)
|
||||
|
||||
req := &RequestProcessor {
|
||||
Opened: mock_main.NewMockPullRequestProcessor(ctl),
|
||||
Closed: mock_main.NewMockPullRequestProcessor(ctl),
|
||||
Synced: mock_main.NewMockPullRequestProcessor(ctl),
|
||||
}
|
||||
|
||||
gitea = mock_common.NewMockGitea(ctl)
|
||||
pr_opened := mock_main.NewMockPullRequestProcessor(ctl)
|
||||
|
||||
pr_opened.EXPECT().Process(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
|
||||
|
||||
event := &common.PullRequestWebhookEvent {
|
||||
Action: "opened",
|
||||
Number: 1,
|
||||
Pull_Request: &common.PullRequest {
|
||||
Id: 1,
|
||||
Base: common.Head {
|
||||
Ref: "HEAD",
|
||||
Sha: "abc",
|
||||
Repo: nil,
|
||||
},
|
||||
Head: common.Head {
|
||||
Ref: "HEAD",
|
||||
Sha: "abc",
|
||||
Repo: nil,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
err := req.ProcessFunc(&common.Request{
|
||||
Data: event,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Error("Error processing open PR", err)
|
||||
t.Error(logBuf.String())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -14,6 +14,9 @@ type PullRequestProcessor interface {
|
||||
|
||||
type RequestProcessor struct {
|
||||
Opened, Synced, Closed PullRequestProcessor
|
||||
|
||||
configuredRepos map[string][]*common.AutogitConfig
|
||||
gitea common.Gitea
|
||||
}
|
||||
|
||||
func (w *RequestProcessor) ProcessFunc(request *common.Request) error {
|
||||
@ -22,7 +25,7 @@ func (w *RequestProcessor) ProcessFunc(request *common.Request) error {
|
||||
return fmt.Errorf("*** Invalid data format for PR processing.")
|
||||
}
|
||||
|
||||
configs := configuredRepos[req.Repository.Owner.Username]
|
||||
configs := w.configuredRepos[req.Repository.Owner.Username]
|
||||
if len(configs) < 1 {
|
||||
// ignoring pull request against unconfigured project (could be just regular sources?)
|
||||
return nil
|
||||
@ -60,5 +63,3 @@ func (w *RequestProcessor) ProcessFunc(request *common.Request) error {
|
||||
|
||||
return fmt.Errorf("Unhandled pull request action: %s", req.Action)
|
||||
}
|
||||
|
||||
|
||||
|
@ -6,7 +6,9 @@ import (
|
||||
"src.opensuse.org/autogits/common"
|
||||
)
|
||||
|
||||
type PullRequestClosed struct {}
|
||||
type PullRequestClosed struct {
|
||||
gitea common.Gitea
|
||||
}
|
||||
|
||||
func (*PullRequestClosed) Process(req *common.PullRequestWebhookEvent, git *common.GitHandler, config *common.AutogitConfig) error {
|
||||
if req.Repository.Name != config.GitProjectName {
|
||||
|
@ -7,9 +7,10 @@ import (
|
||||
)
|
||||
|
||||
type PullRequestOpened struct {
|
||||
gitea common.Gitea
|
||||
}
|
||||
|
||||
func (*PullRequestOpened) Process(req *common.PullRequestWebhookEvent, git *common.GitHandler, config *common.AutogitConfig) error {
|
||||
func (o *PullRequestOpened) Process(req *common.PullRequestWebhookEvent, git *common.GitHandler, config *common.AutogitConfig) error {
|
||||
// requests against project are not handled here
|
||||
if req.Repository.Name == config.GitProjectName {
|
||||
return nil
|
||||
@ -25,7 +26,7 @@ referencing
|
||||
PullRequest: %s/%s#%d`, req.Repository.Owner.Username,
|
||||
req.Repository.Name, GitAuthor, req.Repository.Name, req.Pull_Request.Number)
|
||||
|
||||
prjGit, err := gitea.CreateRepositoryIfNotExist(git, *req.Repository.Owner, config.GitProjectName)
|
||||
prjGit, err := o.gitea.CreateRepositoryIfNotExist(git, *req.Repository.Owner, config.GitProjectName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -34,7 +35,7 @@ PullRequest: %s/%s#%d`, req.Repository.Owner.Username,
|
||||
common.PanicOnError(git.GitExec(common.DefaultGitPrj, "checkout", "-B", branchName, prjGit.DefaultBranch))
|
||||
common.PanicOnError(updateOrCreatePRBranch(req, git, commitMsg, branchName))
|
||||
|
||||
PR, err := gitea.CreatePullRequestIfNotExist(prjGit, branchName, prjGit.DefaultBranch,
|
||||
PR, err := o.gitea.CreatePullRequestIfNotExist(prjGit, branchName, prjGit.DefaultBranch,
|
||||
fmt.Sprintf("Forwarded PR: %s", req.Repository.Name),
|
||||
fmt.Sprintf(`This is a forwarded pull request by %s
|
||||
referencing the following pull request:
|
||||
@ -49,7 +50,7 @@ referencing the following pull request:
|
||||
|
||||
// request build review
|
||||
for _, reviewer := range config.Reviewers {
|
||||
_, err := gitea.RequestReviews(PR, reviewer)
|
||||
_, err := o.gitea.RequestReviews(PR, reviewer)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to create reviewer '%s' for request: %s/%s/%d Err: %w", reviewer, PR.Base.Repo.Owner.UserName, PR.Base.Repo.Name, PR.Index, err)
|
||||
}
|
||||
|
@ -6,13 +6,6 @@ import (
|
||||
|
||||
"src.opensuse.org/autogits/common"
|
||||
)
|
||||
/*
|
||||
func processPrjGitPullRequestSync(req *common.PullRequestWebhookEvent) error {
|
||||
// req := h.Data.(*common.PullRequestAction)
|
||||
|
||||
return nil
|
||||
}
|
||||
*/
|
||||
|
||||
func processPrjGitPullRequestSync(req *common.PullRequestWebhookEvent) error {
|
||||
// req := h.Data.(*common.PullRequestAction)
|
||||
@ -21,16 +14,17 @@ func processPrjGitPullRequestSync(req *common.PullRequestWebhookEvent) error {
|
||||
}
|
||||
|
||||
type PullRequestSynced struct {
|
||||
gitea common.Gitea
|
||||
}
|
||||
|
||||
func (*PullRequestSynced) Process(req *common.PullRequestWebhookEvent, git *common.GitHandler, config *common.AutogitConfig) error {
|
||||
func (o *PullRequestSynced) Process(req *common.PullRequestWebhookEvent, git *common.GitHandler, config *common.AutogitConfig) error {
|
||||
if req.Repository.Name == config.GitProjectName {
|
||||
return processPrjGitPullRequestSync(req)
|
||||
}
|
||||
|
||||
// need to verify that submodule in the PR for prjgit
|
||||
// is still pointing to the HEAD of the PR
|
||||
prjPr, err := gitea.GetAssociatedPrjGitPR(req)
|
||||
prjPr, err := o.gitea.GetAssociatedPrjGitPR(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Cannot get associated PrjGit PR in processPullRequestSync. Err: %w", err)
|
||||
}
|
||||
|
84
workflow-pr/pr_test.go
Normal file
84
workflow-pr/pr_test.go
Normal file
@ -0,0 +1,84 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"log"
|
||||
"testing"
|
||||
|
||||
// "go.uber.org/mock/gomock"
|
||||
"go.uber.org/mock/gomock"
|
||||
"src.opensuse.org/autogits/common"
|
||||
mock_common "src.opensuse.org/autogits/common/mock"
|
||||
mock_main "src.opensuse.org/workflow-pr/mock"
|
||||
// "src.opensuse.org/autogits/common/mock"
|
||||
)
|
||||
|
||||
|
||||
func TestPRProcessor(t *testing.T) {
|
||||
ctl := gomock.NewController(t)
|
||||
defer ctl.Finish()
|
||||
|
||||
var logBuf bytes.Buffer
|
||||
oldOut := log.Writer()
|
||||
log.SetOutput(&logBuf)
|
||||
defer log.SetOutput(oldOut)
|
||||
|
||||
testConfiguration := make(map[string][]*common.AutogitConfig)
|
||||
|
||||
testConfiguration["test"] = make([]*common.AutogitConfig, 1, 1)
|
||||
testConfiguration["test"][0] = &common.AutogitConfig {
|
||||
|
||||
}
|
||||
|
||||
req := &RequestProcessor {
|
||||
Opened: mock_main.NewMockPullRequestProcessor(ctl),
|
||||
Closed: mock_main.NewMockPullRequestProcessor(ctl),
|
||||
Synced: mock_main.NewMockPullRequestProcessor(ctl),
|
||||
|
||||
gitea: mock_common.NewMockGitea(ctl),
|
||||
configuredRepos: testConfiguration,
|
||||
}
|
||||
|
||||
// gitea := mock_common.NewMockGitea(ctl)
|
||||
pr_opened := mock_main.NewMockPullRequestProcessor(ctl)
|
||||
|
||||
pr_opened.EXPECT().Process(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
|
||||
|
||||
event := &common.PullRequestWebhookEvent {
|
||||
Action: "opened",
|
||||
Number: 1,
|
||||
Pull_Request: &common.PullRequest {
|
||||
Id: 1,
|
||||
Base: common.Head {
|
||||
Ref: "HEAD",
|
||||
Sha: "abc",
|
||||
Repo: &common.Repository {
|
||||
Name: "testRepo",
|
||||
},
|
||||
},
|
||||
Head: common.Head {
|
||||
Ref: "HEAD",
|
||||
Sha: "abc",
|
||||
Repo: &common.Repository {
|
||||
Name: "testRepo",
|
||||
},
|
||||
},
|
||||
},
|
||||
Repository: &common.Repository {
|
||||
Owner: &common.Organization {
|
||||
Username: "test",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
err := req.ProcessFunc(&common.Request{
|
||||
Data: event,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Error("Error processing open PR", err)
|
||||
t.Error(logBuf.String())
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user