Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: simplify 'get repo' API implementation #20348

Merged
merged 2 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 2 additions & 24 deletions server/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func (s *Server) List(ctx context.Context, q *repositorypkg.RepoQuery) (*appsv1.

// Get return the requested configured repository by URL and the state of its connections.
func (s *Server) Get(ctx context.Context, q *repositorypkg.RepoQuery) (*appsv1.Repository, error) {
// ListRepositories normalizes the repo, sanitizes it, and augments it with connection details.
repo, err := getRepository(ctx, s.ListRepositories, q)
if err != nil {
return nil, err
Expand All @@ -146,30 +147,7 @@ func (s *Server) Get(ctx context.Context, q *repositorypkg.RepoQuery) (*appsv1.R
return nil, status.Errorf(codes.NotFound, "repo '%s' not found", q.Repo)
}

// For backwards compatibility, if we have no repo type set assume a default
rType := repo.Type
if rType == "" {
rType = common.DefaultRepoType
}
// remove secrets
item := appsv1.Repository{
Repo: repo.Repo,
Type: rType,
Name: repo.Name,
Username: repo.Username,
Insecure: repo.IsInsecure(),
EnableLFS: repo.EnableLFS,
GithubAppId: repo.GithubAppId,
GithubAppInstallationId: repo.GithubAppInstallationId,
GitHubAppEnterpriseBaseURL: repo.GitHubAppEnterpriseBaseURL,
Proxy: repo.Proxy,
Project: repo.Project,
InheritedCreds: repo.InheritedCreds,
}

item.ConnectionState = s.getConnectionState(ctx, item.Repo, item.Project, q.ForceRefresh)

return &item, nil
return repo, nil
}

// ListRepositories returns a list of all configured repositories and the state of their connections
Expand Down
62 changes: 62 additions & 0 deletions server/repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,68 @@ func TestRepositoryServer(t *testing.T) {
assert.Equal(t, "rpc error: code = NotFound desc = repo 'https://test' not found", err.Error())
})

t.Run("Test_GetRepoIsSanitized", func(t *testing.T) {
repoServerClient := mocks.RepoServerServiceClient{}
repoServerClient.On("TestRepository", mock.Anything, mock.Anything).Return(&apiclient.TestRepositoryResponse{}, nil)
repoServerClientset := mocks.Clientset{RepoServerServiceClient: &repoServerClient}

url := "https://test"
db := &dbmocks.ArgoDB{}
db.On("ListRepositories", context.TODO()).Return([]*appsv1.Repository{{Repo: url, Username: "test", Password: "it's a secret"}}, nil)
db.On("GetRepository", context.TODO(), url, "").Return(&appsv1.Repository{Repo: url, Username: "test", Password: "it's a secret"}, nil)
db.On("RepositoryExists", context.TODO(), url, "").Return(true, nil)

s := NewServer(&repoServerClientset, db, enforcer, newFixtures().Cache, appLister, projInformer, testNamespace, settingsMgr)
repo, err := s.Get(context.TODO(), &repository.RepoQuery{
Repo: url,
})
require.NoError(t, err)
assert.Equal(t, "https://test", repo.Repo)
assert.Empty(t, repo.Password)
})

t.Run("Test_GetRepoIsNormalized", func(t *testing.T) {
repoServerClient := mocks.RepoServerServiceClient{}
repoServerClient.On("TestRepository", mock.Anything, mock.Anything).Return(&apiclient.TestRepositoryResponse{}, nil)
repoServerClientset := mocks.Clientset{RepoServerServiceClient: &repoServerClient}

url := "https://test"
db := &dbmocks.ArgoDB{}
db.On("ListRepositories", context.TODO()).Return([]*appsv1.Repository{{Repo: url}}, nil)
db.On("GetRepository", context.TODO(), url, "").Return(&appsv1.Repository{Repo: url, Username: "test"}, nil)
db.On("RepositoryExists", context.TODO(), url, "").Return(true, nil)

s := NewServer(&repoServerClientset, db, enforcer, newFixtures().Cache, appLister, projInformer, testNamespace, settingsMgr)
repo, err := s.Get(context.TODO(), &repository.RepoQuery{
Repo: url,
})
require.NoError(t, err)
assert.Equal(t, "https://test", repo.Repo)
assert.Equal(t, common.DefaultRepoType, repo.Type)
})

t.Run("Test_GetRepoHasConnectionState", func(t *testing.T) {
repoServerClient := mocks.RepoServerServiceClient{}
repoServerClient.On("TestRepository", mock.Anything, mock.Anything).Return(&apiclient.TestRepositoryResponse{
VerifiedRepository: true,
}, nil)
repoServerClientset := mocks.Clientset{RepoServerServiceClient: &repoServerClient}

url := "https://test"
db := &dbmocks.ArgoDB{}
db.On("ListRepositories", context.TODO()).Return([]*appsv1.Repository{{Repo: url}}, nil)
db.On("GetRepository", context.TODO(), url, "").Return(&appsv1.Repository{Repo: url}, nil)
db.On("RepositoryExists", context.TODO(), url, "").Return(true, nil)

s := NewServer(&repoServerClientset, db, enforcer, newFixtures().Cache, appLister, projInformer, testNamespace, settingsMgr)
repo, err := s.Get(context.TODO(), &repository.RepoQuery{
Repo: url,
})
require.NoError(t, err)
require.NotNil(t, repo.ConnectionState)
assert.Equal(t, appsv1.ConnectionStatusSuccessful, repo.ConnectionState.Status)
})

t.Run("Test_CreateRepositoryWithoutUpsert", func(t *testing.T) {
repoServerClient := mocks.RepoServerServiceClient{}
repoServerClient.On("TestRepository", mock.Anything, mock.Anything).Return(&apiclient.TestRepositoryResponse{}, nil)
Expand Down