Skip to content

Commit 557f685

Browse files
claudesallyom
authored andcommitted
Fix GitLab integration test failures from PR #339
Fixes go vet errors introduced in the GitLab integration PR that merged with failing tests. Changes improve code testability by using interfaces instead of concrete types. Changes: - Fix test assertions to use correct struct fields (result.User.Username instead of result.Username, result.User.ID instead of result.GitLabUserID) - Change kubernetes.Clientset to kubernetes.Interface in function signatures to support both real and fake clients for testing - Fix GetCurrentUser call to use package-level function instead of method - Remove unused imports (time, corev1) Updated functions: - k8s.StoreGitLabToken, k8s.GetGitLabToken, k8s.DeleteGitLabToken - k8s.HasGitLabToken, k8s.StoreGitLabConnection, k8s.GetGitLabConnection - k8s.DeleteGitLabConnection, k8s.HasGitLabConnection - k8s.ListGitLabConnections, git.GetGitLabToken - gitlab.NewConnectionManager and ConnectionManager.clientset field This follows Go best practices (accept interfaces, return structs) and makes the code more testable without changing runtime behavior. Related: PR #339 (GitLab integration) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> Signed-off-by: sallyom <[email protected]>
1 parent 87a9d9c commit 557f685

File tree

5 files changed

+16
-17
lines changed

5 files changed

+16
-17
lines changed

components/backend/git/operations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func GetGitHubToken(ctx context.Context, k8sClient *kubernetes.Clientset, dynCli
114114
}
115115

