Skip to content

Commit

Permalink
diff ingester: Allow matching on wildcards
Browse files Browse the repository at this point in the history
  • Loading branch information
jhrozek committed Sep 18, 2023
1 parent 1ce6274 commit cd2dc71
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 4 deletions.
34 changes: 30 additions & 4 deletions internal/engine/ingester/diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ package diff
import (
"context"
"fmt"
"github.com/google/go-github/v53/github"
"log"
"path/filepath"
"strings"

"github.com/google/go-github/v53/github"
"github.com/rs/zerolog"
"google.golang.org/protobuf/reflect/protoreflect"

engif "github.com/stacklok/mediator/internal/engine/interfaces"
Expand All @@ -31,6 +33,8 @@ import (
const (
// DiffRuleDataIngestType is the type of the diff rule data ingest engine
DiffRuleDataIngestType = "diff"

wildcard = "*"
)

// Diff is the diff rule data ingest engine
Expand All @@ -47,6 +51,7 @@ func NewDiffIngester(
if cfg == nil {
cfg = &pb.DiffType{}
}

return &Diff{
cfg: cfg,
cli: cli,
Expand All @@ -64,6 +69,12 @@ func (di *Diff) Ingest(
return nil, fmt.Errorf("entity is not a pull request")
}

logger := zerolog.Ctx(ctx).With().
Int32("pull-number", pr.Number).
Str("repo-owner", pr.RepoOwner).
Str("repo-name", pr.RepoName).
Logger()

allDiffs := make([]*pb.PrDependencies_ContextualDependency, 0)

listOpts := &github.ListOptions{PerPage: 30}
Expand All @@ -72,13 +83,21 @@ func (di *Diff) Ingest(
if err != nil {
return nil, fmt.Errorf("error getting pull request files: %w", err)
}

for _, file := range prFiles {
eco := di.getEcosystemForFile(*file.Filename)
if eco == DepEcosystemNone {
log.Printf("no ecosystem found for file %s", *file.Filename)
logger.Debug().
Str("filename", *file.Filename).
Msg("No ecosystem found, skipping")
continue
}

logger.Debug().
Str("filename", *file.Filename).
Str("package-ecosystem", string(eco)).
Msg("No ecosystem found, skipping")

parser := newEcosystemParser(eco)
if parser == nil {
return nil, fmt.Errorf("no parser found for ecosystem %s", eco)
Expand Down Expand Up @@ -117,8 +136,15 @@ func (di *Diff) Ingest(
}

func (di *Diff) getEcosystemForFile(filename string) DependencyEcosystem {
lastComponent := filepath.Base(filename)

for _, ecoMapping := range di.cfg.Ecosystems {
if filename == ecoMapping.Depfile {
if strings.HasPrefix(ecoMapping.Depfile, wildcard) {
strippedDepfile := strings.TrimPrefix(ecoMapping.Depfile, wildcard)
if lastComponent == strippedDepfile {
return DependencyEcosystem(ecoMapping.Name)
}
} else if filename == ecoMapping.Depfile {
return DependencyEcosystem(ecoMapping.Name)
}
}
Expand Down
92 changes: 92 additions & 0 deletions internal/engine/ingester/diff/diff_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright 2023 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 diff provides the diff rule data ingest engine
package diff

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

pb "github.com/stacklok/mediator/pkg/generated/protobuf/go/mediator/v1"
)

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

tests := []struct {
name string
filename string
diffIngestCfg *pb.DiffType
expectedEcoSys DependencyEcosystem
}{
{
name: "Exact match",
filename: "package-lock.json",
diffIngestCfg: &pb.DiffType{
Ecosystems: []*pb.DiffType_Ecosystem{
{
Name: "npm",
Depfile: "package-lock.json",
},
},
},
expectedEcoSys: DepEcosystemNPM,
},
{
name: "Wildcard match",
filename: "/path/to/package-lock.json",
diffIngestCfg: &pb.DiffType{
Ecosystems: []*pb.DiffType_Ecosystem{
{
Name: "npm",
Depfile: fmt.Sprintf("%s%s", wildcard, "package-lock.json"),
},
},
},
expectedEcoSys: DepEcosystemNPM,
},
{
name: "No match",
filename: "/path/to/README.md",
diffIngestCfg: &pb.DiffType{
Ecosystems: []*pb.DiffType_Ecosystem{
{
Name: "npm",
Depfile: fmt.Sprintf("%s%s", wildcard, "package-lock.json"),
},
},
},
expectedEcoSys: DepEcosystemNone,
},
}

for _, tt := range tests {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()
di := &Diff{
cfg: tt.diffIngestCfg,
}
result := di.getEcosystemForFile(tt.filename)

require.NotNil(t, result)
assert.Equal(t, tt.expectedEcoSys, result)
})
}
}

0 comments on commit cd2dc71

Please sign in to comment.