|
| 1 | +// Copyright 2020-2024 Buf Technologies, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package plugincommitaddlabel |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + |
| 21 | + pluginv1beta1 "buf.build/gen/go/bufbuild/registry/protocolbuffers/go/buf/registry/plugin/v1beta1" |
| 22 | + "connectrpc.com/connect" |
| 23 | + "github.com/bufbuild/buf/private/buf/bufcli" |
| 24 | + "github.com/bufbuild/buf/private/buf/bufprint" |
| 25 | + "github.com/bufbuild/buf/private/bufpkg/bufparse" |
| 26 | + "github.com/bufbuild/buf/private/bufpkg/bufregistryapi/bufregistryapiplugin" |
| 27 | + "github.com/bufbuild/buf/private/pkg/app/appcmd" |
| 28 | + "github.com/bufbuild/buf/private/pkg/app/appext" |
| 29 | + "github.com/bufbuild/buf/private/pkg/slicesext" |
| 30 | + "github.com/bufbuild/buf/private/pkg/uuidutil" |
| 31 | + "github.com/spf13/pflag" |
| 32 | +) |
| 33 | + |
| 34 | +const ( |
| 35 | + formatFlagName = "format" |
| 36 | + labelsFlagName = "label" |
| 37 | +) |
| 38 | + |
| 39 | +// NewCommand returns a new Command. |
| 40 | +func NewCommand( |
| 41 | + name string, |
| 42 | + builder appext.SubCommandBuilder, |
| 43 | + deprecated string, |
| 44 | +) *appcmd.Command { |
| 45 | + flags := newFlags() |
| 46 | + return &appcmd.Command{ |
| 47 | + Use: name + " <remote/owner/plugin:commit> --label <label>", |
| 48 | + Short: "Add labels to a commit", |
| 49 | + Args: appcmd.ExactArgs(1), |
| 50 | + Deprecated: deprecated, |
| 51 | + Run: builder.NewRunFunc( |
| 52 | + func(ctx context.Context, container appext.Container) error { |
| 53 | + return run(ctx, container, flags) |
| 54 | + }, |
| 55 | + ), |
| 56 | + BindFlags: flags.Bind, |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +type flags struct { |
| 61 | + Format string |
| 62 | + Labels []string |
| 63 | +} |
| 64 | + |
| 65 | +func newFlags() *flags { |
| 66 | + return &flags{} |
| 67 | +} |
| 68 | + |
| 69 | +func (f *flags) Bind(flagSet *pflag.FlagSet) { |
| 70 | + flagSet.StringVar( |
| 71 | + &f.Format, |
| 72 | + formatFlagName, |
| 73 | + bufprint.FormatText.String(), |
| 74 | + fmt.Sprintf(`The output format to use. Must be one of %s`, bufprint.AllFormatsString), |
| 75 | + ) |
| 76 | + flagSet.StringSliceVar( |
| 77 | + &f.Labels, |
| 78 | + labelsFlagName, |
| 79 | + nil, |
| 80 | + "The labels to add to the commit. Must have at least one", |
| 81 | + ) |
| 82 | +} |
| 83 | + |
| 84 | +func run( |
| 85 | + ctx context.Context, |
| 86 | + container appext.Container, |
| 87 | + flags *flags, |
| 88 | +) error { |
| 89 | + pluginRef, err := bufparse.ParseRef(container.Arg(0)) |
| 90 | + if err != nil { |
| 91 | + return appcmd.WrapInvalidArgumentError(err) |
| 92 | + } |
| 93 | + if pluginRef.Ref() == "" { |
| 94 | + return appcmd.NewInvalidArgumentError("commit is required") |
| 95 | + } |
| 96 | + commitID := pluginRef.Ref() |
| 97 | + if _, err := uuidutil.FromDashless(commitID); err != nil { |
| 98 | + return appcmd.NewInvalidArgumentErrorf("invalid commit: %w", err) |
| 99 | + } |
| 100 | + labels := flags.Labels |
| 101 | + if len(labels) == 0 { |
| 102 | + return appcmd.NewInvalidArgumentError("must create at least one label") |
| 103 | + } |
| 104 | + format, err := bufprint.ParseFormat(flags.Format) |
| 105 | + if err != nil { |
| 106 | + return appcmd.WrapInvalidArgumentError(err) |
| 107 | + } |
| 108 | + clientConfig, err := bufcli.NewConnectClientConfig(container) |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + pluginClientProvider := bufregistryapiplugin.NewClientProvider(clientConfig) |
| 113 | + labelServiceClient := pluginClientProvider.V1Beta1LabelServiceClient(pluginRef.FullName().Registry()) |
| 114 | + requestValues := slicesext.Map(labels, func(label string) *pluginv1beta1.CreateOrUpdateLabelsRequest_Value { |
| 115 | + return &pluginv1beta1.CreateOrUpdateLabelsRequest_Value{ |
| 116 | + LabelRef: &pluginv1beta1.LabelRef{ |
| 117 | + Value: &pluginv1beta1.LabelRef_Name_{ |
| 118 | + Name: &pluginv1beta1.LabelRef_Name{ |
| 119 | + Owner: pluginRef.FullName().Owner(), |
| 120 | + Plugin: pluginRef.FullName().Name(), |
| 121 | + Label: label, |
| 122 | + }, |
| 123 | + }, |
| 124 | + }, |
| 125 | + CommitId: commitID, |
| 126 | + } |
| 127 | + }) |
| 128 | + resp, err := labelServiceClient.CreateOrUpdateLabels( |
| 129 | + ctx, |
| 130 | + connect.NewRequest( |
| 131 | + &pluginv1beta1.CreateOrUpdateLabelsRequest{ |
| 132 | + Values: requestValues, |
| 133 | + }, |
| 134 | + ), |
| 135 | + ) |
| 136 | + if err != nil { |
| 137 | + // Not explicitly handling error with connect.CodeNotFound as |
| 138 | + // it can be Plugin or Commit not found error. May be caused by |
| 139 | + // a misformatted ID. |
| 140 | + return err |
| 141 | + } |
| 142 | + return bufprint.PrintNames( |
| 143 | + container.Stdout(), |
| 144 | + format, |
| 145 | + slicesext.Map(resp.Msg.Labels, func(label *pluginv1beta1.Label) bufprint.Entity { |
| 146 | + return bufprint.NewLabelEntity(label, pluginRef.FullName()) |
| 147 | + })..., |
| 148 | + ) |
| 149 | +} |
0 commit comments