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

feat: Persist provider configuration in protobuf #1021

Merged
merged 1 commit into from
Sep 26, 2023
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
124 changes: 39 additions & 85 deletions docs/docs/protodocs/proto.md

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

3 changes: 1 addition & 2 deletions internal/controlplane/handlers_repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
github "github.com/stacklok/mediator/internal/providers/github"
"github.com/stacklok/mediator/internal/reconcilers"
pb "github.com/stacklok/mediator/pkg/api/protobuf/go/mediator/v1"
provifv1 "github.com/stacklok/mediator/pkg/providers/v1"
)

// RegisterRepository adds repositories to the database and registers a webhook
Expand Down Expand Up @@ -406,7 +405,7 @@ func (s *Server) SyncRepositories(ctx context.Context, in *pb.SyncRepositoriesRe
}

// Populate the database with the repositories using the GraphQL API
client, err := github.NewRestClient(ctx, &provifv1.GitHubConfig{}, token.AccessToken, owner_filter)
client, err := github.NewRestClient(ctx, &pb.GitHubProviderConfig{}, token.AccessToken, owner_filter)
if err != nil {
return nil, status.Errorf(codes.Internal, "cannot create github client: %v", err)
}
Expand Down
18 changes: 15 additions & 3 deletions internal/providers/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ package github
import (
"context"
"encoding/json"
"fmt"
"net/url"
"time"

"github.com/google/go-github/v53/github"
"golang.org/x/oauth2"

"github.com/stacklok/mediator/internal/db"
mediatorv1 "github.com/stacklok/mediator/pkg/api/protobuf/go/mediator/v1"
provifv1 "github.com/stacklok/mediator/pkg/providers/v1"
)

Expand Down Expand Up @@ -57,7 +59,12 @@ var _ provifv1.GitHub = (*RestClient)(nil)
// BaseURL defaults to the public GitHub API, if needing to use a customer domain
// endpoint (as is the case with GitHub Enterprise), set the Endpoint field in
// the GitHubConfig struct
func NewRestClient(ctx context.Context, config *provifv1.GitHubConfig, token string, owner string) (*RestClient, error) {
func NewRestClient(
ctx context.Context,
config *mediatorv1.GitHubProviderConfig,
token string,
owner string,
) (*RestClient, error) {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
Expand All @@ -81,15 +88,20 @@ func NewRestClient(ctx context.Context, config *provifv1.GitHubConfig, token str
}

// ParseV1Config parses the raw config into a GitHubConfig struct
func ParseV1Config(rawCfg json.RawMessage) (*provifv1.GitHubConfig, error) {
func ParseV1Config(rawCfg json.RawMessage) (*mediatorv1.GitHubProviderConfig, error) {
type wrapper struct {
GitHub *provifv1.GitHubConfig `json:"github" yaml:"github" mapstructure:"github" validate:"required"`
GitHub *mediatorv1.GitHubProviderConfig `json:"github" yaml:"github" mapstructure:"github" validate:"required"`
}

var w wrapper
if err := provifv1.ParseAndValidate(rawCfg, &w); err != nil {
return nil, err
}

// Validate the config according to the protobuf validation rules.
if err := w.GitHub.Validate(); err != nil {
return nil, fmt.Errorf("error validating GitHub v1 provider config: %w", err)
}

return w.GitHub, nil
}
4 changes: 2 additions & 2 deletions internal/providers/github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ import (

"github.com/stretchr/testify/assert"

provifv1 "github.com/stacklok/mediator/pkg/providers/v1"
mediatorv1 "github.com/stacklok/mediator/pkg/api/protobuf/go/mediator/v1"
)

func TestNewRestClient(t *testing.T) {
t.Parallel()

client, err := NewRestClient(context.Background(), &provifv1.GitHubConfig{
client, err := NewRestClient(context.Background(), &mediatorv1.GitHubProviderConfig{
Endpoint: "https://api.github.com",
}, "token", "")
assert.NoError(t, err)
Expand Down
15 changes: 11 additions & 4 deletions internal/providers/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ package http
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"

"golang.org/x/oauth2"

mediatorv1 "github.com/stacklok/mediator/pkg/api/protobuf/go/mediator/v1"
provifv1 "github.com/stacklok/mediator/pkg/providers/v1"
)

Expand All @@ -38,7 +40,7 @@ type REST struct {
var _ provifv1.REST = (*REST)(nil)

// NewREST creates a new RESTful client.
func NewREST(config *provifv1.RESTConfig, tok string) (*REST, error) {
func NewREST(config *mediatorv1.RESTProviderConfig, tok string) (*REST, error) {
var cli *http.Client

if tok != "" {
Expand All @@ -51,7 +53,7 @@ func NewREST(config *provifv1.RESTConfig, tok string) (*REST, error) {
}

var baseURL *url.URL
baseURL, err := baseURL.Parse(config.BaseURL)
baseURL, err := baseURL.Parse(config.GetBaseUrl())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -87,15 +89,20 @@ func (h *REST) Do(ctx context.Context, req *http.Request) (*http.Response, error
}

// ParseV1Config parses the raw config into a HTTPConfig struct
func ParseV1Config(rawCfg json.RawMessage) (*provifv1.RESTConfig, error) {
func ParseV1Config(rawCfg json.RawMessage) (*mediatorv1.RESTProviderConfig, error) {
type wrapper struct {
REST *provifv1.RESTConfig `json:"rest" validate:"required"`
REST *mediatorv1.RESTProviderConfig `json:"rest" validate:"required"`
}

var w wrapper
if err := provifv1.ParseAndValidate(rawCfg, &w); err != nil {
return nil, err
}

// Validate the config according to the protobuf validation rules.
if err := w.REST.Validate(); err != nil {
return nil, fmt.Errorf("error validating REST v1 provider config: %w", err)
}

return w.REST, nil
}
Loading