Skip to content

Commit

Permalink
add tests and consts
Browse files Browse the repository at this point in the history
Signed-off-by: Manabu McCloskey <[email protected]>
  • Loading branch information
nabuskey committed Apr 23, 2024
1 parent aa120a5 commit 249845a
Show file tree
Hide file tree
Showing 11 changed files with 261 additions and 46 deletions.
8 changes: 5 additions & 3 deletions api/v1alpha1/gitrepository_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
)

const (
GitProviderGitea = "gitea"
GitProviderGitHub = "github"
GitProviderGitea = "gitea"
GitProviderGitHub = "github"
GiteaAdminUserName = "giteaAdmin"
)

type GitRepositorySpec struct {
Expand Down Expand Up @@ -42,7 +43,8 @@ type Provider struct {
// +kubebuilder:validation:Pattern=`^https?:\/\/.+$`
GitURL string `json:"gitURL"`
// InternalGitURL is the base URL of Git server accessible within the cluster only.
InternalGitURL string `json:"internalGitURL"`
InternalGitURL string `json:"internalGitURL"`
OrganizationName string `json:"organizationName"`
}

type SecretReference struct {
Expand Down
7 changes: 4 additions & 3 deletions pkg/controllers/custompackage/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,10 @@ func (r *Reconciler) reconcileGitRepo(ctx context.Context, resource *v1alpha1.Cu
Path: absPath,
},
Provider: v1alpha1.Provider{
Name: v1alpha1.GitProviderGitea,
GitURL: resource.Spec.GitServerURL,
InternalGitURL: resource.Spec.InternalGitServeURL,
Name: v1alpha1.GitProviderGitea,
GitURL: resource.Spec.GitServerURL,
InternalGitURL: resource.Spec.InternalGitServeURL,
OrganizationName: v1alpha1.GiteaAdminUserName,
},
SecretRef: resource.Spec.GitServerAuthSecretRef,
}
Expand Down
9 changes: 6 additions & 3 deletions pkg/controllers/custompackage/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

argov1alpha1 "github.com/cnoe-io/argocd-api/api/argo/application/v1alpha1"
"github.com/cnoe-io/idpbuilder/api/v1alpha1"
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sruntime "k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -172,12 +173,14 @@ func TestReconcileCustomPkg(t *testing.T) {
Path: p,
},
Provider: v1alpha1.Provider{
Name: v1alpha1.GitProviderGitea,
GitURL: "https://cnoe.io",
InternalGitURL: "http://internal.cnoe.io",
Name: v1alpha1.GitProviderGitea,
GitURL: "https://cnoe.io",
InternalGitURL: "http://internal.cnoe.io",
OrganizationName: v1alpha1.GiteaAdminUserName,
},
},
}
assert.Equal(t, repo.Spec, expectedRepo.Spec)
ok := reflect.DeepEqual(repo.Spec, expectedRepo.Spec)
if !ok {
t.Fatalf("expected spec does not match")
Expand Down
9 changes: 5 additions & 4 deletions pkg/controllers/gitrepository/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func getRepositoryName(repo v1alpha1.GitRepository) string {
}

func getOrganizationName(repo v1alpha1.GitRepository) string {
return "giteaAdmin"
return repo.Spec.Provider.OrganizationName
}

func GetGitProvider(ctx context.Context, repo *v1alpha1.GitRepository, kubeClient client.Client, scheme *runtime.Scheme, tmplConfig util.CorePackageTemplateConfig) (gitProvider, error) {
Expand All @@ -73,9 +73,10 @@ func GetGitProvider(ctx context.Context, repo *v1alpha1.GitRepository, kubeClien
}, nil
case v1alpha1.GitProviderGitHub:
return &gitHubProvider{
Client: kubeClient,
Scheme: scheme,
config: tmplConfig,
Client: kubeClient,
Scheme: scheme,
config: tmplConfig,
gitHubClient: newGitHubClient(nil),
}, nil
}
return nil, fmt.Errorf("invalid git provider %s ", repo.Spec.Provider.Name)
Expand Down
7 changes: 4 additions & 3 deletions pkg/controllers/gitrepository/git_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ type GiteaClient interface {
SetContext(ctx context.Context)
}

type gitHubClient interface {
getRepo(ctx context.Context, owner, repo string) (*github.Repository, error)
createRepo(ctx context.Context, owner, repo string) (*github.Repository, error)
type gitHubClientI interface {
getRepo(ctx context.Context, owner, repo string) (*github.Repository, *github.Response, error)
createRepo(ctx context.Context, owner string, req *github.Repository) (*github.Repository, *github.Response, error)
setToken(token string) error
}

type repoInfo struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/gitrepository/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,5 @@ func NewGiteaClient(url string, options ...gitea.ClientOption) (GiteaClient, err
}

func getInternalGiteaRepositoryURL(namespace, name, baseUrl string) string {
return fmt.Sprintf("%s/giteaAdmin/%s-%s.git", baseUrl, namespace, name)
return fmt.Sprintf("%s/%s/%s-%s.git", baseUrl, v1alpha1.GiteaAdminUserName, namespace, name)
}
56 changes: 31 additions & 25 deletions pkg/controllers/gitrepository/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,39 @@ const (
)

type ghClient struct {
client *github.Client
c *github.Client
}

func (g *ghClient) getRepo(ctx context.Context, owner, repo string) (*github.Repository, error) {
r, resp, err := g.client.Repositories.Get(ctx, owner, repo)
if resp.StatusCode == http.StatusNotFound {
return nil, notFoundError{}
}
return r, err
func (g *ghClient) getRepo(ctx context.Context, owner, repo string) (*github.Repository, *github.Response, error) {
return g.c.Repositories.Get(ctx, owner, repo)
}

func (g *ghClient) createRepo(ctx context.Context, owner, repo string) (*github.Repository, error) {
r := github.Repository{
Name: &repo,
Private: github.Bool(false),
}
rp, _, err := g.client.Repositories.Create(ctx, owner, &r)
if err != nil {
return nil, err
}
return rp, nil
func (g *ghClient) createRepo(ctx context.Context, owner string, req *github.Repository) (*github.Repository, *github.Response, error) {
return g.c.Repositories.Create(ctx, owner, req)
}

func (g *ghClient) setToken(token string) error {
g.c = g.c.WithAuthToken(token)
return nil
}

type gitHubProvider struct {
client.Client
Scheme *runtime.Scheme
gitHubClient gitHubClient
gitHubClient gitHubClientI
config util.CorePackageTemplateConfig
}

func (g *gitHubProvider) createRepository(ctx context.Context, repo *v1alpha1.GitRepository) (repoInfo, error) {

r, err := g.gitHubClient.createRepo(ctx, getOrganizationName(*repo), getRepositoryName(*repo))
req := github.Repository{
Name: github.String(getRepositoryName(*repo)),
Private: github.Bool(true),
}
r, _, err := g.gitHubClient.createRepo(ctx, getOrganizationName(*repo), &req)
if err != nil {
return repoInfo{}, fmt.Errorf("creating repo: %w", err)
}

return repoInfo{
name: *r.Name,
cloneUrl: *r.CloneURL,
Expand All @@ -64,11 +61,15 @@ func (g *gitHubProvider) createRepository(ctx context.Context, repo *v1alpha1.Gi
}

func (g *gitHubProvider) getRepository(ctx context.Context, repo *v1alpha1.GitRepository) (repoInfo, error) {

r, err := g.gitHubClient.createRepo(ctx, getOrganizationName(*repo), getRepositoryName(*repo))
r, resp, err := g.gitHubClient.getRepo(ctx, getOrganizationName(*repo), getRepositoryName(*repo))
if err != nil {
return repoInfo{}, fmt.Errorf("creating repo: %w", err)
if resp != nil && resp.StatusCode == http.StatusNotFound {
return repoInfo{}, notFoundError{}
} else {
return repoInfo{}, fmt.Errorf("getting repo: %w", err)
}
}

return repoInfo{
name: *r.Name,
cloneUrl: *r.CloneURL,
Expand Down Expand Up @@ -98,10 +99,15 @@ func (g *gitHubProvider) getProviderCredentials(ctx context.Context, repo *v1alp
}

func (g *gitHubProvider) setProviderCredentials(ctx context.Context, repo *v1alpha1.GitRepository, creds gitProviderCredentials) error {
g.gitHubClient = &ghClient{client: github.NewClient(nil).WithAuthToken(creds.accessToken)}
return nil
return g.gitHubClient.setToken(creds.accessToken)
}

func (g *gitHubProvider) updateRepoContent(ctx context.Context, repo *v1alpha1.GitRepository, repoInfo repoInfo, creds gitProviderCredentials) error {
return updateRepoContent(ctx, repo, repoInfo, creds, g.Scheme, g.config)
}

func newGitHubClient(httpClient *http.Client) gitHubClientI {
return &ghClient{
c: github.NewClient(httpClient),
}
}
Loading

0 comments on commit 249845a

Please sign in to comment.