Skip to content

Commit

Permalink
fix: handle VRs w/ multiple conditions
Browse files Browse the repository at this point in the history
Signed-off-by: Tyler Gillson <tyler.gillson@gmail.com>
  • Loading branch information
TylerGillson committed Nov 10, 2023
1 parent 071b960 commit 8a3a243
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 32 deletions.
4 changes: 2 additions & 2 deletions api/v1alpha1/validationresult_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ func (r *ValidationResult) Hash() string {
fmt.Fprint(digester, r.Spec)
fmt.Fprint(digester, r.Status.State)

if len(r.Status.Conditions) > 0 {
c := r.Status.Conditions[0].DeepCopy()
for _, condition := range r.Status.Conditions {
c := condition.DeepCopy()
c.LastValidationTime = metav1.Time{}
fmt.Fprint(digester, c)
}
Expand Down
74 changes: 44 additions & 30 deletions internal/sinks/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,43 +61,57 @@ func (s *SlackSink) Emit(r v1alpha1.ValidationResult) error {
}

func (s *SlackSink) buildSlackBlocks(r v1alpha1.ValidationResult) []slack.Block {
c := r.Status.Conditions[0] // there should only ever be 1 condition + this is validated in the controller

// Basics
// Overall State
state := fmt.Sprintf("*State:* %s :white_check_mark:", r.Status.State)
if r.Status.State != v1alpha1.ValidationSucceeded {
state = fmt.Sprintf("*State:* %s :red_circle:", r.Status.State)
}
blocks := []slack.Block{
slack.NewSectionBlock(newTextBlockObject(fmt.Sprintf("*ValidationResult: %s*", r.Spec.Plugin)), nil, nil),
slack.NewSectionBlock(newTextBlockObject(":information_source: Metadata"), nil, nil),
slack.NewSectionBlock(newTextBlockObject(fmt.Sprintf("*Name:* %s", r.Name)), nil, nil),
slack.NewSectionBlock(newTextBlockObject(fmt.Sprintf("*Validation Type:* %s", c.ValidationType)), nil, nil),
slack.NewSectionBlock(newTextBlockObject(fmt.Sprintf("*Validation Rule:* %s", c.ValidationRule)), nil, nil),
slack.NewSectionBlock(newTextBlockObject(fmt.Sprintf("*State:* %s", r.Status.State)), nil, nil),
slack.NewSectionBlock(newTextBlockObject(fmt.Sprintf("*Message:* %s", c.Message)), nil, nil),
slack.NewSectionBlock(newTextBlockObject(fmt.Sprintf("*%s Validator*: %s", r.Spec.Plugin, r.Name)), nil, nil),
slack.NewSectionBlock(newTextBlockObject(state), nil, nil),
slack.NewDividerBlock(),
}

// Details
if len(c.Details) > 0 {
detailsText := newTextBlockObject(":mag_right: Details")
detailsSection := slack.NewSectionBlock(detailsText, nil, nil)
blocks = append(blocks, detailsSection)

for _, d := range c.Details {
detail := newTextBlockObject(fmt.Sprintf("- %s", d))
detailSection := slack.NewSectionBlock(detail, nil, nil)
blocks = append(blocks, detailSection)
for i, c := range r.Status.Conditions {

// Basics
blocks = append(blocks, []slack.Block{
slack.NewSectionBlock(newTextBlockObject(fmt.Sprintf("*ValidationResult* %d", i+1)), nil, nil),
slack.NewSectionBlock(newTextBlockObject(":information_source: Metadata"), nil, nil),
slack.NewSectionBlock(newTextBlockObject(fmt.Sprintf("*Name:* %s", r.Name)), nil, nil),
slack.NewSectionBlock(newTextBlockObject(fmt.Sprintf("*Validation Type:* %s", c.ValidationType)), nil, nil),
slack.NewSectionBlock(newTextBlockObject(fmt.Sprintf("*Validation Rule:* %s", c.ValidationRule)), nil, nil),
slack.NewSectionBlock(newTextBlockObject(fmt.Sprintf("*Message:* %s", c.Message)), nil, nil),
}...)

// Details
if len(c.Details) > 0 {
detailsText := newTextBlockObject(":mag_right: Details")
detailsSection := slack.NewSectionBlock(detailsText, nil, nil)
blocks = append(blocks, detailsSection)

for _, d := range c.Details {
detail := newTextBlockObject(fmt.Sprintf("- %s", d))
detailSection := slack.NewSectionBlock(detail, nil, nil)
blocks = append(blocks, detailSection)
}
}
}

// Failures
if len(c.Failures) > 0 {
failuresText := newTextBlockObject(":x: Failures")
failuresSection := slack.NewSectionBlock(failuresText, nil, nil)
blocks = append(blocks, failuresSection)

for i, f := range c.Failures {
failure := newTextBlockObject(fmt.Sprintf("%d. %s", i+1, f))
failureSection := slack.NewSectionBlock(failure, nil, nil)
blocks = append(blocks, failureSection)
// Failures
if len(c.Failures) > 0 {
failuresText := newTextBlockObject(":x: Failures")
failuresSection := slack.NewSectionBlock(failuresText, nil, nil)
blocks = append(blocks, failuresSection)

for i, f := range c.Failures {
failure := newTextBlockObject(fmt.Sprintf("%d. %s", i+1, f))
failureSection := slack.NewSectionBlock(failure, nil, nil)
blocks = append(blocks, failureSection)
}
}

blocks = append(blocks, slack.NewDividerBlock())
}

payload, _ := json.Marshal(blocks)
Expand Down

0 comments on commit 8a3a243

Please sign in to comment.