Skip to content

Commit

Permalink
Fix plugin type on create
Browse files Browse the repository at this point in the history
  • Loading branch information
emcfarlane committed Nov 27, 2024
1 parent 97934e2 commit 854095a
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 7 deletions.
56 changes: 54 additions & 2 deletions private/buf/cmd/buf/command/plugin/pluginpush/pluginpush.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/bufbuild/buf/private/pkg/app/appcmd"
"github.com/bufbuild/buf/private/pkg/app/appext"
"github.com/bufbuild/buf/private/pkg/slicesext"
"github.com/bufbuild/buf/private/pkg/stringutil"
"github.com/bufbuild/buf/private/pkg/syserror"
"github.com/spf13/pflag"
)
Expand All @@ -35,6 +36,7 @@ const (
binaryFlagName = "binary"
createFlagName = "create"
createVisibilityFlagName = "create-visibility"
createTypeFlagName = "create-type"
sourceControlURLFlagName = "source-control-url"
)

Expand Down Expand Up @@ -63,6 +65,7 @@ type flags struct {
Binary string
Create bool
CreateVisibility string
CreateType string
SourceControlURL string
}

Expand All @@ -89,8 +92,19 @@ func (f *flags) Bind(flagSet *pflag.FlagSet) {
createFlagName,
false,
fmt.Sprintf(
"Create the plugin if it does not exist. Defaults to creating a private repository if --%s is not set.",
"Create the plugin if it does not exist. Defaults to creating a private repository if --%s is not set. Must be used with --%s.",
createVisibilityFlagName,
createTypeFlagName,
),
)
flagSet.StringVar(
&f.CreateType,
createTypeFlagName,
"",
fmt.Sprintf(
"The plugin's type setting, if created. Can only be set with --%s. Must be one of %s",
createTypeFlagName,
stringutil.SliceToString(bufplugin.AllPluginTypeStrings),
),
)
flagSet.StringVar(
Expand Down Expand Up @@ -156,7 +170,22 @@ func upload(
if err != nil {
return nil, err
}
commits, err := uploader.Upload(ctx, []bufplugin.Plugin{plugin})
var options []bufplugin.UploadOption
if flags.Create {
createPluginVisibility, err := bufplugin.ParsePluginVisibility(flags.CreateVisibility)
if err != nil {
return nil, err
}
createPluginType, err := bufplugin.ParsePluginType(flags.CreateType)
if err != nil {
return nil, err
}
options = append(options, bufplugin.UploadWithCreateIfNotExist(
createPluginVisibility,
createPluginType,
))
}
commits, err := uploader.Upload(ctx, []bufplugin.Plugin{plugin}, options...)
if err != nil {
return nil, err
}
Expand All @@ -173,6 +202,9 @@ func validateFlags(flags *flags) error {
if err := validateTypeFlags(flags); err != nil {
return err
}
if err := validateCreateFlags(flags); err != nil {
return err
}
return nil
}

Expand Down Expand Up @@ -209,3 +241,23 @@ func validateLabelFlagValues(flags *flags) error {
}
return nil
}

func validateCreateFlags(flags *flags) error {
if flags.Create {
if flags.CreateVisibility == "" {
return appcmd.NewInvalidArgumentErrorf("--%s must be set if --%s is set", createVisibilityFlagName, createFlagName)
}
if _, err := bufplugin.ParsePluginVisibility(flags.CreateVisibility); err != nil {
return appcmd.WrapInvalidArgumentError(err)
}
}
if flags.Create {
if flags.CreateType == "" {
return appcmd.NewInvalidArgumentErrorf("--%s must be set if --%s is set", createTypeFlagName, createFlagName)
}
if _, err := bufplugin.ParsePluginType(flags.CreateType); err != nil {
return appcmd.WrapInvalidArgumentError(err)
}
}
return nil
}
11 changes: 10 additions & 1 deletion private/bufpkg/bufplugin/bufpluginapi/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func V1Beta1ProtoToDigest(protoDigest *pluginv1beta1.Digest) (bufplugin.Digest,

// *** PRIVATE ***

func pluginVisibilityToV1Proto(pluginVisibility bufplugin.PluginVisibility) (pluginv1beta1.PluginVisibility, error) {
func pluginVisibilityToV1Beta1Proto(pluginVisibility bufplugin.PluginVisibility) (pluginv1beta1.PluginVisibility, error) {
switch pluginVisibility {
case bufplugin.PluginVisibilityPublic:
return pluginv1beta1.PluginVisibility_PLUGIN_VISIBILITY_PUBLIC, nil
Expand All @@ -57,6 +57,15 @@ func pluginVisibilityToV1Proto(pluginVisibility bufplugin.PluginVisibility) (plu
}
}

func pluginTypeToV1Beta1Proto(pluginType bufplugin.PluginType) (pluginv1beta1.PluginType, error) {
switch pluginType {
case bufplugin.PluginTypeCheck:
return pluginv1beta1.PluginType_PLUGIN_TYPE_CHECK, nil
default:
return 0, fmt.Errorf("unknown PluginType: %v", pluginType)
}
}

func v1beta1ProtoToDigestType(protoDigestType pluginv1beta1.DigestType) (bufplugin.DigestType, error) {
digestType, ok := v1beta1ProtoDigestTypeToDigestType[protoDigestType]
if !ok {
Expand Down
11 changes: 9 additions & 2 deletions private/bufpkg/bufplugin/bufpluginapi/uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func (u *uploader) uploadIndexedPluginsForRegistry(
registry,
plugin,
uploadOptions.CreatePluginVisibility(),
uploadOptions.CreatePluginType(),
); err != nil {
return nil, err
}
Expand Down Expand Up @@ -223,8 +224,13 @@ func (u *uploader) createPluginIfNotExist(
primaryRegistry string,
plugin bufplugin.Plugin,
createPluginVisibility bufplugin.PluginVisibility,
createPluginType bufplugin.PluginType,
) (*pluginv1beta1.Plugin, error) {
v1ProtoCreatePluginVisibility, err := pluginVisibilityToV1Proto(createPluginVisibility)
v1Beta1ProtoCreatePluginVisibility, err := pluginVisibilityToV1Beta1Proto(createPluginVisibility)
if err != nil {
return nil, err
}
v1Beta1ProtoCreatePluginType, err := pluginTypeToV1Beta1Proto(createPluginType)
if err != nil {
return nil, err
}
Expand All @@ -240,7 +246,8 @@ func (u *uploader) createPluginIfNotExist(
},
},
Name: plugin.FullName().Name(),
Visibility: v1ProtoCreatePluginVisibility,
Visibility: v1Beta1ProtoCreatePluginVisibility,
Type: v1Beta1ProtoCreatePluginType,
},
},
},
Expand Down
46 changes: 46 additions & 0 deletions private/bufpkg/bufplugin/plugin_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2020-2024 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package bufplugin

import (
"fmt"
)

const (
// PluginTypeCheck says the Plugin is a check plugin.
PluginTypeCheck = iota + 1
)

var (
// AllPluginTypeStrings is all format strings without aliases.
//
// Sorted in the order we want to display them.
AllPluginTypeStrings = []string{
"check",
}
)

// PluginType is the type of a Plugin.
type PluginType int

// ParsePluginType parses the PluginType from the string.
func ParsePluginType(s string) (PluginType, error) {
switch s {
case "check":
return PluginVisibilityPublic, nil
default:
return 0, fmt.Errorf("unknown PluginType: %q", s)
}
}
14 changes: 12 additions & 2 deletions private/bufpkg/bufplugin/uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ func UploadWithLabels(labels ...string) UploadOption {
}

// UploadWithCreateIfNotExist returns a new UploadOption that will result in the
// Plugins being created on the registry with the given visibility and default label if they do not exist.
func UploadWithCreateIfNotExist(createPluginVisibility PluginVisibility, createDefaultLabel string) UploadOption {
// Plugins being created on the registry with the given visibility if they do not exist.
func UploadWithCreateIfNotExist(createPluginVisibility PluginVisibility, createPluginType PluginType) UploadOption {
return func(uploadOptions *uploadOptions) {
uploadOptions.createIfNotExist = true
uploadOptions.createPluginVisibility = createPluginVisibility
uploadOptions.createPluginType = createPluginType
}
}

Expand All @@ -77,6 +78,10 @@ type UploadOptions interface {
//
// Will always be present if CreateIfNotExist() is true.
CreatePluginVisibility() PluginVisibility
// CreatePluginType returns the type to create Plugins with.
//
// Will always be present if CreateIfNotExist() is true.
CreatePluginType() PluginType
// SourceControlURL returns the source control URL set by the user for the module
// contents uploaded. We set the same source control URL for all module contents.
SourceControlURL() string
Expand Down Expand Up @@ -108,6 +113,7 @@ type uploadOptions struct {
labels []string
createIfNotExist bool
createPluginVisibility PluginVisibility
createPluginType PluginType
sourceControlURL string
}

Expand All @@ -127,6 +133,10 @@ func (u *uploadOptions) CreatePluginVisibility() PluginVisibility {
return u.createPluginVisibility
}

func (u *uploadOptions) CreatePluginType() PluginType {
return u.createPluginType
}

func (u *uploadOptions) SourceControlURL() string {
return u.sourceControlURL
}
Expand Down

0 comments on commit 854095a

Please sign in to comment.