Skip to content

Commit bc83f74

Browse files
committed
fix: move sorting to pipectl
Signed-off-by: kj455 <[email protected]>
1 parent cd13d3b commit bc83f74

File tree

5 files changed

+113
-95
lines changed

5 files changed

+113
-95
lines changed

pkg/app/pipectl/cmd/planpreview/planpreview.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"io"
2222
"os"
23+
"sort"
2324
"strings"
2425
"time"
2526

@@ -49,6 +50,7 @@ type command struct {
4950
timeout time.Duration
5051
pipedHandleTimeout time.Duration
5152
checkInterval time.Duration
53+
sortLabelKeys []string
5254

5355
clientOptions *client.Options
5456
}
@@ -75,6 +77,7 @@ func NewCommand() *cobra.Command {
7577
cmd.Flags().StringVar(&c.out, "out", c.out, "Write planpreview result to the given path.")
7678
cmd.Flags().DurationVar(&c.timeout, "timeout", c.timeout, "Maximum amount of time this command has to complete. Default is 10m.")
7779
cmd.Flags().DurationVar(&c.pipedHandleTimeout, "piped-handle-timeout", c.pipedHandleTimeout, "Maximum amount of time that a Piped can take to handle. Default is 5m.")
80+
cmd.Flags().StringSliceVar(&c.sortLabelKeys, "sort-label-keys", c.sortLabelKeys, "The application label keys to sort the results by. If not specified, the results will be sorted by only PipedID and ApplicationName.")
7881

7982
cmd.MarkFlagRequired("repo-remote-url")
8083
cmd.MarkFlagRequired("head-branch")
@@ -147,11 +150,32 @@ func (c *command) run(ctx context.Context, _ cli.Input) error {
147150
fmt.Printf("Failed to retrieve plan-preview results: %v\n", err)
148151
return err
149152
}
153+
sortResults(results, c.sortLabelKeys)
150154
return printResults(results, os.Stdout, c.out)
151155
}
152156
}
153157
}
154158

