From 50aab4c662fe2ae3907444a2a75c7a6ea256df4d19a6d262c55d4c67eff4a10b Mon Sep 17 00:00:00 2001 From: Adam Majer Date: Mon, 16 Dec 2024 08:15:49 +0100 Subject: [PATCH] wip --- bots-common/gitea_utils.go | 42 ---------------------- bots-common/request_pr.go | 6 ++-- workflow-pr/pr_processor_sync.go | 18 ++++++++-- workflow-pr/pr_processor_sync_test.go | 49 +++++++++++++++++++++---- workflow-pr/repo_check.go | 2 +- workflow-pr/review.go | 3 ++ workflow-pr/review_test.go | 19 ++++++++++ workflow-pr/test_repo_setup.sh | 51 +++++++++++++++++++++++++++ 8 files changed, 136 insertions(+), 54 deletions(-) create mode 100644 workflow-pr/review.go create mode 100644 workflow-pr/review_test.go create mode 100755 workflow-pr/test_repo_setup.sh diff --git a/bots-common/gitea_utils.go b/bots-common/gitea_utils.go index 507f857..c3f1902 100644 --- a/bots-common/gitea_utils.go +++ b/bots-common/gitea_utils.go @@ -74,7 +74,6 @@ type Gitea interface { CreatePullRequestIfNotExist(repo *models.Repository, srcId, targetId, title, body string) (*models.PullRequest, error) RequestReviews(pr *models.PullRequest, reviewer string) ([]*models.PullReview, error) AddReviewComment(pr *models.PullRequest, state models.ReviewStateType, comment string) (*models.PullReview, error) - GetAssociatedPrjGitPR(pr *PullRequestWebhookEvent) (*models.PullRequest, error) GetRepositoryFileContent(org, repo, hash, path string) ([]byte, error) GetPullRequestFileContent(pr *models.PullRequest, path string) ([]byte, error) GetRecentPullRequests(org, repo string) ([]*models.PullRequest, error) @@ -375,47 +374,6 @@ func (gitea *GiteaTransport) AddReviewComment(pr *models.PullRequest, state mode return c.Payload, nil } -func (gitea *GiteaTransport) GetAssociatedPrjGitPR(pr *PullRequestWebhookEvent) (*models.PullRequest, error) { - var page int64 - state := "open" - for { - page++ - prs, err := gitea.client.Repository.RepoListPullRequests( - repository. - NewRepoListPullRequestsParams(). - WithDefaults(). - WithOwner(pr.Repository.Owner.Username). - WithRepo(DefaultGitPrj). - WithState(&state). - WithPage(&page), - gitea.transport.DefaultAuthentication) - - if err != nil { - return nil, fmt.Errorf("cannot fetch PR list for %s / %s : %w", pr.Repository.Owner.Username, pr.Repository.Name, err) - } - - prLine := fmt.Sprintf(PrPattern, pr.Repository.Owner.Username, pr.Repository.Name, pr.Number) - // h.StdLogger.Printf("attemping to match line: '%s'\n", prLine) - - // payload_processing: - for _, pr := range prs.Payload { - lines := strings.Split(pr.Body, "\n") - - for _, line := range lines { - if strings.TrimSpace(line) == prLine { - return pr, nil - } - } - } - - if len(prs.Payload) < 10 { - break - } - } - - return nil, nil -} - func (gitea *GiteaTransport) GetAssociatedPRs(org, repo string, prNo int64) ([]*models.PullRequest, error) { prData, err := gitea.client.Repository.RepoGetPullRequest( repository.NewRepoGetPullRequestParams(). diff --git a/bots-common/request_pr.go b/bots-common/request_pr.go index 884d1f0..caca6f4 100644 --- a/bots-common/request_pr.go +++ b/bots-common/request_pr.go @@ -45,7 +45,7 @@ type IssueLabelDetail struct { type PullRequest struct { Id int Url string - Number int + Number int64 State string Base Head @@ -58,7 +58,7 @@ type PullRequest struct { type PullRequestWebhookEvent struct { Action string - Number int + Number int64 Pull_Request *PullRequest Repository *Repository @@ -89,7 +89,7 @@ func PullRequestFromModel(pr *models.PullRequest) *PullRequest { return &PullRequest{ Id: int(pr.ID), Url: pr.URL, - Number: int(pr.Index), + Number: pr.Index, State: string(pr.State), Base: Head{ diff --git a/workflow-pr/pr_processor_sync.go b/workflow-pr/pr_processor_sync.go index 97ad2b8..56d258f 100644 --- a/workflow-pr/pr_processor_sync.go +++ b/workflow-pr/pr_processor_sync.go @@ -1,9 +1,11 @@ package main import ( + "bufio" "fmt" "log" "path" + "strings" "src.opensuse.org/autogits/common" ) @@ -37,9 +39,21 @@ func (o *PullRequestSynced) Process(req *common.PullRequestWebhookEvent, git com // need to verify that submodule in the PR for prjgit // is still pointing to the HEAD of the PR - prjPr, err := o.gitea.GetAssociatedPrjGitPR(req) + pr, _, err := o.gitea.GetPullRequestAndReviews(req.Repository.Owner.Username, req.Repository.Name, req.Number) + log.Print(pr) if err != nil { - return fmt.Errorf("Cannot get associated PrjGit PR in processPullRequestSync. Err: %w", err) + return fmt.Errorf("Cannot fetch PR data from gitea: %w", err) + } + + _, prs := common.ExtractDescriptionAndPRs(bufio.NewScanner(strings.NewReader(pr.Body))) + if len(prs) != 1 { + return fmt.Errorf("Package update associated with invalid number of projects. Expected 1. Got %d", len(prs)) + } + + prjPr, _, err := o.gitea.GetPullRequestAndReviews(prs[0].Org, prs[0].Repo, prs[0].Num) + + if err != nil { + return fmt.Errorf("Cannot get PrjGit PR in processPullRequestSync. Err: %w", err) } common.PanicOnError(git.GitExec("", "clone", "--branch", prjPr.Head.Name, "--depth", "1", prjPr.Head.Repo.SSHURL, common.DefaultGitPrj)) diff --git a/workflow-pr/pr_processor_sync_test.go b/workflow-pr/pr_processor_sync_test.go index 75522c0..64af618 100644 --- a/workflow-pr/pr_processor_sync_test.go +++ b/workflow-pr/pr_processor_sync_test.go @@ -3,7 +3,6 @@ package main import ( "bytes" "errors" - "log" "os" "path" "strings" @@ -34,7 +33,10 @@ func TestSyncPR(t *testing.T) { Ref: "branch", Sha: "8a6a69a4232cabda04a4d9563030aa888ff5482f75aa4c6519da32a951a072e2", Repo: &common.Repository{ - Name: "testRepo", + Name: "testRepo", + Owner: &common.Organization{ + Username: config.Organization, + }, Default_Branch: "main1", }, }, @@ -49,13 +51,42 @@ func TestSyncPR(t *testing.T) { }, Repository: &common.Repository{ Owner: &common.Organization{ - Username: "test", + Username: config.Organization, + }, + }, + } + + modelPR := &models.PullRequest{ + Index: 42, + Body: "PR: test/prj#24", + Base: &models.PRBranchInfo{ + Ref: "branch", + Sha: "8a6a69a4232cabda04a4d9563030aa888ff5482f75aa4c6519da32a951a072e2", + Repo: &models.Repository{ + Name: "testRepo", + Owner: &models.User{ + UserName: "test", + }, + DefaultBranch: "main1", + }, + }, + Head: &models.PRBranchInfo{ + Ref: "branch", + Sha: "11eb36d5a58d7bb376cac59ac729a1986c6a7bfc63e7818e14382f545ccda985", + Repo: &models.Repository{ + Name: "testRepo", + Owner: &models.User { + UserName: "test", + }, + DefaultBranch: "main1", }, }, } PrjGitPR := &models.PullRequest{ Title: "some pull request", + Body: "PR: test/testRepo#42", + Index: 24, Head: &models.PRBranchInfo{ Name: "testing", Sha: "db8adab91edb476b9762097d10c6379aa71efd6b60933a1c0e355ddacf419a95", @@ -92,11 +123,13 @@ func TestSyncPR(t *testing.T) { git.GitPath = t.TempDir() config.GitProjectName = "prjGit" - event.Repository.Name = "tester" + event.Repository.Name = "testRepo" setupGitForTests(t, git) - mock.EXPECT().GetAssociatedPrjGitPR(event).Return(PrjGitPR, nil) + // mock.EXPECT().GetAssociatedPrjGitPR(event).Return(PrjGitPR, nil) + mock.EXPECT().GetPullRequestAndReviews(config.Organization, "testRepo", event.Pull_Request.Number).Return(modelPR, nil, nil) + mock.EXPECT().GetPullRequestAndReviews(config.Organization, "prj", int64(24)).Return(PrjGitPR, nil, nil) err := pr.Process(event, git, config) @@ -129,9 +162,11 @@ func TestSyncPR(t *testing.T) { t.Run("PR sync", func(t *testing.T) { var b bytes.Buffer + /* w := log.Writer() log.SetOutput(&b) defer log.SetOutput(w) +*/ ctl := gomock.NewController(t) mock := mock_common.NewMockGitea(ctl) @@ -146,7 +181,9 @@ func TestSyncPR(t *testing.T) { git.DebugLogger = true DebugMode = true - mock.EXPECT().GetAssociatedPrjGitPR(event).Return(PrjGitPR, nil) + // mock.EXPECT().GetAssociatedPrjGitPR(event).Return(PrjGitPR, nil) + mock.EXPECT().GetPullRequestAndReviews(config.Organization, "testRepo", event.Pull_Request.Number).Return(modelPR, nil, nil).AnyTimes() + mock.EXPECT().GetPullRequestAndReviews(config.Organization, "prj", int64(24)).Return(PrjGitPR, nil, nil) err := pr.Process(event, git, config) diff --git a/workflow-pr/repo_check.go b/workflow-pr/repo_check.go index 46a2aaf..a3462f4 100644 --- a/workflow-pr/repo_check.go +++ b/workflow-pr/repo_check.go @@ -86,7 +86,7 @@ nextSubmodule: event.Pull_Request = common.PullRequestFromModel(pr) event.Action = string(pr.State) - event.Number = int(pr.Index) + event.Number = pr.Index event.Repository = common.RepositoryFromModel(pr.Base.Repo) event.Sender = *common.UserFromModel(pr.User) event.Requested_reviewer = nil diff --git a/workflow-pr/review.go b/workflow-pr/review.go new file mode 100644 index 0000000..d3ca313 --- /dev/null +++ b/workflow-pr/review.go @@ -0,0 +1,3 @@ +package main + + diff --git a/workflow-pr/review_test.go b/workflow-pr/review_test.go new file mode 100644 index 0000000..7b3eb2d --- /dev/null +++ b/workflow-pr/review_test.go @@ -0,0 +1,19 @@ +package main + +import ( + "os" + "os/exec" + "path" + "testing" +) + +func TestReview(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)) + } + +} + diff --git a/workflow-pr/test_repo_setup.sh b/workflow-pr/test_repo_setup.sh new file mode 100755 index 0000000..45ada9c --- /dev/null +++ b/workflow-pr/test_repo_setup.sh @@ -0,0 +1,51 @@ +#!/usr/bin/bash + +set -x + +export GIT_CONFIG_COUNT=2 + +export GIT_CONFIG_KEY_0=protocol.file.allow +export GIT_CONFIG_VALUE_0=always +export GIT_CONFIG_KEY_1=init.defaultBranch +export GIT_CONFIG_VALUE_1=main + +export GIT_AUTHOR_NAME=testname +export GIT_AUTHOR_EMAIL=test@suse.com +export GIT_AUTHOR_DATE='2005-04-07T22:13:13' +export GIT_COMMITTER_NAME=testname +export GIT_COMMITTER_EMAIL=test@suse.com +export GIT_COMMITTER_DATE='2005-04-07T22:13:13' + +create_prjgit_sample() { + mkdir prjgit + pushd prjgit + + git init -q --object-format=sha256 + echo Project git is here > README.md + git add README.md + + git submodule init + git submodule -q add ../pkgA pkgA + + git commit -q -m 'first commit' + + popd +} + +create_pkgA() { + mkdir pkgA + pushd pkgA + + git init -q --object-format=sha256 + echo "Package A" > README.md + git add README.md + + git commit -q -m 'Something base here' + + popd +} + +create_pkgA +create_prjgit_sample + +