Skip to content

Commit

Permalink
Change ListEvaluationHistory to use default project ID
Browse files Browse the repository at this point in the history
Previously, this code expected the project ID to be explicitly set in
the request context, which typically only happens when the client tries
to override the project ID. This changes the project ID lookup to use
the same logic used in the repo handler.
  • Loading branch information
dmjb committed Jul 11, 2024
1 parent 546457c commit 6678c45
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
14 changes: 14 additions & 0 deletions internal/controlplane/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package controlplane

import (
"context"
"database/sql"
"encoding/json"
"errors"
Expand All @@ -25,6 +26,7 @@ import (
"github.com/google/uuid"
"google.golang.org/grpc/codes"

"github.com/stacklok/minder/internal/engine/engcontext"
"github.com/stacklok/minder/internal/providers/github/clients"
"github.com/stacklok/minder/internal/util"
pb "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
Expand Down Expand Up @@ -190,3 +192,15 @@ func getAlertURLFromMetadata(data []byte, repoSlug string) (string, error) {
"%s/%s/security/advisories/%s", githubURL, repoSlug, jsonMeta.GhsaId,
), nil
}

// GetProjectID retrieves the project ID from the request context.
func GetProjectID(ctx context.Context) uuid.UUID {
entityCtx := engcontext.EntityFromContext(ctx)
return entityCtx.Project.ID
}

// GetProviderName retrieves the provider name from the request context.
func GetProviderName(ctx context.Context) string {
entityCtx := engcontext.EntityFromContext(ctx)
return entityCtx.Provider.Name
}
2 changes: 1 addition & 1 deletion internal/controlplane/handlers_evalstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (s *Server) ListEvaluationHistory(
}

// we always filter by project id
opts = append(opts, history.WithProjectIDStr(in.GetContext().GetProject()))
opts = append(opts, history.WithProjectID(GetProjectID(ctx)))

filter, err := history.NewListEvaluationFilter(opts...)
if err != nil {
Expand Down
22 changes: 6 additions & 16 deletions internal/controlplane/handlers_repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func (s *Server) RegisterRepository(
ctx context.Context,
in *pb.RegisterRepositoryRequest,
) (*pb.RegisterRepositoryResponse, error) {
projectID := getProjectID(ctx)
providerName := getProviderName(ctx)
projectID := GetProjectID(ctx)
providerName := GetProviderName(ctx)

// Validate that the Repository struct in the request
githubRepo := in.GetRepository()
Expand Down Expand Up @@ -182,7 +182,7 @@ func (s *Server) GetRepositoryById(ctx context.Context,
if err != nil {
return nil, util.UserVisibleError(codes.InvalidArgument, "invalid repository ID")
}
projectID := getProjectID(ctx)
projectID := GetProjectID(ctx)

// read the repository
repo, err := s.store.GetRepositoryByIDAndProject(ctx, db.GetRepositoryByIDAndProjectParams{
Expand Down Expand Up @@ -265,7 +265,7 @@ func (s *Server) DeleteRepositoryById(
return nil, util.UserVisibleError(codes.InvalidArgument, "invalid repository ID")
}

projectID := getProjectID(ctx)
projectID := GetProjectID(ctx)

err = s.repos.DeleteByID(ctx, parsedRepositoryID, projectID)
if errors.Is(err, sql.ErrNoRows) {
Expand All @@ -291,8 +291,8 @@ func (s *Server) DeleteRepositoryByName(
return nil, util.UserVisibleError(codes.InvalidArgument, "invalid repository name, needs to have the format: owner/name")
}

projectID := getProjectID(ctx)
providerName := getProviderName(ctx)
projectID := GetProjectID(ctx)
providerName := GetProviderName(ctx)

err := s.repos.DeleteByName(ctx, fragments[0], fragments[1], projectID, providerName)
if errors.Is(err, sql.ErrNoRows) {
Expand Down Expand Up @@ -478,13 +478,3 @@ func (s *Server) inferProviderByOwner(ctx context.Context, owner string, project

return nil, fmt.Errorf("no providers can handle repo owned by %s", owner)
}

func getProjectID(ctx context.Context) uuid.UUID {
entityCtx := engcontext.EntityFromContext(ctx)
return entityCtx.Project.ID
}

func getProviderName(ctx context.Context) string {
entityCtx := engcontext.EntityFromContext(ctx)
return entityCtx.Provider.Name
}

0 comments on commit 6678c45

Please sign in to comment.