Skip to content

Commit

Permalink
fix: improve parsing logic to handle empty results
Browse files Browse the repository at this point in the history
  • Loading branch information
zou2699 committed Jan 14, 2025
1 parent 451ecf5 commit 331b4be
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
11 changes: 7 additions & 4 deletions pkg/termination/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,17 @@ func ParseMessage(logger *zap.SugaredLogger, msg string) ([]result.RunResult, er
return nil, fmt.Errorf("parsing message json: %w, msg: %s", err, msg)
}

for i, rr := range r {
if rr == (result.RunResult{}) {
writeIndex := 0
for _, rr := range r {
if rr != (result.RunResult{}) {
// Erase incorrect result
r[i] = r[len(r)-1]
r = r[:len(r)-1]
r[writeIndex] = rr
writeIndex++
} else {
logger.Errorf("termination message contains non taskrun or pipelineresource result keys")
}
}
r = r[:writeIndex]

// Remove duplicates (last one wins) and sort by key.
m := map[string]result.RunResult{}
Expand Down
11 changes: 11 additions & 0 deletions pkg/termination/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ func TestParseMessage(t *testing.T) {
Key: "foo",
Value: "last",
}},
}, {
desc: "duplicate keys with incorrect result",
msg: `[
{"key":"foo","value":"first"},
{},
{"key":"foo","value":"middle"},
{"key":"foo","value":"last"}]`,
want: []result.RunResult{{
Key: "foo",
Value: "last",
}},
}, {
desc: "sorted by key",
msg: `[
Expand Down

0 comments on commit 331b4be

Please sign in to comment.