-
Notifications
You must be signed in to change notification settings - Fork 243
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
Implement buf alpha repo sync
#2122
Merged
saquibmian
merged 13 commits into
main
from
saquib/bsr-1894-implement-buf-alpha-repo-sync-command
Jun 1, 2023
Merged
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
075a2d6
WIP: Implement `buf alpha repo sync`
saquibmian 2ce3c13
Extract sync logic to a `bufsync.Syncer`
saquibmian ec4b36e
Rename + logs
saquibmian b075db1
Merge branch 'main' into saquib/bsr-1894-implement-buf-alpha-repo-syn…
saquibmian df461ca
Merge remote-tracking branch 'origin' into saquib/bsr-1894-implement-…
saquibmian 00dcb5c
Add a TODO to re-evaluate `--module` args when we support multi modules
saquibmian de7cf17
Externalize error handling from Syncer
saquibmian a039a8e
Merge branch 'main' into saquib/bsr-1894-implement-buf-alpha-repo-syn…
saquibmian bc72d96
Update from API changes
saquibmian ba7a94f
Fix typo
saquibmian f105eb3
Normalize and validate module dir
saquibmian 40b07ca
Move normalization into `newSyncableModule`
saquibmian 63a1e7d
Re-order syncableModule
saquibmian File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
// Copyright 2020-2023 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 bufsync | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/bufbuild/buf/private/bufpkg/bufmodule/bufmoduleref" | ||
"github.com/bufbuild/buf/private/pkg/git" | ||
"github.com/bufbuild/buf/private/pkg/normalpath" | ||
"github.com/bufbuild/buf/private/pkg/storage" | ||
"github.com/bufbuild/buf/private/pkg/storage/storagegit" | ||
"go.uber.org/zap" | ||
) | ||
|
||
// ErrorHandler handles errors reported by the Syncer. If a non-nil | ||
// error is returned by the handler, sync will abort in a partially-synced | ||
// state. | ||
type ErrorHandler interface { | ||
// InvalidModuleConfig is invoked by Syncer upon encountering a module | ||
// with an invalid module config. | ||
InvalidModuleConfig( | ||
module string, | ||
commit git.Commit, | ||
err error, | ||
) error | ||
// BuildFailure is invoked by Syncer upon encountering a module that fails | ||
// build. | ||
BuildFailure( | ||
module string, | ||
moduleIdentity bufmoduleref.ModuleIdentity, | ||
commit git.Commit, | ||
err error, | ||
) error | ||
} | ||
|
||
// Module is a module that will be synced by Syncer. | ||
type Module interface { | ||
// Dir is the path to the module relative to the repository root. | ||
Dir() string | ||
// IdentityOverride is an optional module identity override. If empty, | ||
// the identity specified in the module config file will be used. | ||
// | ||
// Unnamed modules will not have their identity overridden, as they are | ||
// not pushable. | ||
IdentityOverride() bufmoduleref.ModuleIdentity | ||
} | ||
|
||
// NewModule constructs a new module that can be synced with a Syncer. | ||
func NewModule(dir string, identityOverride bufmoduleref.ModuleIdentity) (Module, error) { | ||
path, err := normalpath.NormalizeAndValidate(dir) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return newSyncableModule( | ||
path, | ||
identityOverride, | ||
), nil | ||
} | ||
|
||
// Syncer syncs a modules in a git.Repository. | ||
type Syncer interface { | ||
// Sync syncs the repository using the provided SyncFunc. It processes | ||
// commits in reverse topological order, loads any configured named | ||
// modules, extracts any Git metadata for that commit, and invokes | ||
// SyncFunc with a ModuleCommit. | ||
// | ||
// Only commits/branches belonging to the remote named 'origin' are | ||
// processed. All tags are processed. | ||
Sync(context.Context, SyncFunc) error | ||
} | ||
|
||
// NewSyncer creates a new Syncer. | ||
func NewSyncer( | ||
logger *zap.Logger, | ||
repo git.Repository, | ||
storageGitProvider storagegit.Provider, | ||
errorHandler ErrorHandler, | ||
options ...SyncerOption, | ||
) (Syncer, error) { | ||
return newSyncer( | ||
logger, | ||
repo, | ||
storageGitProvider, | ||
errorHandler, | ||
options..., | ||
) | ||
} | ||
|
||
// SyncerOption configures the creation of a new Syncer. | ||
type SyncerOption func(*syncer) error | ||
|
||
// SyncerWithModule configures a Syncer to sync the specified module. | ||
// | ||
// This option can be provided multiple times to sync multiple distinct modules. | ||
func SyncerWithModule(module Module) SyncerOption { | ||
return func(s *syncer) error { | ||
for _, existingModule := range s.modulesToSync { | ||
if existingModule.Dir() != module.Dir() { | ||
continue | ||
} | ||
if module.IdentityOverride() == nil && existingModule.IdentityOverride() == nil { | ||
return fmt.Errorf("duplicate module %s", module.Dir()) | ||
} | ||
if module.IdentityOverride() != nil && | ||
existingModule.IdentityOverride() != nil && | ||
module.IdentityOverride().IdentityString() == existingModule.IdentityOverride().IdentityString() { | ||
return fmt.Errorf("duplicate module %s:%s", module.Dir(), module.IdentityOverride().IdentityString()) | ||
} | ||
} | ||
s.modulesToSync = append(s.modulesToSync, module) | ||
return nil | ||
} | ||
} | ||
|
||
// SyncFunc is invoked by Syncer to process a sync point. | ||
type SyncFunc func(ctx context.Context, commit ModuleCommit) error | ||
|
||
// ModuleCommit is a module at a particular commit. | ||
type ModuleCommit interface { | ||
// Identity is the identity of the module, accounting for any configured override. | ||
Identity() bufmoduleref.ModuleIdentity | ||
// Bucket is the bucket for the module. | ||
Bucket() storage.ReadBucket | ||
// Commit is the commit that the module is sourced from. | ||
Commit() git.Commit | ||
// Branch is the git branch that this module is sourced from. | ||
Branch() string | ||
// Tags are the git tags associated with Commit. | ||
Tags() []string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2020-2023 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 bufsync | ||
|
||
import ( | ||
"github.com/bufbuild/buf/private/bufpkg/bufmodule/bufmoduleref" | ||
"github.com/bufbuild/buf/private/pkg/git" | ||
"github.com/bufbuild/buf/private/pkg/storage" | ||
) | ||
|
||
type moduleCommit struct { | ||
identity bufmoduleref.ModuleIdentity | ||
bucket storage.ReadBucket | ||
commit git.Commit | ||
branch string | ||
tags []string | ||
} | ||
|
||
func newModuleCommit( | ||
identity bufmoduleref.ModuleIdentity, | ||
bucket storage.ReadBucket, | ||
commit git.Commit, | ||
branch string, | ||
tags []string, | ||
) ModuleCommit { | ||
return &moduleCommit{ | ||
identity: identity, | ||
bucket: bucket, | ||
commit: commit, | ||
branch: branch, | ||
tags: tags, | ||
} | ||
} | ||
|
||
func (m *moduleCommit) Identity() bufmoduleref.ModuleIdentity { | ||
return m.identity | ||
} | ||
|
||
func (m *moduleCommit) Bucket() storage.ReadBucket { | ||
return m.bucket | ||
} | ||
|
||
func (m *moduleCommit) Commit() git.Commit { | ||
return m.commit | ||
} | ||
|
||
func (m *moduleCommit) Branch() string { | ||
return m.branch | ||
} | ||
|
||
func (m *moduleCommit) Tags() []string { | ||
return m.tags | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright 2020-2023 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 bufsync | ||
|
||
import ( | ||
"github.com/bufbuild/buf/private/bufpkg/bufmodule/bufmoduleref" | ||
) | ||
|
||
type syncableModule struct { | ||
dir string | ||
identityOverride bufmoduleref.ModuleIdentity | ||
} | ||
|
||
func (s *syncableModule) Dir() string { | ||
return s.dir | ||
} | ||
|
||
func (s *syncableModule) IdentityOverride() bufmoduleref.ModuleIdentity { | ||
return s.identityOverride | ||
} | ||
|
||
func newSyncableModule( | ||
dir string, | ||
identityOverride bufmoduleref.ModuleIdentity, | ||
) Module { | ||
return &syncableModule{ | ||
dir: dir, | ||
identityOverride: identityOverride, | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I should move this into
newSyncableModule
. Let me do it up.