package main import ( "errors" "testing" "go.uber.org/mock/gomock" "src.opensuse.org/autogits/common" "src.opensuse.org/autogits/common/gitea-generated/models" mock_common "src.opensuse.org/autogits/common/mock" ) func TestSyncPR(t *testing.T) { config := &common.AutogitConfig{ Reviewers: []string{"reviewer1", "reviewer2"}, Branch: "testing", Organization: "test-org", GitProjectName: "test-prj#testing", } git := &common.GitHandlerImpl{ GitCommiter: "tester", GitEmail: "test@suse.com", GitPath: t.TempDir(), } processor := &PRProcessor{ config: config, git: git, } modelPR := &models.PullRequest{ Index: 42, Body: "PR: test-org/test-prj#24", Base: &models.PRBranchInfo{ Ref: "main", Repo: &models.Repository{ Name: "test-repo", Owner: &models.User{UserName: "test-org"}, DefaultBranch: "main", }, }, Head: &models.PRBranchInfo{ Ref: "branch", Sha: "11eb36d5a58d7bb376cac59ac729a1986c6a7bfc63e7818e14382f545ccda985", Repo: &models.Repository{ Name: "test-repo", Owner: &models.User{UserName: "test-org"}, DefaultBranch: "main", }, }, } PrjGitPR := &models.PullRequest{ Title: "some pull request", Body: "PR: test-org/test-repo#42", Index: 24, Base: &models.PRBranchInfo{ Ref: "testing", Repo: &models.Repository{ Name: "test-prj", Owner: &models.User{UserName: "test-org"}, SSHURL: "url", }, }, Head: &models.PRBranchInfo{ Name: "PR_test-repo#42", Sha: "db8adab91edb476b9762097d10c6379aa71efd6b60933a1c0e355ddacf419a95", Repo: &models.Repository{ SSHURL: "url", }, }, } t.Run("PR_sync_request_against_PrjGit_==_no_action", func(t *testing.T) { ctl := gomock.NewController(t) defer ctl.Finish() gitea := mock_common.NewMockGitea(ctl) Gitea = gitea // Common expectations for FetchPRSet and downstream checks gitea.EXPECT().GetPullRequestReviews(gomock.Any(), gomock.Any(), gomock.Any()).Return([]*models.PullReview{}, nil).AnyTimes() gitea.EXPECT().FetchMaintainershipFile(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, "", nil).AnyTimes() gitea.EXPECT().FetchMaintainershipDirFile(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, "", nil).AnyTimes() gitea.EXPECT().SetRepoOptions(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes() prjGitRepoPR := &models.PullRequest{ Index: 100, Base: &models.PRBranchInfo{ Ref: "testing", Repo: &models.Repository{ Name: "test-prj", Owner: &models.User{UserName: "test-org"}, }, }, Head: &models.PRBranchInfo{ Ref: "branch", }, } gitea.EXPECT().GetPullRequest(gomock.Any(), gomock.Any(), gomock.Any()).Return(prjGitRepoPR, nil).AnyTimes() gitea.EXPECT().GetTimeline(gomock.Any(), gomock.Any(), gomock.Any()).Return([]*models.TimelineComment{}, nil).AnyTimes() if err := processor.Process(prjGitRepoPR); err != nil { t.Errorf("Expected nil error for PrjGit sync request, got %v", err) } }) t.Run("Missing PrjGit PR for the sync", func(t *testing.T) { ctl := gomock.NewController(t) defer ctl.Finish() gitea := mock_common.NewMockGitea(ctl) Gitea = gitea gitea.EXPECT().GetPullRequestReviews(gomock.Any(), gomock.Any(), gomock.Any()).Return([]*models.PullReview{}, nil).AnyTimes() gitea.EXPECT().FetchMaintainershipFile(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, "", nil).AnyTimes() gitea.EXPECT().FetchMaintainershipDirFile(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, "", nil).AnyTimes() gitea.EXPECT().SetRepoOptions(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes() gitea.EXPECT().GetTimeline(gomock.Any(), gomock.Any(), gomock.Any()).Return([]*models.TimelineComment{}, nil).AnyTimes() gitea.EXPECT().GetPullRequest(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, errors.New("not found")).AnyTimes() err := processor.Process(modelPR) // It should fail because it can't find the project PR linked in body if err == nil { t.Errorf("Expected error for missing project PR, got nil") } }) t.Run("PR sync", func(t *testing.T) { ctl := gomock.NewController(t) defer ctl.Finish() gitea := mock_common.NewMockGitea(ctl) Gitea = gitea setupGitForTests(t, git) gitea.EXPECT().GetPullRequest(gomock.Any(), gomock.Any(), gomock.Any()).Return(PrjGitPR, nil).AnyTimes() gitea.EXPECT().GetTimeline(gomock.Any(), gomock.Any(), gomock.Any()).Return([]*models.TimelineComment{}, nil).AnyTimes() gitea.EXPECT().GetPullRequestReviews(gomock.Any(), gomock.Any(), gomock.Any()).Return([]*models.PullReview{}, nil).AnyTimes() gitea.EXPECT().FetchMaintainershipFile(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, "", nil).AnyTimes() gitea.EXPECT().FetchMaintainershipDirFile(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, "", nil).AnyTimes() gitea.EXPECT().SetRepoOptions(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes() // For UpdatePrjGitPR gitea.EXPECT().UpdatePullRequest(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes() err := processor.Process(modelPR) if err != nil { t.Errorf("Unexpected error: %v", err) } }) }