Some checks failed
go-generate-check / go-generate-check (pull_request) Failing after 28s
203 lines
4.9 KiB
Go
203 lines
4.9 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"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 TestIssueCommentProcessor(t *testing.T) {
|
|
ctrl := gomock.NewController(t)
|
|
defer ctrl.Finish()
|
|
|
|
mockGitea := mock_common.NewMockGitea(ctrl)
|
|
groupName := "testgroup"
|
|
bot := &ReviewBot{
|
|
gitea: mockGitea,
|
|
groupName: groupName,
|
|
}
|
|
bot.InitRegex(groupName)
|
|
|
|
processor := &IssueCommentProcessor{bot: bot}
|
|
|
|
org := "myorg"
|
|
repo := "myrepo"
|
|
index := 123
|
|
|
|
event := &common.IssueCommentWebhookEvent{
|
|
Repository: &common.Repository{
|
|
Name: repo,
|
|
Owner: &common.Organization{
|
|
Username: org,
|
|
},
|
|
},
|
|
Issue: &common.IssueDetail{
|
|
Number: index,
|
|
},
|
|
}
|
|
|
|
req := &common.Request{
|
|
Type: common.RequestType_IssueComment,
|
|
Data: event,
|
|
}
|
|
|
|
t.Run("Successful Processing", func(t *testing.T) {
|
|
pr := &models.PullRequest{
|
|
Index: int64(index),
|
|
Base: &models.PRBranchInfo{
|
|
Name: "main",
|
|
Repo: &models.Repository{
|
|
Name: repo,
|
|
Owner: &models.User{UserName: org},
|
|
},
|
|
},
|
|
Head: &models.PRBranchInfo{
|
|
Sha: "headsha",
|
|
Repo: &models.Repository{
|
|
Name: repo,
|
|
Owner: &models.User{UserName: org},
|
|
},
|
|
},
|
|
User: &models.User{UserName: "submitter"},
|
|
RequestedReviewers: []*models.User{{UserName: groupName}},
|
|
}
|
|
|
|
mockGitea.EXPECT().GetPullRequest(org, repo, int64(index)).Return(pr, nil)
|
|
|
|
prjConfig := &common.AutogitConfig{
|
|
GitProjectName: org + "/" + repo + "#main",
|
|
ReviewGroups: []*common.ReviewGroup{{Name: groupName, Reviewers: []string{"r1"}}},
|
|
}
|
|
bot.configs = common.AutogitConfigs{prjConfig}
|
|
mockGitea.EXPECT().GetPullRequestReviews(org, repo, int64(index)).Return(nil, nil)
|
|
mockGitea.EXPECT().GetTimeline(org, repo, int64(index)).Return(nil, nil)
|
|
mockGitea.EXPECT().RequestReviews(pr, "r1").Return(nil, nil)
|
|
mockGitea.EXPECT().AddComment(pr, gomock.Any()).Return(nil)
|
|
|
|
err := processor.ProcessFunc(req)
|
|
if err != ReviewNotFinished {
|
|
t.Errorf("Expected ReviewNotFinished, got %v", err)
|
|
}
|
|
})
|
|
|
|
t.Run("Gitea error in GetPullRequest", func(t *testing.T) {
|
|
mockGitea.EXPECT().GetPullRequest(org, repo, int64(index)).Return(nil, fmt.Errorf("gitea error"))
|
|
err := processor.ProcessFunc(req)
|
|
if err == nil {
|
|
t.Error("Expected error, got nil")
|
|
}
|
|
})
|
|
|
|
t.Run("Wrong Request Type", func(t *testing.T) {
|
|
wrongReq := &common.Request{Type: common.RequestType_Push}
|
|
err := processor.ProcessFunc(wrongReq)
|
|
if err == nil {
|
|
t.Error("Expected error for wrong request type, got nil")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestConfigUpdatePush(t *testing.T) {
|
|
ctrl := gomock.NewController(t)
|
|
defer ctrl.Finish()
|
|
|
|
groupName := "testgroup"
|
|
bot := &ReviewBot{
|
|
groupName: groupName,
|
|
}
|
|
bot.InitRegex(groupName)
|
|
|
|
configChan := make(chan *common.AutogitConfig, 1)
|
|
processor := &ConfigUpdatePush{
|
|
bot: bot,
|
|
config_modified: configChan,
|
|
}
|
|
|
|
org := "myorg"
|
|
repo := "myrepo"
|
|
branch := "main"
|
|
|
|
prjConfig := &common.AutogitConfig{
|
|
GitProjectName: org + "/" + repo + "#" + branch,
|
|
Organization: org,
|
|
Branch: branch,
|
|
}
|
|
bot.configs = common.AutogitConfigs{prjConfig}
|
|
|
|
event := &common.PushWebhookEvent{
|
|
Ref: "refs/heads/" + branch,
|
|
Repository: &common.Repository{
|
|
Name: repo,
|
|
Owner: &common.Organization{
|
|
Username: org,
|
|
},
|
|
},
|
|
Commits: []common.Commit{
|
|
{
|
|
Modified: []string{common.ProjectConfigFile},
|
|
},
|
|
},
|
|
}
|
|
|
|
req := &common.Request{
|
|
Type: common.RequestType_Push,
|
|
Data: event,
|
|
}
|
|
|
|
t.Run("Config Modified", func(t *testing.T) {
|
|
err := processor.ProcessFunc(req)
|
|
if err != nil {
|
|
t.Errorf("Expected nil error, got %v", err)
|
|
}
|
|
|
|
select {
|
|
case modified := <-configChan:
|
|
if modified != prjConfig {
|
|
t.Errorf("Expected modified config to be %v, got %v", prjConfig, modified)
|
|
}
|
|
default:
|
|
t.Error("Expected config modification signal, but none received")
|
|
}
|
|
})
|
|
|
|
t.Run("No Config Modified", func(t *testing.T) {
|
|
noConfigEvent := *event
|
|
noConfigEvent.Commits = []common.Commit{{Modified: []string{"README.md"}}}
|
|
noConfigReq := &common.Request{Type: common.RequestType_Push, Data: &noConfigEvent}
|
|
|
|
err := processor.ProcessFunc(noConfigReq)
|
|
if err != nil {
|
|
t.Errorf("Expected nil error, got %v", err)
|
|
}
|
|
|
|
select {
|
|
case <-configChan:
|
|
t.Error("Did not expect config modification signal")
|
|
default:
|
|
// Success
|
|
}
|
|
})
|
|
|
|
t.Run("Wrong Branch Ref", func(t *testing.T) {
|
|
wrongBranchEvent := *event
|
|
wrongBranchEvent.Ref = "refs/tags/v1.0"
|
|
wrongBranchReq := &common.Request{Type: common.RequestType_Push, Data: &wrongBranchEvent}
|
|
|
|
err := processor.ProcessFunc(wrongBranchReq)
|
|
if err == nil {
|
|
t.Error("Expected error for wrong branch ref, got nil")
|
|
}
|
|
})
|
|
|
|
t.Run("Config Not Found", func(t *testing.T) {
|
|
bot.configs = common.AutogitConfigs{}
|
|
err := processor.ProcessFunc(req)
|
|
if err != nil {
|
|
t.Errorf("Expected nil error even if config not found, got %v", err)
|
|
}
|
|
})
|
|
} |