Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add buf plugin registry commit commands #3498

Merged
merged 3 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Add `buf registry plugin {create,delete,info,update}` commands to manage BSR plugins.
- Breaking analysis support for `buf beta lsp`.
- Add `buf registry plugin commit {add-label,info,list,resolve}` to manage BSR plugin commits.

## [v1.47.2] - 2024-11-14

Expand Down
44 changes: 27 additions & 17 deletions private/buf/bufprint/bufprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,28 +199,38 @@ func PrintEntity(writer io.Writer, format Format, entity Entity) error {
}
}

// NewLabelEntity returns a new label entity to print.
func NewLabelEntity(label *modulev1.Label, moduleFullName bufparse.FullName) Entity {
// NewLabelEntity returns a new label entity to print. It takes a label as an
// interface to allow for modulev1.Label and pluginv1beta1.Label to be passed.
func NewLabelEntity(label interface {
GetName() string
GetCommitId() string
GetCreateTime() *timestamppb.Timestamp
GetArchiveTime() *timestamppb.Timestamp
}, moduleFullName bufparse.FullName) Entity {
var archiveTime *time.Time
if label.ArchiveTime != nil {
timeValue := label.ArchiveTime.AsTime()
if label.GetArchiveTime() != nil {
timeValue := label.GetArchiveTime().AsTime()
archiveTime = &timeValue
}
return outputLabel{
Name: label.Name,
Commit: label.CommitId,
CreateTime: label.CreateTime.AsTime(),
Name: label.GetName(),
Commit: label.GetCommitId(),
CreateTime: label.GetCreateTime().AsTime(),
ArchiveTime: archiveTime,
moduleFullName: moduleFullName,
entityFullName: moduleFullName,
}
}

// NewCommitEntity returns a new commit entity to print.
func NewCommitEntity(commit *modulev1.Commit, moduleFullName bufparse.FullName) Entity {
// NewCommitEntity returns a new commit entity to print. It takes a commit as
// an interface to allow for modulev1.Commit and pluginv1beta1.Commit to be passed.
func NewCommitEntity(commit interface {
GetId() string
GetCreateTime() *timestamppb.Timestamp
}, moduleFullName bufparse.FullName) Entity {
return outputCommit{
Commit: commit.Id,
CreateTime: commit.CreateTime.AsTime(),
moduleFullName: moduleFullName,
Commit: commit.GetId(),
CreateTime: commit.GetCreateTime().AsTime(),
entityFullName: moduleFullName,
}
}

Expand Down Expand Up @@ -434,22 +444,22 @@ type outputLabel struct {
CreateTime time.Time `json:"create_time,omitempty" bufprint:"Create Time"`
ArchiveTime *time.Time `json:"archive_time,omitempty" bufprint:"Archive Time,omitempty"`

moduleFullName bufparse.FullName
entityFullName bufparse.FullName
}

func (l outputLabel) fullName() string {
return fmt.Sprintf("%s:%s", l.moduleFullName.String(), l.Name)
return fmt.Sprintf("%s:%s", l.entityFullName.String(), l.Name)
}

type outputCommit struct {
Commit string `json:"commit,omitempty" bufprint:"Commit"`
CreateTime time.Time `json:"create_time,omitempty" bufprint:"Create Time"`

moduleFullName bufparse.FullName
entityFullName bufparse.FullName
}

func (c outputCommit) fullName() string {
return fmt.Sprintf("%s:%s", c.moduleFullName.String(), c.Commit)
return fmt.Sprintf("%s:%s", c.entityFullName.String(), c.Commit)
}

type outputModule struct {
Expand Down
14 changes: 14 additions & 0 deletions private/buf/cmd/buf/buf.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ import (
"github.com/bufbuild/buf/private/buf/cmd/buf/command/registry/organization/organizationdelete"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/registry/organization/organizationinfo"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/registry/organization/organizationupdate"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/registry/plugin/plugincommit/plugincommitaddlabel"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/registry/plugin/plugincommit/plugincommitinfo"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/registry/plugin/plugincommit/plugincommitlist"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/registry/plugin/plugincommit/plugincommitresolve"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/registry/plugin/plugincreate"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/registry/plugin/plugindelete"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/registry/plugin/plugininfo"
Expand Down Expand Up @@ -260,6 +264,16 @@ func NewRootCommand(name string) *appcmd.Command {
Use: "plugin",
Short: "Manage BSR plugins",
SubCommands: []*appcmd.Command{
{
Use: "commit",
Short: "Manage a plugin's commits",
SubCommands: []*appcmd.Command{
plugincommitaddlabel.NewCommand("add-label", builder, ""),
plugincommitinfo.NewCommand("info", builder, ""),
plugincommitlist.NewCommand("list", builder, ""),
plugincommitresolve.NewCommand("resolve", builder, ""),
},
},
plugincreate.NewCommand("create", builder),
plugininfo.NewCommand("info", builder),
plugindelete.NewCommand("delete", builder),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
// 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 plugincommitaddlabel

import (
"context"
"fmt"

pluginv1beta1 "buf.build/gen/go/bufbuild/registry/protocolbuffers/go/buf/registry/plugin/v1beta1"
"connectrpc.com/connect"
"github.com/bufbuild/buf/private/buf/bufcli"
"github.com/bufbuild/buf/private/buf/bufprint"
"github.com/bufbuild/buf/private/bufpkg/bufparse"
"github.com/bufbuild/buf/private/bufpkg/bufregistryapi/bufregistryapiplugin"
"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/uuidutil"
"github.com/spf13/pflag"
)

const (
formatFlagName = "format"
labelsFlagName = "label"
)

// NewCommand returns a new Command.
func NewCommand(
name string,
builder appext.SubCommandBuilder,
deprecated string,
) *appcmd.Command {
flags := newFlags()
return &appcmd.Command{
Use: name + " <remote/owner/plugin:commit> --label <label>",
Short: "Add labels to a commit",
Args: appcmd.ExactArgs(1),
Deprecated: deprecated,
Run: builder.NewRunFunc(
func(ctx context.Context, container appext.Container) error {
return run(ctx, container, flags)
},
),
BindFlags: flags.Bind,
}
}

type flags struct {
Format string
Labels []string
}

func newFlags() *flags {
return &flags{}
}

func (f *flags) Bind(flagSet *pflag.FlagSet) {
flagSet.StringVar(
&f.Format,
formatFlagName,
bufprint.FormatText.String(),
fmt.Sprintf(`The output format to use. Must be one of %s`, bufprint.AllFormatsString),
)
flagSet.StringSliceVar(
&f.Labels,
labelsFlagName,
nil,
"The labels to add to the commit. Must have at least one",
)
}

func run(
ctx context.Context,
container appext.Container,
flags *flags,
) error {
pluginRef, err := bufparse.ParseRef(container.Arg(0))
if err != nil {
return appcmd.WrapInvalidArgumentError(err)
}
if pluginRef.Ref() == "" {
return appcmd.NewInvalidArgumentError("commit is required")
}
commitID := pluginRef.Ref()
if _, err := uuidutil.FromDashless(commitID); err != nil {
return appcmd.NewInvalidArgumentErrorf("invalid commit: %w", err)
}
labels := flags.Labels
if len(labels) == 0 {
return appcmd.NewInvalidArgumentError("must create at least one label")
}
format, err := bufprint.ParseFormat(flags.Format)
if err != nil {
return appcmd.WrapInvalidArgumentError(err)
}
clientConfig, err := bufcli.NewConnectClientConfig(container)
if err != nil {
return err
}
pluginClientProvider := bufregistryapiplugin.NewClientProvider(clientConfig)
labelServiceClient := pluginClientProvider.V1Beta1LabelServiceClient(pluginRef.FullName().Registry())
requestValues := slicesext.Map(labels, func(label string) *pluginv1beta1.CreateOrUpdateLabelsRequest_Value {
return &pluginv1beta1.CreateOrUpdateLabelsRequest_Value{
LabelRef: &pluginv1beta1.LabelRef{
Value: &pluginv1beta1.LabelRef_Name_{
Name: &pluginv1beta1.LabelRef_Name{
Owner: pluginRef.FullName().Owner(),
Plugin: pluginRef.FullName().Name(),
Label: label,
},
},
},
CommitId: commitID,
}
})
resp, err := labelServiceClient.CreateOrUpdateLabels(
ctx,
connect.NewRequest(
&pluginv1beta1.CreateOrUpdateLabelsRequest{
Values: requestValues,
},
),
)
if err != nil {
// Not explicitly handling error with connect.CodeNotFound as
// it can be Plugin or Commit not found error. May be caused by
// a misformatted ID.
return err
}
return bufprint.PrintNames(
container.Stdout(),
format,
slicesext.Map(resp.Msg.Labels, func(label *pluginv1beta1.Label) bufprint.Entity {
return bufprint.NewLabelEntity(label, pluginRef.FullName())
})...,
)
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading