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

Implement CanImplement method for providers #3115

Merged
merged 4 commits into from
Apr 18, 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
86 changes: 86 additions & 0 deletions internal/db/domain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright 2024 Stacklok, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package db

import (
"slices"
"strings"

"github.com/sqlc-dev/pqtype"
)

// This file contains domain-level methods for db structs

// CanImplement returns true if the provider implements the given type.
func (p *Provider) CanImplement(impl ProviderType) bool {
return slices.Contains(p.Implements, impl)
}

// ProfileRow is an interface row in the profiles table
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved across from store.go - I think it's best to separate out these domain-level methods and data types

type ProfileRow interface {
GetProfile() Profile
GetEntityProfile() NullEntities
GetContextualRules() pqtype.NullRawMessage
}

// GetProfile returns the profile
func (r ListProfilesByProjectIDAndLabelRow) GetProfile() Profile {
return r.Profile
}

// GetEntityProfile returns the entity profile
func (r ListProfilesByProjectIDAndLabelRow) GetEntityProfile() NullEntities {
return r.ProfilesWithEntityProfile.Entity
}

// GetContextualRules returns the contextual rules
func (r ListProfilesByProjectIDAndLabelRow) GetContextualRules() pqtype.NullRawMessage {
return r.ProfilesWithEntityProfile.ContextualRules
}

// GetProfile returns the profile
func (r ListProfilesByProjectIDRow) GetProfile() Profile {
return r.Profile
}

// GetEntityProfile returns the entity profile
func (r ListProfilesByProjectIDRow) GetEntityProfile() NullEntities {
return r.ProfilesWithEntityProfile.Entity
}

// GetContextualRules returns the contextual rules
func (r ListProfilesByProjectIDRow) GetContextualRules() pqtype.NullRawMessage {
return r.ProfilesWithEntityProfile.ContextualRules
}

// LabelsFromFilter parses the filter string and populates the IncludeLabels and ExcludeLabels fields
func (lp *ListProfilesByProjectIDAndLabelParams) LabelsFromFilter(filter string) {
// otherwise Split would have returned a slice with one empty string
if filter == "" {
return
}

for _, label := range strings.Split(filter, ",") {
switch {
case label == "*":
lp.IncludeLabels = append(lp.IncludeLabels, label)
case strings.HasPrefix(label, "!"):
// if the label starts with a "!", it is a negative filter, add it to the negative list
lp.ExcludeLabels = append(lp.ExcludeLabels, label[1:])
default:
lp.IncludeLabels = append(lp.IncludeLabels, label)
}
}
}
59 changes: 0 additions & 59 deletions internal/db/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ import (
"context"
"database/sql"
"fmt"
"strings"

"github.com/google/uuid"
"github.com/sqlc-dev/pqtype"
)

// ExtendQuerier extends the Querier interface with custom queries
Expand Down Expand Up @@ -133,60 +131,3 @@ func WithTransaction[T any](store Store, fn func(querier ExtendQuerier) (T, erro
}
return result, store.Commit(tx)
}

// ProfileRow is an interface row in the profiles table
type ProfileRow interface {
GetProfile() Profile
GetEntityProfile() NullEntities
GetContextualRules() pqtype.NullRawMessage
}

// GetProfile returns the profile
func (r ListProfilesByProjectIDAndLabelRow) GetProfile() Profile {
return r.Profile
}

// GetEntityProfile returns the entity profile
func (r ListProfilesByProjectIDAndLabelRow) GetEntityProfile() NullEntities {
return r.ProfilesWithEntityProfile.Entity
}

// GetContextualRules returns the contextual rules
func (r ListProfilesByProjectIDAndLabelRow) GetContextualRules() pqtype.NullRawMessage {
return r.ProfilesWithEntityProfile.ContextualRules
}

// GetProfile returns the profile
func (r ListProfilesByProjectIDRow) GetProfile() Profile {
return r.Profile
}

// GetEntityProfile returns the entity profile
func (r ListProfilesByProjectIDRow) GetEntityProfile() NullEntities {
return r.ProfilesWithEntityProfile.Entity
}

// GetContextualRules returns the contextual rules
func (r ListProfilesByProjectIDRow) GetContextualRules() pqtype.NullRawMessage {
return r.ProfilesWithEntityProfile.ContextualRules
}

// LabelsFromFilter parses the filter string and populates the IncludeLabels and ExcludeLabels fields
func (lp *ListProfilesByProjectIDAndLabelParams) LabelsFromFilter(filter string) {
// otherwise Split would have returned a slice with one empty string
if filter == "" {
return
}

for _, label := range strings.Split(filter, ",") {
switch {
case label == "*":
lp.IncludeLabels = append(lp.IncludeLabels, label)
case strings.HasPrefix(label, "!"):
// if the label starts with a "!", it is a negative filter, add it to the negative list
lp.ExcludeLabels = append(lp.ExcludeLabels, label[1:])
default:
lp.IncludeLabels = append(lp.IncludeLabels, label)
}
}
}
7 changes: 7 additions & 0 deletions internal/providers/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/go-git/go-git/v5/plumbing/transport"
"github.com/go-git/go-git/v5/storage/memory"

minderv1 "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
provifv1 "github.com/stacklok/minder/pkg/providers/v1"
)

Expand All @@ -44,6 +45,12 @@ func NewGit(token provifv1.GitCredential) *Git {
}
}

// CanImplement returns true/false depending on whether the Provider
// can implement the specified trait
func (_ *Git) CanImplement(trait minderv1.ProviderType) bool {
return trait == minderv1.ProviderType_PROVIDER_TYPE_GIT
}

// Clone clones a git repository
func (g *Git) Clone(ctx context.Context, url, branch string) (*git.Repository, error) {
opts := &git.CloneOptions{
Expand Down
9 changes: 9 additions & 0 deletions internal/providers/github/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ func NewGitHub(
}
}

// CanImplement returns true/false depending on whether the Provider
// can implement the specified trait
func (_ *GitHub) CanImplement(trait minderv1.ProviderType) bool {
return trait == minderv1.ProviderType_PROVIDER_TYPE_GITHUB ||
trait == minderv1.ProviderType_PROVIDER_TYPE_GIT ||
trait == minderv1.ProviderType_PROVIDER_TYPE_REST ||
trait == minderv1.ProviderType_PROVIDER_TYPE_REPO_LISTER
}

// ListPackagesByRepository returns a list of all packages for a specific repository
func (c *GitHub) ListPackagesByRepository(
ctx context.Context,
Expand Down
70 changes: 70 additions & 0 deletions internal/providers/github/mock/github.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions internal/providers/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ func NewREST(
}, nil
}

// CanImplement returns true/false depending on whether the Provider
// can implement the specified trait
func (_ *REST) CanImplement(trait minderv1.ProviderType) bool {
return trait == minderv1.ProviderType_PROVIDER_TYPE_REST
}

// GetBaseURL returns the base URL for the REST API.
func (h *REST) GetBaseURL() string {
return h.baseURL.String()
Expand Down
2 changes: 1 addition & 1 deletion internal/providers/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func NewProviderBuilder(

// Implements returns true if the provider implements the given type.
func (pb *ProviderBuilder) Implements(impl db.ProviderType) bool {
return slices.Contains(pb.p.Implements, impl)
return pb.p.CanImplement(impl)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have decided to leave this method for now. When my ProviderFactory PR comes along, the ProviderBuilder will be removed entirely. It doesn't seem like a good use of time to clean up all usages of this method.

}

// GetName returns the name of the provider instance as defined in the
Expand Down
3 changes: 3 additions & 0 deletions pkg/providers/v1/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ const (

// Provider is the general interface for all providers
type Provider interface {
// CanImplement returns true/false depending on whether the Provider
// can implement the specified trait
CanImplement(trait minderv1.ProviderType) bool
}

// Git is the interface for git providers
Expand Down