Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unified PR machinery for actions like pull request comment #5223

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions internal/engine/actions/alert/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,23 @@ func NewRuleAlert(
if alertCfg.GetPullRequestComment() == nil {
return nil, fmt.Errorf("alert engine missing pull_request_review configuration")
}
client, err := provinfv1.As[provinfv1.GitHub](provider)
client, err := provinfv1.As[provinfv1.PullRequestCommenter](provider)
if err != nil {
zerolog.Ctx(ctx).Debug().Str("rule-type", ruletype.GetName()).
Msg("provider is not a GitHub provider. Silently skipping alerts.")
return noop.NewNoopAlert(ActionType)
}
return pull_request_comment.NewPullRequestCommentAlert(
ActionType, alertCfg.GetPullRequestComment(), client, setting)
ActionType, alertCfg.GetPullRequestComment(), client, setting,
defaultName(ruletype))
}

return nil, fmt.Errorf("unknown alert type: %s", alertCfg.GetType())
}

func defaultName(ruletype *pb.RuleType) string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you rely on this to be unique now or at some point? Asking because I think we don't conflict on display_name clashes but name only.

if ruletype.GetDisplayName() != "" {
return ruletype.GetDisplayName()
}
return ruletype.GetName()
}
78 changes: 78 additions & 0 deletions internal/engine/actions/alert/pull_request_comment/flush.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// SPDX-FileCopyrightText: Copyright 2024 The Minder Authors
// SPDX-License-Identifier: Apache-2.0
Comment on lines +1 to +2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// SPDX-FileCopyrightText: Copyright 2024 The Minder Authors
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright 2025 The Minder Authors
// SPDX-License-Identifier: Apache-2.0


package pull_request_comment

import (
"context"
"fmt"
"slices"
"strings"

"github.com/rs/zerolog"

"github.com/mindersec/minder/internal/entities/properties"
provifv1 "github.com/mindersec/minder/pkg/providers/v1"
)

// alertFlusher aggregates a list of comments and flushes them to the PR
// as a single comment. The idea is that we can aggregate multiple alerts
// into a single comment without needing to flood the PR with multiple comments.
// This is only instantiated once; the first creation is the only one that will
// be used.
type alertFlusher struct {
props *properties.Properties
commitSha string
commenter provifv1.PullRequestCommenter
}

func newAlertFlusher(props *properties.Properties, commitSha string, commenter provifv1.PullRequestCommenter) *alertFlusher {
return &alertFlusher{
props: props,
commitSha: commitSha,
commenter: commenter,
}
}

func (a *alertFlusher) Flush(ctx context.Context, items ...any) error {
title := title1("Minder Alerts")

aggregatedAlerts := getAlerts(ctx, items...)

_, err := a.commenter.CommentOnPullRequest(ctx, a.props, provifv1.PullRequestCommentInfo{
Commit: a.commitSha,
Body: fmt.Sprintf("%s\n\n%s", title, aggregatedAlerts),
})
if err != nil {
return fmt.Errorf("error creating PR review: %w", err)
}

return nil
}

func getAlerts(ctx context.Context, items ...any) string {
logger := zerolog.Ctx(ctx)

if len(items) == 0 {
return "Minder found no issues."
}

var alerts []string

// iterate and aggregate
for _, item := range items {
fp, ok := item.(*provifv1.PullRequestCommentInfo)
if !ok {
logger.Error().Msgf("expected PullRequestCommentInfo, got %T", item)
continue
}

alerts = append(alerts, alert(fp.Header, fp.Body))
}

// Ensure predictable ordering
// TODO: This should be sorted by severity
slices.Sort(alerts)
Comment on lines +73 to +75
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: how is stable ordering guaranteed between two runs?
I assume the main ordering key is actually fp.Header, and if two alerts have the same header then sorting goes further into the string to sort by whatever comes next.

If my reasoning is correct, this might warrant a longer comment explaining how we expect this comment to be "stable".


return strings.Join(alerts, spacing())
}
31 changes: 31 additions & 0 deletions internal/engine/actions/alert/pull_request_comment/format.go
Copy link
Contributor

@blkt blkt Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: given this is Go, I'm not really fond of this approach and would rather replace this with templates which are, for better or worse, more idiomatic. No need to change this now, I'm just sharing a thought.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// SPDX-FileCopyrightText: Copyright 2024 The Minder Authors
// SPDX-License-Identifier: Apache-2.0

package pull_request_comment

import "fmt"

func formatTitle(displayName string) string {
return fmt.Sprintf("Rule '%s' Alert", displayName)
}

// Formats the comment for a single alert as markdown
func alert(title, body string) string {
return fmt.Sprintf("%s\n\n%s", title2(title), body)
}

func title1(title string) string {
return fmt.Sprintf("# %s", title)
}

func title2(title string) string {
return fmt.Sprintf("## %s", title)
}

func spacing() string {
return "\n\n"
}

func separator() string {
return "---"
}
Loading
Loading