diff --git a/bots-common/gitea_utils.go b/bots-common/gitea_utils.go index 8f9134e..0945ccc 100644 --- a/bots-common/gitea_utils.go +++ b/bots-common/gitea_utils.go @@ -85,6 +85,7 @@ type Gitea interface { GiteaReviewRequester GiteaReviewer GiteaPRFetcher + GiteaReviewFetcher GetPullNotifications(since *time.Time) ([]*models.NotificationThread, error) SetNotificationRead(notificationId int64) error @@ -137,10 +138,9 @@ func (gitea *GiteaTransport) GetPullRequest(org, project string, num int64) (*mo return pr.Payload, err } -func (gitea *GiteaTransport) GetPullReviews(org, project string, num int64) ([]*models.PullReview, error) { +func (gitea *GiteaTransport) GetPullRequestReviews(org, project string, PRnum int64) ([]*models.PullReview, error) { limit := int64(20) var page int64 - var allReviews []*models.PullReview for { @@ -149,7 +149,7 @@ func (gitea *GiteaTransport) GetPullReviews(org, project string, num int64) ([]* WithDefaults(). WithOwner(org). WithRepo(project). - WithIndex(num). + WithIndex(PRnum). WithPage(&page). WithLimit(&limit), gitea.transport.DefaultAuthentication, diff --git a/workflow-pr/pr.go b/workflow-pr/pr.go index 2a85b05..2cf7228 100644 --- a/workflow-pr/pr.go +++ b/workflow-pr/pr.go @@ -111,3 +111,31 @@ func (rs *PRSet) IsReviewed(gitea common.GiteaReviewFetcher) bool { } return is_reviewed } + +func (rs *PRSet) Merge() error { + prjgit, err := rs.GetPrjGitPR() + if err != nil { + return err + } + + gh:=common.GitHandlerGeneratorImpl{} + git, err := gh.CreateGitHandler(GitAuthor, GitEmail, prjgit.Base.Name) + if err != nil { + return err + } + git.GitExecOrPanic("", "clone", "--depth", "1", prjgit.Base.Repo.SSHURL, common.DefaultGitPrj) + git.GitExecOrPanic(common.DefaultGitPrj, "fetch", common.DefaultGitPrj, "origin", prjgit.Base.Sha, prjgit.Head.Sha) + + // if other changes merged, check if we have conflicts + rev := git.GitExecWithOutputOrPanic(common.DefaultGitPrj, "rev-list", "-1", "HEAD") + if rev != prjgit.Base.Sha { + panic("FIXME") + } + + msg := "haha" + + git.GitExecOrPanic(common.DefaultGitPrj, "merge", "--no-ff", "-m", msg, prjgit.Head.Sha) + git.GitExecOrPanic(common.DefaultGitPrj, "push", "origin") + return nil +} + diff --git a/workflow-pr/pr_processor_reviewed.go b/workflow-pr/pr_processor_reviewed.go index 47f6675..6872df2 100644 --- a/workflow-pr/pr_processor_reviewed.go +++ b/workflow-pr/pr_processor_reviewed.go @@ -3,9 +3,18 @@ package main import "src.opensuse.org/autogits/common" type PullRequestReviewed struct { + gitea common.Gitea } func (o *PullRequestReviewed) Process(req *common.PullRequestWebhookEvent, git common.Git, config *common.AutogitConfig) error { + prset, err := FetchPRSet(o.gitea, req.Repository.Owner.Username, req.Repository.Name, req.Number, config) + if err != nil { + return err + } + + if prset.IsReviewed(o.gitea) { + prset.Merge() + } return nil } diff --git a/workflow-pr/pr_processor_reviewed_test.go b/workflow-pr/pr_processor_reviewed_test.go index 0c1bf60..21221d9 100644 --- a/workflow-pr/pr_processor_reviewed_test.go +++ b/workflow-pr/pr_processor_reviewed_test.go @@ -3,7 +3,9 @@ package main import ( "testing" + "go.uber.org/mock/gomock" "src.opensuse.org/autogits/common" + mock_common "src.opensuse.org/autogits/common/mock" ) func TestPRReviewed(t *testing.T) { @@ -39,6 +41,7 @@ func TestPRReviewed(t *testing.T) { }, }, Repository: &common.Repository{ + Name: "testRepo", Owner: &common.Organization{ Username: "test", }, @@ -47,7 +50,15 @@ func TestPRReviewed(t *testing.T) { for _, test := range testData { t.Run(test.title, func(t *testing.T) { - s := PullRequestReviewed{} + ctl := gomock.NewController(t) + mock := mock_common.NewMockGitea(ctl) + + s := PullRequestReviewed{ + gitea: mock, + } + + mock.EXPECT().GetPullRequest("test", "testRepo", int64(1)).Return(nil, nil) + if err := s.Process(event, nil, nil); err != test.error { t.Error("unexected error:", err, "Expected:", test.error) } diff --git a/workflow-pr/pr_test.go b/workflow-pr/pr_test.go index 4e0043d..1dbfa18 100644 --- a/workflow-pr/pr_test.go +++ b/workflow-pr/pr_test.go @@ -14,13 +14,6 @@ import ( ) func TestPR(t *testing.T) { - cwd, _ := os.Getwd() - cmd := exec.Command("/usr/bin/bash", path.Join(cwd, "test_repo_setup.sh")) - cmd.Dir = t.TempDir() - if out, err := cmd.CombinedOutput(); err != nil { - t.Fatal(string(out)) - } - baseConfig := common.AutogitConfig{ Reviewers: []string{"super1", "super2"}, Branch: "branch", @@ -116,7 +109,7 @@ func TestPR(t *testing.T) { resLen: 1, prjGitPRIndex: 0, consistentSet: true, - reviewed: true, + reviewed: true, reviewSetFetcher: func(mock *mock_common.MockGiteaPRFetcher) (*PRSet, error) { return FetchPRSet(mock, "foo", "barPrj", 42, &baseConfig) }, @@ -225,3 +218,29 @@ func TestPR(t *testing.T) { } }) } + +func TestPRMerge(t *testing.T) { + cwd, _ := os.Getwd() + cmd := exec.Command("/usr/bin/bash", path.Join(cwd, "test_repo_setup.sh")) + cmd.Dir = t.TempDir() + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatal(string(out)) + } + + tests := []struct { + name string + prjgit_base, prjgit_head string + }{ + { + name: "Merge conflicts in submodules", + prjgit_base: "base_add_b1", + prjgit_head: "base_add_b2", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + + }) + } +} diff --git a/workflow-pr/test_repo_setup.sh b/workflow-pr/test_repo_setup.sh index 45ada9c..d41ee27 100755 --- a/workflow-pr/test_repo_setup.sh +++ b/workflow-pr/test_repo_setup.sh @@ -26,6 +26,8 @@ create_prjgit_sample() { git submodule init git submodule -q add ../pkgA pkgA + git submodule -q add ../pkgB pkgB + git submodule -q add ../pkgC pkgC git commit -q -m 'first commit' @@ -45,7 +47,62 @@ create_pkgA() { popd } +create_pkgB() { + mkdir pkgB + pushd pkgB + + git init -q --object-format=sha256 + echo "Package B" > README.md + git add README.md + + git commit -q -m 'Something also base here' + + popd +} + +create_pkgB1() { + mkdir pkgB1 + pushd pkgB1 + + git init -q --object-format=sha256 + echo "Package B1" > README.md + git add README.md + + git commit -q -m 'Something also base here' + + popd +} + +create_pkgB2() { + mkdir pkgB2 + pushd pkgB2 + + git init -q --object-format=sha256 + echo "Package B2" > README.md + git add README.md + + git commit -q -m 'Something also base here' + + popd +} + +create_pkgC() { + mkdir pkgC + pushd pkgC + + git init -q --object-format=sha256 + echo "Package C" > README.md + git add README.md + + git commit -q -m 'Something another base here' + + popd +} + create_pkgA +create_pkgB +create_pkgB1 +create_pkgB2 +create_pkgC create_prjgit_sample -