Skip to content

[SDK] Implement ListStageCommands() in SDK #5639

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

Merged
merged 3 commits into from
Mar 10, 2025
Merged
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
284 changes: 135 additions & 149 deletions pkg/plugin/pipedservice/service.pb.go

Large diffs are not rendered by default.

15 changes: 0 additions & 15 deletions pkg/plugin/pipedservice/service.pb.validate.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions pkg/plugin/pipedservice/service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ service PluginService {
rpc GetDeploymentSharedMetadata(GetDeploymentSharedMetadataRequest) returns (GetDeploymentSharedMetadataResponse) {}

// ListStageCommands lists unhandled commands of the given stage and type.
// Currently, supported types are only APPROVE_STAGE and SKIP_STAGE.
rpc ListStageCommands(ListStageCommandsRequest) returns (ListStageCommandsResponse) {}
}

Expand Down Expand Up @@ -188,7 +187,6 @@ message GetDeploymentSharedMetadataResponse {
message ListStageCommandsRequest {
string deployment_id = 1 [(validate.rules).string.min_len = 1];
string stage_id = 2 [(validate.rules).string.min_len = 1];
model.Command.Type type = 3 [(validate.rules).enum.defined_only = true];
}

message ListStageCommandsResponse {
Expand Down
2 changes: 0 additions & 2 deletions pkg/plugin/pipedservice/service_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions pkg/plugin/sdk/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,20 @@

import (
"context"
"iter"
"slices"
"time"

"github.com/pipe-cd/pipecd/pkg/model"
"github.com/pipe-cd/pipecd/pkg/plugin/pipedapi"
"github.com/pipe-cd/pipecd/pkg/plugin/pipedservice"
"github.com/pipe-cd/pipecd/pkg/plugin/toolregistry"
)

const (
listStageCommandsInterval = 5 * time.Second
)

// Client is a toolkit for interacting with the piped service.
// It provides methods to call the piped service APIs.
// It's a wrapper around the raw piped service client.
Expand Down Expand Up @@ -164,3 +172,48 @@
func (c *Client) ToolRegistry() *toolregistry.ToolRegistry {
return c.toolRegistry
}

// ListStageCommands returns the list of stage commands of the given command types.
func (c Client) ListStageCommands(ctx context.Context, commandTypes ...model.Command_Type) iter.Seq2[*StageCommand, error] {
return func(yield func(*StageCommand, error) bool) {
returned := map[string]struct{}{}

for {
resp, err := c.base.ListStageCommands(ctx, &pipedservice.ListStageCommandsRequest{
DeploymentId: c.deploymentID,
StageId: c.stageID,
})
if err != nil {
if !yield(nil, err) {
return
}
continue

Check warning on line 190 in pkg/plugin/sdk/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/sdk/client.go#L177-L190

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

for _, command := range resp.Commands {
if !slices.Contains(commandTypes, command.Type) {
continue

Check warning on line 195 in pkg/plugin/sdk/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/sdk/client.go#L193-L195

Added lines #L193 - L195 were not covered by tests
}

if _, ok := returned[command.Id]; ok {
continue

Check warning on line 199 in pkg/plugin/sdk/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/sdk/client.go#L198-L199

Added lines #L198 - L199 were not covered by tests
}
returned[command.Id] = struct{}{}

stageCommand, err := newStageCommand(command)
if err != nil {
if !yield(nil, err) {
return
}
continue

Check warning on line 208 in pkg/plugin/sdk/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/sdk/client.go#L201-L208

Added lines #L201 - L208 were not covered by tests
}

if !yield(&stageCommand, nil) {
return
}

Check warning on line 213 in pkg/plugin/sdk/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/sdk/client.go#L211-L213

Added lines #L211 - L213 were not covered by tests
}

time.Sleep(listStageCommandsInterval)

Check warning on line 216 in pkg/plugin/sdk/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/sdk/client.go#L216

Added line #L216 was not covered by tests
}
}
}
32 changes: 32 additions & 0 deletions pkg/plugin/sdk/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,3 +688,35 @@
return model.StageStatus_STAGE_FAILURE
}
}

// StageCommand represents a command for a stage.
type StageCommand struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please add comments as others?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you, I fixed it on e95e38b

Commander string
Type CommandType
}

// CommandType represents the type of the command.
type CommandType int32

const (
CommandTypeApproveStage CommandType = iota
CommandTypeSkipStage
)

// newStageCommand converts the model.Command to the internal representation.
func newStageCommand(c *model.Command) (StageCommand, error) {
switch c.Type {
case model.Command_APPROVE_STAGE:
return StageCommand{
Commander: c.GetCommander(),
Type: CommandTypeApproveStage,
}, nil
case model.Command_SKIP_STAGE:
return StageCommand{
Commander: c.GetCommander(),
Type: CommandTypeSkipStage,
}, nil
default:
return StageCommand{}, fmt.Errorf("invalid command type: %d", c.Type)

Check warning on line 720 in pkg/plugin/sdk/deployment.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/sdk/deployment.go#L707-L720

Added lines #L707 - L720 were not covered by tests
}
}