Skip to content

Commit b999433

Browse files
committed
Sort results of plan-preview
1 parent 2ce020e commit b999433

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

pkg/app/piped/planpreview/builder.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"context"
2020
"fmt"
2121
"os"
22+
"sort"
2223

2324
"go.uber.org/zap"
2425
"google.golang.org/grpc/codes"
@@ -41,6 +42,7 @@ const (
4142
workspacePattern = "plan-preview-builder-*"
4243
defaultWorkerAppNum = 3
4344
maxWorkerNum = 100
45+
labelEnvKey = "env"
4446
)
4547

4648
var (
@@ -197,10 +199,25 @@ func (b *builder) build(ctx context.Context, id string, cmd model.Command_BuildP
197199
results = append(results, r)
198200
}
199201

202+
sortResults(results)
203+
200204
logger.Info("successfully collected plan-preview results of all applications")
201205
return results, nil
202206
}
203207

208+
func sortResults(results []*model.ApplicationPlanPreviewResult) {
209+
sort.SliceStable(results, func(i, j int) bool {
210+
a, b := results[i], results[j]
211+
if a.ApplicationName != b.ApplicationName {
212+
return a.ApplicationName < b.ApplicationName
213+
}
214+
if a.Labels[labelEnvKey] != b.Labels[labelEnvKey] {
215+
return a.Labels[labelEnvKey] < b.Labels[labelEnvKey]
216+
}
217+
return a.ApplicationKind < b.ApplicationKind
218+
})
219+
}
220+
204221
func (b *builder) buildApp(ctx context.Context, worker int, command string, app *model.Application, repo git.Repo, mergedCommit string) *model.ApplicationPlanPreviewResult {
205222
logger := b.logger.With(
206223
zap.Int("worker", worker),
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2024 The PipeCD Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package planpreview
16+
17+
import (
18+
"testing"
19+
20+
"github.com/pipe-cd/pipecd/pkg/model"
21+
"github.com/stretchr/testify/require"
22+
)
23+
24+
func TestSortResults(t *testing.T) {
25+
t.Parallel()
26+
27+
tests := []struct {
28+
name string
29+
input []*model.ApplicationPlanPreviewResult
30+
expected []*model.ApplicationPlanPreviewResult
31+
}{
32+
{
33+
name: "sort by name, kind",
34+
input: []*model.ApplicationPlanPreviewResult{
35+
{ApplicationName: "appB", ApplicationKind: model.ApplicationKind_CLOUDRUN},
36+
{ApplicationName: "appB", ApplicationKind: model.ApplicationKind_KUBERNETES},
37+
{ApplicationName: "appA", ApplicationKind: model.ApplicationKind_CLOUDRUN},
38+
{ApplicationName: "appA", ApplicationKind: model.ApplicationKind_KUBERNETES},
39+
},
40+
expected: []*model.ApplicationPlanPreviewResult{
41+
{ApplicationName: "appA", ApplicationKind: model.ApplicationKind_KUBERNETES},
42+
{ApplicationName: "appA", ApplicationKind: model.ApplicationKind_CLOUDRUN},
43+
{ApplicationName: "appB", ApplicationKind: model.ApplicationKind_KUBERNETES},
44+
{ApplicationName: "appB", ApplicationKind: model.ApplicationKind_CLOUDRUN},
45+
},
46+
},
47+
{
48+
name: "sort by name, env, kind",
49+
input: []*model.ApplicationPlanPreviewResult{
50+
{ApplicationName: "appB", Labels: map[string]string{labelEnvKey: "dev"}, ApplicationKind: model.ApplicationKind_CLOUDRUN},
51+
{ApplicationName: "appB", Labels: map[string]string{labelEnvKey: "dev"}, ApplicationKind: model.ApplicationKind_KUBERNETES},
52+
{ApplicationName: "appA", Labels: map[string]string{labelEnvKey: "prd"}, ApplicationKind: model.ApplicationKind_CLOUDRUN},
53+
{ApplicationName: "appA", Labels: map[string]string{labelEnvKey: "dev"}, ApplicationKind: model.ApplicationKind_KUBERNETES},
54+
},
55+
expected: []*model.ApplicationPlanPreviewResult{
56+
{ApplicationName: "appA", Labels: map[string]string{labelEnvKey: "dev"}, ApplicationKind: model.ApplicationKind_KUBERNETES},
57+
{ApplicationName: "appA", Labels: map[string]string{labelEnvKey: "prd"}, ApplicationKind: model.ApplicationKind_CLOUDRUN},
58+
{ApplicationName: "appB", Labels: map[string]string{labelEnvKey: "dev"}, ApplicationKind: model.ApplicationKind_KUBERNETES},
59+
{ApplicationName: "appB", Labels: map[string]string{labelEnvKey: "dev"}, ApplicationKind: model.ApplicationKind_CLOUDRUN},
60+
},
61+
},
62+
}
63+
64+
for _, tt := range tests {
65+
t.Run(tt.name, func(t *testing.T) {
66+
t.Parallel()
67+
sortResults(tt.input)
68+
require.Equal(t, tt.expected, tt.input)
69+
})
70+
}
71+
}

0 commit comments

Comments
 (0)