-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use both app matchers, when appropriate (#182)
- Loading branch information
Showing
6 changed files
with
192 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package affected_apps | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
func NewMultiMatcher(matchers ...Matcher) Matcher { | ||
return MultiMatcher{matchers: matchers} | ||
} | ||
|
||
type MultiMatcher struct { | ||
matchers []Matcher | ||
} | ||
|
||
func (m MultiMatcher) AffectedApps(ctx context.Context, changeList []string, targetBranch string) (AffectedItems, error) { | ||
var total AffectedItems | ||
|
||
for index, matcher := range m.matchers { | ||
items, err := matcher.AffectedApps(ctx, changeList, targetBranch) | ||
if err != nil { | ||
return total, errors.Wrapf(err, "failed to find items in matcher #%d", index) | ||
} | ||
total = total.Union(items) | ||
} | ||
|
||
return total, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package affected_apps | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" | ||
"github.com/stretchr/testify/require" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
type fakeMatcher struct { | ||
items AffectedItems | ||
} | ||
|
||
func (f fakeMatcher) AffectedApps(ctx context.Context, changeList []string, targetBranch string) (AffectedItems, error) { | ||
return f.items, nil | ||
} | ||
|
||
func TestMultiMatcher(t *testing.T) { | ||
t.Run("exists in one but not two", func(t *testing.T) { | ||
app1 := v1alpha1.Application{ObjectMeta: v1.ObjectMeta{Name: "app-1"}} | ||
matcher1 := fakeMatcher{ | ||
items: AffectedItems{ | ||
Applications: []v1alpha1.Application{app1}, | ||
}, | ||
} | ||
matcher2 := fakeMatcher{} | ||
|
||
ctx := context.Background() | ||
matcher := NewMultiMatcher(matcher1, matcher2) | ||
total, err := matcher.AffectedApps(ctx, nil, "") | ||
|
||
require.NoError(t, err) | ||
require.Len(t, total.Applications, 1) | ||
require.Equal(t, app1, total.Applications[0]) | ||
}) | ||
|
||
t.Run("exists in two but not one", func(t *testing.T) { | ||
app1 := v1alpha1.Application{ObjectMeta: v1.ObjectMeta{Name: "app-1"}} | ||
matcher1 := fakeMatcher{} | ||
matcher2 := fakeMatcher{ | ||
items: AffectedItems{ | ||
Applications: []v1alpha1.Application{app1}, | ||
}, | ||
} | ||
|
||
ctx := context.Background() | ||
matcher := NewMultiMatcher(matcher1, matcher2) | ||
total, err := matcher.AffectedApps(ctx, nil, "") | ||
|
||
require.NoError(t, err) | ||
require.Len(t, total.Applications, 1) | ||
require.Equal(t, app1, total.Applications[0]) | ||
}) | ||
|
||
t.Run("exists in both", func(t *testing.T) { | ||
app1 := v1alpha1.Application{ObjectMeta: v1.ObjectMeta{Name: "app-1"}} | ||
matcher1 := fakeMatcher{ | ||
items: AffectedItems{ | ||
Applications: []v1alpha1.Application{app1}, | ||
}, | ||
} | ||
matcher2 := fakeMatcher{ | ||
items: AffectedItems{ | ||
Applications: []v1alpha1.Application{app1}, | ||
}, | ||
} | ||
|
||
ctx := context.Background() | ||
matcher := NewMultiMatcher(matcher1, matcher2) | ||
total, err := matcher.AffectedApps(ctx, nil, "") | ||
|
||
require.NoError(t, err) | ||
require.Len(t, total.Applications, 1) | ||
require.Equal(t, app1, total.Applications[0]) | ||
}) | ||
|
||
t.Run("each contains unique app", func(t *testing.T) { | ||
app1 := v1alpha1.Application{ObjectMeta: v1.ObjectMeta{Name: "app-1"}} | ||
app2 := v1alpha1.Application{ObjectMeta: v1.ObjectMeta{Name: "app-2"}} | ||
matcher1 := fakeMatcher{ | ||
items: AffectedItems{ | ||
Applications: []v1alpha1.Application{app1}, | ||
}, | ||
} | ||
matcher2 := fakeMatcher{ | ||
items: AffectedItems{ | ||
Applications: []v1alpha1.Application{app2}, | ||
}, | ||
} | ||
|
||
ctx := context.Background() | ||
matcher := NewMultiMatcher(matcher1, matcher2) | ||
total, err := matcher.AffectedApps(ctx, nil, "") | ||
|
||
require.NoError(t, err) | ||
require.Len(t, total.Applications, 2) | ||
require.Equal(t, app1, total.Applications[0]) | ||
require.Equal(t, app2, total.Applications[1]) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters