Skip to content

Commit

Permalink
take the target revision into account when matching
Browse files Browse the repository at this point in the history
  • Loading branch information
djeebus committed Dec 14, 2023
1 parent 93f1ac7 commit 7b7dc86
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 23 deletions.
4 changes: 2 additions & 2 deletions pkg/affected_apps/argocd_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ func getArgocdApps(vcsToArgoMap config.VcsToArgoMap, repo *repo.Repo) *config.Ap
return repoApps
}

func (a *ArgocdMatcher) AffectedApps(ctx context.Context, changeList []string) (AffectedItems, error) {
func (a *ArgocdMatcher) AffectedApps(ctx context.Context, changeList []string, targetBranch string) (AffectedItems, error) {
if a.appsDirectory == nil {
return AffectedItems{}, nil
}

appsSlice := a.appsDirectory.FindAppsBasedOnChangeList(changeList)
appsSlice := a.appsDirectory.FindAppsBasedOnChangeList(changeList, targetBranch)
return AffectedItems{Applications: appsSlice}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/affected_apps/argocd_matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestFindAffectedAppsWithNilAppsDirectory(t *testing.T) {
)

matcher := ArgocdMatcher{}
items, err := matcher.AffectedApps(ctx, changeList)
items, err := matcher.AffectedApps(ctx, changeList, "main")

// verify results
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/affected_apps/best_effort.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func NewBestEffortMatcher(repoName string, repoFileList []string) *BestEffort {
}
}

func (b *BestEffort) AffectedApps(_ context.Context, changeList []string) (AffectedItems, error) {
func (b *BestEffort) AffectedApps(_ context.Context, changeList []string, targetBranch string) (AffectedItems, error) {
appsMap := make(map[string]string)

for _, file := range changeList {
Expand Down
3 changes: 2 additions & 1 deletion pkg/affected_apps/best_effort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

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

"github.com/zapier/kubechecks/pkg/config"
)

Expand Down Expand Up @@ -158,7 +159,7 @@ func TestBestEffortMatcher(t *testing.T) {
var err error

matcher := NewBestEffortMatcher(tt.args.repoName, testRepoFiles)
got, err = matcher.AffectedApps(context.TODO(), tt.args.fileList)
got, err = matcher.AffectedApps(context.TODO(), tt.args.fileList, "master")
require.NoError(t, err)

assert.Equal(t, len(tt.want.Applications), len(got.Applications))
Expand Down
3 changes: 2 additions & 1 deletion pkg/affected_apps/config_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/rs/zerolog/log"

"github.com/zapier/kubechecks/pkg/argo_client"
"github.com/zapier/kubechecks/pkg/config"
"github.com/zapier/kubechecks/pkg/repo_config"
Expand All @@ -22,7 +23,7 @@ func NewConfigMatcher(cfg *repo_config.Config) *ConfigMatcher {
return &ConfigMatcher{cfg: cfg, argoClient: argoClient}
}

func (b *ConfigMatcher) AffectedApps(ctx context.Context, changeList []string) (AffectedItems, error) {
func (b *ConfigMatcher) AffectedApps(ctx context.Context, changeList []string, targetBranch string) (AffectedItems, error) {
appsMap := make(map[string]string)
var appSetList []ApplicationSet

Expand Down
2 changes: 1 addition & 1 deletion pkg/affected_apps/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type ApplicationSet struct {
}

type Matcher interface {
AffectedApps(ctx context.Context, changeList []string) (AffectedItems, error)
AffectedApps(ctx context.Context, changeList []string, targetBranch string) (AffectedItems, error)
}

// modifiedDirs filters a list of changed files down to a list
Expand Down
29 changes: 19 additions & 10 deletions pkg/config/app_directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

type ApplicationStub struct {
Name, Path string
Name, Path, TargetRevision string

IsHelm, IsKustomize bool
}
Expand Down Expand Up @@ -51,7 +51,7 @@ func (d *AppDirectory) ProcessApp(app v1alpha1.Application) {

// common data
srcPath := src.Path
d.AddAppStub(appName, srcPath, src.IsHelm(), !src.Kustomize.IsZero())
d.AddAppStub(appName, srcPath, app.Spec.Source.TargetRevision, src.IsHelm(), !src.Kustomize.IsZero())

// handle extra helm paths
if helm := src.Helm; helm != nil {
Expand All @@ -67,10 +67,9 @@ func (d *AppDirectory) ProcessApp(app v1alpha1.Application) {
}
}

func (d *AppDirectory) FindAppsBasedOnChangeList(changeList []string) []ApplicationStub {
func (d *AppDirectory) FindAppsBasedOnChangeList(changeList []string, targetBranch string) []ApplicationStub {
log.Debug().Msgf("checking %d changes", len(changeList))

appsMap := make(map[string]string)
appsSet := make(map[string]struct{})
for _, changePath := range changeList {
log.Debug().Msgf("change: %s", changePath)
Expand Down Expand Up @@ -101,10 +100,19 @@ func (d *AppDirectory) FindAppsBasedOnChangeList(changeList []string) []Applicat
log.Warn().Msgf("failed to find matched app named '%s'", appName)
continue
}
if app.TargetRevision == "HEAD" && (targetBranch == "main" || targetBranch == "master") {
log.Debug().Msgf("target revision of %s is HEAD and '%s' is not main or master", appName, targetBranch)
continue
}

if app.TargetRevision != "" && app.TargetRevision != targetBranch {
log.Debug().Msgf("target revision of %s is %s and does not match '%s'", appName, app.TargetRevision, targetBranch)
continue
}
appsSlice = append(appsSlice, app)
}

log.Debug().Msgf("matched %d files into %d apps", len(appsMap), len(appsSet))
log.Debug().Msgf("matched %d files into %d apps", len(changeList), len(appsSet))
return appsSlice
}

Expand All @@ -119,12 +127,13 @@ func (d *AppDirectory) GetApps(filter func(stub ApplicationStub) bool) []Applica
return result
}

func (d *AppDirectory) AddAppStub(appName, srcPath string, isHelm, isKustomize bool) {
func (d *AppDirectory) AddAppStub(appName, srcPath, targetRevision string, isHelm, isKustomize bool) {
d.appsMap[appName] = ApplicationStub{
Name: appName,
Path: srcPath,
IsHelm: isHelm,
IsKustomize: isKustomize,
Name: appName,
TargetRevision: targetRevision,
Path: srcPath,
IsHelm: isHelm,
IsKustomize: isKustomize,
}
d.AddDir(appName, srcPath)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/config/walk_kustomize_files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ resources:
)

appdir := NewAppDirectory()
appdir.AddAppStub(kustomizeApp1Name, kustomizeApp1Path, false, true)
appdir.AddAppStub(kustomizeApp2Name, kustomizeApp2Path, false, true)
appdir.AddAppStub(kustomizeBaseName, kustomizeBasePath, false, true)
appdir.AddAppStub(kustomizeApp1Name, kustomizeApp1Path, "HEAD", false, true)
appdir.AddAppStub(kustomizeApp2Name, kustomizeApp2Path, "HEAD", false, true)
appdir.AddAppStub(kustomizeBaseName, kustomizeBasePath, "HEAD", false, true)

err = walkKustomizeFiles(appdir, fs, kustomizeApp1Name, kustomizeApp1Path)
require.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/events/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (ce *CheckEvent) GetListOfChangedFiles(ctx context.Context) ([]string, erro
}

// Walks the repo to find any apps or appsets impacted by the changes in the MR/PR.
func (ce *CheckEvent) GenerateListOfAffectedApps(ctx context.Context) error {
func (ce *CheckEvent) GenerateListOfAffectedApps(ctx context.Context, targetBranch string) error {
_, span := otel.Tracer("Kubechecks").Start(ctx, "GenerateListOfAffectedApps")
defer span.End()
var err error
Expand Down Expand Up @@ -170,7 +170,7 @@ func (ce *CheckEvent) GenerateListOfAffectedApps(ctx context.Context) error {
}
matcher = affected_apps.NewBestEffortMatcher(ce.repo.Name, ce.repoFiles)
}
ce.affectedItems, err = matcher.AffectedApps(ctx, ce.fileList)
ce.affectedItems, err = matcher.AffectedApps(ctx, ce.fileList, targetBranch)
if err != nil {
telemetry.SetError(span, err, "Get Affected Apps")
ce.logger.Error().Err(err).Msg("could not get list of affected apps and appsets")
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/hook_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (h *VCSHookHandler) processCheckEvent(ctx context.Context, repo *repo.Repo)
}

// Generate a list of affected apps, storing them within the CheckEvent (also returns but discarded here)
err = cEvent.GenerateListOfAffectedApps(ctx)
err = cEvent.GenerateListOfAffectedApps(ctx, repo.BaseRef)
if err != nil {
// TODO: Cancel if gitlab etc
//mEvent.CancelEvent(ctx, err, "Generate List of Affected Apps")
Expand Down

0 comments on commit 7b7dc86

Please sign in to comment.