Skip to content

Commit

Permalink
add piped-handle-timeout(input args)
Browse files Browse the repository at this point in the history
Signed-off-by: mi11km <mi11km.program@gmail.com>
  • Loading branch information
mi11km committed Nov 13, 2023
1 parent 5fef6cb commit d0cb1a5
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 15 deletions.
8 changes: 4 additions & 4 deletions tool/actions-plan-preview/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ type GraphQLClient interface {
// parsePullRequestEvent uses the given environment variables
// to parse and build githubEvent struct.
// Currently, we support 2 kinds of event as below:
// - PullRequestEvent
// https://pkg.go.dev/github.com/google/go-github/v36/github#PullRequestEvent
// - IssueCommentEvent
// https://pkg.go.dev/github.com/google/go-github/v36/github#IssueCommentEvent
// - PullRequestEvent
// https://pkg.go.dev/github.com/google/go-github/v36/github#PullRequestEvent
// - IssueCommentEvent
// https://pkg.go.dev/github.com/google/go-github/v36/github#IssueCommentEvent
func parseGitHubEvent(
ctx context.Context,
pullSvc PullRequestsService,
Expand Down
32 changes: 22 additions & 10 deletions tool/actions-plan-preview/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ const (
commentEventName = "issue_comment"
pushEventName = "push"

argAddress = "address"
argAPIKey = "api-key"
argToken = "token"
argTimeout = "timeout"
argPRNum = "pull-request-number"
argAddress = "address"
argAPIKey = "api-key"
argToken = "token"
argTimeout = "timeout"
argPipedHandleTimeout = "piped-handle-timeout"
argPRNum = "pull-request-number"
)

func main() {
Expand Down Expand Up @@ -121,6 +122,7 @@ func main() {
args.Address,
args.APIKey,
args.Timeout,
args.PipedHandleTimeout,
)
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -166,11 +168,12 @@ func main() {
}

type arguments struct {
Address string
APIKey string
Token string
Timeout time.Duration
PRNum int
Address string
APIKey string
Token string
Timeout time.Duration
PipedHandleTimeout time.Duration
PRNum int
}

func parseArgs(args []string) (arguments, error) {
Expand All @@ -194,6 +197,12 @@ func parseArgs(args []string) (arguments, error) {
return arguments{}, err
}
out.Timeout = d
case argPipedHandleTimeout:
d, err := time.ParseDuration(ps[1])
if err != nil {
return arguments{}, err
}
out.PipedHandleTimeout = d
case argPRNum:
if ps[1] == "" {
continue
Expand Down Expand Up @@ -221,6 +230,9 @@ func parseArgs(args []string) (arguments, error) {
if out.Timeout == 0 {
out.Timeout = defaultTimeout
}
if out.PipedHandleTimeout == 0 {
out.PipedHandleTimeout = defaultTimeout
}

return out, nil
}
Expand Down
134 changes: 134 additions & 0 deletions tool/actions-plan-preview/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ package main

import (
"embed"
"fmt"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand All @@ -33,3 +36,134 @@ func readTestdataFile(t *testing.T, name string) []byte {
func boolPointer(b bool) *bool {
return &b
}

func Test_parseArgs(t *testing.T) {
type args struct {
args []string
}
tests := []struct {
name string
args args
want arguments
wantErr assert.ErrorAssertionFunc
}{
{
name: "minimum required args with no error",
args: args{
args: []string{
"address=localhost:8080",
"api-key=xxxxxxxxxxxxxx",
"token=xxxxxxxxxxxxxxxx",
},
},
want: arguments{
Address: "localhost:8080",
APIKey: "xxxxxxxxxxxxxx",
Token: "xxxxxxxxxxxxxxxx",
Timeout: defaultTimeout,
PipedHandleTimeout: defaultTimeout,
},
wantErr: assert.NoError,
},
{
name: "minimum required args and specified timeout arg with no error",
args: args{
args: []string{
"address=localhost:8080",
"api-key=xxxxxxxxxxxxxx",
"token=xxxxxxxxxxxxxxxx",
"timeout=10m",
},
},
want: arguments{
Address: "localhost:8080",
APIKey: "xxxxxxxxxxxxxx",
Token: "xxxxxxxxxxxxxxxx",
Timeout: 10 * time.Minute,
PipedHandleTimeout: defaultTimeout,
},
wantErr: assert.NoError,
},
{
name: "minimum required args and specified piped-handle-timeout arg with no error",
args: args{
args: []string{
"address=localhost:8080",
"api-key=xxxxxxxxxxxxxx",
"token=xxxxxxxxxxxxxxxx",
"piped-handle-timeout=10m",
},
},
want: arguments{
Address: "localhost:8080",
APIKey: "xxxxxxxxxxxxxx",
Token: "xxxxxxxxxxxxxxxx",
Timeout: defaultTimeout,
PipedHandleTimeout: 10 * time.Minute,
},
wantErr: assert.NoError,
},
{
name: "minimum required args and specified timeout and piped-handle-timeout arg with no error",
args: args{
args: []string{
"address=localhost:8080",
"api-key=xxxxxxxxxxxxxx",
"token=xxxxxxxxxxxxxxxx",
"timeout=12m",
"piped-handle-timeout=15m",
},
},
want: arguments{
Address: "localhost:8080",
APIKey: "xxxxxxxxxxxxxx",
Token: "xxxxxxxxxxxxxxxx",
Timeout: 12 * time.Minute,
PipedHandleTimeout: 15 * time.Minute,
},
wantErr: assert.NoError,
},
{
name: "missing required args (address) returns error",
args: args{
args: []string{
"api-key=xxxxxxxxxxxxxx",
"token=xxxxxxxxxxxxxxxx",
},
},
want: arguments{},
wantErr: assert.Error,
},
{
name: "missing required args (api-key) returns error",
args: args{
args: []string{
"address=localhost:8080",
"token=xxxxxxxxxxxxxxxx",
},
},
want: arguments{},
wantErr: assert.Error,
},
{
name: "missing required args (token) returns error",
args: args{
args: []string{
"address=localhost:8080",
"api-key=xxxxxxxxxxxxxx",
},
},
want: arguments{},
wantErr: assert.Error,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := parseArgs(tt.args.args)
if tt.wantErr(t, err, fmt.Sprintf("parseArgs(%v)", tt.args.args)) {
return
}
assert.Equalf(t, tt.want, got, "parseArgs(%v)", tt.args.args)
})
}
}
3 changes: 2 additions & 1 deletion tool/actions-plan-preview/planpreview.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func retrievePlanPreview(
address,
apiKey string,
timeout time.Duration,
pipedHandleTimeout time.Duration,
) (*PlanPreviewResult, error) {

dir, err := os.MkdirTemp("", "")
Expand All @@ -101,7 +102,7 @@ func retrievePlanPreview(
"--address", address,
"--api-key", apiKey,
"--timeout", timeout.String(),
"--piped-handle-timeout", timeout.String(),
"--piped-handle-timeout", pipedHandleTimeout.String(),
"--out", outPath,
}
cmd := exec.CommandContext(ctx, "pipectl", args...)
Expand Down

0 comments on commit d0cb1a5

Please sign in to comment.