Skip to content

Commit

Permalink
move asking user confirmation to shared file
Browse files Browse the repository at this point in the history
Signed-off-by: Shivam Mukhade <smukhade@redhat.com>
  • Loading branch information
Shivam Mukhade committed Apr 25, 2022
1 parent b46b9f6 commit e524518
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 58 deletions.
10 changes: 0 additions & 10 deletions pkg/cli/webhook/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,13 @@ func (gh *gitHubConfig) GetName() string {
}

func (gh *gitHubConfig) Run(ctx context.Context, opts *Options) (*response, error) {
msg := "Would you like me to configure a GitHub Webhook for your repository? "
var configureWebhook bool
if err := prompt.SurveyAskOne(&survey.Confirm{Message: msg, Default: true}, &configureWebhook); err != nil {
return nil, err
}
if !configureWebhook {
return &response{UserDeclined: true}, nil
}

err := gh.askGHWebhookConfig(opts.RepositoryURL, opts.ControllerURL)
if err != nil {
return nil, err
}
gh.APIURL = opts.ProviderAPIURL

return &response{
UserDeclined: false,
ControllerURL: gh.controllerURL,
PersonalAccessToken: gh.personalAccessToken,
WebhookSecret: gh.webhookSecret,
Expand Down
36 changes: 0 additions & 36 deletions pkg/cli/webhook/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,6 @@ import (
rtesting "knative.dev/pkg/reconciler/testing"
)

func TestGithubWebhook(t *testing.T) {
tests := []struct {
name string
wantErrStr string
askStubs func(*prompt.AskStubber)
response response
}{
{
name: "declined by user",
askStubs: func(as *prompt.AskStubber) {
as.StubOne("n")
},
response: response{UserDeclined: true},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx, _ := rtesting.SetupFakeContext(t)
as, teardown := prompt.InitAskStubber()
defer teardown()
if tt.askStubs != nil {
tt.askStubs(as)
}
gh := gitHubConfig{}
res, err := gh.Run(ctx, &Options{})
if tt.wantErrStr != "" {
assert.Equal(t, err.Error(), tt.wantErrStr)
return
}
assert.NilError(t, err)
assert.Equal(t, res.UserDeclined, tt.response.UserDeclined)
})
}
}

func TestAskGHWebhookConfig(t *testing.T) {
tests := []struct {
name string
Expand Down
10 changes: 0 additions & 10 deletions pkg/cli/webhook/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,13 @@ func (gl *gitLabConfig) GetName() string {
}

func (gl *gitLabConfig) Run(ctx context.Context, opts *Options) (*response, error) {
msg := "Would you like me to configure a GitLab Webhook for your repository? "
var configureWebhook bool
if err := prompt.SurveyAskOne(&survey.Confirm{Message: msg, Default: true}, &configureWebhook); err != nil {
return nil, err
}
if !configureWebhook {
return &response{UserDeclined: true}, nil
}

err := gl.askGLWebhookConfig(opts.ControllerURL)
if err != nil {
return nil, err
}
gl.APIURL = opts.ProviderAPIURL

return &response{
UserDeclined: false,
ControllerURL: gl.controllerURL,
PersonalAccessToken: gl.personalAccessToken,
WebhookSecret: gl.webhookSecret,
Expand Down
14 changes: 12 additions & 2 deletions pkg/cli/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package webhook

import (
"context"
"fmt"
"strings"

"github.com/AlecAivazis/survey/v2"
Expand Down Expand Up @@ -32,7 +33,6 @@ type Options struct {
}

type response struct {
UserDeclined bool
ControllerURL string
WebhookSecret string
PersonalAccessToken string
Expand Down Expand Up @@ -65,6 +65,16 @@ func (w *Options) Install(ctx context.Context) error {
return nil
}

msg := fmt.Sprintf("Would you like me to configure a %s Webhook for your repository? ",
strings.TrimSuffix(webhookProvider.GetName(), "Webhook"))
var configureWebhook bool
if err := prompt.SurveyAskOne(&survey.Confirm{Message: msg, Default: true}, &configureWebhook); err != nil {
return err
}
if !configureWebhook {
return nil
}

// check if info configmap has url then use that otherwise try to detec
if pacInfo.ControllerURL != "" && w.ControllerURL == "" {
w.ControllerURL = pacInfo.ControllerURL
Expand All @@ -73,7 +83,7 @@ func (w *Options) Install(ctx context.Context) error {
}

response, err := webhookProvider.Run(ctx, w)
if err != nil || response.UserDeclined {
if err != nil {
return err
}

Expand Down

0 comments on commit e524518

Please sign in to comment.