116116
// GetGitLabToken retrieves a GitLab Personal Access Token for a user
117-
func GetGitLabToken(ctx context.Context, k8sClient *kubernetes.Clientset, project, userID string) (string, error) {
117+
func GetGitLabToken(ctx context.Context, k8sClient kubernetes.Interface, project, userID string) (string, error) {
118118
if k8sClient == nil {
119119
log.Printf("Cannot read GitLab token: k8s client is nil")
120120
return "", fmt.Errorf("no GitLab credentials available. Please connect your GitLab account")

components/backend/gitlab/connection.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ import (
1414

1515
// ConnectionManager handles GitLab connection operations
1616
type ConnectionManager struct {
17-
clientset *kubernetes.Clientset
17+
clientset kubernetes.Interface
1818
namespace string
1919
}
2020

2121
// NewConnectionManager creates a new connection manager
22-
func NewConnectionManager(clientset *kubernetes.Clientset, namespace string) *ConnectionManager {
22+
func NewConnectionManager(clientset kubernetes.Interface, namespace string) *ConnectionManager {
2323
return &ConnectionManager{
2424
clientset: clientset,
2525
namespace: namespace,

components/backend/k8s/configmap.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const (
1919
)
2020

2121
// StoreGitLabConnection stores GitLab connection metadata in a ConfigMap
22-
func StoreGitLabConnection(ctx context.Context, clientset *kubernetes.Clientset, namespace string, connection *types.GitLabConnection) error {
22+
func StoreGitLabConnection(ctx context.Context, clientset kubernetes.Interface, namespace string, connection *types.GitLabConnection) error {
2323
configMapsClient := clientset.CoreV1().ConfigMaps(namespace)
2424

2525
// Serialize connection to JSON
@@ -68,7 +68,7 @@ func StoreGitLabConnection(ctx context.Context, clientset *kubernetes.Clientset,
6868
}
6969

7070
// GetGitLabConnection retrieves GitLab connection metadata from a ConfigMap
71-
func GetGitLabConnection(ctx context.Context, clientset *kubernetes.Clientset, namespace, userID string) (*types.GitLabConnection, error) {
71+
func GetGitLabConnection(ctx context.Context, clientset kubernetes.Interface, namespace, userID string) (*types.GitLabConnection, error) {
7272
configMapsClient := clientset.CoreV1().ConfigMaps(namespace)
7373

7474
configMap, err := configMapsClient.Get(ctx, GitLabConnectionsConfigMapName, metav1.GetOptions{})
@@ -93,7 +93,7 @@ func GetGitLabConnection(ctx context.Context, clientset *kubernetes.Clientset, n
9393
}
9494

9595
// DeleteGitLabConnection removes GitLab connection metadata from a ConfigMap
96-
func DeleteGitLabConnection(ctx context.Context, clientset *kubernetes.Clientset, namespace, userID string) error {
96+
func DeleteGitLabConnection(ctx context.Context, clientset kubernetes.Interface, namespace, userID string) error {
9797
configMapsClient := clientset.CoreV1().ConfigMaps(namespace)
9898

9999
configMap, err := configMapsClient.Get(ctx, GitLabConnectionsConfigMapName, metav1.GetOptions{})
@@ -119,7 +119,7 @@ func DeleteGitLabConnection(ctx context.Context, clientset *kubernetes.Clientset
119119
}
120120

121121
// HasGitLabConnection checks if a user has a GitLab connection stored
122-
func HasGitLabConnection(ctx context.Context, clientset *kubernetes.Clientset, namespace, userID string) (bool, error) {
122+
func HasGitLabConnection(ctx context.Context, clientset kubernetes.Interface, namespace, userID string) (bool, error) {
123123
configMapsClient := clientset.CoreV1().ConfigMaps(namespace)
124124

125125
configMap, err := configMapsClient.Get(ctx, GitLabConnectionsConfigMapName, metav1.GetOptions{})
@@ -135,7 +135,7 @@ func HasGitLabConnection(ctx context.Context, clientset *kubernetes.Clientset, n
135135
}
136136

137137
// ListGitLabConnections retrieves all GitLab connections from a ConfigMap
138-
func ListGitLabConnections(ctx context.Context, clientset *kubernetes.Clientset, namespace string) ([]*types.GitLabConnection, error) {
138+
func ListGitLabConnections(ctx context.Context, clientset kubernetes.Interface, namespace string) ([]*types.GitLabConnection, error) {
139139
configMapsClient := clientset.CoreV1().ConfigMaps(namespace)
140140

141141
configMap, err := configMapsClient.Get(ctx, GitLabConnectionsConfigMapName, metav1.GetOptions{})

components/backend/k8s/secrets.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const (
1818

1919
// StoreGitLabToken stores a GitLab Personal Access Token in Kubernetes Secrets
2020
// Uses optimistic concurrency control with retry to handle concurrent updates
21-
func StoreGitLabToken(ctx context.Context, clientset *kubernetes.Clientset, namespace, userID, token string) error {
21+
func StoreGitLabToken(ctx context.Context, clientset kubernetes.Interface, namespace, userID, token string) error {
2222
secretsClient := clientset.CoreV1().Secrets(namespace)
2323

2424
// Retry up to 3 times with exponential backoff
@@ -88,7 +88,7 @@ func StoreGitLabToken(ctx context.Context, clientset *kubernetes.Clientset, name
8888
}
8989

9090
// GetGitLabToken retrieves a GitLab Personal Access Token from Kubernetes Secrets
91-
func GetGitLabToken(ctx context.Context, clientset *kubernetes.Clientset, namespace, userID string) (string, error) {
91+
func GetGitLabToken(ctx context.Context, clientset kubernetes.Interface, namespace, userID string) (string, error) {
9292
secretsClient := clientset.CoreV1().Secrets(namespace)
9393

9494
secret, err := secretsClient.Get(ctx, GitLabTokensSecretName, metav1.GetOptions{})
@@ -109,7 +109,7 @@ func GetGitLabToken(ctx context.Context, clientset *kubernetes.Clientset, namesp
109109

110110
// DeleteGitLabToken removes a GitLab Personal Access Token from Kubernetes Secrets
111111
// Uses optimistic concurrency control with retry to handle concurrent updates
112-
func DeleteGitLabToken(ctx context.Context, clientset *kubernetes.Clientset, namespace, userID string) error {
112+
func DeleteGitLabToken(ctx context.Context, clientset kubernetes.Interface, namespace, userID string) error {
113113
secretsClient := clientset.CoreV1().Secrets(namespace)
114114

115115
// Retry up to 3 times with exponential backoff
@@ -155,7 +155,7 @@ func DeleteGitLabToken(ctx context.Context, clientset *kubernetes.Clientset, nam
155155
}
156156

157157
// HasGitLabToken checks if a user has a GitLab token stored
158-
func HasGitLabToken(ctx context.Context, clientset *kubernetes.Clientset, namespace, userID string) (bool, error) {
158+
func HasGitLabToken(ctx context.Context, clientset kubernetes.Interface, namespace, userID string) (bool, error) {
159159
secretsClient := clientset.CoreV1().Secrets(namespace)
160160

161161
secret, err := secretsClient.Get(ctx, GitLabTokensSecretName, metav1.GetOptions{})

components/backend/tests/integration/gitlab/gitlab_integration_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"os"
66
"testing"
7-
"time"
87

98
"ambient-code-backend/git"
109
"ambient-code-backend/gitlab"
@@ -14,7 +13,6 @@ import (
1413

1514
"github.com/stretchr/testify/assert"
1615
"github.com/stretchr/testify/require"
17-
corev1 "k8s.io/api/core/v1"
1816
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1917
"k8s.io/client-go/kubernetes/fake"
2018
)
@@ -48,8 +46,9 @@ func TestGitLabIntegrationEnd2End(t *testing.T) {
4846
result, err := gitlab.ValidateGitLabToken(ctx, gitlabToken, "https://gitlab.com")
4947
require.NoError(t, err, "Token validation should succeed")
5048
assert.True(t, result.Valid, "Token should be valid")
51-
assert.NotEmpty(t, result.Username, "Username should be populated")
52-
assert.NotEmpty(t, result.GitLabUserID, "GitLab user ID should be populated")
49+
assert.NotNil(t, result.User, "User should be populated")
50+
assert.NotEmpty(t, result.User.Username, "Username should be populated")
51+
assert.NotZero(t, result.User.ID, "GitLab user ID should be populated")
5352
})
5453

5554
// Test token storage
@@ -362,7 +361,7 @@ func TestGitLabClientLogging(t *testing.T) {
362361
client := gitlab.NewClient("https://gitlab.com/api/v4", token)
363362

364363
// Make a simple API request
365-
resp, err := client.GetCurrentUser(ctx)
364+
resp, err := gitlab.GetCurrentUser(ctx, client)
366365
require.NoError(t, err, "API call should succeed")
367366
assert.NotNil(t, resp, "Response should not be nil")
368367

0 commit comments

Comments
 (0)