159+
// sortResults sorts the given results by pipedID and the given sortLabelKeys.
160+
// If sortLabelKeys is not specified or the all values of sortLabelKeys are the same, it sorts by pipedID and ApplicationName.
161+
func sortResults(allResults []*model.PlanPreviewCommandResult, sortLabelKeys []string) {
162+
sort.SliceStable(allResults, func(i, j int) bool {
163+
return allResults[i].PipedId < allResults[j].PipedId
164+
})
165+
for _, resultsPerPiped := range allResults {
166+
results := resultsPerPiped.Results
167+
sort.SliceStable(results, func(i, j int) bool {
168+
a, b := results[i], results[j]
169+
for _, key := range sortLabelKeys {
170+
if a.Labels[key] != b.Labels[key] {
171+
return a.Labels[key] < b.Labels[key]
172+
}
173+
}
174+
return a.ApplicationName < b.ApplicationName
175+
})
176+
}
177+
}
178+
155179
func printResults(results []*model.PlanPreviewCommandResult, stdout io.Writer, outFile string) error {
156180
r := convert(results)
157181

pkg/app/pipectl/cmd/planpreview/planpreview_test.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,92 @@ NOTE: An error occurred while building plan-preview for applications of the foll
244244
})
245245
}
246246
}
247+
func TestSortResults(t *testing.T) {
248+
testcases := []struct {
249+
name string
250+
results []*model.PlanPreviewCommandResult
251+
sortLabelKeys []string
252+
expected []*model.PlanPreviewCommandResult
253+
}{
254+
{
255+
name: "sort by pipedID and application name",
256+
results: []*model.PlanPreviewCommandResult{
257+
{
258+
PipedId: "piped-2",
259+
Results: []*model.ApplicationPlanPreviewResult{
260+
{ApplicationName: "app-2"},
261+
{ApplicationName: "app-1"},
262+
},
263+
},
264+
{
265+
PipedId: "piped-1",
266+
Results: []*model.ApplicationPlanPreviewResult{
267+
{ApplicationName: "app-2"},
268+
{ApplicationName: "app-1"},
269+
},
270+
},
271+
},
272+
sortLabelKeys: []string{},
273+
expected: []*model.PlanPreviewCommandResult{
274+
{
275+
PipedId: "piped-1",
276+
Results: []*model.ApplicationPlanPreviewResult{
277+
{ApplicationName: "app-1"},
278+
{ApplicationName: "app-2"},
279+
},
280+
},
281+
{
282+
PipedId: "piped-2",
283+
Results: []*model.ApplicationPlanPreviewResult{
284+
{ApplicationName: "app-1"},
285+
{ApplicationName: "app-2"},
286+
},
287+
},
288+
},
289+
},
290+
{
291+
name: "sort by label keys",
292+
results: []*model.PlanPreviewCommandResult{
293+
{
294+
PipedId: "piped-1",
295+
Results: []*model.ApplicationPlanPreviewResult{
296+
{ApplicationName: "app-1", Labels: map[string]string{"env": "staging"}},
297+
{ApplicationName: "app-1", Labels: map[string]string{"env": "prod"}},
298+
},
299+
},
300+
{
301+
PipedId: "piped-2",
302+
Results: []*model.ApplicationPlanPreviewResult{
303+
{ApplicationName: "app-2", Labels: map[string]string{"env": "staging"}},
304+
{ApplicationName: "app-2", Labels: map[string]string{"env": "prod"}},
305+
},
306+
},
307+
},
308+
sortLabelKeys: []string{"env"},
309+
expected: []*model.PlanPreviewCommandResult{
310+
{
311+
PipedId: "piped-1",
312+
Results: []*model.ApplicationPlanPreviewResult{
313+
{ApplicationName: "app-1", Labels: map[string]string{"env": "prod"}},
314+
{ApplicationName: "app-1", Labels: map[string]string{"env": "staging"}},
315+
},
316+
},
317+
{
318+
PipedId: "piped-2",
319+
Results: []*model.ApplicationPlanPreviewResult{
320+
{ApplicationName: "app-2", Labels: map[string]string{"env": "prod"}},
321+
{ApplicationName: "app-2", Labels: map[string]string{"env": "staging"}},
322+
},
323+
},
324+
},
325+
},
326+
}
327+
328+
for _, tc := range testcases {
329+
t.Run(tc.name, func(t *testing.T) {
330+
t.Parallel()
331+
sortResults(tc.results, tc.sortLabelKeys)
332+
assert.Equal(t, tc.expected, tc.results)
333+
})
334+
}
335+
}

pkg/app/piped/planpreview/builder.go

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

2423
"go.uber.org/zap"
2524
"google.golang.org/grpc/codes"
@@ -42,7 +41,6 @@ const (
4241
workspacePattern = "plan-preview-builder-*"
4342
defaultWorkerAppNum = 3
4443
maxWorkerNum = 100
45-
labelEnvKey = "env"
4644
)
4745

4846
var (
@@ -199,25 +197,10 @@ func (b *builder) build(ctx context.Context, id string, cmd model.Command_BuildP
199197
results = append(results, r)
200198
}
201199

202-
sortResults(results)
203-
204200
logger.Info("successfully collected plan-preview results of all applications")
205201
return results, nil
206202
}
207203

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-
221204
func (b *builder) buildApp(ctx context.Context, worker int, command string, app *model.Application, repo git.Repo, mergedCommit string) *model.ApplicationPlanPreviewResult {
222205
logger := b.logger.With(
223206
zap.Int("worker", worker),

pkg/app/piped/planpreview/builder_test.go

Lines changed: 0 additions & 72 deletions
This file was deleted.

pkg/app/server/grpcapi/api.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -848,12 +848,6 @@ func (a *API) RequestPlanPreview(ctx context.Context, req *apiservice.RequestPla
848848
Value: false,
849849
},
850850
},
851-
Orders: []datastore.Order{
852-
{
853-
Field: "Id",
854-
Direction: datastore.Asc,
855-
},
856-
},
857851
})
858852
if err != nil {
859853
return nil, gRPCStoreError(err, "list pipeds")

0 commit comments

Comments
 (0)