Skip to content

Commit d7af69c

Browse files
authored
[SDK] Implement ListStageCommands() in SDK (#5639)
* Simplify ListStageCommands() Signed-off-by: t-kikuc <[email protected]> * Impl ListStageCommands() Signed-off-by: t-kikuc <[email protected]> * Fix cases and add comments Signed-off-by: t-kikuc <[email protected]> --------- Signed-off-by: t-kikuc <[email protected]>
1 parent 569c4ab commit d7af69c

File tree

6 files changed

+220
-168
lines changed

6 files changed

+220
-168
lines changed

pkg/plugin/pipedservice/service.pb.go

+135-149
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/plugin/pipedservice/service.pb.validate.go

-15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/plugin/pipedservice/service.proto

-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ service PluginService {
4848
rpc GetDeploymentSharedMetadata(GetDeploymentSharedMetadataRequest) returns (GetDeploymentSharedMetadataResponse) {}
4949

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

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

194192
message ListStageCommandsResponse {

pkg/plugin/pipedservice/service_grpc.pb.go

-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/plugin/sdk/client.go

+53
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,20 @@ package sdk
1616

1717
import (
1818
"context"
19+
"iter"
20+
"slices"
21+
"time"
1922

23+
"github.com/pipe-cd/pipecd/pkg/model"
2024
"github.com/pipe-cd/pipecd/pkg/plugin/pipedapi"
2125
"github.com/pipe-cd/pipecd/pkg/plugin/pipedservice"
2226
"github.com/pipe-cd/pipecd/pkg/plugin/toolregistry"
2327
)
2428

29+
const (
30+
listStageCommandsInterval = 5 * time.Second
31+
)
32+
2533
// Client is a toolkit for interacting with the piped service.
2634
// It provides methods to call the piped service APIs.
2735
// It's a wrapper around the raw piped service client.
@@ -164,3 +172,48 @@ func (c *Client) LogPersister() StageLogPersister {
164172
func (c *Client) ToolRegistry() *toolregistry.ToolRegistry {
165173
return c.toolRegistry
166174
}
175+
176+
// ListStageCommands returns the list of stage commands of the given command types.
177+
func (c Client) ListStageCommands(ctx context.Context, commandTypes ...model.Command_Type) iter.Seq2[*StageCommand, error] {
178+
return func(yield func(*StageCommand, error) bool) {
179+
returned := map[string]struct{}{}
180+
181+
for {
182+
resp, err := c.base.ListStageCommands(ctx, &pipedservice.ListStageCommandsRequest{
183+
DeploymentId: c.deploymentID,
184+
StageId: c.stageID,
185+
})
186+
if err != nil {
187+
if !yield(nil, err) {
188+
return
189+
}
190+
continue
191+
}
192+
193+
for _, command := range resp.Commands {
194+
if !slices.Contains(commandTypes, command.Type) {
195+
continue
196+
}
197+
198+
if _, ok := returned[command.Id]; ok {
199+
continue
200+
}
201+
returned[command.Id] = struct{}{}
202+
203+
stageCommand, err := newStageCommand(command)
204+
if err != nil {
205+
if !yield(nil, err) {
206+
return
207+
}
208+
continue
209+
}
210+
211+
if !yield(&stageCommand, nil) {
212+
return
213+
}
214+
}
215+
216+
time.Sleep(listStageCommandsInterval)
217+
}
218+
}
219+
}

pkg/plugin/sdk/deployment.go

+32
Original file line numberDiff line numberDiff line change
@@ -688,3 +688,35 @@ func (o StageStatus) toModelEnum() model.StageStatus {
688688
return model.StageStatus_STAGE_FAILURE
689689
}
690690
}
691+
692+
// StageCommand represents a command for a stage.
693+
type StageCommand struct {
694+
Commander string
695+
Type CommandType
696+
}
697+
698+
// CommandType represents the type of the command.
699+
type CommandType int32
700+
701+
const (
702+
CommandTypeApproveStage CommandType = iota
703+
CommandTypeSkipStage
704+
)
705+
706+
// newStageCommand converts the model.Command to the internal representation.
707+
func newStageCommand(c *model.Command) (StageCommand, error) {
708+
switch c.Type {
709+
case model.Command_APPROVE_STAGE:
710+
return StageCommand{
711+
Commander: c.GetCommander(),
712+
Type: CommandTypeApproveStage,
713+
}, nil
714+
case model.Command_SKIP_STAGE:
715+
return StageCommand{
716+
Commander: c.GetCommander(),
717+
Type: CommandTypeSkipStage,
718+
}, nil
719+
default:
720+
return StageCommand{}, fmt.Errorf("invalid command type: %d", c.Type)
721+
}
722+
}

0 commit comments

Comments
 (0)