Skip to content

Commit

Permalink
Bug 1590743 - Include all webhook types when listing all
Browse files Browse the repository at this point in the history
  • Loading branch information
soltysh committed Mar 12, 2019
1 parent e8e7325 commit 92d104a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 34 deletions.
55 changes: 24 additions & 31 deletions pkg/oc/cli/startbuild/startbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,13 +347,24 @@ func (o *StartBuildOptions) Validate() error {
if len(o.FromBuild) != 0 && o.AsBinary {
// TODO: we should support this, it should be possible to clone a build to run again with new uploaded artifacts.
// Doing so requires introducing a new clonebinary endpoint.
return fmt.Errorf("Cannot use '--from-build' flag with binary builds")
return fmt.Errorf("cannot use '--from-build' flag with binary builds")
}

if len(o.Name) == 0 && len(o.FromWebhook) == 0 && len(o.FromBuild) == 0 {
return fmt.Errorf("a resource name is required either as an argument or by using --from-build")
}

if len(o.ListWebhooks) > 0 {
switch o.ListWebhooks {
case "all":
case "generic":
case "github":
// do nothing
default:
return fmt.Errorf("--list-webhooks must be 'all', 'generic', or 'github'")
}
}

return nil
}

Expand Down Expand Up @@ -429,7 +440,7 @@ func (o *StartBuildOptions) Run() error {
case len(o.FromBuild) > 0:
if newBuild, err = o.BuildClient.Builds(o.Namespace).Clone(request.Name, request); err != nil {
if isInvalidSourceInputsError(err) {
return fmt.Errorf("Build %s/%s has no valid source inputs and '--from-build' cannot be used for binary builds", o.Namespace, o.Name)
return fmt.Errorf("build %s/%s has no valid source inputs and '--from-build' cannot be used for binary builds", o.Namespace, o.Name)
}
if kerrors.IsAlreadyExists(err) {
return transformIsAlreadyExistsError(err, o.Name)
Expand All @@ -439,7 +450,7 @@ func (o *StartBuildOptions) Run() error {
default:
if newBuild, err = o.BuildClient.BuildConfigs(o.Namespace).Instantiate(request.Name, request); err != nil {
if isInvalidSourceInputsError(err) {
return fmt.Errorf("Build configuration %s/%s has no valid source inputs, if this is a binary build you must specify one of '--from-dir', '--from-repo', or '--from-file'", o.Namespace, o.Name)
return fmt.Errorf("build configuration %s/%s has no valid source inputs, if this is a binary build you must specify one of '--from-dir', '--from-repo', or '--from-file'", o.Namespace, o.Name)
}
if kerrors.IsAlreadyExists(err) {
return transformIsAlreadyExistsError(err, o.Name)
Expand All @@ -449,7 +460,7 @@ func (o *StartBuildOptions) Run() error {
}

if err := o.Printer.PrintObj(newBuild, o.Out); err != nil {
fmt.Fprintf(o.ErrOut, "error: %v\n", err)
fmt.Fprintf(o.ErrOut, "%v\n", err)
}

// Stream the logs from the build
Expand Down Expand Up @@ -495,38 +506,17 @@ func (o *StartBuildOptions) streamBuildLogs(build *buildv1.Build) error {

// RunListBuildWebHooks prints the webhooks for the provided build config.
func (o *StartBuildOptions) RunListBuildWebHooks() error {
generic, github := false, false
prefix := false
switch o.ListWebhooks {
case "all":
generic, github = true, true
prefix = true
case "generic":
generic = true
case "github":
github = true
default:
return fmt.Errorf("--list-webhooks must be 'all', 'generic', or 'github'")
}

config, err := o.BuildClient.BuildConfigs(o.Namespace).Get(o.Name, metav1.GetOptions{})
if err != nil {
return err
}

webhookClient := buildclientmanual.NewWebhookURLClient(o.BuildClient.RESTClient(), o.Namespace)
for _, t := range config.Spec.Triggers {
hookType := ""
switch {
case t.GenericWebHook != nil && generic:
if prefix {
hookType = "generic "
}
case t.GitHubWebHook != nil && github:
if prefix {
hookType = "github "
}
default:
if t.Type == buildv1.ImageChangeBuildTriggerType {
continue
}
if o.ListWebhooks != "all" && o.ListWebhooks != strings.ToLower(string(t.Type)) {
continue
}
u, err := webhookClient.WebHookURL(o.Name, &t)
Expand All @@ -536,8 +526,11 @@ func (o *StartBuildOptions) RunListBuildWebHooks() error {
}
continue
}
if o.ListWebhooks == "all" {
fmt.Fprintf(o.Out, "%s ", t.Type)
}
urlStr, _ := url.PathUnescape(u.String())
fmt.Fprintf(o.Out, "%s%s\n", hookType, urlStr)
fmt.Fprintf(o.Out, "%s\n", urlStr)
}
return nil
}
Expand Down Expand Up @@ -965,7 +958,7 @@ func WaitForBuildComplete(c buildv1client.BuildInterface, name string) error {
return nil
}
if name != e.Name || isFailed(e) {
return fmt.Errorf("The build %s/%s status is %q", e.Namespace, name, e.Status.Phase)
return fmt.Errorf("the build %s/%s status is %q", e.Namespace, name, e.Status.Phase)
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions test/cmd/builds.sh
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,13 @@ os::cmd::expect_success_and_text 'oc describe buildConfigs ruby-sample-build' "$
os::cmd::expect_success_and_text 'oc describe buildConfigs ruby-sample-build' "Webhook GitHub"
os::cmd::expect_success_and_text 'oc describe buildConfigs ruby-sample-build' "${url}/apis/build.openshift.io/v1/namespaces/${project}/buildconfigs/ruby-sample-build/webhooks/<secret>/generic"
os::cmd::expect_success_and_text 'oc describe buildConfigs ruby-sample-build' "Webhook Generic"
os::cmd::expect_success_and_text 'oc set triggers bc/ruby-sample-build --from-gitlab' "triggers updated"
os::cmd::expect_success_and_text 'oc set triggers bc/ruby-sample-build --from-bitbucket' "triggers updated"
os::cmd::expect_success 'oc start-build --list-webhooks=all ruby-sample-build'
os::cmd::expect_success_and_text 'oc start-build --list-webhooks=all bc/ruby-sample-build' 'generic'
os::cmd::expect_success_and_text 'oc start-build --list-webhooks=all ruby-sample-build' 'github'
os::cmd::expect_success_and_text 'oc start-build --list-webhooks=all ruby-sample-build' 'gitlab'
os::cmd::expect_success_and_text 'oc start-build --list-webhooks=all ruby-sample-build' 'bitbucket'
os::cmd::expect_success_and_text 'oc start-build --list-webhooks=github ruby-sample-build' '<secret>'
os::cmd::expect_failure 'oc start-build --list-webhooks=blah'
hook=$(oc start-build --list-webhooks='generic' ruby-sample-build | head -n 1)
Expand Down Expand Up @@ -123,9 +127,9 @@ started="$(basename $(oc start-build -o=name ruby-sample-build-invalidtag))"
os::cmd::expect_success_and_text "oc describe build ${started}" 'centos/ruby-22-centos7$'
frombuild="$(basename $(oc start-build -o=name --from-build="${started}"))"
os::cmd::expect_success_and_text "oc describe build ${frombuild}" 'centos/ruby-22-centos7$'
os::cmd::expect_failure_and_text "oc start-build ruby-sample-build-invalid-tag --from-dir=. --from-build=${started}" "Cannot use '--from-build' flag with binary builds"
os::cmd::expect_failure_and_text "oc start-build ruby-sample-build-invalid-tag --from-file=. --from-build=${started}" "Cannot use '--from-build' flag with binary builds"
os::cmd::expect_failure_and_text "oc start-build ruby-sample-build-invalid-tag --from-repo=. --from-build=${started}" "Cannot use '--from-build' flag with binary builds"
os::cmd::expect_failure_and_text "oc start-build ruby-sample-build-invalid-tag --from-dir=. --from-build=${started}" "cannot use '--from-build' flag with binary builds"
os::cmd::expect_failure_and_text "oc start-build ruby-sample-build-invalid-tag --from-file=. --from-build=${started}" "cannot use '--from-build' flag with binary builds"
os::cmd::expect_failure_and_text "oc start-build ruby-sample-build-invalid-tag --from-repo=. --from-build=${started}" "cannot use '--from-build' flag with binary builds"
# --incremental flag should override Spec.Strategy.SourceStrategy.Incremental
os::cmd::expect_success 'oc create -f test/extended/testdata/builds/test-s2i-build.json'
build_name="$(oc start-build -o=name test)"
Expand Down

0 comments on commit 92d104a

Please sign in to comment.