Skip to content

Commit

Permalink
Change buf beta registry label archive/unarchive to `buf beta regis…
Browse files Browse the repository at this point in the history
…try archive/unarchive` (#2980)
  • Loading branch information
doriable authored May 15, 2024
1 parent 4a0f6b4 commit bcf427c
Show file tree
Hide file tree
Showing 10 changed files with 337 additions and 204 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
- Deprecate `buf mod open`.
- Delete `buf beta migrate-v1beta1` This is now replaced with `buf config migrate`.
- Add `buf registry sdk version` to get the version of a Generated SDK for a module and plugin.
- Add `buf beta registry label archive` and `buf beta registry label unarchive` commands for
managing labels on the BSR.
- Add `buf beta registry archive` and `buf beta registry unarchive` commands for archiving and
unarchiving labels on the BSR.
- Add support for Protobuf Editions. This allows `buf` to be used with sources that use edition
2023, instead of proto2 or proto3 syntax. This also updates the `protoc-gen-buf-breaking` and
`protoc-gen-buf-lint` Protobuf plugins to support files that use edition 2023.
Expand Down
14 changes: 4 additions & 10 deletions private/buf/cmd/buf/buf.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ import (
"github.com/bufbuild/buf/private/buf/cmd/buf/command/alpha/registry/token/tokenget"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/alpha/registry/token/tokenlist"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/beta/price"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/beta/registry/archive"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/beta/registry/commit/commitget"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/beta/registry/commit/commitlist"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/beta/registry/draft/draftdelete"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/beta/registry/draft/draftlist"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/beta/registry/label/labelarchive"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/beta/registry/label/labelunarchive"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/beta/registry/organization/organizationcreate"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/beta/registry/organization/organizationdelete"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/beta/registry/organization/organizationget"
Expand All @@ -51,6 +50,7 @@ import (
"github.com/bufbuild/buf/private/buf/cmd/buf/command/beta/registry/repository/repositoryupdate"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/beta/registry/tag/tagcreate"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/beta/registry/tag/taglist"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/beta/registry/unarchive"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/beta/registry/webhook/webhookcreate"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/beta/registry/webhook/webhookdelete"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/beta/registry/webhook/webhooklist"
Expand Down Expand Up @@ -189,6 +189,8 @@ func NewRootCommand(name string) *appcmd.Command {
Use: "registry",
Short: "Manage assets on the Buf Schema Registry",
SubCommands: []*appcmd.Command{
archive.NewCommand("archive", builder),
unarchive.NewCommand("unarchive", builder),
{
Use: "organization",
Short: "Manage organizations",
Expand Down Expand Up @@ -252,14 +254,6 @@ func NewRootCommand(name string) *appcmd.Command {
plugindelete.NewCommand("delete", builder),
},
},
{
Use: "label",
Short: "Manage labels on the Buf Schema Registry",
SubCommands: []*appcmd.Command{
labelarchive.NewCommand("archive", builder),
labelunarchive.NewCommand("unarchive", builder),
},
},
},
},
},
Expand Down
133 changes: 133 additions & 0 deletions private/buf/cmd/buf/command/beta/registry/archive/archive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// 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 archive

import (
"context"

v1 "buf.build/gen/go/bufbuild/registry/protocolbuffers/go/buf/registry/module/v1"
"connectrpc.com/connect"
"github.com/bufbuild/buf/private/buf/bufcli"
"github.com/bufbuild/buf/private/buf/bufctl"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/beta/registry/internal"
"github.com/bufbuild/buf/private/bufpkg/bufapi"
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
"github.com/bufbuild/buf/private/pkg/app/appcmd"
"github.com/bufbuild/buf/private/pkg/app/appext"
"github.com/spf13/pflag"
)

const (
labelFlagName = "label"
)

// NewCommand returns a new Command.
func NewCommand(
name string,
builder appext.SubCommandBuilder,
) *appcmd.Command {
flags := newFlags()
return &appcmd.Command{
Use: name + " <input> --label <label>",
Short: "Archive one or more labels for the given input",
Args: appcmd.MaximumNArgs(1),
Run: builder.NewRunFunc(
func(ctx context.Context, container appext.Container) error {
return run(ctx, container, flags)
},
),
BindFlags: flags.Bind,
}
}

type flags struct {
Labels []string
// special
InputHashtag string
}

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

func (f *flags) Bind(flagSet *pflag.FlagSet) {
bufcli.BindInputHashtag(flagSet, &f.InputHashtag)
flagSet.StringSliceVar(
&f.Labels,
labelFlagName,
nil,
"Label(s) to archive. Must have at least one.",
)
}

func run(
ctx context.Context,
container appext.Container,
flags *flags,
) error {
if len(flags.Labels) < 1 {
return appcmd.NewInvalidArgumentError("must archive at last one label")
}
controller, err := bufcli.NewController(container)
if err != nil {
return err
}
source, err := bufcli.GetInputValue(container, flags.InputHashtag, ".")
if err != nil {
return err
}
workspace, err := controller.GetWorkspace(
ctx,
source,
// If we do not set this configuration, we would allow users to run `buf beta registry archive`
// against a v1 workspace. This is somewhat safe, since archiving labels does not need
// to happen in any special order, however, it would create an inconsistency with
// `buf push`, where we do have that constraint.
bufctl.WithIgnoreAndDisallowV1BufWorkYAMLs(),
)
if err != nil {
return err
}
clientConfig, err := bufcli.NewConnectClientConfig(container)
if err != nil {
return err
}
registryToModuleFullNames := map[string][]bufmodule.ModuleFullName{}
for _, module := range workspace.Modules() {
if moduleFullName := module.ModuleFullName(); moduleFullName != nil {
moduleFullNames, ok := registryToModuleFullNames[moduleFullName.Registry()]
if !ok {
registryToModuleFullNames[moduleFullName.Registry()] = []bufmodule.ModuleFullName{moduleFullName}
continue
}
registryToModuleFullNames[moduleFullName.Registry()] = append(moduleFullNames, moduleFullName)
}
}
for registry, moduleFullNames := range registryToModuleFullNames {
labelServiceClient := bufapi.NewClientProvider(clientConfig).V1LabelServiceClient(registry)
// ArchiveLabelsResponse is empty.
if _, err := labelServiceClient.ArchiveLabels(
ctx,
connect.NewRequest(
&v1.ArchiveLabelsRequest{
LabelRefs: internal.GetLabelRefsForModuleFullNamesAndLabels(moduleFullNames, flags.Labels),
},
),
); err != nil {
return err
}
}
return nil
}

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

44 changes: 44 additions & 0 deletions private/buf/cmd/buf/command/beta/registry/internal/internal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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 internal

import (
v1 "buf.build/gen/go/bufbuild/registry/protocolbuffers/go/buf/registry/module/v1"
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
)

// GetLabelRefsForModuleFullNamesAndLabels takes a slice of moduleFullNames and a slice
// of labels and creates LabelRefs for all combinations of moduleFullNames and labels.
// This function is shared by `buf beta registry archive`and `buf beta registry unarchive`.
func GetLabelRefsForModuleFullNamesAndLabels(
moduleFullNames []bufmodule.ModuleFullName,
labels []string,
) []*v1.LabelRef {
var labelRefs []*v1.LabelRef
for _, moduleFullName := range moduleFullNames {
for _, label := range labels {
labelRefs = append(labelRefs, &v1.LabelRef{
Value: &v1.LabelRef_Name_{
Name: &v1.LabelRef_Name{
Owner: moduleFullName.Owner(),
Module: moduleFullName.Name(),
Label: label,
},
},
})
}
}
return labelRefs
}

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

This file was deleted.

Loading

0 comments on commit bcf427c

Please sign in to comment.