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 11 commits into
base: master
Choose a base branch
from
73 changes: 72 additions & 1 deletion pkg/app/pipedv1/cmd/piped/grpcapi/plugin_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@

import (
"context"
"encoding/json"
"fmt"
"strings"

"github.com/pipe-cd/pipecd/pkg/app/pipedv1/metadatastore"
"github.com/pipe-cd/pipecd/pkg/app/server/service/pipedservice"
config "github.com/pipe-cd/pipecd/pkg/configv1"
"github.com/pipe-cd/pipecd/pkg/model"
service "github.com/pipe-cd/pipecd/pkg/plugin/pipedservice"

"go.uber.org/zap"
Expand All @@ -36,19 +39,31 @@
toolRegistry *toolRegistry
Logger *zap.Logger
metadataStoreRegistry *metadatastore.MetadataStoreRegistry
notifier notifier
}

type apiClient interface {
ReportStageLogs(ctx context.Context, req *pipedservice.ReportStageLogsRequest, opts ...grpc.CallOption) (*pipedservice.ReportStageLogsResponse, error)
ReportStageLogsFromLastCheckpoint(ctx context.Context, in *pipedservice.ReportStageLogsFromLastCheckpointRequest, opts ...grpc.CallOption) (*pipedservice.ReportStageLogsFromLastCheckpointResponse, error)
}

type notifier interface {
Notify(event model.NotificationEvent)
}

// Register registers all handling of this service into the specified gRPC server.
func (a *PluginAPI) Register(server *grpc.Server) {
service.RegisterPluginServiceServer(server, a)
}

func NewPluginAPI(cfg *config.PipedSpec, apiClient apiClient, toolsDir string, logger *zap.Logger, metadataStoreRegistry *metadatastore.MetadataStoreRegistry) (*PluginAPI, error) {
func NewPluginAPI(
cfg *config.PipedSpec,
apiClient apiClient,
toolsDir string,
logger *zap.Logger,
metadataStoreRegistry *metadatastore.MetadataStoreRegistry,
notifier notifier,
) (*PluginAPI, error) {

Check warning on line 66 in pkg/app/pipedv1/cmd/piped/grpcapi/plugin_api.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/cmd/piped/grpcapi/plugin_api.go#L66

Added line #L66 was not covered by tests
toolRegistry, err := newToolRegistry(toolsDir)
if err != nil {
return nil, fmt.Errorf("failed to create tool registry: %w", err)
Expand All @@ -60,6 +75,7 @@
toolRegistry: toolRegistry,
Logger: logger.Named("plugin-api"),
metadataStoreRegistry: metadataStoreRegistry,
notifier: notifier,

Check warning on line 78 in pkg/app/pipedv1/cmd/piped/grpcapi/plugin_api.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/cmd/piped/grpcapi/plugin_api.go#L78

Added line #L78 was not covered by tests
}, nil
}

Expand Down Expand Up @@ -143,3 +159,58 @@
func (a *PluginAPI) GetDeploymentSharedMetadata(ctx context.Context, req *service.GetDeploymentSharedMetadataRequest) (*service.GetDeploymentSharedMetadataResponse, error) {
return a.metadataStoreRegistry.GetDeploymentSharedMetadata(ctx, req)
}

func (a *PluginAPI) NotifyWaitApproval(ctx context.Context, req *service.NotifyWaitApprovalRequest) (*service.NotifyWaitApprovalResponse, error) {
users, groups, err := getMentionTargets(ctx, model.NotificationEventType_EVENT_DEPLOYMENT_WAIT_APPROVAL, req.Deployment.Id, a.metadataStoreRegistry)
if err != nil {
return nil, err
}

Check warning on line 167 in pkg/app/pipedv1/cmd/piped/grpcapi/plugin_api.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/cmd/piped/grpcapi/plugin_api.go#L163-L167

Added lines #L163 - L167 were not covered by tests

a.notifier.Notify(model.NotificationEvent{
Type: model.NotificationEventType_EVENT_DEPLOYMENT_WAIT_APPROVAL,
Metadata: &model.NotificationEventDeploymentWaitApproval{
Deployment: req.Deployment,
MentionedAccounts: users,
MentionedGroups: groups,
},
})
return &service.NotifyWaitApprovalResponse{}, nil

Check warning on line 177 in pkg/app/pipedv1/cmd/piped/grpcapi/plugin_api.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/cmd/piped/grpcapi/plugin_api.go#L169-L177

Added lines #L169 - L177 were not covered by tests
}

func (a *PluginAPI) NotifyApproved(ctx context.Context, req *service.NotifyApprovedRequest) (*service.NotifyApprovedResponse, error) {
users, groups, err := getMentionTargets(ctx, model.NotificationEventType_EVENT_DEPLOYMENT_APPROVED, req.Deployment.Id, a.metadataStoreRegistry)
if err != nil {
return nil, err
}
a.notifier.Notify(model.NotificationEvent{
Type: model.NotificationEventType_EVENT_DEPLOYMENT_APPROVED,
Metadata: &model.NotificationEventDeploymentApproved{
Deployment: req.Deployment,
Approver: strings.Join(req.Approvers, ","),
MentionedAccounts: users,
MentionedGroups: groups,
},
})
return &service.NotifyApprovedResponse{}, nil

Check warning on line 194 in pkg/app/pipedv1/cmd/piped/grpcapi/plugin_api.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/cmd/piped/grpcapi/plugin_api.go#L180-L194

Added lines #L180 - L194 were not covered by tests
}

// getMentionTargets returns the list of users and groups who should be mentioned in the notification.
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 nil, nil, err
}
if !n.Found {
return nil, nil, nil
}

Check warning on line 208 in pkg/app/pipedv1/cmd/piped/grpcapi/plugin_api.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/cmd/piped/grpcapi/plugin_api.go#L198-L208

Added lines #L198 - L208 were not covered by tests

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)
}

Check warning on line 213 in pkg/app/pipedv1/cmd/piped/grpcapi/plugin_api.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/cmd/piped/grpcapi/plugin_api.go#L210-L213

Added lines #L210 - L213 were not covered by tests

return notif.FindSlackUsers(e), notif.FindSlackGroups(e), nil

Check warning on line 215 in pkg/app/pipedv1/cmd/piped/grpcapi/plugin_api.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/cmd/piped/grpcapi/plugin_api.go#L215

Added line #L215 was not covered by tests
}
2 changes: 1 addition & 1 deletion pkg/app/pipedv1/cmd/piped/piped.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@
// Start running plugin service server.
{
var (
service, err = grpcapi.NewPluginAPI(cfg, apiClient, p.toolsDir, input.Logger, metadataStoreRegistry)
service, err = grpcapi.NewPluginAPI(cfg, apiClient, p.toolsDir, input.Logger, metadataStoreRegistry, notifier)

Check warning on line 305 in pkg/app/pipedv1/cmd/piped/piped.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/cmd/piped/piped.go#L305

Added line #L305 was not covered by tests
opts = []rpc.Option{
rpc.WithPort(p.pluginServicePort),
rpc.WithGracePeriod(p.gracePeriod),
Expand Down
Loading