From ac6fb96534c82e70e7782dcd99441115d58e61e88c18f2bf59b7b8d9128a397e Mon Sep 17 00:00:00 2001 From: Adam Majer Date: Mon, 16 Dec 2024 18:12:54 +0100 Subject: [PATCH] wip --- bots-common/git_utils.go | 15 +++++----- bots-common/gitea_utils.go | 43 ++++++++++----------------- workflow-pr/pr_processor_sync_test.go | 16 +++++----- workflow-pr/review.go | 28 +++++++++++++++++ workflow-pr/review_old.go | 14 --------- workflow-pr/review_test.go | 32 +++++++++++++++++++- 6 files changed, 92 insertions(+), 56 deletions(-) diff --git a/bots-common/git_utils.go b/bots-common/git_utils.go index 71d7a8d..dbe4b3d 100644 --- a/bots-common/git_utils.go +++ b/bots-common/git_utils.go @@ -688,13 +688,6 @@ func (e *GitHandlerImpl) GitSubmoduleList(gitPath, commitId string) (submoduleLi } func (e *GitHandlerImpl) GitSubmoduleCommitId(cwd, packageName, commitId string) (subCommitId string, valid bool) { - defer func() { - if recover() != nil { - commitId = "" - valid = false - } - }() - data_in, data_out := ChanIO{make(chan byte, 256)}, ChanIO{make(chan byte, 70)} var wg sync.WaitGroup @@ -705,6 +698,14 @@ func (e *GitHandlerImpl) GitSubmoduleCommitId(cwd, packageName, commitId string) } go func() { + defer func() { + if recover() != nil { + subCommitId = "wrong" + commitId = "ok" + valid = false + } + }() + defer wg.Done() defer close(data_out.ch) diff --git a/bots-common/gitea_utils.go b/bots-common/gitea_utils.go index c3f1902..e681d51 100644 --- a/bots-common/gitea_utils.go +++ b/bots-common/gitea_utils.go @@ -23,7 +23,6 @@ import ( "io" "os" "path/filepath" - "strings" "time" transport "github.com/go-openapi/runtime/client" @@ -56,31 +55,39 @@ const ( ReviewStateUnknown models.ReviewStateType = "" ) -type GiteaPRFetcher interface { - GetAssociatedPRs(org, repo string, prNo int64) ([]*models.PullRequest, error) -} - type GiteaMaintainershipInterface interface { FetchMaintainershipFile(org, prjGit, branch string) ([]byte, error) } -type Gitea interface { +type GiteaPRReviewFetcher interface { GetPullRequestAndReviews(org, project string, num int64) (*models.PullRequest, []*models.PullReview, error) +} + +type GiteaReviewRequester interface { + RequestReviews(pr *models.PullRequest, reviewer string) ([]*models.PullReview, error) +} + +type GiteaReviewer interface { + AddReviewComment(pr *models.PullRequest, state models.ReviewStateType, comment string) (*models.PullReview, error) +} + +type Gitea interface { + GiteaReviewRequester + GiteaReviewer + GiteaPRReviewFetcher + GetPullNotifications(since *time.Time) ([]*models.NotificationThread, error) SetNotificationRead(notificationId int64) error GetOrganization(orgName string) (*models.Organization, error) GetOrganizationRepositories(orgName string) ([]*models.Repository, error) CreateRepositoryIfNotExist(git Git, org Organization, repoName string) (*models.Repository, error) 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) GetRepositoryFileContent(org, repo, hash, path string) ([]byte, error) GetPullRequestFileContent(pr *models.PullRequest, path string) ([]byte, error) GetRecentPullRequests(org, repo string) ([]*models.PullRequest, error) GetRecentCommits(org, repo, branch string, commitNo int64) ([]*models.Commit, error) GiteaMaintainershipInterface - GiteaPRFetcher } type GiteaTransport struct { @@ -374,24 +381,6 @@ func (gitea *GiteaTransport) AddReviewComment(pr *models.PullRequest, state mode return c.Payload, nil } -func (gitea *GiteaTransport) GetAssociatedPRs(org, repo string, prNo int64) ([]*models.PullRequest, error) { - prData, err := gitea.client.Repository.RepoGetPullRequest( - repository.NewRepoGetPullRequestParams(). - WithOwner(org). - WithRepo(repo). - WithIndex(prNo), - gitea.transport.DefaultAuthentication) - - if err != nil { - return nil, err - } - - desc := prData.Payload.Body - strings.Split(desc, "\n") - - return nil, nil -} - func (gitea *GiteaTransport) GetRepositoryFileContent(org, repo, hash, path string) ([]byte, error) { var retData []byte diff --git a/workflow-pr/pr_processor_sync_test.go b/workflow-pr/pr_processor_sync_test.go index 3afcac6..b373210 100644 --- a/workflow-pr/pr_processor_sync_test.go +++ b/workflow-pr/pr_processor_sync_test.go @@ -75,8 +75,8 @@ func TestSyncPR(t *testing.T) { Ref: "branch", Sha: "11eb36d5a58d7bb376cac59ac729a1986c6a7bfc63e7818e14382f545ccda985", Repo: &models.Repository{ - Name: "testRepo", - Owner: &models.User { + Name: "testRepo", + Owner: &models.User{ UserName: "test", }, DefaultBranch: "main1", @@ -116,7 +116,6 @@ func TestSyncPR(t *testing.T) { } }) - /* t.Run("Missing submodule in prjgit", func(t *testing.T) { ctl := gomock.NewController(t) mock := mock_common.NewMockGitea(ctl) @@ -125,20 +124,23 @@ 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) + oldSha := PrjGitPR.Head.Sha + defer func() { PrjGitPR.Head.Sha = oldSha }() + PrjGitPR.Head.Sha = "ab8adab91edb476b9762097d10c6379aa71efd6b60933a1c0e355ddacf419a95" + + 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) - if err == nil || err.Error() != "Cannot fetch submodule commit id in prjgit for 'tester'" { + if err == nil || err.Error() != "Cannot fetch submodule commit id in prjgit for 'testRepo'" { t.Error("Invalid error received.", err) } }) -*/ t.Run("Missing PrjGit PR for the sync", func(t *testing.T) { ctl := gomock.NewController(t) diff --git a/workflow-pr/review.go b/workflow-pr/review.go index d3ca313..3763e75 100644 --- a/workflow-pr/review.go +++ b/workflow-pr/review.go @@ -1,3 +1,31 @@ package main +import ( + "errors" + "src.opensuse.org/autogits/common" + "src.opensuse.org/autogits/common/gitea-generated/models" +) + +type Review interface { + IsApproved() (bool, error) +} + +type PRInfo struct { + pr *models.PullRequest + reviews []*models.PullReview +} + +type ReviewSet struct { + maintainers MaintainershipData + prs []PRInfo +} + +func FetchReviewSet(gitea common.GiteaPRReviewFetcher, org, repo string, num int64) (*ReviewSet, error) { + _,_, err := gitea.GetPullRequestAndReviews(org, repo, num) + if err != nil { + return nil, err + } + + return nil, errors.New("Error") +} diff --git a/workflow-pr/review_old.go b/workflow-pr/review_old.go index 756070c..74c67dc 100644 --- a/workflow-pr/review_old.go +++ b/workflow-pr/review_old.go @@ -9,20 +9,6 @@ import ( "src.opensuse.org/autogits/common/gitea-generated/models" ) -type Review interface { - IsApproved() (bool, error) -} - -type PRInfo struct { - pr *models.PullRequest - reviews []*models.PullReview -} - -type ReviewSet struct { - maintainers MaintainershipData - prs []PRInfo -} - func fetchPRInfo(gitea GiteaPRInterface, pr common.BasicPR) PRInfo { data, reviews, _ := gitea.GetPullRequestAndReviews(pr.Org, pr.Repo, pr.Num) return PRInfo{ diff --git a/workflow-pr/review_test.go b/workflow-pr/review_test.go index 7b3eb2d..57f76f2 100644 --- a/workflow-pr/review_test.go +++ b/workflow-pr/review_test.go @@ -1,10 +1,15 @@ package main import ( + "errors" "os" "os/exec" "path" "testing" + + "go.uber.org/mock/gomock" + "src.opensuse.org/autogits/common/gitea-generated/models" + mock_common "src.opensuse.org/autogits/common/mock" ) func TestReview(t *testing.T) { @@ -15,5 +20,30 @@ func TestReview(t *testing.T) { t.Fatal(string(out)) } -} + tests := []struct { + name string + org string + repo string + pr_no int64 + pr *models.PullRequest + err error + }{ + {name: "Error fetching ReviewSet if there are missing links", org: "test", repo: "repo", pr_no: 42, pr: &models.PullRequest{Body: ""}}, + {name: "Error fetching ReviewSet if there are missing links", org: "test", repo: "repo", pr_no: 42, pr: &models.PullRequest{Body: ""}, err: errors.New("Missing PR")}, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + ctl := gomock.NewController(t) + mock := mock_common.NewMockGiteaPRReviewFetcher(ctl) + + mock.EXPECT().GetPullRequestAndReviews(test.org, test.repo, test.pr_no).Return(test.pr, nil, test.err) + + _, err := FetchReviewSet(mock, "test", "repo", 42) + if test.err != nil && err == nil { + t.Fatal("Expected", test.err, "but got", err) + } + }) + } +}