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

[WaitApproval Plugin] Add NotifyEvent methods #5613

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

t-kikuc
Copy link
Member

@t-kikuc t-kikuc commented Feb 28, 2025

What this PR does:

I implemented NotifyEvent methods in both piped and plugin side.

Why we need it:

The WaitApproval plugin needs to:

  • notify approvers when the stage starts (NotificationEventDeploymentWaitApproval)
  • notify when approval is completed (NotificationEventDeploymentApproved)

cf.

func (e *Executor) reportApproved(approver string) {
users, groups, err := e.getApplicationNotificationMentions(model.NotificationEventType_EVENT_DEPLOYMENT_APPROVED)
if err != nil {
e.Logger.Error("failed to get the list of users or groups", zap.Error(err))
}
e.Notifier.Notify(model.NotificationEvent{
Type: model.NotificationEventType_EVENT_DEPLOYMENT_APPROVED,
Metadata: &model.NotificationEventDeploymentApproved{
Deployment: e.Deployment,
Approver: approver,
MentionedAccounts: users,
MentionedGroups: groups,
},
})
}
func (e *Executor) reportRequiringApproval() {
users, groups, err := e.getApplicationNotificationMentions(model.NotificationEventType_EVENT_DEPLOYMENT_WAIT_APPROVAL)
if err != nil {
e.Logger.Error("failed to get the list of users or groups", zap.Error(err))
}
e.Notifier.Notify(model.NotificationEvent{
Type: model.NotificationEventType_EVENT_DEPLOYMENT_WAIT_APPROVAL,
Metadata: &model.NotificationEventDeploymentWaitApproval{
Deployment: e.Deployment,
MentionedAccounts: users,
MentionedGroups: groups,
},
})
}

Which issue(s) this PR fixes:

Part of #5367

Does this PR introduce a user-facing change?:

  • How are users affected by this change:
  • Is this breaking change:
  • How to migrate (if breaking change):

Comment on lines 198 to 215
func getMentionTargets(ctx context.Context, e model.NotificationEventType, deploymentID string, msr *metadatastore.MetadataStoreRegistry) (users []string, groups []string, err error) {
n, err := msr.GetDeploymentSharedMetadata(ctx, &service.GetDeploymentSharedMetadataRequest{
DeploymentId: deploymentID,
Key: model.MetadataKeyDeploymentNotification,
})
if err != nil {
return []string{}, []string{}, err
}
if !n.Found {
return []string{}, []string{}, nil
}

var notif config.DeploymentNotification
if err := json.Unmarshal([]byte(n.Value), &notif); err != nil {
return nil, nil, fmt.Errorf("could not extract mentions users and groups config: %w", err)
}

return notif.FindSlackUsers(e), notif.FindSlackGroups(e), nil
Copy link
Member Author

Choose a reason for hiding this comment

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

I referred to here:

// getMentionedUsers returns the list of users groups who should be mentioned in the notification.
func (e *Executor) getApplicationNotificationMentions(event model.NotificationEventType) ([]string, []string, error) {
n, ok := e.MetadataStore.Shared().Get(model.MetadataKeyDeploymentNotification)
if !ok {
return []string{}, []string{}, nil
}
var notification config.DeploymentNotification
if err := json.Unmarshal([]byte(n), &notification); err != nil {
return nil, nil, fmt.Errorf("could not extract mentions users and groups config: %w", err)
}
return notification.FindSlackUsers(event), notification.FindSlackGroups(event), nil
}

Copy link

codecov bot commented Feb 28, 2025

Codecov Report

Attention: Patch coverage is 4.83871% with 59 lines in your changes missing coverage. Please review.

Project coverage is 26.52%. Comparing base (93e9a27) to head (49402ed).
Report is 5 commits behind head on master.

Files with missing lines Patch % Lines
pkg/app/pipedv1/cmd/piped/grpcapi/plugin_api.go 0.00% 47 Missing ⚠️
pkg/plugin/sdk/client.go 0.00% 11 Missing ⚠️
pkg/app/pipedv1/cmd/piped/piped.go 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5613      +/-   ##
==========================================
- Coverage   26.58%   26.52%   -0.06%     
==========================================
  Files         474      475       +1     
  Lines       50546    50660     +114     
==========================================
+ Hits        13438    13439       +1     
- Misses      36045    36158     +113     
  Partials     1063     1063              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@Warashi Warashi left a comment

Choose a reason for hiding this comment

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

Almost LGTM. I commented on one nitpick.

Comment on lines 203 to 208
if err != nil {
return []string{}, []string{}, err
}
if !n.Found {
return []string{}, []string{}, nil
}
Copy link
Contributor

Choose a reason for hiding this comment

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

[nits]
We can use nil as the zero-value of slices.

Suggested change
if err != nil {
return []string{}, []string{}, err
}
if !n.Found {
return []string{}, []string{}, nil
}
if err != nil {
return nil, nil, err
}
if !n.Found {
return nil, nil, nil
}

Copy link
Member Author

Choose a reason for hiding this comment

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

@Warashi Thank you! I fixed it here 49402ed

Copy link
Contributor

@Warashi Warashi left a comment

Choose a reason for hiding this comment

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

LGTM

@t-kikuc t-kikuc enabled auto-merge (squash) March 4, 2025 06:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants