This commit is contained in:
Adam Majer 2024-12-16 18:12:54 +01:00
parent f6bd0c10c0
commit ac6fb96534
6 changed files with 92 additions and 56 deletions

View File

@ -688,13 +688,6 @@ func (e *GitHandlerImpl) GitSubmoduleList(gitPath, commitId string) (submoduleLi
} }
func (e *GitHandlerImpl) GitSubmoduleCommitId(cwd, packageName, commitId string) (subCommitId string, valid bool) { 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)} data_in, data_out := ChanIO{make(chan byte, 256)}, ChanIO{make(chan byte, 70)}
var wg sync.WaitGroup var wg sync.WaitGroup
@ -705,6 +698,14 @@ func (e *GitHandlerImpl) GitSubmoduleCommitId(cwd, packageName, commitId string)
} }
go func() { go func() {
defer func() {
if recover() != nil {
subCommitId = "wrong"
commitId = "ok"
valid = false
}
}()
defer wg.Done() defer wg.Done()
defer close(data_out.ch) defer close(data_out.ch)

View File

@ -23,7 +23,6 @@ import (
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"time" "time"
transport "github.com/go-openapi/runtime/client" transport "github.com/go-openapi/runtime/client"
@ -56,31 +55,39 @@ const (
ReviewStateUnknown models.ReviewStateType = "" ReviewStateUnknown models.ReviewStateType = ""
) )
type GiteaPRFetcher interface {
GetAssociatedPRs(org, repo string, prNo int64) ([]*models.PullRequest, error)
}
type GiteaMaintainershipInterface interface { type GiteaMaintainershipInterface interface {
FetchMaintainershipFile(org, prjGit, branch string) ([]byte, error) FetchMaintainershipFile(org, prjGit, branch string) ([]byte, error)
} }
type Gitea interface { type GiteaPRReviewFetcher interface {
GetPullRequestAndReviews(org, project string, num int64) (*models.PullRequest, []*models.PullReview, error) 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) GetPullNotifications(since *time.Time) ([]*models.NotificationThread, error)
SetNotificationRead(notificationId int64) error SetNotificationRead(notificationId int64) error
GetOrganization(orgName string) (*models.Organization, error) GetOrganization(orgName string) (*models.Organization, error)
GetOrganizationRepositories(orgName string) ([]*models.Repository, error) GetOrganizationRepositories(orgName string) ([]*models.Repository, error)
CreateRepositoryIfNotExist(git Git, org Organization, repoName 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) 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) GetRepositoryFileContent(org, repo, hash, path string) ([]byte, error)
GetPullRequestFileContent(pr *models.PullRequest, path string) ([]byte, error) GetPullRequestFileContent(pr *models.PullRequest, path string) ([]byte, error)
GetRecentPullRequests(org, repo string) ([]*models.PullRequest, error) GetRecentPullRequests(org, repo string) ([]*models.PullRequest, error)
GetRecentCommits(org, repo, branch string, commitNo int64) ([]*models.Commit, error) GetRecentCommits(org, repo, branch string, commitNo int64) ([]*models.Commit, error)
GiteaMaintainershipInterface GiteaMaintainershipInterface
GiteaPRFetcher
} }
type GiteaTransport struct { type GiteaTransport struct {
@ -374,24 +381,6 @@ func (gitea *GiteaTransport) AddReviewComment(pr *models.PullRequest, state mode
return c.Payload, nil 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) { func (gitea *GiteaTransport) GetRepositoryFileContent(org, repo, hash, path string) ([]byte, error) {
var retData []byte var retData []byte

View File

@ -76,7 +76,7 @@ func TestSyncPR(t *testing.T) {
Sha: "11eb36d5a58d7bb376cac59ac729a1986c6a7bfc63e7818e14382f545ccda985", Sha: "11eb36d5a58d7bb376cac59ac729a1986c6a7bfc63e7818e14382f545ccda985",
Repo: &models.Repository{ Repo: &models.Repository{
Name: "testRepo", Name: "testRepo",
Owner: &models.User { Owner: &models.User{
UserName: "test", UserName: "test",
}, },
DefaultBranch: "main1", DefaultBranch: "main1",
@ -116,7 +116,6 @@ func TestSyncPR(t *testing.T) {
} }
}) })
/*
t.Run("Missing submodule in prjgit", func(t *testing.T) { t.Run("Missing submodule in prjgit", func(t *testing.T) {
ctl := gomock.NewController(t) ctl := gomock.NewController(t)
mock := mock_common.NewMockGitea(ctl) mock := mock_common.NewMockGitea(ctl)
@ -125,20 +124,23 @@ func TestSyncPR(t *testing.T) {
git.GitPath = t.TempDir() git.GitPath = t.TempDir()
config.GitProjectName = "prjGit" config.GitProjectName = "prjGit"
event.Repository.Name = "tester" event.Repository.Name = "testRepo"
setupGitForTests(t, git) 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) mock.EXPECT().GetPullRequestAndReviews(config.Organization, "prj", int64(24)).Return(PrjGitPR, nil, nil)
err := pr.Process(event, git, config) 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.Error("Invalid error received.", err)
} }
}) })
*/
t.Run("Missing PrjGit PR for the sync", func(t *testing.T) { t.Run("Missing PrjGit PR for the sync", func(t *testing.T) {
ctl := gomock.NewController(t) ctl := gomock.NewController(t)

View File

@ -1,3 +1,31 @@
package main 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")
}

View File

@ -9,20 +9,6 @@ import (
"src.opensuse.org/autogits/common/gitea-generated/models" "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 { func fetchPRInfo(gitea GiteaPRInterface, pr common.BasicPR) PRInfo {
data, reviews, _ := gitea.GetPullRequestAndReviews(pr.Org, pr.Repo, pr.Num) data, reviews, _ := gitea.GetPullRequestAndReviews(pr.Org, pr.Repo, pr.Num)
return PRInfo{ return PRInfo{

View File

@ -1,10 +1,15 @@
package main package main
import ( import (
"errors"
"os" "os"
"os/exec" "os/exec"
"path" "path"
"testing" "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) { func TestReview(t *testing.T) {
@ -15,5 +20,30 @@ func TestReview(t *testing.T) {
t.Fatal(string(out)) 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)
}
})
}
}